2013-08-19 20:43:26 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-08-19 21:42:43 -07:00
|
|
|
"bitbucket.org/hackerbots/botserv/protocol"
|
|
|
|
v "bitbucket.org/hackerbots/vector"
|
2013-08-19 20:43:26 -07:00
|
|
|
"code.google.com/p/go.net/websocket"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var addr = flag.String("addr", ":8666", "http service address")
|
|
|
|
var velocity = flag.Float64("velocity", 30, "")
|
|
|
|
var tick = flag.Int("tick", 33, "")
|
|
|
|
var weapon_radius = flag.Int("weapon_radius", 35, "")
|
|
|
|
var verbose = flag.Bool("verbose", false, "")
|
|
|
|
var width = flag.Float64("width", 800, "width of field")
|
|
|
|
var height = flag.Float64("height", 550, "height of field")
|
|
|
|
|
|
|
|
var delta float64
|
|
|
|
var g = game{
|
|
|
|
register: make(chan *player),
|
|
|
|
unregister: make(chan *player),
|
|
|
|
projectiles: make(map[*projectile]bool),
|
|
|
|
splosions: make(map[*splosion]bool),
|
|
|
|
players: make(map[*player]bool),
|
|
|
|
turn: 0,
|
2013-08-19 22:23:35 -07:00
|
|
|
spectators: make(map[*Spectator]bool),
|
|
|
|
sregister: make(chan *Spectator),
|
|
|
|
sunregister: make(chan *Spectator),
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
delta = float64(*tick) / 1000
|
|
|
|
|
|
|
|
http.Handle("/ws/", websocket.Handler(addPlayer))
|
|
|
|
|
|
|
|
go g.run()
|
|
|
|
|
|
|
|
err := http.ListenAndServe(*addr, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("unable to start server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addPlayer(ws *websocket.Conn) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
id := fmt.Sprintf("robot%d", <-g.id)
|
|
|
|
log.Printf("sending robot id: %s", id)
|
|
|
|
|
|
|
|
err = websocket.JSON.Send(ws, protocol.NewIdRequest(id))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s: problem sending initial identification", id)
|
|
|
|
websocket.JSON.Send(ws, protocol.NewFailure("generic server error"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var clientid protocol.ClientID
|
|
|
|
err = websocket.JSON.Receive(ws, &clientid)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s: problem parsing clientID", id)
|
|
|
|
websocket.JSON.Send(ws, protocol.NewFailure("could not parse id"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if v, msg := clientid.Valid(); !v {
|
|
|
|
log.Printf("%s: invalid clientid: %+v", id, clientid)
|
|
|
|
websocket.JSON.Send(
|
|
|
|
ws,
|
|
|
|
protocol.NewFailure(msg),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
gameParam := protocol.NewGameParam(*width, *height)
|
|
|
|
err = websocket.JSON.Send(ws, gameParam)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("%s: problem sending game info: %+v", id, gameParam)
|
|
|
|
websocket.JSON.Send(ws, protocol.NewFailure("generic server error"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-19 22:23:35 -07:00
|
|
|
switch clientid.Type {
|
|
|
|
case "robot":
|
|
|
|
var conf Config
|
|
|
|
for {
|
|
|
|
err = websocket.JSON.Receive(ws, &conf)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if conf.Stats.valid() {
|
|
|
|
_ = websocket.JSON.Send(ws, protocol.NewHandshake(id, true))
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
_ = websocket.JSON.Send(ws, protocol.NewHandshake(id, false))
|
|
|
|
log.Printf("%s invalid config", id)
|
|
|
|
}
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
2013-08-19 22:23:35 -07:00
|
|
|
log.Printf("%s eventually sent valid config: %+v", id, conf)
|
2013-08-19 20:43:26 -07:00
|
|
|
|
2013-08-19 22:23:35 -07:00
|
|
|
start_pos := v.Point2d{
|
|
|
|
X: rand.Float64() * *width,
|
|
|
|
Y: rand.Float64() * *height,
|
|
|
|
}
|
|
|
|
p := &player{
|
|
|
|
Robot: robot{
|
|
|
|
Stats: conf.Stats,
|
|
|
|
Position: start_pos,
|
|
|
|
MoveTo: start_pos,
|
|
|
|
Id: id,
|
|
|
|
Health: conf.Stats.Hp,
|
|
|
|
Scanners: make([]scanner, 0)},
|
|
|
|
send: make(chan *boardstate),
|
|
|
|
ws: ws,
|
|
|
|
}
|
|
|
|
g.register <- p
|
|
|
|
defer func() {
|
|
|
|
g.unregister <- p
|
|
|
|
}()
|
|
|
|
go p.sender()
|
|
|
|
p.recv()
|
|
|
|
log.Printf("%v has been disconnect from this game\n", p.Robot.Id)
|
|
|
|
case "spectator":
|
|
|
|
s := &Spectator{
|
|
|
|
send: make(chan *boardstate),
|
|
|
|
ws: ws,
|
|
|
|
}
|
|
|
|
g.sregister <- s
|
|
|
|
defer func() {
|
|
|
|
g.sunregister <- s
|
|
|
|
}()
|
|
|
|
s.sender()
|
|
|
|
log.Printf("%+v has been disconnect from this game\n", s)
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
}
|