Exemplo n.º 1
0
/* 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()
}
Exemplo n.º 2
0
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
}
Exemplo n.º 3
0
func (g *Game) AddPlayer(player interfaces.Player, entity interfaces.Entity) {
	g.Players[player.GetPlayerId()] = player
	g.AddEntity(entity)
	g.PlayersToEntities[player] = entity
}