Example #1
0
        private async Task newDoBattle()
        {
            game.StartTime = DateTime.Now;
            int CardBonusCount = 0;

            while (game.Players.Count() > 1 && game.GameState == GameState.Attacking && game.Players.Any(p => game.PlayerCanAttack(p)))
            {
                for (int i = 0; i < game.Players.Count() && game.Players.Count() > 1; i++)
                {
                    var currentPlayer = game.Players.Skip(i).First() as ApiPlayer;
                    if (game.GetNumTerritories(currentPlayer) > 0)
                    {
                        var usedCardBonus = await DeployPlayerArmies(currentPlayer, CardBonusCount);

                        if (usedCardBonus)
                        {
                            CardBonusCount++;
                        }

                        if (!removedPlayers.Contains(currentPlayer))
                        {
                            await DoPlayerBattle(currentPlayer);
                        }
                        if (!removedPlayers.Contains(currentPlayer))
                        {
                            await PlayerManeuver(currentPlayer);
                        }
                        else
                        {
                            i--;
                        }
                    }
                }
            }
            logger.LogInformation("Game Over");
            game.SetGameOver();
        }
Example #2
0
        private async Task doBattle()
        {
            game.StartTime = DateTime.Now;
            while (game.Players.Count() > 1 && game.GameState == GameState.Attacking && game.Players.Any(p => game.PlayerCanAttack(p)))
            {
                var consecutivePacifistTurns = 0;

                for (int i = 0; i < game.Players.Count() && game.Players.Count() > 1; i++)
                {
                    var currentPlayer = game.Players.Skip(i).First() as ApiPlayer;
                    if (game.PlayerCanAttack(currentPlayer))
                    {
                        var failedTries = 0;

                        TryAttackResult attackResult = new TryAttackResult {
                            AttackInvalid = false
                        };
                        Territory attackingTerritory = null;
                        Territory defendingTerritory = null;

                        logger.LogInformation($"Asking {currentPlayer.Name} what action they want to perform...");
                        var actionResponse = await askForActionAsync(currentPlayer, ActionStatus.PreviousActionRequestFailed);

                        if (actionResponse.userAction == UserAction.Attack)
                        {
                            consecutivePacifistTurns = 0;
                            do
                            {
                                logger.LogInformation($"Asking {currentPlayer.Name} where they want to attack...");

                                var beginAttackResponse = await askForAttackLocationAsync(currentPlayer, BeginAttackStatus.PreviousAttackRequestFailed);

                                try
                                {
                                    attackingTerritory = game.Board.GetTerritory(beginAttackResponse.From);
                                    defendingTerritory = game.Board.GetTerritory(beginAttackResponse.To);

                                    logger.LogInformation($"{currentPlayer.Name} wants to attack from {attackingTerritory} to {defendingTerritory}");

                                    attackResult = game.TryAttack(currentPlayer.Token, attackingTerritory, defendingTerritory);
                                }
                                catch (Exception ex)
                                {
                                    attackResult = new TryAttackResult {
                                        AttackInvalid = true, Message = ex.Message
                                    };
                                }
                                if (attackResult.AttackInvalid)
                                {
                                    logger.LogError($"Invalid attack request! {currentPlayer.Name} from {attackingTerritory} to {defendingTerritory} ");
                                    failedTries++;
                                    if (failedTries == MaxFailedTries)
                                    {
                                        BootPlayerFromGame(currentPlayer);
                                        i--;
                                        break;
                                    }
                                }
                            } while (attackResult.AttackInvalid);

                            while (attackResult.CanContinue)
                            {
                                var continueResponse = await askContinueAttackingAsync(currentPlayer, attackingTerritory, defendingTerritory);

                                if (continueResponse.ContinueAttacking)
                                {
                                    logger.LogInformation("Keep attacking!");
                                    attackResult = game.TryAttack(currentPlayer.Token, attackingTerritory, defendingTerritory);
                                }
                                else
                                {
                                    logger.LogInformation("run away!");
                                    break;
                                }
                            }
                        }
                        else if (actionResponse.userAction == UserAction.Pacifism)
                        {
                            consecutivePacifistTurns++;
                            if (consecutivePacifistTurns == game.Players.Count())
                            {
                                logger.LogInformation("Game Over");
                                game.SetGameOver();
                                return;
                            }
                        }
                    }
                    else
                    {
                        logger.LogWarning($"{currentPlayer.Name} cannot attack.");
                    }
                }
            }
            logger.LogInformation("Game Over");
            game.SetGameOver();
        }