server/protocol.go

275 lines
5.7 KiB
Go
Raw Normal View History

2013-08-21 07:53:34 -07:00
package main
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"
)
// < the name of the game we want to join
2013-09-27 22:27:05 -07:00
type GameID struct {
Id string `json:"id"`
}
// > identify
type PlayerID struct {
Type string `json:"type"`
Hash string `json:"id"`
2013-09-27 22:27:05 -07:00
Failure
}
func NewPlayerID(id string) *PlayerID {
return &PlayerID{
Type: "idreq",
Hash: id,
2013-09-27 22:27:05 -07:00
}
}
// < [robot | spectator], name, client-type, game ID
type ClientID struct {
Type string `json:"type"`
Name string `json:"name"`
Useragent string `json:"useragent"`
}
func (c *ClientID) Valid() (bool, string) {
switch c.Type {
case "robot", "spectator":
return true, ""
}
return false, "usergent must be 'robot' or 'spectator'"
}
type ClientConfig struct {
2013-11-08 21:25:42 -08:00
ID string `json:"id"`
Stats map[string]StatsRequest `json:"stats"`
}
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
}
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
}
type GameParam struct {
BoardSize BoardSize `json:"boardsize"`
Type string `json:"type"`
}
// > [OK | FULL | NOT AUTH], board size, game params
func NewGameParam(w, h float32) *GameParam {
2013-09-27 22:27:05 -07:00
return &GameParam{
BoardSize: BoardSize{
Width: w,
Height: h,
},
Type: "gameparam",
}
}
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",
}
}
2013-11-13 22:24:54 -08:00
type Message interface {
}
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",
}
}
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
}
}
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",
}
}
func 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 := games.get(gid.Id)
if game == nil {
game = NewGame(
gid.Id,
float32(conf.Width),
float32(conf.Height),
2013-11-16 19:57:13 -08:00
conf.Obstacles,
conf.Tick,
2013-11-29 00:10:49 -08:00
conf.MaxPoints,
"",
)
go game.run()
games.add(game)
}
player_id := 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
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
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
}
gameParam := NewGameParam(game.width, game.height)
2013-08-21 07:53:34 -07:00
err = websocket.JSON.Send(ws, gameParam)
if err != nil {
log.Printf("%s %s game param parse error", gid.Id, player_id)
websocket.JSON.Send(ws, NewFailure("game param parse error"))
return
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)
2013-09-27 00:03:32 -07:00
log.Printf("conf received: %+v", conf)
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) {
2013-11-08 21:25:42 -08:00
log.Printf("Config is Valid, continuing")
_ = websocket.JSON.Send(ws, NewHandshake(player_id, true))
2013-08-21 07:53:34 -07:00
break
} else {
2013-11-08 21:25:42 -08:00
log.Printf("Config is INVALID, abort")
_ = websocket.JSON.Send(ws, NewHandshake(player_id, false))
2013-08-21 07:53:34 -07:00
}
}
p := &player{
2013-11-08 22:26:56 -08:00
Robots: []*Robot{},
2013-11-13 22:24:54 -08:00
send: make(chan Message),
2013-11-08 21:25:42 -08:00
ws: ws,
2014-01-16 00:02:59 -08:00
Id: idg.Hash(),
2013-11-08 21:25:42 -08:00
}
2013-11-08 22:59:56 -08:00
for name, stats := range conf.Stats {
2013-11-08 21:25:42 -08:00
r := Robot{
Stats: DeriveStats(stats),
2013-11-08 22:26:56 -08:00
Id: idg.Hash(),
2013-11-08 22:59:56 -08:00
Name: name,
2013-11-08 21:25:42 -08:00
Health: 10,
Heading: v.Vector2d{1, 0},
2013-11-08 21:25:42 -08:00
Scanners: make([]Scanner, 0)}
r.Health = r.Stats.Hp
2013-11-13 23:47:42 -08:00
log.Printf("Adding Robot: %+v", r)
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
game.register <- p
2014-01-16 00:02:59 -08:00
defer func() {
game.unregister <- p
}()
go p.sender()
p.recv()
2013-11-08 21:25:42 -08:00
log.Printf("game %s: player %v has been disconnected from this game\n", gid.Id, p.Robots[0].Id)
2013-08-21 07:53:34 -07:00
case "spectator":
s := &Spectator{
2013-11-13 22:24:54 -08:00
send: make(chan Message),
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)
2013-08-21 07:53:34 -07:00
}
}