コード例 #1
0
        private bool ReFundChatCommand(IcPlayer player, string command, IDATokenClass text, TextMessageEnum chatType, object data)
        {
            // Acronym for a building is specified
            if (text.Size > 0)
            {
                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 (!buildingFunds.TryGetValue(destroyedBuilding.ID, out BuildingFund fund) || fund.Refund(player) <= 0.0f)
                {
                    DA.PagePlayer(player, $"You haven't funded the {DATranslationManager.Translate(destroyedBuilding)}.");

                    return(false);
                }

                DA.TeamColorMessageWithTeamColor(player.PlayerType, $"{DA.MessagePrefix}{(int)(fund.TotalFunds + 0.5f)} out of {(int)(mappedDefinition.CalculateTotalRestoreCost(Engine.GetTeamPlayerCount(player.PlayerType)) + 0.5f)} credit(s) gathered to restore the {DATranslationManager.Translate(destroyedBuilding)}.");

                return(true);
            }
            else //  Assume they want all their money back
            {
                int playerCount = Engine.GetTeamPlayerCount(player.PlayerType);

                float totalRefundAmount = 0.0f;
                foreach (BuildingFund fund in buildingFunds.Values)
                {
                    float currentRefundAmount = fund.Refund(player);
                    if (currentRefundAmount > 0.0f)
                    {
                        totalRefundAmount += currentRefundAmount;

                        DA.TeamColorMessageWithTeamColor(player.PlayerType, $"{DA.MessagePrefix}{(int)(fund.TotalFunds + 0.5f)} out of {(int)(fund.MappedDefinition.CalculateTotalRestoreCost(playerCount) + 0.5f)} credit(s) gathered to restore the {DATranslationManager.Translate(fund.BuildingObj)}.");
                    }
                }

                if (totalRefundAmount <= 0.0f)
                {
                    DA.PagePlayer(player, "You haven't funded anything.");

                    return(false);
                }

                return(true);
            }
        }
コード例 #2
0
        public void RefundAll()
        {
            foreach (var fundsPair in funds)
            {
                float playerFunds = fundsPair.Value;
                if (playerFunds > 0.0f)
                {
                    IcPlayer player = Engine.FindPlayer(fundsPair.Key);
                    if (player != null)
                    {
                        DA.PagePlayer(player, $"You have been refunded {(int)playerFunds} credit(s) for the {DATranslationManager.Translate(BuildingObj)}.");
                    }
                }
            }

            funds.Clear();
        }
コード例 #3
0
        public float Refund(IcPlayer player)
        {
            if (player == null)
            {
                throw new ArgumentNullException(nameof(player));
            }

            string playerName = player.PlayerName;

            if (funds.TryGetValue(playerName, out float fundAmount))
            {
                funds.Remove(playerName);

                player.Money += fundAmount;
                player.SetObjectDirtyBit(DirtyBit.BitOccasional, true);

                DA.PagePlayer(player, $"You have been refunded {(int)fundAmount} credit(s) for the {DATranslationManager.Translate(BuildingObj)}.");

                return(fundAmount);
            }
            else
            {
                return(0.0f);
            }
        }
コード例 #4
0
        public void NotifyPlayerContribution(IcPlayer player)
        {
            funds.TryGetValue(player.PlayerName, out float playerFunds);

            DA.PagePlayer(player, $"Your contribution towards restoring the {DATranslationManager.Translate(BuildingObj)} is {(int)playerFunds} credit(s).");
        }
コード例 #5
0
        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)}.");
                }
            }
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }