some clean up

Mainly adding some comments, and some function renaming
This commit is contained in:
Stephen McQuay 2013-09-28 12:59:04 -07:00
parent c5bddcdc31
commit 56c965add7
6 changed files with 47 additions and 38 deletions

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)
}
}

View File

@ -224,6 +224,5 @@ func (g *game) nudgeProjectiles() (rprojectiles []Projectile) {
p.Position.Y = newPos.Y
rprojectiles = append(rprojectiles, *p)
}
return
}

6
id.go
View File

@ -7,6 +7,12 @@ import (
"time"
)
// This thing contains a channel that when initialized (see NewIdGenerator)
// will return a bunch of (as best as I can tell) unique md5 hashes.
//
// we use this for naming players, games, etc.
//
// It will consume a single goroutine
type IdGenerator struct {
id chan int64
}

View File

@ -25,6 +25,7 @@ type MapLock struct {
sync.RWMutex
}
// This is the main, global collection of games
var games MapLock
func main() {

View File

@ -62,7 +62,7 @@ func (p *player) recv() {
p.ws.Close()
}
func (p *player) check_collisions(g *game, move_vector v.Vector2d) (bool, v.Point2d) {
func (p *player) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2d) {
collision := false
intersection_point := v.Point2d{X: 0, Y: 0}
@ -114,7 +114,7 @@ func (p *player) nudge(g *game) {
}
move_vector := new_heading.Scale(p.Robot.Speed * delta)
collision, _ := p.check_collisions(g, move_vector)
collision, _ := p.checkCollisions(g, move_vector)
if collision {
// p.Robot.Position = intersection_point
p.Robot.Speed = 0

View File

@ -92,7 +92,6 @@ func NewFailure(reason string) *Failure {
}
func addPlayer(ws *websocket.Conn) {
// route to appropriate game ...
var gid GameID
err := websocket.JSON.Receive(ws, &gid)
if err != nil {
@ -184,7 +183,6 @@ func addPlayer(ws *websocket.Conn) {
p.recv()
log.Printf("game %s: player %v has been disconnected from this game\n", gid.Id, p.Robot.Id)
case "spectator":
//return nil, nil
s := &Spectator{
send: make(chan *Boardstate),
ws: ws,