add games lobby
This commit is contained in:
48
lobby/lobby.go
Normal file
48
lobby/lobby.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user