server/player.go

102 lines
2.0 KiB
Go
Raw Normal View History

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"
2013-08-19 20:43:26 -07:00
"log"
)
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
2013-08-19 20:43:26 -07:00
}
func (p *player) sender() {
for things := range p.send {
2013-11-13 22:24:54 -08:00
err := websocket.JSON.Send(p.ws, things)
2013-08-19 20:43:26 -07:00
if err != nil {
break
}
}
p.ws.Close()
2013-11-08 21:25:42 -08:00
// TODO
log.Printf("player %s: sender close", p.Robots[0].Id)
2013-08-19 20:43:26 -07:00
}
func (p *player) recv() {
for {
// XXX: need to mark myself as having received something, also binding
// such action to a particular game turn ID
2013-11-08 22:26:56 -08:00
var msgs map[string]Instruction
2013-11-08 21:25:42 -08:00
err := websocket.JSON.Receive(p.ws, &msgs)
2013-11-08 22:26:56 -08:00
2013-08-19 20:43:26 -07:00
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)
2013-08-19 20:43:26 -07:00
break
}
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 22:26:56 -08:00
// log.Printf("%v", msg.FireAt)
2013-11-08 21:25:42 -08:00
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
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
}
if msg.Heading != nil {
r.DesiredHeading = msg.Heading
}
2013-11-08 21:25:42 -08:00
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
}
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
log.Printf("player %s: recv close", p.Robots[0].Id)
2013-08-19 20:43:26 -07:00
p.ws.Close()
}