private bool ValidateStandardizedCards(SquadList squad) { Dictionary <string, GenericUpgrade> standardizedUpgradesFound = new Dictionary <string, GenericUpgrade>(); foreach (var shipConfig in squad.Ships) { foreach (var upgrade in shipConfig.Instance.UpgradeBar.GetUpgradesAll()) { if (upgrade.UpgradeInfo.IsStandardazed) { if (standardizedUpgradesFound.ContainsKey(shipConfig.Instance.ShipInfo.ShipName)) { if (standardizedUpgradesFound[shipConfig.Instance.ShipInfo.ShipName].GetType() != upgrade.GetType()) { Messages.ShowError($"All {shipConfig.Instance.ShipInfo.ShipName} ships must have the same Standardized upgrade installed"); return(false); } } else { standardizedUpgradesFound.Add(shipConfig.Instance.ShipInfo.ShipName, upgrade); } } } } foreach (var standardizedPair in standardizedUpgradesFound) { foreach (var shipConfig in squad.Ships) { if (shipConfig.Instance.ShipInfo.ShipName == standardizedPair.Key) { if (!shipConfig.Instance.UpgradeBar.HasUpgradeInstalled(standardizedPair.Value.GetType())) { Messages.ShowError($"All {shipConfig.Instance.ShipInfo.ShipName} ships must have the Standardized upgrade {standardizedPair.Value.UpgradeInfo.Name} installed"); return(false); } } } } return(true); }
private static bool ValidateUpgradePostChecks(PlayerNo playerNo) { bool result = true; SquadList squadList = GetSquadList(playerNo); foreach (var shipHolder in squadList.GetShips()) { foreach (var upgradeHolder in shipHolder.Instance.UpgradeBar.GetUpgradesAll()) { if (!upgradeHolder.IsAllowedForSquadBuilderPostCheck(squadList)) { return(false); } } } return(result); }
private static bool ValidateShipAiReady(PlayerNo playerNo) { bool result = true; SquadList aiSquadlist = GetSquadList(playerNo); if (aiSquadlist.PlayerType == typeof(HotacAiPlayer)) { foreach (var shipConfig in aiSquadlist.GetShips()) { if (shipConfig.Instance.HotacManeuverTable == null) { Messages.ShowError("AI for " + shipConfig.Instance.Type + " is not ready. It can be controlled only by human."); return(false); } } } return(result); }
public static void SaveSquadronToFile(SquadList squad, string squadName) { squad.Name = CleanFileName(squadName); // check that directory exists, if not create it string directoryPath = Application.persistentDataPath + "/" + Edition.Current.Name + "/SavedSquadrons"; if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } string filePath = $"{directoryPath}/{squad.Name}.json"; if (File.Exists(filePath)) { File.Delete(filePath); } File.WriteAllText(filePath, GetSquadInJson(squad).ToString()); }
private bool ValidateUpgradePostChecks(SquadList squad) { bool result = true; foreach (var shipHolder in squad.Ships) { if (!shipHolder.Instance.IsAllowedForSquadBuilderPostCheck(squad)) { return(false); } foreach (var upgradeHolder in shipHolder.Instance.UpgradeBar.GetUpgradesAll()) { if (!upgradeHolder.IsAllowedForSquadBuilderPostCheck(squad)) { return(false); } } } return(result); }
public static void SaveSquadron(SquadList squadList, string squadName, Action callback) { squadList.Name = CleanFileName(squadName); // check that directory exists, if not create it string directoryPath = Application.persistentDataPath + "/" + Edition.Current.Name + "/SavedSquadrons"; if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } string filePath = directoryPath + "/" + squadList.Name + ".json"; if (File.Exists(filePath)) { File.Delete(filePath); } File.WriteAllText(filePath, GetSquadInJson(squadList.PlayerNo).ToString()); callback(); }
public static void SetAiType(string aiName) { SquadList currentSquadList = GetSquadList(CurrentPlayer); switch (aiName) { case "AI: Aggressor": currentSquadList.PlayerType = typeof(AggressorAiPlayer); break; case "AI: HotAC": currentSquadList.PlayerType = typeof(HotacAiPlayer); break; default: break; } GameObject.Find("UI/Panels/SquadBuilderPanel/Panel/SquadBuilderTop").transform.Find("AIButton").GetComponentInChildren <UnityEngine.UI.Text>().text = aiName; Options.AiType = aiName; Options.ChangeParameterValue("AiType", aiName); }
public bool IsSquadValid(SquadList squad) { if (!ValidateShipsCount(squad)) { return(false); } if (!ValidateMaxSameShipsCount(squad)) { return(false); } if (!ValidateLimitedCards(squad)) { return(false); } if (!ValidateSquadCost(squad)) { return(false); } if (!ValidateSolitaryCards(squad)) { return(false); } if (!ValidateStandardizedCards(squad)) { return(false); } if (!ValidateUpgradePostChecks(squad)) { return(false); } if (!ValidateSpecialSlotsRequirements(squad)) { return(false); } return(true); }
private bool ValidateMaxSameShipsCount(SquadList squad) { Dictionary <string, int> shipTypesCount = new Dictionary <string, int>(); foreach (GenericShip ship in squad.Ships.Select(n => n.Instance)) { if (!shipTypesCount.ContainsKey(ship.ShipInfo.ShipName)) { shipTypesCount.Add(ship.ShipInfo.ShipName, 1); } else { shipTypesCount[ship.ShipInfo.ShipName]++; if (shipTypesCount[ship.ShipInfo.ShipName] > Edition.Current.MaxShipsCount) { Messages.ShowError($"Too many ships of type {ship.ShipInfo.ShipName}"); Messages.ShowError($"The maximum allowed number of same ships in squad is: {Edition.Current.MaxShipsCount}"); return(false); } } } return(true); }
public static void SetPlayerSquadFromImportedJson(JSONObject squadJson, PlayerNo playerNo, Action callBack) { ClearShipsOfPlayer(playerNo); try { SquadList squadList = GetSquadList(playerNo); if (squadJson.HasField("name")) { squadList.Name = squadJson["name"].str; } string factionNameXws = squadJson["faction"].str; Faction faction = XWSToFaction(factionNameXws); squadList.SquadFaction = faction; squadList.Points = (int)squadJson["points"].i; if (squadJson.HasField("pilots")) { JSONObject pilotJsons = squadJson["pilots"]; foreach (JSONObject pilotJson in pilotJsons.list) { string shipNameXws = pilotJson["ship"].str; string shipNameGeneral = AllShips.Find(n => n.ShipNameCanonical == shipNameXws).ShipName; string pilotNameXws = pilotJson["name"].str; if (!AllPilots.Any(n => n.PilotNameCanonical == pilotNameXws)) { Debug.Log("Cannot find pilot: " + pilotNameXws); Console.Write("Cannot find pilot: " + pilotNameXws, LogTypes.Errors, true, "red"); } string pilotNameGeneral = AllPilots.Find(n => n.PilotNameCanonical == pilotNameXws).PilotName; PilotRecord pilotRecord = AllPilots.Find(n => n.PilotName == pilotNameGeneral && n.PilotShip.ShipName == shipNameGeneral && n.PilotFaction == faction); GenericShip newShipInstance = (GenericShip)Activator.CreateInstance(Type.GetType(pilotRecord.PilotTypeName)); RuleSet.Instance.AdaptShipToRules(newShipInstance); RuleSet.Instance.AdaptPilotToRules(newShipInstance); SquadBuilderShip newShip = AddPilotToSquad(newShipInstance, playerNo); List <string> upgradesThatCannotBeInstalled = new List <string>(); JSONObject upgradeJsons = pilotJson["upgrades"]; foreach (string upgradeType in upgradeJsons.keys) { JSONObject upgradeNames = upgradeJsons[upgradeType]; foreach (JSONObject upgradeRecord in upgradeNames.list) { if (!AllUpgrades.Any(n => n.UpgradeNameCanonical == upgradeRecord.str)) { Debug.Log("Cannot find upgrade: " + upgradeRecord.str); Console.Write("Cannot find upgrade: " + upgradeRecord.str, LogTypes.Errors, true, "red"); } string upgradeName = AllUpgrades.Find(n => n.UpgradeNameCanonical == upgradeRecord.str).UpgradeName; bool upgradeInstalledSucessfully = InstallUpgrade(newShip, upgradeName); if (!upgradeInstalledSucessfully) { upgradesThatCannotBeInstalled.Add(upgradeName); } } } while (upgradeJsons.Count != 0) { List <string> upgradesThatCannotBeInstalledCopy = new List <string>(upgradesThatCannotBeInstalled); bool wasSuccess = false; foreach (var upgradeName in upgradesThatCannotBeInstalledCopy) { bool upgradeInstalledSucessfully = InstallUpgrade(newShip, upgradeName); if (upgradeInstalledSucessfully) { wasSuccess = true; upgradesThatCannotBeInstalled.Remove(upgradeName); } } 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.SkinName = myVendorData["skin"].str; } } } } } else { Messages.ShowError("No pilots"); } callBack(); } catch (Exception) { Messages.ShowError("Error during creation of squadron"); ClearShipsOfPlayer(playerNo); //throw; } }
public SquadBuilderShip(GenericShip ship, SquadList list) { List = list; InitializeShip(ship); }
public static void SetPlayerSquadFromImportedJson(JSONObject squadJson, PlayerNo playerNo, Action callBack) { ClearShipsOfPlayer(playerNo); try { SquadList squadList = GetSquadList(playerNo); if (squadJson.HasField("name")) { squadList.Name = squadJson["name"].str; } string factionNameXws = squadJson["faction"].str; Faction faction = XWSToFaction(factionNameXws); squadList.SquadFaction = faction; squadList.Points = (int)squadJson["points"].i; if (squadJson.HasField("pilots")) { JSONObject pilotJsons = squadJson["pilots"]; foreach (JSONObject pilotJson in pilotJsons.list) { string shipNameXws = pilotJson["ship"].str; string shipNameGeneral = AllShips.Find(n => n.ShipNameCanonical == shipNameXws).ShipName; string pilotNameXws = pilotJson["name"].str; string pilotNameGeneral = AllPilots.Find(n => n.PilotNameCanonical == pilotNameXws).PilotName; PilotRecord pilotRecord = AllPilots.Find(n => n.PilotName == pilotNameGeneral && n.PilotShip.ShipName == shipNameGeneral && n.PilotFaction == faction); GenericShip newShipInstance = (GenericShip)Activator.CreateInstance(Type.GetType(pilotRecord.PilotTypeName)); SquadBuilderShip newShip = AddPilotToSquad(newShipInstance, playerNo); JSONObject upgradeJsons = pilotJson["upgrades"]; foreach (string upgradeType in upgradeJsons.keys) { JSONObject upgradeNames = upgradeJsons[upgradeType]; foreach (JSONObject upgradeRecord in upgradeNames.list) { string upgradeName = AllUpgrades.Find(n => n.UpgradeNameCanonical == upgradeRecord.str).UpgradeName; InstallUpgrade(newShip, upgradeName); } } if (pilotJson.HasField("vendor")) { JSONObject vendorData = pilotJson["vendor"]; if (vendorData.HasField("Sandrem.FlyCasual")) { JSONObject myVendorData = vendorData["Sandrem.FlyCasual"]; if (myVendorData.HasField("skin")) { newShip.Instance.SkinName = myVendorData["skin"].str; } } } } } else { Messages.ShowError("No pilots"); } callBack(); } catch (Exception) { Messages.ShowError("Error during creation of squadron"); ClearShipsOfPlayer(playerNo); //throw; } }
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(); } }
/*public JSONObject GetSquadInJsonCompact(PlayerNo playerNo) * { * JSONObject squadJson = new JSONObject(); * * JSONObject[] squadPilotsArrayJson = new JSONObject[Ships.Count]; * for (int i = 0; i < squadPilotsArrayJson.Length; i++) * { * squadPilotsArrayJson[i] = GenerateSquadPilotCompact(Ships[i]); * } * JSONObject squadPilotsJson = new JSONObject(squadPilotsArrayJson); * squadJson.AddField("pilots", squadPilotsJson); * * return squadJson; * } * * private JSONObject GenerateSquadPilotCompact(SquadListShip shipHolder) * { * JSONObject pilotJson = new JSONObject(); * pilotJson.AddField("n", shipHolder.Instance.PilotNameCanonical); * * string upgradesList = ""; * foreach (var installedUpgrade in shipHolder.Instance.UpgradeBar.GetUpgradesAll()) * { * upgradesList += installedUpgrade.NameCanonical + " "; * } * JSONObject upgradesDictJson = new JSONObject(upgradesList); * * pilotJson.AddField("u", upgradesDictJson); * * return pilotJson; * }*/ public static void CreateSquadFromImportedJson(SquadList squad, string jsonString) { JSONObject squadJson = new JSONObject(jsonString); SetPlayerSquadFromImportedJson(squad, squadJson); }