Пример #1
0
func DamageUnit(attacker *Unit, index int, defender *Unit) (bool, error) {
	if len(attacker.attacks) > index {
		alive, err := defender.receiveDamage(calculateDamage(attacker.attacks[index], defender.armor))
		logger.Info("Defender is %t still alive with %d health", alive, defender.health)
		return alive, err
	} else {
		logger.Warn("Player does not have %d attacks (has %d)", index, len(attacker.attacks))
		return true, errors.New("Player does not have that many attacks")
	}
}
Пример #2
0
func (gh *game_hub) processNewGameRequests() {
	for ng := range gh.gameRequests {
		// look for an existing game to satisfy the new request
		gm, err := gh.findGame(ng)
		// an error here implies duplicate player ids in game requests
		if err != nil {
			logger.Warnf("Player with id %d attempted a \"self\" game", ng.cconn.info.id)
			return
		}
		// create a game if one can't be found
		if gm == nil {
			logger.Info("Couldn't find an available game. Creating a new one")
			gm = gh.makeGame(ng.NumPlayers)
		} else {
			logger.Info("Found existing game. Slotting in")
		}
		gm.proxy.slotClientConnection(gm.currentPlayers, ng.cconn)
		gm.currentPlayers += 1
		if gm.currentPlayers == gm.numPlayers {
			gh.commitGame(gm)
		}
	}
}
Пример #3
0
func (gh *game_hub) commitGame(game *game) {
	delete(gh.uncommittedGames, game.numPlayers)
	// make connection to server

	conn, err := connectToServer()
	if err != nil {
		logger.Errorf("Could not connect to server, this game is going to hang...")
		return
	}
	game.proxy.server = &serverConnection{conn: conn}
	game.channelInHandler(game.proxy)
	go game.proxy.serverReadPump()
	game.proxy.sendInitialGameInfo()
	logger.Info("Committed a game, proxying its messages")

	gh.committedGames.PushBack(game)
}
Пример #4
0
func (gh *game_hub) handleDisconnection(message string, cconn *clientConnection) {
	// Find the connection and kill it, cleaning up its game if necessary
	logger.Info("Client Disconnected. Cleaning up...")
	for np, game := range gh.uncommittedGames {
		for i := 0; i < game.currentPlayers; i++ {
			if game.proxy.proxyConns[i].info.id == cconn.info.id {
				game.proxy.removeClientConnection(i)
				game.currentPlayers -= 1
				if game.currentPlayers == 0 {
					logger.Infof("%d player uncommitted game empty. Dropping", np)
					delete(gh.uncommittedGames, np)
				}
				break
			}
		}
	}
}