2013-08-19 20:43:26 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-08-19 21:42:43 -07:00
|
|
|
"code.google.com/p/go.net/websocket"
|
2014-02-04 08:00:15 -08:00
|
|
|
"encoding/json"
|
2013-08-19 20:43:26 -07:00
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2014-02-16 23:59:17 -08:00
|
|
|
const maxMessageSize = 1024
|
|
|
|
|
2013-08-19 20:43:26 -07:00
|
|
|
type player struct {
|
|
|
|
ws *websocket.Conn
|
2013-11-08 22:26:56 -08:00
|
|
|
Robots []*Robot
|
2013-11-13 22:24:54 -08:00
|
|
|
send chan Message
|
2013-09-27 22:27:05 -07:00
|
|
|
Instruction Instruction
|
2014-01-16 00:02:59 -08:00
|
|
|
Id string
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) sender() {
|
2014-01-29 22:12:26 -08:00
|
|
|
log.Printf("%s: sender launched", p.Id)
|
2013-08-19 20:43:26 -07:00
|
|
|
for things := range p.send {
|
2014-02-16 23:59:17 -08:00
|
|
|
b, err := json.Marshal(things)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// XXX: send the count to our bandwidth analyzer ...
|
|
|
|
_, err = p.ws.Write(b)
|
2013-08-19 20:43:26 -07:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.ws.Close()
|
2014-01-23 21:09:27 -08:00
|
|
|
log.Printf("%s: robot (%s) sender close", p.Id, p.Robots[0].Id)
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) recv() {
|
2014-02-16 23:59:17 -08:00
|
|
|
buff := make([]byte, maxMessageSize)
|
|
|
|
var msgs map[string]Instruction
|
2013-08-19 20:43:26 -07:00
|
|
|
for {
|
2014-02-16 23:59:17 -08:00
|
|
|
n, err := p.ws.Read(buff)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s: problem reading from player: %s", p.Id, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// XXX: send n to our bandwidth analyzer ...
|
|
|
|
if n == len(buff) {
|
|
|
|
log.Printf("%s: read buffer overfull: %s", p.Id, string(buff))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(buff[:n], &msgs)
|
2013-08-19 20:43:26 -07:00
|
|
|
if err != nil {
|
2014-02-16 23:59:17 -08:00
|
|
|
log.Printf("%s: problem reading from player: %s", p.Id, err)
|
2013-08-19 20:43:26 -07:00
|
|
|
break
|
|
|
|
}
|
2013-09-27 01:30:07 -07:00
|
|
|
|
2013-11-08 21:25:42 -08:00
|
|
|
for _, r := range p.Robots {
|
2013-11-06 22:50:59 -08:00
|
|
|
|
2013-11-08 22:26:56 -08:00
|
|
|
msg, ok := msgs[r.Id]
|
|
|
|
|
2013-11-08 21:25:42 -08:00
|
|
|
if !ok {
|
|
|
|
continue
|
2013-11-06 22:50:59 -08:00
|
|
|
}
|
|
|
|
|
2013-11-08 21:25:42 -08:00
|
|
|
if msg.Repair != nil && *msg.Repair == true {
|
2013-11-17 08:21:13 -08:00
|
|
|
r.ActiveScan = false
|
2013-11-08 21:25:42 -08:00
|
|
|
r.TargetSpeed = 0
|
|
|
|
r.FireAt = nil
|
|
|
|
r.MoveTo = nil
|
|
|
|
if r.RepairCounter <= 0 {
|
|
|
|
r.RepairCounter = 3.0
|
|
|
|
}
|
|
|
|
} else if msg.Scan != nil && *msg.Scan == true {
|
2013-11-17 08:21:13 -08:00
|
|
|
r.RepairCounter = 0
|
2013-11-08 21:25:42 -08:00
|
|
|
r.TargetSpeed = 0
|
|
|
|
r.FireAt = nil
|
|
|
|
r.MoveTo = nil
|
|
|
|
r.ActiveScan = true
|
2013-11-06 22:50:59 -08:00
|
|
|
} else {
|
2013-11-08 21:25:42 -08:00
|
|
|
r.RepairCounter = 0
|
|
|
|
r.ActiveScan = false
|
|
|
|
|
|
|
|
// Reapiring halts all other activity
|
|
|
|
if msg.MoveTo != nil {
|
|
|
|
r.MoveTo = msg.MoveTo
|
|
|
|
}
|
2013-11-11 21:36:11 -08:00
|
|
|
if msg.Heading != nil {
|
|
|
|
r.DesiredHeading = msg.Heading
|
|
|
|
}
|
2013-11-08 21:25:42 -08:00
|
|
|
if msg.FireAt != nil {
|
|
|
|
r.FireAt = msg.FireAt
|
2013-11-27 08:55:51 -08:00
|
|
|
} else {
|
|
|
|
r.FireAt = nil
|
2013-11-08 21:25:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if msg.TargetSpeed != nil {
|
|
|
|
r.TargetSpeed = float32(*msg.TargetSpeed)
|
|
|
|
} else {
|
|
|
|
r.TargetSpeed = r.Stats.Speed
|
|
|
|
}
|
2013-11-27 20:33:57 -08:00
|
|
|
}
|
2013-11-08 21:25:42 -08:00
|
|
|
|
2013-11-27 20:33:57 -08:00
|
|
|
if msg.Probe != nil {
|
|
|
|
r.Probe = msg.Probe
|
|
|
|
r.ProbeResult = nil
|
|
|
|
} else {
|
|
|
|
r.Probe = nil
|
2013-11-06 22:50:59 -08:00
|
|
|
}
|
2013-11-06 23:26:29 -08:00
|
|
|
|
2013-11-08 21:25:42 -08:00
|
|
|
if msg.Message != nil {
|
|
|
|
r.Message = *msg.Message
|
2013-11-06 23:26:29 -08:00
|
|
|
}
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
}
|
2013-11-08 21:25:42 -08:00
|
|
|
|
2014-01-23 21:09:27 -08:00
|
|
|
log.Printf("%s: recv close", p.Id)
|
2013-08-19 20:43:26 -07:00
|
|
|
p.ws.Close()
|
|
|
|
}
|