added ability to autovivify games in debug mode
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user