public static AlwaysTrue FetchOrAdd(Contract contract)
 {
     // Fetch the AlwaysTrue wrapper
     IEnumerable<ContractParameter> parameters = contract.AllParameters.Where<ContractParameter>(p => p.GetType() == typeof(AlwaysTrue));
     if (parameters.Count() == 0)
     {
         AlwaysTrue alwaysTrue = new AlwaysTrue();
         contract.AddParameter(alwaysTrue);
         return alwaysTrue;
     }
     else
     {
         return parameters.First() as AlwaysTrue;
     }
 }
Exemple #2
0
        public static AlwaysTrue FetchOrAdd(Contract contract)
        {
            // Fetch the AlwaysTrue wrapper
            IEnumerable <ContractParameter> parameters = contract.AllParameters.Where <ContractParameter>(p => p.GetType() == typeof(AlwaysTrue));

            if (parameters.Count() == 0)
            {
                AlwaysTrue alwaysTrue = new AlwaysTrue();
                contract.AddParameter(alwaysTrue);
                return(alwaysTrue);
            }
            else
            {
                return(parameters.First() as AlwaysTrue);
            }
        }
        /// <summary>
        /// Called when a draft succeeds.
        /// </summary>
        /// <param name="kerbalName">The name of the drafted viewer.</param>
        void DraftSuccess(Dictionary <string, string> info)
        {
            // Resets failures. The addon should only destroy after 5 consecutive failures.
            failures = 0;

            // Get the next contract in the queue.
            Contract toMod = contractsToModify.Dequeue();

            // Create a ConfigNode to save the contract into.
            ConfigNode replacement = new ConfigNode("CONTRACT");

            toMod.Save(replacement);

            // Get the old Kerbal name for later use.
            string oldName = replacement.GetValue("kerbalName");

            // Replace the old name with the new.
            replacement.SetValue("kerbalName", info["name"]);

            // For each PARAM node in the CONTRACT node,
            foreach (ConfigNode node in replacement.nodes)
            {
                // Get the name of the contract parameter.
                string paramName = node.GetValue("name");

                // Perform certain replacement functions for each parameter.
                switch (paramName)
                {
                case "AcquireCrew":
                {
                    node.SetValue("title", "Save " + info["name"]);
                    break;
                }

                case "AcquirePart":
                {
                    string firstName = info["name"].Substring(0, info["name"].IndexOf(' '));
                    node.SetValue("title", "Obtain " + firstName + "'s Scrap");
                    break;
                }

                case "RecoverKerbal":
                {
                    node.SetValue("title", "Recover " + info["name"] + " on Kerbin");
                    break;
                }

                case "RecoverPart":
                {
                    string firstName = info["name"].Substring(0, info["name"].IndexOf(' '));
                    node.SetValue("title", "Recover " + firstName + "'s Scrap on Kerbin");
                    break;
                }
                }
            }

            // Get a count of parameters currently held by the contract.
            int parameters = toMod.ParameterCount;

            // Iterate using this count, removing the one parameter each time, effectively clearing the list.
            for (int i = 0; i < parameters; i++)
            {
                // Remove the first parameter.
                toMod.RemoveParameter(0);
            }

            // Add the custom parameter indicating DTV has modified this contract.
            toMod.AddParameter(new ModifiedByDTV());

            // Reload the contract.
            Contract.Load(toMod, replacement);

            // Get the old Kerbal and rename it.
            ProtoCrewMember toRename = HighLogic.CurrentGame.CrewRoster[oldName];

            toRename.ChangeName(info["name"]);

            // Logging.
            Logger.DebugLog("Draft Success (" + contractsToModify.Count.ToString() + " contracts waiting): " + info["name"]);

            // Refresh the contract list by firing the onContractListChanged event.
            GameEvents.Contract.onContractsListChanged.Fire();

            // If the queue is not empty,
            if (contractsToModify.Count > 0)
            {
                // Begin another draft.
                StartCoroutine(ScenarioDraftManager.DraftKerbal(DraftSuccess, DraftFailure, false, false, "Any"));
            }
            // Else, the queue is empty.
            else
            {
                // Indicate a stop in waiting status.
                working = false;
            }
        }
        /// <summary>
        /// Called when a draft succeeds.
        /// </summary>
        /// <param name="kerbalName">The name of the drafted viewer.</param>
        void DraftSuccess(Dictionary <string, string> info)
        {
            // Enqueue the name first thing, since it needs to be in the queue whether it has enough with it or not.
            draftNames.Enqueue(info["name"]);

            // Resets failures. The addon should only destroy after 5 consecutive failures.
            failures = 0;

            // Peek at the next contract in the queue instead of dequeueing because there might not yet be enough names to cover the contract.
            Contract toMod = contractsToModify.Peek();

            // Create a ConfigNode to save the contract into.
            ConfigNode replacement = new ConfigNode("CONTRACT");

            toMod.Save(replacement);

            // Obtain a list of the old tourists in the contract.
            string[] oldTourists = replacement.GetValue("tourists").Split('|');

            // If the count of names in the queue plus this name equals the number of tourists,
            if (draftNames.Count == oldTourists.Length)
            {
                // Dequeue the contract we peeked because there are enough names for it.
                contractsToModify.Dequeue();

                // Create an array from the queue and clear it.
                string[] newTourists = draftNames.ToArray();
                draftNames.Clear();

                // Replace the contract "tourists" string.
                replacement.SetValue("tourists", string.Join("|", newTourists));

                // Get a list of PARAM nodes in the contract.
                ConfigNode[] paramNodes = replacement.GetNodes("PARAM");

                // Iterate through them,
                for (int i = 0; i < paramNodes.Length; i++)
                {
                    // And replace their kerbalName values.
                    paramNodes[i].SetValue("kerbalName", newTourists[i]);

                    // Iterate through any sub-PARAMS,
                    foreach (ConfigNode subParam in paramNodes[i].GetNodes("PARAM"))
                    {
                        // And replace their kerbalName values as well.
                        subParam.SetValue("kerbalName", newTourists[i]);
                    }

                    // Remove the parameter from the actual contract to prevent duplicates.
                    toMod.RemoveParameter(0);

                    // Get an old Kerbal and rename it.
                    ProtoCrewMember toRename = HighLogic.CurrentGame.CrewRoster[oldTourists[i]];
                    toRename.ChangeName(newTourists[i]);
                }

                // Add the custom parameter indicating DTV has modified this contract.
                toMod.AddParameter((ContractParameter) new ModifiedByDTV());

                // Reload the contract.
                Contract.Load(toMod, replacement);

                // Logging.
                Logger.DebugLog("Draft Success (" + contractsToModify.Count.ToString() + " contracts waiting): " + string.Join("|", newTourists));

                // Refresh the contract list by firing the onContractListChanged event.
                GameEvents.Contract.onContractsListChanged.Fire();

                // If the queue is not empty,
                if (contractsToModify.Count > 0)
                {
                    // Begin another draft.
                    StartCoroutine(ScenarioDraftManager.DraftKerbal(DraftSuccess, DraftFailure, false, false, "Any"));
                }
                // Else, the queue is empty.
                else
                {
                    // Indicate a stop in waiting status.
                    working = false;
                }
            }
            // Else, run another draft.
            else
            {
                StartCoroutine(ScenarioDraftManager.DraftKerbal(DraftSuccess, DraftFailure, false, false, "Any"));
            }
        }