|
|
@ -64,7 +64,7 @@ func (c *Controller) StartGame(w http.ResponseWriter, req *http.Request) { |
|
|
|
mode = cfg.Mode |
|
|
|
} |
|
|
|
|
|
|
|
g := c.Games.get(requested_game_name) |
|
|
|
g := c.Games.Get(requested_game_name) |
|
|
|
if g == nil { |
|
|
|
log.Printf("Game '%s' non-existant; making it now", requested_game_name) |
|
|
|
var err error |
|
|
@ -76,7 +76,7 @@ func (c *Controller) StartGame(w http.ResponseWriter, req *http.Request) { |
|
|
|
return |
|
|
|
} |
|
|
|
go g.run() |
|
|
|
c.Games.add(g) |
|
|
|
c.Games.Add(g) |
|
|
|
} else { |
|
|
|
log.Printf("Game '%s' already exists: %p", requested_game_name, g) |
|
|
|
b, _ := json.Marshal(NewFailure("game already exists")) |
|
|
@ -254,6 +254,7 @@ func (c *Controller) getGameId(path string) (string, error) { |
|
|
|
return key, err |
|
|
|
} |
|
|
|
|
|
|
|
// MapLock is simply a map and a RWMutex
|
|
|
|
type MapLock struct { |
|
|
|
M map[string]*Game |
|
|
|
sync.RWMutex |
|
|
@ -262,14 +263,15 @@ type MapLock struct { |
|
|
|
// get is a function that returns a game if found, and creates one if
|
|
|
|
// not found and force is true. In order to get a hash (rather than use
|
|
|
|
// the string you pass) send "" for id.
|
|
|
|
func (ml *MapLock) get(id string) *Game { |
|
|
|
func (ml *MapLock) Get(id string) *Game { |
|
|
|
ml.Lock() |
|
|
|
g, _ := ml.M[id] |
|
|
|
ml.Unlock() |
|
|
|
return g |
|
|
|
} |
|
|
|
|
|
|
|
func (ml *MapLock) add(g *Game) { |
|
|
|
// add is used to insert a new game into this MapLock
|
|
|
|
func (ml *MapLock) Add(g *Game) { |
|
|
|
ml.Lock() |
|
|
|
ml.M[g.id] = g |
|
|
|
ml.Unlock() |
|
|
|