You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

106 lines
2.1 KiB

package main
import (
"code.google.com/p/go.net/websocket"
"log"
)
type player struct {
ws *websocket.Conn
Robots []*Robot
send chan Message
Instruction Instruction
Id string
}
func (p *player) sender() {
log.Printf("%s: sender launched", p.Id)
for things := range p.send {
err := websocket.JSON.Send(p.ws, things)
if err != nil {
break
}
}
p.ws.Close()
log.Printf("%s: robot (%s) sender close", p.Id, 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.Printf("%s: problem receiving JSON from player: %s", p.Id, 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.ActiveScan = false
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.RepairCounter = 0
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
} else {
r.FireAt = nil
}
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("%s: recv close", p.Id)
p.ws.Close()
}