added ability to autovivify games in debug mode

This commit is contained in:
sm
2013-10-18 20:49:57 -07:00
parent 2edc7c61af
commit d19b3f71fa
4 changed files with 36 additions and 17 deletions
+32
View File
@@ -2,8 +2,10 @@ package main
import (
v "bitbucket.org/hackerbots/vector"
"errors"
"log"
"sort"
"sync"
"time"
)
@@ -37,6 +39,36 @@ type Scanner struct {
Stats Stats `json:"stats"`
}
type MapLock struct {
m map[string]*game
sync.RWMutex
}
func (ml *MapLock) getOrCreate(id string) (*game, error) {
ml.Lock()
g, ok := games.m[id]
ml.Unlock()
if ok {
return g, nil
}
if !*debug {
return nil, errors.New("game not found")
}
new_game_name := idg.Hash()
_g := NewGame(id, *width, *height)
go _g.run()
ml.Lock()
ml.m[new_game_name] = _g
ml.Unlock()
return _g, nil
}
type game struct {
id string
players map[*player]bool