server/protocol.go

371 lines
9.5 KiB
Go
Raw Normal View History

2014-04-23 14:28:13 -07:00
package server
2013-08-21 07:53:34 -07:00
import (
2013-11-29 00:10:49 -08:00
"log"
v "bitbucket.org/hackerbots/vector"
2013-08-21 07:53:34 -07:00
"code.google.com/p/go.net/websocket"
)
2014-04-14 00:26:41 -07:00
// GameID is essentially the name of the game we want to join
2013-09-27 22:27:05 -07:00
type GameID struct {
Id string `json:"id"`
}
2014-04-14 00:26:41 -07:00
// PlayerID is the internal hash we give to a client
type PlayerID struct {
Type string `json:"type"`
Hash string `json:"id"`
2013-09-27 22:27:05 -07:00
}
func NewPlayerID(id string) *PlayerID {
return &PlayerID{
Type: "idreq",
Hash: id,
2013-09-27 22:27:05 -07:00
}
}
2014-04-14 00:26:41 -07:00
// ClientID is how a player wants to be known
2013-09-27 22:27:05 -07:00
type ClientID struct {
Type string `json:"type"`
Name string `json:"name"`
Useragent string `json:"useragent"`
}
2014-04-14 00:26:41 -07:00
// ClientID.Valid is used to be sure the player connecting is of appropriate
// type.
2013-09-27 22:27:05 -07:00
func (c *ClientID) Valid() (bool, string) {
switch c.Type {
case "robot", "spectator":
return true, ""
}
2014-03-10 00:03:51 -07:00
return false, "useragent must be 'robot' or 'spectator'"
2013-09-27 22:27:05 -07:00
}
2014-04-14 00:26:41 -07:00
// ClientConfig embodies a map of stats requests
type ClientConfig struct {
2013-11-08 21:25:42 -08:00
ID string `json:"id"`
Stats map[string]StatsRequest `json:"stats"`
}
2014-04-14 00:26:41 -07:00
// ClientConfig.Valid is what determins if a player has asked for too many
// points.
2013-11-29 00:10:49 -08:00
func (config ClientConfig) Valid(max int) bool {
2013-11-08 21:25:42 -08:00
total := 0
for _, s := range config.Stats {
total += (s.Speed +
s.Hp +
s.WeaponRadius +
s.ScannerRadius +
s.Acceleration +
s.TurnSpeed +
s.FireRate +
s.WeaponDamage +
s.WeaponSpeed)
}
2013-11-29 00:10:49 -08:00
if total > max {
2013-11-08 21:25:42 -08:00
return false
}
return true
}
2014-04-14 00:26:41 -07:00
// BoardSize is the response containing the geometry of the requested game.
2013-09-27 22:27:05 -07:00
type BoardSize struct {
Width float32 `json:"width"`
Height float32 `json:"height"`
2013-09-27 22:27:05 -07:00
}
2014-04-14 00:26:41 -07:00
// GameParam is sent to the client to tell them of the geometry of the game
// requested, how many points they may use, and what encoding the server will
// use to communicate with the client.
2013-09-27 22:27:05 -07:00
type GameParam struct {
// TODO: should have information about max points in here
2013-09-27 22:27:05 -07:00
BoardSize BoardSize `json:"boardsize"`
MaxPoints int `json:"max_points"`
2014-04-08 23:45:07 -07:00
Encoding string `json:"encoding"`
2013-09-27 22:27:05 -07:00
Type string `json:"type"`
}
2014-04-14 00:26:41 -07:00
// Handshake is simply the response to a client to let them know if the number
// of stats they've asked for is reasonable. If false it means try again.
2013-09-27 22:27:05 -07:00
type Handshake struct {
ID string `json:"id"`
Success bool `json:"success"`
Type string `json:"type"`
}
func NewHandshake(id string, success bool) *Handshake {
return &Handshake{
ID: id,
Success: success,
Type: "handshake",
}
}
2014-04-14 00:26:41 -07:00
// Message is an empty interface used to send out arbitrary JSON/gob to
// clients, both players/spectators. We might send out Boardstate or GameOver,
// hence the interface{}.
type Message interface{}
2013-11-13 22:24:54 -08:00
2014-04-14 00:26:41 -07:00
// Boardstate is the main struct calculated every tick (per player) and sent
// out to clients. It contains the appropriate subset of all data needed by
// each player/spectator.
2013-11-13 22:24:54 -08:00
type Boardstate struct {
MyRobots []Robot `json:"my_robots"`
OtherRobots []OtherRobot `json:"robots"`
Projectiles []Projectile `json:"projectiles"`
Splosions []Splosion `json:"splosions"`
Objects [][4]int `json:"objects"`
2013-11-13 22:24:54 -08:00
Type string `json:"type"`
Turn int `json:"turn"`
AllBots []BotHealth `json:"all_bots"`
Messages []string `json:"messages"`
}
func NewBoardstate() *Boardstate {
return &Boardstate{
MyRobots: []Robot{},
OtherRobots: []OtherRobot{},
Projectiles: []Projectile{},
Splosions: []Splosion{},
AllBots: []BotHealth{},
Type: "boardstate",
}
}
2014-04-14 00:26:41 -07:00
// Special outbound message with a []string of winners.
2013-11-13 22:24:54 -08:00
type GameOver struct {
2013-11-13 23:45:02 -08:00
Winners []string `json:"winners"`
Type string `json:"type"`
2013-11-13 22:24:54 -08:00
}
func NewGameOver() *GameOver {
return &GameOver{
2013-11-13 23:45:02 -08:00
Type: "gameover",
Winners: make([]string, 0),
2013-11-13 22:24:54 -08:00
}
}
2014-04-14 00:26:41 -07:00
// Failure is a simple stuct that is typically converted to JSON and sent out
// to the clients so they can know why something has failed.
2013-09-27 22:27:05 -07:00
type Failure struct {
Reason string `json:"reason"`
Type string `json:"type"`
}
func NewFailure(reason string) *Failure {
return &Failure{
Reason: reason,
Type: "failure",
}
}
2014-04-14 00:26:41 -07:00
// Controller.AddPlayer is the HTTP -> websocket route that is used to
// negociate a connection with a player/spectator.
func (c *Controller) AddPlayer(ws *websocket.Conn) {
var gid GameID
err := websocket.JSON.Receive(ws, &gid)
if err != nil {
log.Println("problem parsing the requested game id")
return
}
game := c.Games.Get(gid.Id)
if game == nil {
var err error
game, err = NewGame(
gid.Id,
float32(c.Conf.Width),
float32(c.Conf.Height),
c.Conf.Obstacles,
c.Conf.Tick,
c.Conf.MaxPoints,
"",
)
if err != nil {
log.Printf("problem creating game: %s", gid.Id)
websocket.JSON.Send(ws, NewFailure("game creation error"))
return
}
go game.run()
c.Games.Add(game)
}
player_id := c.Idg.Hash()
err = websocket.JSON.Send(ws, NewPlayerID(player_id))
2013-08-21 07:53:34 -07:00
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
2014-01-29 22:12:26 -08:00
} else {
log.Printf("game %s: sent player id: %s", gid.Id, player_id)
2013-08-21 07:53:34 -07:00
}
2013-09-27 22:27:05 -07:00
var clientid ClientID
2013-08-21 07:53:34 -07:00
err = websocket.JSON.Receive(ws, &clientid)
if err != nil {
log.Printf("unable to parse ClientID: gid: %s, player: %s", gid.Id, player_id)
websocket.JSON.Send(ws, NewFailure("parse error"))
return
2014-01-29 22:12:26 -08:00
} else {
log.Printf("game %s: recieved: %+v", gid.Id, clientid)
2013-08-21 07:53:34 -07:00
}
if v, msg := clientid.Valid(); !v {
2013-09-27 00:03:32 -07:00
log.Printf("clientid is invalid: %+v", clientid)
2013-08-21 07:53:34 -07:00
websocket.JSON.Send(
ws,
2013-09-27 22:27:05 -07:00
NewFailure(msg),
2013-08-21 07:53:34 -07:00
)
return
2013-08-21 07:53:34 -07:00
}
reqEncs := []string{}
err = websocket.JSON.Receive(ws, &reqEncs)
if err != nil {
log.Printf("%s %s unable to parse requested encodings", gid.Id, player_id)
websocket.JSON.Send(ws, NewFailure("encoding recieve error"))
return
}
prefEncs := []string{
"gob",
"json",
}
var encoding string
encodingLoops:
for _, prefEnc := range prefEncs {
for _, reqEnc := range reqEncs {
if reqEnc == prefEnc {
encoding = prefEnc
log.Println("selected following encoding:", encoding)
break encodingLoops
}
}
}
if encoding == "" {
log.Printf("%s %s unable to negociate encoding", gid.Id, player_id)
websocket.JSON.Send(
ws,
NewFailure("no overlap on supported encodings; I suggest using json"),
)
return
}
gameParam := game.gameParam()
gp := struct {
GameParam
Encoding string `json:"encoding"`
}{
GameParam: *gameParam,
Encoding: encoding,
}
err = websocket.JSON.Send(ws, gp)
2013-08-21 07:53:34 -07:00
if err != nil {
log.Printf("%s %s game param send error", gid.Id, player_id)
websocket.JSON.Send(ws, NewFailure("game param send error"))
return
2014-01-29 22:12:26 -08:00
} else {
log.Printf("%s -> %s: sent %+v", gid.Id, player_id, gameParam)
2013-08-21 07:53:34 -07:00
}
2013-08-21 07:53:34 -07:00
switch clientid.Type {
case "robot":
var conf ClientConfig
2013-08-21 07:53:34 -07:00
for {
log.Printf("%s Waiting for client to send conf ...", player_id)
2013-08-21 07:53:34 -07:00
err = websocket.JSON.Receive(ws, &conf)
2014-01-29 22:12:26 -08:00
log.Printf("%s: conf received: %s", player_id, conf.ID)
2013-11-08 21:25:42 -08:00
2013-08-21 07:53:34 -07:00
if err != nil {
log.Printf("%s %s config parse error", gid.Id, player_id)
websocket.JSON.Send(ws, NewFailure("config parse error"))
return
2013-08-21 07:53:34 -07:00
}
2013-11-08 21:25:42 -08:00
// TODO: verify conf's type
2013-11-29 00:10:49 -08:00
if conf.Valid(game.maxPoints) {
2014-01-29 22:12:26 -08:00
log.Printf("%s -> %s: valid client config", gid.Id, player_id)
_ = websocket.JSON.Send(ws, NewHandshake(player_id, true))
2013-08-21 07:53:34 -07:00
break
} else {
log.Printf("%s: Config is INVALID, abort", player_id)
_ = websocket.JSON.Send(ws, NewFailure("invalid config"))
return
2013-08-21 07:53:34 -07:00
}
}
p := NewPlayer(player_id, ws, game.bw, encoding)
2014-01-29 22:12:26 -08:00
log.Printf("%s: made a player: %s", gid.Id, p.Id)
2013-11-08 21:25:42 -08:00
convertedStats := map[string]Stats{}
2013-11-08 22:59:56 -08:00
for name, stats := range conf.Stats {
dstat := DeriveStats(stats)
convertedStats[name] = dstat
2013-11-08 21:25:42 -08:00
r := Robot{
Stats: dstat,
Id: c.Idg.Hash(),
2013-11-08 22:59:56 -08:00
Name: name,
2013-11-08 21:25:42 -08:00
Health: 10,
2014-04-14 00:39:42 -07:00
Heading: v.Vector2d{X: 1, Y: 0},
Scanners: make([]Scanner, 0),
Delta: c.Conf.Delta,
idg: c.Idg,
}
2013-11-08 21:25:42 -08:00
r.Health = r.Stats.Hp
2014-01-23 21:09:27 -08:00
log.Printf("%s: adding robot: %s", p.Id, r.Id)
2013-11-08 21:25:42 -08:00
r.reset(game)
2013-11-08 22:26:56 -08:00
p.Robots = append(p.Robots, &r)
}
2013-11-08 21:25:42 -08:00
statsPayload := struct {
Stats map[string]Stats `json:"stats"`
Type string `json:"type"`
}{
Stats: convertedStats,
Type: "stats",
}
err = websocket.JSON.Send(ws, &statsPayload)
if err != nil {
log.Printf("error sending convertedStats to client: %s", err)
websocket.JSON.Send(ws, NewFailure("protocol error: convertedStats"))
return
2014-01-29 22:12:26 -08:00
} else {
log.Printf("%s -> %s: sent stats payload", gid.Id, p.Id)
}
2014-01-29 22:12:26 -08:00
log.Printf("%s, %s: about to register this player", gid.Id, p.Id)
game.register <- p
2014-01-29 22:12:26 -08:00
log.Printf("%s, %s: registered player", gid.Id, p.Id)
2014-01-16 00:02:59 -08:00
defer func() {
2014-01-29 22:12:26 -08:00
log.Printf("%s, %s: about to unregister this player", gid.Id, p.Id)
game.unregister <- p
2014-01-29 22:12:26 -08:00
log.Printf("%s, %s: unregistered player", gid.Id, p.Id)
}()
2014-04-14 00:26:41 -07:00
go p.Sender()
2014-01-29 22:12:26 -08:00
log.Printf("%s -> %s: p.sender went", gid.Id, p.Id)
2014-04-14 00:26:41 -07:00
p.Recv()
2014-01-23 21:09:27 -08:00
log.Printf(
"%s (player): %v (robot) has been disconnected from %s (game)",
p.Id,
p.Robots[0].Id,
gid.Id,
)
2013-08-21 07:53:34 -07:00
case "spectator":
s := NewSpectator(player_id, ws, game.bw, encoding)
2014-01-29 22:12:26 -08:00
log.Printf("%s, %s: about to register this spectator", gid.Id, s.Id)
game.sregister <- s
2014-01-29 22:12:26 -08:00
log.Printf("%s, %s: registered spectator", gid.Id, s.Id)
defer func() {
2014-01-29 22:12:26 -08:00
log.Printf("%s, %s: about to unregister this spectator", gid.Id, s.Id)
game.sunregister <- s
2014-01-29 22:12:26 -08:00
log.Printf("%s, %s: unregistered spectator", gid.Id, s.Id)
}()
2014-04-14 00:26:41 -07:00
go s.Sender()
2014-01-29 22:12:26 -08:00
log.Printf("%s -> %s: s.sender went", gid.Id, s.Id)
2014-04-14 00:26:41 -07:00
s.Recv()
2014-01-29 22:12:26 -08:00
log.Printf("game %s: spectator %+v has been disconnected from this game", gid.Id, s)
2013-08-21 07:53:34 -07:00
}
log.Printf("exiting AddPlayer")
2013-08-21 07:53:34 -07:00
}