public async Task PlayerManeuver(ApiPlayer player) { var response = await askForManeuverkLocationAsync(player); if (response.Decide is false) { return; } var fromTerritory = game.Board.GetTerritory(response.From); var toTerritory = game.Board.GetTerritory(response.To); Boolean result = new Boolean(); var failedTrys = 0; do { logger.LogInformation($"{player.Name} wants to maneuver from {fromTerritory} to {toTerritory}"); result = game.TryManeuver(player, fromTerritory, toTerritory); if (!result) { logger.LogError($"Invalid maneuver request! {player.Name} from {fromTerritory} to {toTerritory} "); failedTrys++; } if (failedTrys >= 3) { BootPlayerFromGame(player); logger.LogError($"Player {player.Name} Booted! Due to maneuver error."); return; } } while (!result); }
public async Task Reinforce(ApiPlayer player, int armiesRemaining) { while (armiesRemaining > 0) { var reinforceResponse = await askForReinforceLocationAsync(player, DeploymentStatus.YourTurn, armiesRemaining); logger.LogDebug($"{player.Name} wants to reinforce to {reinforceResponse.DesiredLocation}"); var failedTries = 0; //check that this location exists and is available to be used (e.g. not occupied by another army) while (game.TryReinforceArmy(player.Token, reinforceResponse.DesiredLocation) is false) { failedTries++; logger.LogError($"Invalid Reinforce request! {player.Name} to {reinforceResponse.DesiredLocation}"); if (failedTries == MaxFailedTries) { BootPlayerFromGame(player); logger.LogError($"Player {player.Name} Booted! Due to reinforce error"); return; } else { reinforceResponse = await askForReinforceLocationAsync(player, DeploymentStatus.PreviousAttemptFailed, armiesRemaining); } } armiesRemaining--; } }
public bool HasCardBonus(ApiPlayer player) { int infantryCards = 0; int cavalryCards = 0; int artilleryCards = 0; bool hasBonus = false; if (player.PlayerCards.Count > 2) { foreach (var card in player.PlayerCards) { if (card.Type == "Infantry") { infantryCards++; } if (card.Type == "Cavalry") { cavalryCards++; } if (card.Type == "Artillery") { artilleryCards++; } } if (infantryCards > 2) { hasBonus = true; for (int i = 0; i < 3; i++) { player.PlayerCards.Remove(player.PlayerCards.Where(c => c.Type == "Infantry").First()); } } else if (cavalryCards > 2) { hasBonus = true; for (int i = 0; i < 3; i++) { player.PlayerCards.Remove(player.PlayerCards.Where(c => c.Type == "Cavalry").First()); } } else if (artilleryCards > 2) { hasBonus = true; for (int i = 0; i < 3; i++) { player.PlayerCards.Remove(player.PlayerCards.Where(c => c.Type == "Artillery").First()); } } else if (infantryCards > 0 && cavalryCards > 0 && artilleryCards > 0) { hasBonus = true; player.PlayerCards.Remove(player.PlayerCards.Where(c => c.Type == "Infantry").First()); player.PlayerCards.Remove(player.PlayerCards.Where(c => c.Type == "Cavalry").First()); player.PlayerCards.Remove(player.PlayerCards.Where(c => c.Type == "Artillery").First()); } } return(hasBonus); }
//pacifism private async Task <ActionResponse> askForActionAsync(ApiPlayer player, ActionStatus actionStatus) { var actionRequest = new ActionRequest { Status = actionStatus }; return(await(await player.HttpClient.PostAsJsonAsync("/beginAction", actionRequest)) .EnsureSuccessStatusCode() .Content.ReadFromJsonAsync <ActionResponse>()); }
private async Task <ManeuverResponse> askForManeuverkLocationAsync(ApiPlayer player) { var maneuverRequest = new ManeuverRequest { Board = game.Board.SerializableTerritories }; return(await(await player.HttpClient.PostAsJsonAsync("/maneuver", maneuverRequest)) .EnsureSuccessStatusCode() .Content.ReadFromJsonAsync <ManeuverResponse>()); }
private async Task <BeginAttackResponse> askForAttackLocationAsync(ApiPlayer player, BeginAttackStatus beginAttackStatus) { var beginAttackRequest = new BeginAttackRequest { Board = game.Board.SerializableTerritories, Status = beginAttackStatus }; return(await(await player.HttpClient.PostAsJsonAsync("/beginAttack", beginAttackRequest)) .EnsureSuccessStatusCode() .Content.ReadFromJsonAsync <BeginAttackResponse>()); }
private async Task <ContinueAttackResponse> AskMakeAnotherAttackAsync(ApiPlayer player, Territory attackingTerritory, Territory defendingTerritory) { var newAttackRequest = new ContinueAttackRequest { Board = game.Board.SerializableTerritories, AttackingTerritorry = attackingTerritory, DefendingTerritorry = defendingTerritory }; return(await(await player.HttpClient.PostAsJsonAsync("/makeNewAttack", newAttackRequest)) .EnsureSuccessStatusCode() .Content.ReadFromJsonAsync <ContinueAttackResponse>()); }
private async Task <ContinueAttackResponse> askContinueAttackingAsync(ApiPlayer currentPlayer, Territory attackingTerritory, Territory defendingTerritory) { var continueAttackingRequest = new ContinueAttackRequest { Board = game.Board.SerializableTerritories, AttackingTerritorry = attackingTerritory, DefendingTerritorry = defendingTerritory }; var continueAttackingResponse = await(await currentPlayer.HttpClient.PostAsJsonAsync("/continueAttacking", continueAttackingRequest)) .EnsureSuccessStatusCode() .Content.ReadFromJsonAsync <ContinueAttackResponse>(); return(continueAttackingResponse); }
private async Task <DeployArmyResponse> askForDeployLocationAsync(ApiPlayer currentPlayer, DeploymentStatus deploymentStatus) { var deployArmyRequest = new DeployArmyRequest { Board = game.Board.SerializableTerritories, Status = deploymentStatus, ArmiesRemaining = game.GetPlayerRemainingArmies(currentPlayer.Token) }; var json = System.Text.Json.JsonSerializer.Serialize(deployArmyRequest); var deployArmyResponse = (await currentPlayer.HttpClient.PostAsJsonAsync("/deployArmy", deployArmyRequest)); deployArmyResponse.EnsureSuccessStatusCode(); var r = await deployArmyResponse.Content.ReadFromJsonAsync <DeployArmyResponse>(); return(r); }
public async Task <bool> DeployPlayerArmies(ApiPlayer player, int cardBonusLevel) { bool cardBonusUsed = HasCardBonus(player); int armiesForTerritories = FindNumberBonusArmiesFromTerritories(player); int totalBonusArmies = armiesForTerritories; if (cardBonusUsed) { totalBonusArmies += (((cardBonusLevel + 1) % 19) + 1) * 5; logger.LogInformation($"Card Bonus of level: {cardBonusLevel} for {totalBonusArmies} armies total"); } await Reinforce(player, totalBonusArmies); return(cardBonusUsed); }
public void BootPlayerFromGame(ApiPlayer player) { RemovePlayerFromBoard(player.Token); RemovePlayerFromGame(player.Token); }
public async Task DoPlayerBattle(ApiPlayer player) { if (game.PlayerCanAttack(player)) { var failedTries = 0; bool hasCard = false; ContinueAttackResponse anotherAttackResponse = new ContinueAttackResponse { ContinueAttacking = false }; do { TryAttackResult attackResult = new TryAttackResult { AttackInvalid = false }; Territory attackingTerritory = null; Territory defendingTerritory = null; do { logger.LogInformation($"Asking {player.Name} where they want to attack..."); var beginAttackResponse = await askForAttackLocationAsync(player, BeginAttackStatus.PreviousAttackRequestFailed); try { attackingTerritory = game.Board.GetTerritory(beginAttackResponse.From); defendingTerritory = game.Board.GetTerritory(beginAttackResponse.To); logger.LogInformation($"{player.Name} wants to attack from {attackingTerritory} to {defendingTerritory}"); attackResult = game.TryAttack(player.Token, attackingTerritory, defendingTerritory); if (attackResult.BattleWasWon && !hasCard) { player.PlayerCards.Add(new Card()); hasCard = true; } } catch (Exception ex) { attackResult = new TryAttackResult { AttackInvalid = true, Message = ex.Message }; } if (attackResult.AttackInvalid) { logger.LogError($"Invalid attack request! {player.Name} from {attackingTerritory} to {defendingTerritory} "); failedTries++; if (failedTries == MaxFailedTries) { BootPlayerFromGame(player); logger.LogError($"Player {player.Name} Booted! Due to attack error"); return; } } } while (attackResult.AttackInvalid); while (attackResult.CanContinue) { var continueResponse = await askContinueAttackingAsync(player, attackingTerritory, defendingTerritory); if (continueResponse.ContinueAttacking) { logger.LogInformation("Keep attacking!"); attackResult = game.TryAttack(player.Token, attackingTerritory, defendingTerritory); if (attackResult.BattleWasWon && !hasCard) { player.PlayerCards.Add(new Card()); hasCard = true; } } else { logger.LogInformation("run away!"); break; } } if (game.PlayerCanAttack(player)) { anotherAttackResponse = await AskMakeAnotherAttackAsync(player, attackingTerritory, defendingTerritory); } else { anotherAttackResponse.ContinueAttacking = false; } } while (anotherAttackResponse.ContinueAttacking); } else { logger.LogWarning($"{player.Name} cannot attack."); } }
public int FindNumberBonusArmiesFromTerritories(ApiPlayer player) { return(game.GetNumTerritories(player) / 3); }