moved all negociation code into a single function

This commit is contained in:
Stephen McQuay 2013-09-28 12:37:23 -07:00
parent b142845b5c
commit 2a2f4f6f96
3 changed files with 84 additions and 97 deletions

View File

@ -9,13 +9,6 @@ import (
const maxPlayer = 128 const maxPlayer = 128
type Config struct {
ID string `json:"id"`
Name string `json:"name"`
// TODO: candidate for embedding?
Stats Stats `json:"stats"`
}
type Boardstate struct { type Boardstate struct {
Robots []Robot `json:"robots"` Robots []Robot `json:"robots"`
Projectiles []Projectile `json:"projectiles"` Projectiles []Projectile `json:"projectiles"`

61
http.go
View File

@ -1,10 +1,8 @@
package main package main
import ( import (
"code.google.com/p/go.net/websocket"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"strings" "strings"
) )
@ -77,62 +75,3 @@ func listGames(w http.ResponseWriter, req *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
} }
func addPlayer(ws *websocket.Conn) {
// route to appropriate game ...
var gid GameID
err := websocket.JSON.Receive(ws, &gid)
if err != nil {
log.Println("problem parsing the requested game id")
return
}
games.Lock()
game, ok := games.m[gid.Id]
games.Unlock()
if !ok {
log.Println("ERROR: game not found")
websocket.JSON.Send(ws, NewFailure("game 404"))
return
}
id := idg.Hash()
conf, err := Negociate(ws, id, game.width, game.height)
if err != nil {
websocket.JSON.Send(ws, NewFailure(err.Error()))
}
if conf != nil {
p := &player{
Robot: Robot{
Stats: conf.Stats,
Id: id,
Name: conf.Name,
Health: conf.Stats.Hp,
Scanners: make([]Scanner, 0)},
send: make(chan *Boardstate),
ws: ws,
}
p.reset()
game.register <- p
defer func() {
game.unregister <- p
}()
go p.sender()
p.recv()
log.Printf("game %s: player %v has been disconnected from this game\n", gid.Id, p.Robot.Id)
} else {
s := &Spectator{
send: make(chan *Boardstate),
ws: ws,
}
game.sregister <- s
defer func() {
game.sunregister <- s
}()
s.sender()
log.Printf("game %s: spectator %+v has been disconnected from this game\n", s)
}
}

View File

@ -2,25 +2,25 @@ package main
import ( import (
"code.google.com/p/go.net/websocket" "code.google.com/p/go.net/websocket"
"errors"
"log" "log"
) )
// < the name of the game we want to join
type GameID struct { type GameID struct {
Id string `json:"id"` Id string `json:"id"`
} }
// > identify // > identify
type IdRequest struct { type PlayerID struct {
Type string `json:"type"` Type string `json:"type"`
AssignedID string `json:"id"` Hash string `json:"id"`
Failure Failure
} }
func NewIdRequest(id string) *IdRequest { func NewPlayerID(id string) *PlayerID {
return &IdRequest{ return &PlayerID{
Type: "idreq", Type: "idreq",
AssignedID: id, Hash: id,
} }
} }
@ -39,6 +39,11 @@ func (c *ClientID) Valid() (bool, string) {
return false, "usergent must be 'robot' or 'spectator'" return false, "usergent must be 'robot' or 'spectator'"
} }
type ClientConfig struct {
ID string `json:"id"`
Stats Stats `json:"stats"`
}
type BoardSize struct { type BoardSize struct {
Width float64 `json:"width"` Width float64 `json:"width"`
Height float64 `json:"height"` Height float64 `json:"height"`
@ -86,18 +91,39 @@ func NewFailure(reason string) *Failure {
} }
} }
func Negociate(ws *websocket.Conn, id string, width, height float64) (*Config, error) { func addPlayer(ws *websocket.Conn) {
var err error // route to appropriate game ...
var gid GameID
err = websocket.JSON.Send(ws, NewIdRequest(id)) err := websocket.JSON.Receive(ws, &gid)
if err != nil { if err != nil {
return nil, errors.New("generic server error") log.Println("problem parsing the requested game id")
return
}
games.Lock()
game, ok := games.m[gid.Id]
games.Unlock()
if !ok {
log.Printf("ERROR: game %s not found", gid.Id)
websocket.JSON.Send(ws, NewFailure("game 404"))
return
}
player_id := idg.Hash()
err = websocket.JSON.Send(ws, NewPlayerID(player_id))
if err != nil {
log.Printf("game %s: unable to send player_id to player %s", gid.Id, player_id)
websocket.JSON.Send(ws, NewFailure("send error"))
return
} }
var clientid ClientID var clientid ClientID
err = websocket.JSON.Receive(ws, &clientid) err = websocket.JSON.Receive(ws, &clientid)
if err != nil { if err != nil {
return nil, errors.New("could not parse id") log.Printf("unable to parse ClientID: gid: %s, player: %s", gid.Id, player_id)
websocket.JSON.Send(ws, NewFailure("parse error"))
return
} }
if v, msg := clientid.Valid(); !v { if v, msg := clientid.Valid(); !v {
log.Printf("clientid is invalid: %+v", clientid) log.Printf("clientid is invalid: %+v", clientid)
@ -105,40 +131,69 @@ func Negociate(ws *websocket.Conn, id string, width, height float64) (*Config, e
ws, ws,
NewFailure(msg), NewFailure(msg),
) )
return nil, errors.New(msg) return
} }
log.Printf("clientid: %+v", clientid) log.Printf("%s: %s clientid: %+v", gid.Id, player_id, clientid)
gameParam := NewGameParam(width, height) gameParam := NewGameParam(game.width, game.height)
err = websocket.JSON.Send(ws, gameParam) err = websocket.JSON.Send(ws, gameParam)
if err != nil { if err != nil {
websocket.JSON.Send(ws, NewFailure("generic server error")) log.Printf("%s %s game param parse error", gid.Id, player_id)
return nil, err websocket.JSON.Send(ws, NewFailure("game param parse error"))
return
} }
log.Printf("gameparam: %+v", gameParam) log.Printf("gameparam: %+v", gameParam)
switch clientid.Type { switch clientid.Type {
case "robot": case "robot":
var conf Config var conf ClientConfig
log.Printf("got here?")
for { for {
log.Printf("%s Waiting for client to send conf ...", id) log.Printf("%s Waiting for client to send conf ...", player_id)
err = websocket.JSON.Receive(ws, &conf) err = websocket.JSON.Receive(ws, &conf)
log.Printf("conf received: %+v", conf) log.Printf("conf received: %+v", conf)
if err != nil { if err != nil {
return nil, err log.Printf("%s %s config parse error", gid.Id, player_id)
websocket.JSON.Send(ws, NewFailure("config parse error"))
return
} }
// TODO: verify conf's type // TODO: verify conf's type
if conf.Stats.Valid() { if conf.Stats.Valid() {
_ = websocket.JSON.Send(ws, NewHandshake(id, true)) _ = websocket.JSON.Send(ws, NewHandshake(player_id, true))
break break
} else { } else {
_ = websocket.JSON.Send(ws, NewHandshake(id, false)) _ = websocket.JSON.Send(ws, NewHandshake(player_id, false))
} }
} }
conf.Name = clientid.Name
return &conf, nil p := &player{
Robot: Robot{
Stats: conf.Stats,
Id: player_id,
Name: clientid.Name,
Health: conf.Stats.Hp,
Scanners: make([]Scanner, 0)},
send: make(chan *Boardstate),
ws: ws,
}
p.reset()
game.register <- p
defer func() {
game.unregister <- p
}()
go p.sender()
p.recv()
log.Printf("game %s: player %v has been disconnected from this game\n", gid.Id, p.Robot.Id)
case "spectator": case "spectator":
return nil, nil //return nil, nil
s := &Spectator{
send: make(chan *Boardstate),
ws: ws,
}
game.sregister <- s
defer func() {
game.sunregister <- s
}()
s.sender()
log.Printf("game %s: spectator %+v has been disconnected from this game\n", s)
} }
return nil, nil
} }