2024-01-21 04:35:21 -06:00
|
|
|
package lobby
|
2024-01-21 01:33:44 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.sunturtle.xyz/studio/shotgun/game"
|
2024-01-21 12:28:46 -06:00
|
|
|
"git.sunturtle.xyz/studio/shotgun/serve"
|
2024-01-21 01:33:44 -06:00
|
|
|
)
|
|
|
|
|
2024-01-21 12:28:46 -06:00
|
|
|
type GameID = serve.GameID
|
2024-01-21 01:33:44 -06:00
|
|
|
|
|
|
|
// 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]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish removes a game from the lobby.
|
|
|
|
func (l *Lobby) Finish(id GameID) {
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
delete(l.games, id)
|
|
|
|
}
|