2013-08-28 23:46:12 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-09-01 23:00:09 -07:00
|
|
|
"encoding/json"
|
2013-09-28 23:00:29 -07:00
|
|
|
"log"
|
2013-08-28 23:46:12 -07:00
|
|
|
"net/http"
|
2013-09-28 23:00:29 -07:00
|
|
|
"runtime/pprof"
|
2013-09-08 09:32:24 -07:00
|
|
|
"strings"
|
2013-08-28 23:46:12 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2013-09-01 23:00:09 -07:00
|
|
|
|
|
|
|
func startGame(w http.ResponseWriter, req *http.Request) {
|
|
|
|
new_game_name := idg.Hash()
|
|
|
|
|
|
|
|
_g := NewGame(new_game_name, *width, *height)
|
|
|
|
go _g.run()
|
|
|
|
|
2013-09-05 18:14:41 -07:00
|
|
|
games.Lock()
|
|
|
|
games.m[new_game_name] = _g
|
|
|
|
games.Unlock()
|
2013-09-01 23:00:09 -07:00
|
|
|
|
2013-09-28 12:59:04 -07:00
|
|
|
game_json := struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
}{
|
|
|
|
Id: new_game_name,
|
2013-09-08 09:32:24 -07:00
|
|
|
}
|
2013-09-28 12:59:04 -07:00
|
|
|
if err := json.NewEncoder(w).Encode(game_json); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-09-08 09:32:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 23:00:09 -07:00
|
|
|
func listGames(w http.ResponseWriter, req *http.Request) {
|
2013-09-05 18:14:41 -07:00
|
|
|
games.RLock()
|
|
|
|
defer games.RUnlock()
|
2013-09-26 21:51:57 -07:00
|
|
|
type pout struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Id string `json:"id"`
|
|
|
|
}
|
2013-09-07 19:12:46 -07:00
|
|
|
type gl struct {
|
2013-09-26 21:51:57 -07:00
|
|
|
Id string `json:"id"`
|
|
|
|
Players []pout `json:"players"`
|
2013-09-07 19:12:46 -07:00
|
|
|
}
|
2013-09-05 23:31:24 -07:00
|
|
|
ids := make([]gl, 0)
|
|
|
|
for id, g := range games.m {
|
2013-09-26 21:51:57 -07:00
|
|
|
players := make([]pout, 0)
|
2013-09-07 19:12:46 -07:00
|
|
|
for p, _ := range g.players {
|
2013-09-26 21:51:57 -07:00
|
|
|
players = append(players, pout{
|
|
|
|
Name: p.Robot.Name,
|
|
|
|
Id: p.Robot.Id,
|
|
|
|
})
|
2013-09-07 19:12:46 -07:00
|
|
|
}
|
2013-09-05 23:31:24 -07:00
|
|
|
ids = append(ids, gl{
|
2013-09-07 19:12:46 -07:00
|
|
|
Id: id,
|
|
|
|
Players: players,
|
|
|
|
})
|
2013-09-01 23:00:09 -07:00
|
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(ids); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2013-09-28 12:59:04 -07:00
|
|
|
|
|
|
|
func stopGame(w http.ResponseWriter, req *http.Request) {
|
|
|
|
trimmed := strings.Trim(req.URL.Path, "/")
|
|
|
|
fullPath := strings.Split(trimmed, "/")
|
|
|
|
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
|
|
|
|
}
|
2013-09-28 23:00:29 -07:00
|
|
|
|
|
|
|
func killServer(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if *profile != "" {
|
2013-09-28 23:03:42 -07:00
|
|
|
log.Print("trying to stop cpu profile")
|
2013-09-28 23:00:29 -07:00
|
|
|
pprof.StopCPUProfile()
|
2013-09-28 23:03:42 -07:00
|
|
|
log.Print("stopped cpu profile")
|
2013-09-28 23:00:29 -07:00
|
|
|
}
|
2013-09-28 23:03:42 -07:00
|
|
|
log.Fatal("shit got fucked up")
|
2013-09-28 23:00:29 -07:00
|
|
|
}
|