2013-08-28 23:46:12 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-09-01 23:00:09 -07:00
|
|
|
"encoding/json"
|
2013-11-13 23:45:02 -08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-11-06 21:12:10 -08:00
|
|
|
"io/ioutil"
|
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) {
|
2013-10-14 00:17:12 -07:00
|
|
|
log.Println("asked to create a game")
|
2013-11-06 21:12:10 -08:00
|
|
|
|
|
|
|
requested_game_name := idg.Hash()
|
2013-11-13 20:38:57 -08:00
|
|
|
width, height := float32(conf.Width), float32(conf.Height)
|
|
|
|
tick := conf.Tick
|
2013-11-06 21:12:10 -08:00
|
|
|
|
|
|
|
// 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()
|
2013-11-13 20:38:57 -08:00
|
|
|
cfg := struct {
|
|
|
|
Width float32 `json:"width"`
|
2013-11-06 21:12:10 -08:00
|
|
|
Height float32 `json:"height"`
|
|
|
|
Name string `json:"name"`
|
2013-11-13 20:38:57 -08:00
|
|
|
Tick int `json:"tick"`
|
2013-11-06 21:12:10 -08:00
|
|
|
}{}
|
2013-11-13 20:38:57 -08:00
|
|
|
err = json.Unmarshal(body, &cfg)
|
2013-11-06 21:12:10 -08:00
|
|
|
if err != nil {
|
|
|
|
if err := json.NewEncoder(w).Encode(NewFailure(err.Error())); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2013-11-13 20:38:57 -08:00
|
|
|
requested_game_name = cfg.Name
|
|
|
|
width = cfg.Width
|
|
|
|
height = cfg.Height
|
|
|
|
tick = cfg.Tick
|
2013-11-06 21:12:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
g := games.get(requested_game_name)
|
|
|
|
if g == nil {
|
|
|
|
log.Printf("Game '%s' non-existant; making it now", requested_game_name)
|
2013-11-13 20:38:57 -08:00
|
|
|
g = NewGame(requested_game_name, width, height, tick)
|
2013-11-06 21:12:10 -08:00
|
|
|
go g.run()
|
|
|
|
games.add(g)
|
|
|
|
} else {
|
|
|
|
log.Printf("Game '%s' found: %p", requested_game_name, g)
|
2013-10-18 23:55:52 -07:00
|
|
|
}
|
2013-09-01 23:00:09 -07:00
|
|
|
|
2013-09-28 12:59:04 -07:00
|
|
|
game_json := struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
}{
|
2013-10-18 23:55:52 -07:00
|
|
|
Id: g.id,
|
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-10-14 00:17:12 -07:00
|
|
|
log.Println("games list requested")
|
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-11-08 21:25:42 -08:00
|
|
|
// TODO - players instead of robots?
|
2013-11-09 21:52:03 -08:00
|
|
|
for p := range g.players {
|
2013-11-08 21:25:42 -08:00
|
|
|
for _, r := range p.Robots {
|
|
|
|
players = append(players, pout{
|
|
|
|
Name: r.Name,
|
|
|
|
Id: r.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
|
|
|
|
2013-11-13 23:45:02 -08:00
|
|
|
func gameStats(w http.ResponseWriter, req *http.Request) {
|
2013-11-15 08:54:52 -08:00
|
|
|
// TODO: wrap this up in something similar to the JsonHandler to verify the
|
|
|
|
// url? Look at gorilla routing?
|
2013-11-13 23:45:02 -08:00
|
|
|
key, err := getGameId(req.URL.Path)
|
|
|
|
if err != nil {
|
2013-11-15 08:54:52 -08:00
|
|
|
b, _ := json.Marshal(NewFailure(err.Error()))
|
|
|
|
http.Error(w, string(b), http.StatusBadRequest)
|
|
|
|
return
|
2013-11-13 23:45:02 -08:00
|
|
|
}
|
|
|
|
log.Printf("requested stats for game: %s", key)
|
|
|
|
games.RLock()
|
|
|
|
g, ok := games.m[key]
|
|
|
|
games.RUnlock()
|
|
|
|
if !ok {
|
2013-11-15 08:54:52 -08:00
|
|
|
b, _ := json.Marshal(NewFailure("game not found"))
|
|
|
|
http.Error(w, string(b), http.StatusNotFound)
|
2013-09-28 12:59:04 -07:00
|
|
|
return
|
|
|
|
}
|
2013-11-13 23:45:02 -08:00
|
|
|
g.winners.RLock()
|
|
|
|
defer g.winners.RUnlock()
|
|
|
|
if err := json.NewEncoder(w).Encode(g.winners.m); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopGame(w http.ResponseWriter, req *http.Request) {
|
|
|
|
key, err := getGameId(req.URL.Path)
|
|
|
|
if err != nil {
|
2013-11-15 08:54:52 -08:00
|
|
|
b, _ := json.Marshal(NewFailure(err.Error()))
|
|
|
|
http.Error(w, string(b), http.StatusBadRequest)
|
|
|
|
return
|
2013-11-13 23:45:02 -08:00
|
|
|
}
|
2013-09-28 12:59:04 -07:00
|
|
|
games.Lock()
|
2013-11-13 23:45:02 -08:00
|
|
|
g, ok := games.m[key]
|
2013-09-28 12:59:04 -07:00
|
|
|
defer games.Unlock()
|
|
|
|
if !ok {
|
|
|
|
http.NotFound(w, req)
|
|
|
|
return
|
|
|
|
}
|
2013-11-14 00:02:26 -08:00
|
|
|
log.Println("prekill")
|
2013-11-13 23:45:02 -08:00
|
|
|
g.kill <- true
|
2013-11-14 00:02:26 -08:00
|
|
|
log.Println("postkill")
|
2013-11-13 23:45:02 -08:00
|
|
|
message := struct {
|
|
|
|
Ok bool `json:"ok"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}{
|
|
|
|
Ok: true,
|
|
|
|
Message: fmt.Sprintf("Successfully stopped game: %s", key),
|
|
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(message); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2013-09-28 12:59:04 -07:00
|
|
|
}
|
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
|
|
|
}
|
2013-11-07 22:05:20 -08:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 23:45:02 -08:00
|
|
|
|
|
|
|
func getGameId(path string) (string, error) {
|
|
|
|
var err error
|
|
|
|
trimmed := strings.Trim(path, "/")
|
|
|
|
fullPath := strings.Split(trimmed, "/")
|
|
|
|
if len(fullPath) != 3 {
|
|
|
|
return "", errors.New("improperly formed url")
|
|
|
|
}
|
|
|
|
key := fullPath[2]
|
|
|
|
return key, err
|
|
|
|
}
|