diff options
Diffstat (limited to 'go/activities/session.go')
| -rw-r--r-- | go/activities/session.go | 99 |
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 +} |
