Exemplo n.º 1
0
 public void SetDefaultObstacles()
 {
     ChosenObstacles.AddRange
     (
         new List <GenericObstacle>()
     {
         ObstaclesManager.GetPossibleObstacle("coreasteroid5"),
         ObstaclesManager.GetPossibleObstacle("core2asteroid5"),
         ObstaclesManager.GetPossibleObstacle("core2asteroid4")
     }
     );
 }
Exemplo n.º 2
0
        public static void SetPlayerSquadFromImportedJson(SquadList squad, JSONObject squadJson)
        {
            if (squadJson.HasField("name"))
            {
                squad.Name = squadJson["name"].str;
            }

            string  factionNameXws = squadJson["faction"].str;
            Faction faction        = Edition.Current.XwsToFaction(factionNameXws);

            squad.SquadFaction = faction;

            if (squadJson.HasField("pilots"))
            {
                JSONObject pilotJsons = squadJson["pilots"];
                foreach (JSONObject pilotJson in pilotJsons.list)
                {
                    string shipNameXws = pilotJson["ship"].str;

                    string     shipNameGeneral = "";
                    ShipRecord shipRecord      = SquadBuilder.Instance.Database.AllShips.FirstOrDefault(n => n.ShipNameCanonical == shipNameXws);
                    if (shipRecord == null)
                    {
                        Messages.ShowError("Cannot find ship: " + shipNameXws);
                        continue;
                    }

                    shipNameGeneral = shipRecord.ShipName;

                    string      pilotNameXws = pilotJson["id"].str;
                    PilotRecord pilotRecord  = SquadBuilder.Instance.Database.AllPilots.FirstOrDefault(n => n.PilotNameCanonical == pilotNameXws && n.Ship.ShipName == shipNameGeneral && n.PilotFaction == faction);
                    if (pilotRecord == null)
                    {
                        Messages.ShowError("Cannot find pilot: " + pilotNameXws);
                        continue;
                    }

                    GenericShip newShipInstance = (GenericShip)Activator.CreateInstance(Type.GetType(pilotRecord.PilotTypeName));
                    Edition.Current.AdaptShipToRules(newShipInstance);
                    SquadListShip newShip = squad.AddPilotToSquad(newShipInstance);

                    Dictionary <string, string> upgradesThatCannotBeInstalled = new Dictionary <string, string>();

                    if (pilotJson.HasField("upgrades"))
                    {
                        JSONObject upgradeJsons = pilotJson["upgrades"];
                        if (upgradeJsons.keys != null)
                        {
                            foreach (string upgradeType in upgradeJsons.keys)
                            {
                                JSONObject upgradeNames = upgradeJsons[upgradeType];
                                foreach (JSONObject upgradeRecord in upgradeNames.list)
                                {
                                    UpgradeRecord newUpgradeRecord = SquadBuilder.Instance.Database.AllUpgrades.FirstOrDefault(n => n.UpgradeNameCanonical == upgradeRecord.str);
                                    if (newUpgradeRecord == null)
                                    {
                                        Messages.ShowError("Cannot find upgrade: " + upgradeRecord.str);
                                    }

                                    bool upgradeInstalledSucessfully = newShip.InstallUpgrade(upgradeRecord.str, Edition.Current.XwsToUpgradeType(upgradeType));
                                    if (!upgradeInstalledSucessfully && !upgradesThatCannotBeInstalled.ContainsKey(upgradeRecord.str))
                                    {
                                        upgradesThatCannotBeInstalled.Add(upgradeRecord.str, upgradeType);
                                    }
                                }
                            }

                            while (upgradeJsons.Count != 0)
                            {
                                Dictionary <string, string> upgradesThatCannotBeInstalledCopy = new Dictionary <string, string>(upgradesThatCannotBeInstalled);

                                bool wasSuccess = false;
                                foreach (var upgrade in upgradesThatCannotBeInstalledCopy)
                                {
                                    bool upgradeInstalledSucessfully = newShip.InstallUpgrade(upgrade.Key, Edition.Current.XwsToUpgradeType(upgrade.Value));
                                    if (upgradeInstalledSucessfully)
                                    {
                                        wasSuccess = true;
                                        upgradesThatCannotBeInstalled.Remove(upgrade.Key);
                                    }
                                }

                                if (!wasSuccess)
                                {
                                    break;
                                }
                            }
                        }
                    }

                    if (pilotJson.HasField("vendor"))
                    {
                        JSONObject vendorData = pilotJson["vendor"];
                        if (vendorData.HasField("Sandrem.FlyCasual"))
                        {
                            JSONObject myVendorData = vendorData["Sandrem.FlyCasual"];
                            if (myVendorData.HasField("skin"))
                            {
                                newShip.Instance.ModelInfo.SkinName = myVendorData["skin"].str;
                            }
                        }
                    }
                }
            }
            else
            {
                Messages.ShowError("The squad has no pilots");
            }

            if (squadJson.HasField("obstacles"))
            {
                squad.ChosenObstacles.AddRange(
                    new List <GenericObstacle>()
                {
                    ObstaclesManager.GetPossibleObstacle(squadJson["obstacles"][0].str),
                    ObstaclesManager.GetPossibleObstacle(squadJson["obstacles"][1].str),
                    ObstaclesManager.GetPossibleObstacle(squadJson["obstacles"][2].str)
                }
                    );
            }
            else
            {
                squad.SetDefaultObstacles();
            }
        }