e95d526266
Fixes #2.
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package lobby
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"git.sunturtle.xyz/studio/shotgun/game"
|
|
"git.sunturtle.xyz/studio/shotgun/player"
|
|
"git.sunturtle.xyz/studio/shotgun/serve"
|
|
)
|
|
|
|
type GameID = serve.GameID
|
|
|
|
// 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
|
|
// matches is dealers waiting for a match. It MUST be unbuffered.
|
|
matches chan match
|
|
}
|
|
|
|
func New() *Lobby {
|
|
return &Lobby{
|
|
games: make(map[GameID]*game.Game),
|
|
matches: make(chan match),
|
|
}
|
|
}
|
|
|
|
// 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 begins a new game in the lobby.
|
|
// The caller must be able to distinguish the dealer's and challenger's conns
|
|
// in order to provide correct game start DTOs to each.
|
|
func (l *Lobby) Start(id GameID, dealer, challenger player.ID) {
|
|
g := game.New(dealer, challenger)
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
l.games[id] = g
|
|
}
|
|
|
|
// Finish removes a game from the lobby.
|
|
func (l *Lobby) Finish(id GameID) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
delete(l.games, id)
|
|
}
|