-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_manager.go
56 lines (46 loc) · 971 Bytes
/
game_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package desert
import (
"log"
)
type GameId uint64
const maxGameId GameId = ^GameId(0)
type GameManager struct {
games map[GameId]*Game
}
type NewGameData struct {
Name string `json:"name"`
}
func (g *GameManager) NewGame(ngd NewGameData) (*Game, error) {
if g.games == nil {
g.games = make(map[GameId]*Game)
}
nextId := g.nextGameId()
game := NewGame(nextId, ngd.Name)
g.games[nextId] = game
log.Printf("Created new game with id %v, named %v", game.Id, game.Name)
return game, nil
}
func (g *GameManager) FindGame(id GameId) *Game {
if g, ok := g.games[id]; ok {
return g
}
return nil
}
func (g *GameManager) nextGameId() GameId {
var nextId GameId
for nextId = 0; nextId < maxGameId; nextId++ {
if _, ok := g.games[nextId]; ok == false {
return nextId
}
}
panic("Out of game ids!")
}
func (g *GameManager) ListGames() []*Game {
a := make([]*Game, len(g.games))
i := 0
for _, g := range g.games {
a[i] = g
i++
}
return a
}