initial spectator implementation

This commit is contained in:
Stephen McQuay 2013-08-19 22:23:35 -07:00
parent e391cd3a0b
commit 27afc65297
3 changed files with 86 additions and 36 deletions

12
game.go
View File

@ -14,6 +14,9 @@ type game struct {
unregister chan *player unregister chan *player
id chan int id chan int
turn int turn int
spectators map[*Spectator]bool
sregister chan *Spectator
sunregister chan *Spectator
} }
type Config struct { type Config struct {
@ -55,6 +58,12 @@ func (g *game) run() {
case p := <-g.unregister: case p := <-g.unregister:
delete(g.players, p) delete(g.players, p)
close(p.send) close(p.send)
case s := <-g.sregister:
log.Printf("registering a spectator: %+v", s)
g.spectators[s] = true
case s := <-g.sunregister:
delete(g.spectators, s)
close(s.send)
case <-time.Tick(time.Duration(*tick) * time.Millisecond): case <-time.Tick(time.Duration(*tick) * time.Millisecond):
g.turn++ g.turn++
t0 := time.Now() t0 := time.Now()
@ -111,6 +120,9 @@ func (g *game) run() {
for p := range g.players { for p := range g.players {
p.send <- payload p.send <- payload
} }
for s := range g.spectators {
s.send <- payload
}
t1 = time.Now() t1 = time.Now()
if *verbose { if *verbose {

89
main.go
View File

@ -28,6 +28,9 @@ var g = game{
splosions: make(map[*splosion]bool), splosions: make(map[*splosion]bool),
players: make(map[*player]bool), players: make(map[*player]bool),
turn: 0, turn: 0,
spectators: make(map[*Spectator]bool),
sregister: make(chan *Spectator),
sunregister: make(chan *Spectator),
} }
func main() { func main() {
@ -83,43 +86,57 @@ func addPlayer(ws *websocket.Conn) {
return return
} }
var conf Config switch clientid.Type {
for { case "robot":
err = websocket.JSON.Receive(ws, &conf) var conf Config
if err != nil { for {
log.Print(err) err = websocket.JSON.Receive(ws, &conf)
return 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)
}
} }
if conf.Stats.valid() { log.Printf("%s eventually sent valid config: %+v", id, conf)
_ = websocket.JSON.Send(ws, protocol.NewHandshake(id, true))
break
} else {
_ = websocket.JSON.Send(ws, protocol.NewHandshake(id, false))
log.Printf("%s invalid config", id)
}
}
log.Printf("%s eventually sent valid config: %+v", id, conf)
start_pos := v.Point2d{ start_pos := v.Point2d{
X: rand.Float64() * *width, X: rand.Float64() * *width,
Y: rand.Float64() * *height, 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)
} }
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)
} }

21
spectator.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"code.google.com/p/go.net/websocket"
)
type Spectator struct {
ws *websocket.Conn
send chan *boardstate
}
func (s *Spectator) sender() {
for things := range s.send {
// log.Printf("%v\n", things)
err := websocket.JSON.Send(s.ws, *things)
if err != nil {
break
}
}
s.ws.Close()
}