server/control.go
2013-11-08 21:25:42 -08:00

146 lines
3.3 KiB
Go

package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"runtime/pprof"
"strings"
)
type JsonHandler func(http.ResponseWriter, *http.Request)
func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
h(w, req)
}
func startGame(w http.ResponseWriter, req *http.Request) {
log.Println("asked to create a game")
requested_game_name := idg.Hash()
width, height := float32(*width), float32(*height)
// here we determine if we are going to run with defaults or pick them off
// a posted json blob
if req.Method == "POST" {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("unable to read request body:", err)
}
req.Body.Close()
conf := struct {
With float32 `json:"width"`
Height float32 `json:"height"`
Name string `json:"name"`
}{}
err = json.Unmarshal(body, &conf)
if err != nil {
NewFailure(err.Error())
if err := json.NewEncoder(w).Encode(NewFailure(err.Error())); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
requested_game_name = conf.Name
width = conf.With
height = conf.Height
}
g := games.get(requested_game_name)
if g == nil {
log.Printf("Game '%s' non-existant; making it now", requested_game_name)
g = NewGame(requested_game_name, width, height)
go g.run()
games.add(g)
} else {
log.Printf("Game '%s' found: %p", requested_game_name, g)
}
game_json := struct {
Id string `json:"id"`
}{
Id: g.id,
}
if err := json.NewEncoder(w).Encode(game_json); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func listGames(w http.ResponseWriter, req *http.Request) {
log.Println("games list requested")
games.RLock()
defer games.RUnlock()
type pout struct {
Name string `json:"name"`
Id string `json:"id"`
}
type gl struct {
Id string `json:"id"`
Players []pout `json:"players"`
}
ids := make([]gl, 0)
for id, g := range games.m {
players := make([]pout, 0)
// TODO - players instead of robots?
for p, _ := range g.players {
for _, r := range p.Robots {
players = append(players, pout{
Name: r.Name,
Id: r.Id,
})
}
}
ids = append(ids, gl{
Id: id,
Players: players,
})
}
if err := json.NewEncoder(w).Encode(ids); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func stopGame(w http.ResponseWriter, req *http.Request) {
trimmed := strings.Trim(req.URL.Path, "/")
fullPath := strings.Split(trimmed, "/")
log.Println(req.URL.Path)
if len(fullPath) != 3 {
http.Error(w, "improperly formed url", http.StatusBadRequest)
return
}
key := fullPath[2]
games.Lock()
gameid, ok := games.m[key]
defer games.Unlock()
if !ok {
http.NotFound(w, req)
return
}
gameid.kill <- true
}
func killServer(w http.ResponseWriter, req *http.Request) {
if *profile != "" {
log.Print("trying to stop cpu profile")
pprof.StopCPUProfile()
log.Print("stopped cpu profile")
}
log.Fatal("shit got fucked up")
}
func index(w http.ResponseWriter, req *http.Request) {
log.Println("version requested")
version := struct {
Version string `json:"version"`
Name string `json:"name"`
}{
Version: "0.1.2",
Name: "Hackerbots",
}
if err := json.NewEncoder(w).Encode(version); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}