From 2b31c5691ad273e400c3ab61e499b146b7e8245e Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Mon, 28 Jul 2025 18:23:27 +0200 Subject: fixup! initial draftsim commit --- go/activities/session.go | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 go/activities/session.go 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 +} -- cgit v1.2.3