initial spectator implementation
This commit is contained in:
parent
e391cd3a0b
commit
27afc65297
12
game.go
12
game.go
@ -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 {
|
||||||
|
17
main.go
17
main.go
@ -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,6 +86,8 @@ func addPlayer(ws *websocket.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch clientid.Type {
|
||||||
|
case "robot":
|
||||||
var conf Config
|
var conf Config
|
||||||
for {
|
for {
|
||||||
err = websocket.JSON.Receive(ws, &conf)
|
err = websocket.JSON.Receive(ws, &conf)
|
||||||
@ -122,4 +127,16 @@ func addPlayer(ws *websocket.Conn) {
|
|||||||
go p.sender()
|
go p.sender()
|
||||||
p.recv()
|
p.recv()
|
||||||
log.Printf("%v has been disconnect from this game\n", p.Robot.Id)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
21
spectator.go
Normal file
21
spectator.go
Normal 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()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user