ignore new binary

Create new bserv binary

Herein lie the following ideas:

- change package visibility of many things
- try to stash the globals into structs
- the code is far from correct; it merely compiles
This commit is contained in:
sm
2014-03-31 23:21:14 -07:00
parent 62222a9ad9
commit c8efd34080
16 changed files with 146 additions and 126 deletions
+41 -33
View File
@@ -1,4 +1,4 @@
package main
package botserv
import (
"encoding/json"
@@ -19,13 +19,21 @@ func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h(w, req)
}
func startGame(w http.ResponseWriter, req *http.Request) {
type Controller struct {
Idg *IdGenerator
Conf Config
Games MapLock
Memprofile string
Profile string
}
func (c *Controller) StartGame(w http.ResponseWriter, req *http.Request) {
log.Println("asked to create a game")
requested_game_name := idg.Hash()
width, height := float32(conf.Width), float32(conf.Height)
requested_game_name := c.Idg.Hash()
width, height := float32(c.Conf.Width), float32(c.Conf.Height)
obstacles := 0
maxPoints := conf.MaxPoints
maxPoints := c.Conf.MaxPoints
mode := "deathmatch"
// here we determine if we are going to run with defaults or pick them off
@@ -55,10 +63,10 @@ func startGame(w http.ResponseWriter, req *http.Request) {
mode = cfg.Mode
}
g := 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)
g, err := NewGame(requested_game_name, width, height, obstacles, conf.Tick, maxPoints, mode)
g, err := NewGame(requested_game_name, width, height, obstacles, c.Conf.Tick, maxPoints, mode)
if err != nil {
log.Printf("problem creating game: %s: %s", requested_game_name, err)
b, _ := json.Marshal(NewFailure("game creation failure"))
@@ -66,7 +74,7 @@ func startGame(w http.ResponseWriter, req *http.Request) {
return
}
go g.run()
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"))
@@ -84,10 +92,10 @@ func startGame(w http.ResponseWriter, req *http.Request) {
}
}
func listGames(w http.ResponseWriter, req *http.Request) {
func (c *Controller) ListGames(w http.ResponseWriter, req *http.Request) {
log.Println("games list requested")
games.RLock()
defer games.RUnlock()
c.Games.RLock()
defer c.Games.RUnlock()
type pout struct {
Name string `json:"name"`
Id string `json:"id"`
@@ -97,7 +105,7 @@ func listGames(w http.ResponseWriter, req *http.Request) {
Players []pout `json:"players"`
}
ids := make([]gl, 0)
for id, g := range games.m {
for id, g := range c.Games.M {
players := make([]pout, 0)
// TODO - players instead of robots?
for p := range g.players {
@@ -118,19 +126,19 @@ func listGames(w http.ResponseWriter, req *http.Request) {
}
}
func gameStats(w http.ResponseWriter, req *http.Request) {
func (c *Controller) GameStats(w http.ResponseWriter, req *http.Request) {
// TODO: wrap this up in something similar to the JsonHandler to verify the
// url? Look at gorilla routing?
key, err := getGameId(req.URL.Path)
key, err := c.getGameId(req.URL.Path)
if err != nil {
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusBadRequest)
return
}
log.Printf("requested stats for game: %s", key)
games.RLock()
g, ok := games.m[key]
games.RUnlock()
c.Games.RLock()
g, ok := c.Games.M[key]
c.Games.RUnlock()
if !ok {
b, _ := json.Marshal(NewFailure("game not found"))
http.Error(w, string(b), http.StatusNotFound)
@@ -143,19 +151,19 @@ func gameStats(w http.ResponseWriter, req *http.Request) {
}
}
func bw(w http.ResponseWriter, req *http.Request) {
func (c *Controller) BW(w http.ResponseWriter, req *http.Request) {
// TODO: wrap this up in something similar to the JsonHandler to verify the
// url? Look at gorilla routing?
key, err := getGameId(req.URL.Path)
key, err := c.getGameId(req.URL.Path)
if err != nil {
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusBadRequest)
return
}
log.Printf("requested bandwidth for game: %s", key)
games.RLock()
g, ok := games.m[key]
games.RUnlock()
c.Games.RLock()
g, ok := c.Games.M[key]
c.Games.RUnlock()
if !ok {
b, _ := json.Marshal(NewFailure("game not found"))
http.Error(w, string(b), http.StatusNotFound)
@@ -170,16 +178,16 @@ func bw(w http.ResponseWriter, req *http.Request) {
}
}
func stopGame(w http.ResponseWriter, req *http.Request) {
key, err := getGameId(req.URL.Path)
func (c *Controller) StopGame(w http.ResponseWriter, req *http.Request) {
key, err := c.getGameId(req.URL.Path)
if err != nil {
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusBadRequest)
return
}
games.Lock()
g, ok := games.m[key]
defer games.Unlock()
c.Games.Lock()
g, ok := c.Games.M[key]
defer c.Games.Unlock()
if !ok {
http.NotFound(w, req)
return
@@ -197,15 +205,15 @@ func stopGame(w http.ResponseWriter, req *http.Request) {
}
}
func killServer(w http.ResponseWriter, req *http.Request) {
if *profile != "" {
func (c *Controller) KillServer(w http.ResponseWriter, req *http.Request) {
if c.Profile != "" {
log.Print("trying to stop cpu profile")
pprof.StopCPUProfile()
log.Print("stopped cpu profile")
}
if *mprofile != "" {
if c.Memprofile != "" {
log.Print("trying to dump memory profile")
f, err := os.Create(*mprofile)
f, err := os.Create(c.Memprofile)
if err != nil {
log.Fatal(err)
}
@@ -216,7 +224,7 @@ func killServer(w http.ResponseWriter, req *http.Request) {
log.Fatal("shit got fucked up")
}
func index(w http.ResponseWriter, req *http.Request) {
func (c *Controller) Index(w http.ResponseWriter, req *http.Request) {
log.Println("version requested")
version := struct {
Version string `json:"version"`
@@ -230,7 +238,7 @@ func index(w http.ResponseWriter, req *http.Request) {
}
}
func getGameId(path string) (string, error) {
func (c *Controller) getGameId(path string) (string, error) {
var err error
trimmed := strings.Trim(path, "/")
fullPath := strings.Split(trimmed, "/")