moved MapLock to control

This commit is contained in:
Stephen McQuay 2014-03-29 01:57:29 -07:00
parent e02d220026
commit e185365e2d
2 changed files with 22 additions and 21 deletions

View File

@ -10,6 +10,7 @@ import (
"os" "os"
"runtime/pprof" "runtime/pprof"
"strings" "strings"
"sync"
) )
type JsonHandler func(http.ResponseWriter, *http.Request) type JsonHandler func(http.ResponseWriter, *http.Request)
@ -252,3 +253,24 @@ func (c *Controller) getGameId(path string) (string, error) {
key := fullPath[2] key := fullPath[2]
return key, err return key, err
} }
type MapLock struct {
M map[string]*Game
sync.RWMutex
}
// 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 {
ml.Lock()
g, _ := ml.M[id]
ml.Unlock()
return g
}
func (ml *MapLock) add(g *Game) {
ml.Lock()
ml.M[g.id] = g
ml.Unlock()
}

21
game.go
View File

@ -23,27 +23,6 @@ type Scanner struct {
Type string `json:"type"` Type string `json:"type"`
} }
type MapLock struct {
M map[string]*Game
sync.RWMutex
}
// 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 {
ml.Lock()
g, _ := ml.M[id]
ml.Unlock()
return g
}
func (ml *MapLock) add(g *Game) {
ml.Lock()
ml.M[g.id] = g
ml.Unlock()
}
type BotStats struct { type BotStats struct {
Kills int Kills int
Deaths int Deaths int