102 lines
2.0 KiB
Go
102 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"code.google.com/p/go.net/websocket"
|
|
"log"
|
|
)
|
|
|
|
type player struct {
|
|
ws *websocket.Conn
|
|
Robots []*Robot
|
|
send chan Message
|
|
Instruction Instruction
|
|
}
|
|
|
|
func (p *player) sender() {
|
|
for things := range p.send {
|
|
err := websocket.JSON.Send(p.ws, things)
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
p.ws.Close()
|
|
// TODO
|
|
log.Printf("player %s: sender close", p.Robots[0].Id)
|
|
}
|
|
|
|
func (p *player) recv() {
|
|
for {
|
|
// XXX: need to mark myself as having received something, also binding
|
|
// such action to a particular game turn ID
|
|
var msgs map[string]Instruction
|
|
err := websocket.JSON.Receive(p.ws, &msgs)
|
|
|
|
if err != nil {
|
|
// TODO: perhaps we could be a bit more precise in the handling of
|
|
// this 'error' by selecting on some kill signal channel and this
|
|
// json read?
|
|
log.Print("problem receiving JSON from player: ", err)
|
|
break
|
|
}
|
|
|
|
for _, r := range p.Robots {
|
|
|
|
msg, ok := msgs[r.Id]
|
|
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
// log.Printf("%v", msg.FireAt)
|
|
|
|
if msg.Repair != nil && *msg.Repair == true {
|
|
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 {
|
|
r.TargetSpeed = 0
|
|
r.FireAt = nil
|
|
r.MoveTo = nil
|
|
r.ActiveScan = true
|
|
} else {
|
|
r.RepairCounter = 0
|
|
r.ActiveScan = false
|
|
|
|
// Reapiring halts all other activity
|
|
if msg.MoveTo != nil {
|
|
r.MoveTo = msg.MoveTo
|
|
}
|
|
if msg.Heading != nil {
|
|
r.DesiredHeading = msg.Heading
|
|
}
|
|
if msg.FireAt != nil {
|
|
r.FireAt = msg.FireAt
|
|
}
|
|
|
|
if msg.TargetSpeed != nil {
|
|
r.TargetSpeed = float32(*msg.TargetSpeed)
|
|
} else {
|
|
r.TargetSpeed = r.Stats.Speed
|
|
}
|
|
|
|
if msg.Probe != nil {
|
|
r.Probe = msg.Probe
|
|
r.ProbeResult = nil
|
|
} else {
|
|
r.Probe = nil
|
|
}
|
|
}
|
|
|
|
if msg.Message != nil {
|
|
r.Message = *msg.Message
|
|
}
|
|
}
|
|
}
|
|
|
|
log.Printf("player %s: recv close", p.Robots[0].Id)
|
|
p.ws.Close()
|
|
}
|