From 491d972a4b9eb36573927a3021e78d3500f60784 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Mon, 17 Feb 2014 01:07:29 -0800 Subject: [PATCH] added NewSpectator and NewPlayer funcs --- player.go | 23 +++++++++++++++++++++++ protocol.go | 17 ++--------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/player.go b/player.go index f173996..13f86de 100644 --- a/player.go +++ b/player.go @@ -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) diff --git a/protocol.go b/protocol.go index d303bc4..1124145 100644 --- a/protocol.go +++ b/protocol.go @@ -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)