added NewSpectator and NewPlayer funcs

This commit is contained in:
Stephen McQuay 2014-02-17 01:07:29 -08:00
parent 0bc1575f81
commit 491d972a4b
2 changed files with 25 additions and 15 deletions

View File

@ -13,9 +13,19 @@ const maxMessageSize = 1024
type protoTalker struct { type protoTalker struct {
ws *websocket.Conn ws *websocket.Conn
send chan Message send chan Message
buff []byte
Id string Id string
} }
func NewProtoTalker(id string, ws *websocket.Conn) *protoTalker {
return &protoTalker{
send: make(chan Message, 16),
ws: ws,
Id: id,
buff: make([]byte, maxMessageSize),
}
}
func (pt *protoTalker) sender() { func (pt *protoTalker) sender() {
log.Printf("%s: client launched", pt.Id) log.Printf("%s: client launched", pt.Id)
for things := range pt.send { for things := range pt.send {
@ -60,6 +70,13 @@ type player struct {
protoTalker protoTalker
} }
func NewPlayer(id string, ws *websocket.Conn) *player {
return &player{
Robots: []*Robot{},
protoTalker: *NewProtoTalker(id, ws),
}
}
func (p *player) recv() { func (p *player) recv() {
for { for {
msgs, err := p.readJSON(p.buff) msgs, err := p.readJSON(p.buff)
@ -135,6 +152,12 @@ type Spectator struct {
protoTalker protoTalker
} }
func NewSpectator(id string, ws *websocket.Conn) *Spectator {
return &Spectator{
protoTalker: *NewProtoTalker(id, ws),
}
}
func (s *Spectator) recv() { func (s *Spectator) recv() {
for { for {
_, err := s.readJSON(s.buff) _, err := s.readJSON(s.buff)

View File

@ -230,14 +230,7 @@ func addPlayer(ws *websocket.Conn) {
} }
} }
p := &player{ p := NewPlayer(player_id, ws)
Robots: []*Robot{},
protoTalker: protoTalker{
send: make(chan Message, 16),
ws: ws,
Id: player_id,
},
}
log.Printf("%s: made a player: %s", gid.Id, p.Id) log.Printf("%s: made a player: %s", gid.Id, p.Id)
convertedStats := map[string]Stats{} convertedStats := map[string]Stats{}
@ -292,13 +285,7 @@ func addPlayer(ws *websocket.Conn) {
gid.Id, gid.Id,
) )
case "spectator": case "spectator":
s := &Spectator{ s := NewSpectator(player_id, ws)
protoTalker{
send: make(chan Message, 16),
ws: ws,
Id: player_id,
},
}
log.Printf("%s, %s: about to register this spectator", gid.Id, s.Id) log.Printf("%s, %s: about to register this spectator", gid.Id, s.Id)
game.sregister <- s game.sregister <- s
log.Printf("%s, %s: registered spectator", gid.Id, s.Id) log.Printf("%s, %s: registered spectator", gid.Id, s.Id)