/// <summary> /// Action to create or update the constellation /// </summary> private void actionClick() { try { try { short constellFreq = short.Parse(frequencyInput.uiItem.GetComponent<TMP_InputField>().text); string constellName = nameInput.uiItem.GetComponent<TMP_InputField>().text; //Check name if (constellName.Length <= 0) { throw new Exception("Name cannot be empty"); } //Check frequency if (constellFreq < 0) { throw new Exception("Frequency cannot be negative"); } else if (!Constellation.isFrequencyValid(constellFreq)) { throw new Exception("Frequency must be between 0 and " + short.MaxValue); } else if (this.existingConstellation != null) { if (this.existingConstellation.frequency == CNCSettings.Instance.PublicRadioFrequency) //public one { if (constellFreq != CNCSettings.Instance.PublicRadioFrequency) throw new Exception("Public frequency " + CNCSettings.Instance.PublicRadioFrequency + " is locked"); } /* else if(constellFreq == CNCSettings.Instance.PublicRadioFrequency) // not public but new freq is public { throw new Exception("New frequency cannot be " + CNCSettings.Instance.PublicRadioFrequency); } */ else if (GameUtils.NonLinqAny(CNCCommNetScenario.Instance.constellations, constellFreq) && this.existingConstellation.frequency != constellFreq) { throw new Exception("Frequency is in use already"); } } else if (this.existingConstellation == null && GameUtils.NonLinqAny(CNCCommNetScenario.Instance.constellations, constellFreq)) { throw new Exception("Frequency is in use already"); } //ALL OK if (this.existingConstellation == null && creationCallback != null) { Constellation newConstellation = new Constellation(constellFreq, constellName, this.constellColor); CNCCommNetScenario.Instance.constellations.Add(newConstellation); creationCallback(newConstellation); string message = string.Format("New constellation '{0}' of frequency {1} is created", constellName, constellFreq); ScreenMessages.PostScreenMessage(new ScreenMessage(message, CNCSettings.ScreenMessageDuration, ScreenMessageStyle.UPPER_CENTER)); CNCLog.Debug("New constellation: {0}, {1}", constellName, constellFreq); this.dismiss(); } else if (this.existingConstellation != null && updateCallback != null) { bool changesCommitted = false; short prevFreq = this.existingConstellation.frequency; if (this.existingConstellation.frequency != constellFreq) // new frequency { this.existingConstellation.frequency = constellFreq; List<CNCCommNetVessel> affectedVessels = CNCCommNetScenario.Instance.getCommNetVessels().FindAll(x => x.getFrequencyList().Contains(prevFreq)); for (int i = 0; i < affectedVessels.Count; i++) { affectedVessels[i].replaceAllFrequencies(prevFreq, this.existingConstellation.frequency); affectedVessels[i].OnAntennaChange(); } List<CNCCommNetHome> affectedStations = CNCCommNetScenario.Instance.groundStations.FindAll(x => x.getFrequencyList().Contains(prevFreq)); for(int i=0; i < affectedStations.Count; i++) { affectedStations[i].replaceFrequency(prevFreq, this.existingConstellation.frequency); } ScreenMessage msg = new ScreenMessage(string.Format("Constellation has the new frequency {0}", constellFreq), CNCSettings.ScreenMessageDuration, ScreenMessageStyle.UPPER_CENTER); ScreenMessages.PostScreenMessage(msg); changesCommitted = true; } if(!this.existingConstellation.name.Equals(constellName)) // different name { this.existingConstellation.name = constellName; string message = string.Format("Constellation is renamed to '{0}'", constellName); ScreenMessages.PostScreenMessage(new ScreenMessage(message, CNCSettings.ScreenMessageDuration, ScreenMessageStyle.UPPER_CENTER)); changesCommitted = true; } if (!this.existingConstellation.color.Equals(this.constellColor)) // new color { this.existingConstellation.color = this.constellColor; string message = string.Format("Constellation color becomes '{0}'", UIUtils.colorToHex(this.constellColor)); ScreenMessages.PostScreenMessage(new ScreenMessage(message, CNCSettings.ScreenMessageDuration, ScreenMessageStyle.UPPER_CENTER)); changesCommitted = true; } CNCLog.Debug("Updated constellation: {0}, {1}", constellName, constellFreq); if (changesCommitted) { updateCallback(this.existingConstellation, prevFreq); this.dismiss(); } } } catch (FormatException e) { throw new FormatException("Frequency must be numeric only"); } catch (OverflowException e) { throw new OverflowException(string.Format("Frequency must be equal to or less than {0}", short.MaxValue)); } } catch (Exception e) { ScreenMessage msg = new ScreenMessage("<color=red>" + e.Message + "</color>", CNCSettings.ScreenMessageDuration, ScreenMessageStyle.UPPER_CENTER); ScreenMessages.PostScreenMessage(msg); } }