@ -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. I dg. Hash ( )
width , height := float32 ( c . C onf. Width ) , float32 ( c . C onf. Height )
obstacles := 0
maxPoints := conf . MaxPoints
maxPoints := c . C onf. 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 := g ames. get ( requested_game_name )
g := c. G ames. 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 , c onf. Tick , maxPoints , mode )
g , err := NewGame ( requested_game_name , width , height , obstacles , c . C onf. 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 ( )
g ames. add ( g )
c. G ames. 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 l istGames( w http . ResponseWriter , req * http . Request ) {
func ( c * Controller ) L istGames( w http . ResponseWriter , req * http . Request ) {
log . Println ( "games list requested" )
g ames. RLock ( )
defer g ames. RUnlock ( )
c. G ames. RLock ( )
defer c. G ames. 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 g ameStats( w http . ResponseWriter , req * http . Request ) {
func ( c * Controller ) G ameStats( 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 )
g ames. RLock ( )
g , ok := games. m [ key ]
g ames. RUnlock ( )
c. G ames. RLock ( )
g , ok := c. Games . M [ key ]
c. G ames. 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 )
g ames. RLock ( )
g , ok := games. m [ key ]
g ames. RUnlock ( )
c. G ames. RLock ( )
g , ok := c. Games . M [ key ]
c. G ames. 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 s topGame( w http . ResponseWriter , req * http . Request ) {
key , err := getGameId ( req . URL . Path )
func ( c * Controller ) S topGame( 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
}
g ames. Lock ( )
g , ok := games. m [ key ]
defer g ames. Unlock ( )
c. G ames. Lock ( )
g , ok := c. Games . M [ key ]
defer c. G ames. Unlock ( )
if ! ok {
http . NotFound ( w , req )
return
@ -197,15 +205,15 @@ func stopGame(w http.ResponseWriter, req *http.Request) {
}
}
func k illServer( w http . ResponseWriter , req * http . Request ) {
if * p rofile != "" {
func ( c * Controller ) K illServer( w http . ResponseWriter , req * http . Request ) {
if c . P rofile != "" {
log . Print ( "trying to stop cpu profile" )
pprof . StopCPUProfile ( )
log . Print ( "stopped cpu profile" )
}
if * mprofile != "" {
if c . Me mprofile != "" {
log . Print ( "trying to dump memory profile" )
f , err := os . Create ( * mprofile)
f , err := os . Create ( c . Me mprofile)
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 i ndex( w http . ResponseWriter , req * http . Request ) {
func ( c * Controller ) I ndex( 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 , "/" )