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
|
||||
id chan int
|
||||
turn int
|
||||
spectators map[*Spectator]bool
|
||||
sregister chan *Spectator
|
||||
sunregister chan *Spectator
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@ -55,6 +58,12 @@ func (g *game) run() {
|
||||
case p := <-g.unregister:
|
||||
delete(g.players, p)
|
||||
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):
|
||||
g.turn++
|
||||
t0 := time.Now()
|
||||
@ -111,6 +120,9 @@ func (g *game) run() {
|
||||
for p := range g.players {
|
||||
p.send <- payload
|
||||
}
|
||||
for s := range g.spectators {
|
||||
s.send <- payload
|
||||
}
|
||||
|
||||
t1 = time.Now()
|
||||
if *verbose {
|
||||
|
89
main.go
89
main.go
@ -28,6 +28,9 @@ var g = game{
|
||||
splosions: make(map[*splosion]bool),
|
||||
players: make(map[*player]bool),
|
||||
turn: 0,
|
||||
spectators: make(map[*Spectator]bool),
|
||||
sregister: make(chan *Spectator),
|
||||
sunregister: make(chan *Spectator),
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -83,43 +86,57 @@ func addPlayer(ws *websocket.Conn) {
|
||||
return
|
||||
}
|
||||
|
||||
var conf Config
|
||||
for {
|
||||
err = websocket.JSON.Receive(ws, &conf)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
switch clientid.Type {
|
||||
case "robot":
|
||||
var conf Config
|
||||
for {
|
||||
err = websocket.JSON.Receive(ws, &conf)
|
||||
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() {
|
||||
_ = 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)
|
||||
log.Printf("%s eventually sent valid config: %+v", id, conf)
|
||||
|
||||
start_pos := v.Point2d{
|
||||
X: rand.Float64() * *width,
|
||||
Y: rand.Float64() * *height,
|
||||
start_pos := v.Point2d{
|
||||
X: rand.Float64() * *width,
|
||||
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
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