Added docs around MapLock, change visibility of methods

Removed unused global
This commit is contained in:
Stephen McQuay 2014-03-29 02:00:09 -07:00
parent e185365e2d
commit 0d11b8a0ba
3 changed files with 8 additions and 10 deletions

View File

@ -21,8 +21,6 @@ var netprofile = flag.Bool("netprof", false, "if specified will run with net/htt
var verbose = flag.Bool("verbose", false, "") var verbose = flag.Bool("verbose", false, "")
var config = flag.String("config", "~/.config/hackerbots/config.json", "location of config file") var config = flag.String("config", "~/.config/hackerbots/config.json", "location of config file")
var delta float32
func main() { func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
log.Printf("Starting Server...") log.Printf("Starting Server...")
@ -49,8 +47,6 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
delta = (float32(conf.Tick) / 1000.0) * float32(conf.Timescale)
sm := http.NewServeMux() sm := http.NewServeMux()
c := botserv.Controller{ c := botserv.Controller{

View File

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

View File

@ -151,7 +151,7 @@ func (c *Controller) AddPlayer(ws *websocket.Conn) {
return return
} }
game := c.Games.get(gid.Id) game := c.Games.Get(gid.Id)
if game == nil { if game == nil {
var err error var err error
game, err = NewGame( game, err = NewGame(
@ -169,7 +169,7 @@ func (c *Controller) AddPlayer(ws *websocket.Conn) {
return return
} }
go game.run() go game.run()
c.Games.add(game) c.Games.Add(game)
} }
player_id := c.Idg.Hash() player_id := c.Idg.Hash()