/* Keep the player's websocket alive and continue reading from it forever */ func PlayerKeepReading(p interfaces.Player, ws *websocket.Conn) { for { var message string playerOrder := struct { CommandCode int TickNum int Queue []string }{} if e := websocket.Message.Receive(ws, &message); e != nil { break } else { log.Println("Received message on websocket:", message) err := json.Unmarshal([]byte(message), &playerOrder) if err != nil { log.Fatalln("Error Decoding Order: ", string(message)) break } o := order.NewOrder(playerOrder.CommandCode, playerOrder.TickNum, playerOrder.Queue, p, GAME) RUNTIME.AddOrder(o) } } log.Printf("Closing socket for %v", p.GetPlayerId()) ws.Close() }
func NewPlayer(p interfaces.Player) *Player { me := &Player{} me.Id = p.GetPlayerId() me.EntityId = p.GetEntityId() actions := p.GetAvailableActions() for k := 0; k < len(actions); k++ { me.AvailableActions = append(me.AvailableActions, NewAction(actions[k])) } return me }
func (g *Game) AddPlayer(player interfaces.Player, entity interfaces.Entity) { g.Players[player.GetPlayerId()] = player g.AddEntity(entity) g.PlayersToEntities[player] = entity }