From 56c965add7e320c2baf6d7b2c74643905a2fb87a Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Sat, 28 Sep 2013 12:59:04 -0700 Subject: [PATCH] some clean up Mainly adding some comments, and some function renaming --- control.go | 71 ++++++++++++++++++++++++++++------------------------- game.go | 1 - id.go | 6 +++++ main.go | 1 + player.go | 4 +-- protocol.go | 2 -- 6 files changed, 47 insertions(+), 38 deletions(-) diff --git a/control.go b/control.go index 18d16f1..f9328c0 100644 --- a/control.go +++ b/control.go @@ -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) - } -} diff --git a/game.go b/game.go index deee642..608d70d 100644 --- a/game.go +++ b/game.go @@ -224,6 +224,5 @@ func (g *game) nudgeProjectiles() (rprojectiles []Projectile) { p.Position.Y = newPos.Y rprojectiles = append(rprojectiles, *p) } - return } diff --git a/id.go b/id.go index 22965fd..8e549bc 100644 --- a/id.go +++ b/id.go @@ -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 } diff --git a/main.go b/main.go index 377ba73..e7c073e 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ type MapLock struct { sync.RWMutex } +// This is the main, global collection of games var games MapLock func main() { diff --git a/player.go b/player.go index d97e32a..9eeb0ab 100644 --- a/player.go +++ b/player.go @@ -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 diff --git a/protocol.go b/protocol.go index 139deb7..ef84673 100644 --- a/protocol.go +++ b/protocol.go @@ -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,