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

View File

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