add games lobby

This commit is contained in:
Branden J Brown 2024-01-21 01:33:44 -06:00
parent 5a5f709dd6
commit 4f6e00bd3d
3 changed files with 53 additions and 1 deletions

4
go.mod
View File

@ -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

2
go.sum Normal file
View File

@ -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=

48
lobby/lobby.go Normal file
View File

@ -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)
}