2013-08-21 07:53:34 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-09-03 23:26:40 -07:00
|
|
|
"bitbucket.org/hackerbots/bot"
|
2013-08-21 07:53:34 -07:00
|
|
|
"code.google.com/p/go.net/websocket"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
2013-09-03 23:26:40 -07:00
|
|
|
func Negociate(ws *websocket.Conn, id string, width, height float64) (*bot.Config, error) {
|
2013-08-21 07:53:34 -07:00
|
|
|
var err error
|
|
|
|
|
2013-09-03 23:26:40 -07:00
|
|
|
err = websocket.JSON.Send(ws, bot.NewIdRequest(id))
|
2013-08-21 07:53:34 -07:00
|
|
|
if err != nil {
|
2013-09-04 23:23:05 -07:00
|
|
|
return nil, errors.New("generic server error")
|
2013-08-21 07:53:34 -07:00
|
|
|
}
|
|
|
|
|
2013-09-03 23:26:40 -07:00
|
|
|
var clientid bot.ClientID
|
2013-08-21 07:53:34 -07:00
|
|
|
err = websocket.JSON.Receive(ws, &clientid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("could not parse id")
|
|
|
|
}
|
|
|
|
if v, msg := clientid.Valid(); !v {
|
|
|
|
websocket.JSON.Send(
|
|
|
|
ws,
|
2013-09-04 23:23:05 -07:00
|
|
|
bot.NewFailure(msg),
|
2013-08-21 07:53:34 -07:00
|
|
|
)
|
|
|
|
return nil, errors.New(msg)
|
|
|
|
}
|
|
|
|
|
2013-09-03 23:26:40 -07:00
|
|
|
gameParam := bot.NewGameParam(width, height)
|
2013-08-21 07:53:34 -07:00
|
|
|
err = websocket.JSON.Send(ws, gameParam)
|
|
|
|
if err != nil {
|
2013-09-04 23:23:05 -07:00
|
|
|
websocket.JSON.Send(ws, bot.NewFailure("generic server error"))
|
2013-08-21 07:53:34 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch clientid.Type {
|
|
|
|
case "robot":
|
2013-09-03 23:26:40 -07:00
|
|
|
var conf bot.Config
|
2013-08-21 07:53:34 -07:00
|
|
|
for {
|
|
|
|
err = websocket.JSON.Receive(ws, &conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-08-26 23:09:39 -07:00
|
|
|
// TODO: verify conf's type
|
2013-09-03 23:26:40 -07:00
|
|
|
if conf.Stats.Valid() {
|
|
|
|
_ = websocket.JSON.Send(ws, bot.NewHandshake(id, true))
|
2013-08-21 07:53:34 -07:00
|
|
|
break
|
|
|
|
} else {
|
2013-09-03 23:26:40 -07:00
|
|
|
_ = websocket.JSON.Send(ws, bot.NewHandshake(id, false))
|
2013-08-21 07:53:34 -07:00
|
|
|
}
|
|
|
|
}
|
2013-09-26 21:51:57 -07:00
|
|
|
conf.Name = clientid.Name
|
2013-08-21 07:53:34 -07:00
|
|
|
return &conf, nil
|
|
|
|
case "spectator":
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|