From 4f6e00bd3d9f30e7618892285628bf426a42ac59 Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Sun, 21 Jan 2024 01:33:44 -0600 Subject: [PATCH] add games lobby --- go.mod | 4 +++- go.sum | 2 ++ lobby/lobby.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 go.sum create mode 100644 lobby/lobby.go diff --git a/go.mod b/go.mod index adba1b7..af45d49 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.sunturtle.xyz/studio/shotgun -go 1.21.3 +go 1.21.6 + +require github.com/google/uuid v1.5.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..040e221 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/lobby/lobby.go b/lobby/lobby.go new file mode 100644 index 0000000..e4ec76e --- /dev/null +++ b/lobby/lobby.go @@ -0,0 +1,48 @@ +package serve + +import ( + "sync" + + "github.com/google/uuid" + + "git.sunturtle.xyz/studio/shotgun/game" +) + +type GameID = uuid.UUID + +// Lobby is a set of active games. +type Lobby struct { + mu sync.Mutex + // games is the set of all active games in the lobby. + games map[GameID]*game.Game +} + +func New() *Lobby { + return &Lobby{ + games: make(map[GameID]*game.Game), + } +} + +// Game returns the game with the given ID. +func (l *Lobby) Game(id GameID) *game.Game { + l.mu.Lock() + defer l.mu.Unlock() + return l.games[id] +} + +// Start starts a new game in the lobby. +func (l *Lobby) Start() GameID { + id := uuid.New() + g := game.New() + l.mu.Lock() + defer l.mu.Unlock() + l.games[id] = g + return id +} + +// Finish removes a game from the lobby. +func (l *Lobby) Finish(id GameID) { + l.mu.Lock() + defer l.mu.Unlock() + delete(l.games, id) +}