aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-07-28 18:23:27 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2025-07-28 18:36:02 +0200
commit2b31c5691ad273e400c3ab61e499b146b7e8245e (patch)
tree585514abd86373a8a42d5a77d678d526ff466219
parent9b7d7cd127499eb2b1485df3cd5e74caa31dbaf1 (diff)
downloadmuhqs-game-2b31c5691ad273e400c3ab61e499b146b7e8245e.tar.gz
muhqs-game-2b31c5691ad273e400c3ab61e499b146b7e8245e.zip
fixup! initial draftsim commit
-rw-r--r--go/activities/session.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/go/activities/session.go b/go/activities/session.go
new file mode 100644
index 00000000..f910c67c
--- /dev/null
+++ b/go/activities/session.go
@@ -0,0 +1,99 @@
+package activities
+
+import (
+ "muhq.space/muhqs-game/go/game"
+ "muhq.space/muhqs-game/go/ui"
+)
+
+type Session struct {
+ ui.Collection
+ playerList *ui.Buffer
+ ctrl game.PlayerControl
+ handleNotification func(game.PlayerNotification)
+}
+
+func (s *Session) AddPlayer(name string) {
+ // TODO: remove a AI
+ s.playerList.AddLine(name)
+}
+
+func (s *Session) ReadyPlayer(name string) {
+ i := 0
+ for l := range s.playerList.LinesSeq() {
+ if l == name {
+ s.playerList.PrefixLine(i, "!")
+ return
+ }
+ i = i + 1
+ }
+}
+
+func (s *Session) Update() error {
+ n, err := s.ctrl.RecvNotification()
+ if err != nil {
+ // FIXME
+ }
+
+ if n.Valid() {
+ switch n.Notification {
+ case game.JoinedPlayerNotification:
+ s.AddPlayer(n.Context.(string))
+ case game.ReadyPlayerNotification:
+ s.ReadyPlayer(n.Context.(string))
+ default:
+ s.handleNotification(n)
+ }
+ }
+
+ if err := ui.Update(); err != nil {
+ return err
+ }
+
+ return s.Collection.Update()
+}
+
+func (s *Session) Layout(int, int) (int, int) {
+ return s.Collection.Layout()
+}
+
+func NewSession(wdth, hght int, title string, ctrl game.PlayerControl, startFunc func(), handleNotificataion func(game.PlayerNotification)) *Session {
+ s := &Session{
+ Collection: ui.Collection{Width: wdth, Height: hght},
+ ctrl: ctrl,
+ handleNotification: handleNotificataion,
+ }
+
+ titleTextBox := ui.NewAutoTextBox(
+ s.Width/2-50,
+ 50,
+ title)
+ s.AddWidget(titleTextBox)
+
+ s.playerList = ui.NewBuffer(
+ 0,
+ 70,
+ s.Width,
+ s.Height-70-40)
+
+ s.AddWidget(ui.NewSimpleButton(
+ s.Width/2-200,
+ s.Height-40,
+ 200,
+ 40,
+ "ready",
+ func(*ui.SimpleButton) {
+ ctrl.SendNotification(game.NewReadyPlayerNotification(ctrl.Player()))
+ }))
+
+ s.AddWidget(ui.NewSimpleButton(
+ s.Width/2+200,
+ s.Height-40,
+ 200,
+ 40,
+ "start",
+ func(*ui.SimpleButton) {
+ startFunc()
+ }))
+
+ return s
+}