some clean up

Mainly adding some comments, and some function renaming
This commit is contained in:
sm
2013-09-28 12:59:17 -07:00
parent c5bddcdc31
commit 56c965add7
6 changed files with 47 additions and 38 deletions
+38 -33
View File
@@ -2,7 +2,6 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
@@ -24,7 +23,44 @@ func startGame(w http.ResponseWriter, req *http.Request) {
games.m[new_game_name] = _g
games.Unlock()
w.Write([]byte(fmt.Sprintf(`{"id": "%s"}`, new_game_name)))
game_json := struct {
Id string `json:"id"`
}{
Id: new_game_name,
}
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) {
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)
for p, _ := range g.players {
players = append(players, pout{
Name: p.Robot.Name,
Id: p.Robot.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) {
@@ -44,34 +80,3 @@ func stopGame(w http.ResponseWriter, req *http.Request) {
}
gameid.kill <- true
}
func listGames(w http.ResponseWriter, req *http.Request) {
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)
for p, _ := range g.players {
// XXX: change this to be the user-provided bot name?
players = append(players, pout{
Name: p.Robot.Name,
Id: p.Robot.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)
}
}