private void TryRestoreBuilding(BuildingFund fund, int playerCountModifier = 0) { IBuildingGameObj buildingObj = fund.BuildingObj; if (buildingObj == null) { return; } int playerCount = Engine.GetTeamPlayerCount(buildingObj.PlayerType) + playerCountModifier; if (playerCount > 0) { // Check if funding is completed float totalRestoreCost = fund.MappedDefinition.CalculateTotalRestoreCost(playerCount); if (fund.FundCompleted(totalRestoreCost)) { // Remove first so that we don't assume it was restored outside of our control buildingFunds.Remove(buildingObj.ID); fund.MappedDefinition.CurrentRestoreCount++; // Now do restore Engine.RestoreBuilding(buildingObj); DA.HostMessage($"{Engine.GetTeamName(buildingObj.PlayerType)} has restored their {DATranslationManager.Translate(buildingObj)}."); } else // Not enough funds gathered yet { DA.TeamColorMessageWithTeamColor(buildingObj.PlayerType, $"{DA.MessagePrefix}{(int)(fund.TotalFunds + 0.5f)} out of {(int)(totalRestoreCost + 0.5f)} credit(s) gathered to restore the {DATranslationManager.Translate(buildingObj)}."); } } }
private bool TotalFundChatCommand(IcPlayer player, string command, IDATokenClass text, TextMessageEnum chatType, object data) { if (!BRFEnabled) { DA.PagePlayer(player, "Building funding is not enabled for this map."); return(false); } string acronym = text[1]; MappedBuildingDefinition mappedDefinition = FindMappedDefinition(acronym); ICollection <IDefinitionClass> validatedDefinitions; if (mappedDefinition == null || (validatedDefinitions = mappedDefinition.ValidatedDefinitions) == null) { DA.PagePlayer(player, $"No building found for acronym '{acronym}'."); return(false); } IScriptableGameObj destroyedBuilding = FindDestroyedBuilding(validatedDefinitions, player.PlayerType); if (destroyedBuilding == null) { DA.PagePlayer(player, $"No destroyed building found for acronym '{acronym}'."); return(false); } if (mappedDefinition.RestoreCountExceeded) { DA.PagePlayer(player, $"The maximum amount of restores per map for the {DATranslationManager.Translate(destroyedBuilding)} is exceeded."); return(false); } if (!buildingFunds.TryGetValue(destroyedBuilding.ID, out BuildingFund fund)) { fund = new BuildingFund(mappedDefinition, destroyedBuilding.ID); buildingFunds.Add(destroyedBuilding.ID, fund); } float totalRestoreCost = mappedDefinition.CalculateTotalRestoreCost(Engine.GetTeamPlayerCount(player.PlayerType)); fund.NotifyPlayerContribution(player); DA.TeamColorMessageWithTeamColor(player.PlayerType, $"{DA.MessagePrefix}{(int)(fund.TotalFunds + 0.5f)} out of {(int)(totalRestoreCost + 0.5f)} credit(s) gathered to restore the {DATranslationManager.Translate(destroyedBuilding)}."); return(true); }
private bool FundChatCommand(IcPlayer player, string command, IDATokenClass text, TextMessageEnum chatType, object data) { if (!BRFEnabled) { DA.PagePlayer(player, "Building funding is not enabled for this map."); return(false); } string acronym = text[1]; MappedBuildingDefinition mappedDefinition = FindMappedDefinition(acronym); ICollection <IDefinitionClass> validatedDefinitions; if (mappedDefinition == null || (validatedDefinitions = mappedDefinition.ValidatedDefinitions) == null) { DA.PagePlayer(player, $"No building found for acronym '{acronym}'."); return(false); } IScriptableGameObj destroyedBuilding = FindDestroyedBuilding(validatedDefinitions, player.PlayerType); if (destroyedBuilding == null) { DA.PagePlayer(player, $"No destroyed building found for acronym '{acronym}'."); return(false); } if (mappedDefinition.RestoreCountExceeded) { DA.PagePlayer(player, $"The maximum amount of restores per map for the {DATranslationManager.Translate(destroyedBuilding)} is exceeded."); return(false); } // First check if the player specified an amount float offeredFundAmount; if (text.Size > 1) { // Make sure to parse as int so that player can't input some weird decimal stuff string newFundsStr = text[2]; if (newFundsStr == null || !int.TryParse(newFundsStr, out int parsedFundAmount) || parsedFundAmount <= 0) { DA.PagePlayer(player, $"Invalid fund amount '{newFundsStr}'."); return(false); } offeredFundAmount = parsedFundAmount; offeredFundAmount = Math.Min(offeredFundAmount, player.Money); } else // No amount was given, assume they want to donate everything { offeredFundAmount = player.Money; if (offeredFundAmount <= 0.0f) { DA.PagePlayer(player, $"You do not have enough money to fund the '{DATranslationManager.Translate(destroyedBuilding)}'."); return(false); } } float totalRestoreCost = mappedDefinition.CalculateTotalRestoreCost(Engine.GetTeamPlayerCount(player.PlayerType)); // Check if we already have a fund for this building and how much is left until fully funded float calculatedFundAmount; if (buildingFunds.TryGetValue(destroyedBuilding.ID, out BuildingFund fund)) { float remainingFundAmount = totalRestoreCost - fund.TotalFunds; calculatedFundAmount = Math.Min(remainingFundAmount, offeredFundAmount); // Do not exceed the amount required to refund } else { calculatedFundAmount = Math.Min(totalRestoreCost, offeredFundAmount); // Do not exceed the amount required to refund fund = new BuildingFund(mappedDefinition, destroyedBuilding.ID); buildingFunds.Add(destroyedBuilding.ID, fund); } // Add this player's fund to the total funds fund.AddFund(player, calculatedFundAmount); DA.TeamColorMessageWithTeamColor(player.PlayerType, $"{player.PlayerName} deposited {(int)(calculatedFundAmount + 0.5f)} credit(s) towards the funding of the {DATranslationManager.Translate(destroyedBuilding)}."); // See if we have enough to restore this building TryRestoreBuilding(fund); return(true); }