public string RegisterCommander(string CommanderName) { foreach (EDRacerProfile racerProfile in _commanderProfiles.Values) if (racerProfile.CommanderName.ToLower() == CommanderName.ToLower()) return "ERROR: Commander name already in use"; if (String.IsNullOrEmpty(CommanderName)) return "ERROR: Commander name cannot be blank"; if (CommanderName.Contains(Environment.NewLine)) return "ERROR: Commander name must be a single line"; if (!CommanderName.All(IsValidCommanderNameCharacter)) return "ERROR: Commander name can only contain letters and numbers"; try { Guid commanderGuid = Guid.NewGuid(); EDRacerProfile racerProfile = new EDRacerProfile(); racerProfile.CommanderName = CommanderName; _commanderProfiles.Add(commanderGuid, racerProfile); SaveCommanderProfiles(); return commanderGuid.ToString(); } catch (Exception ex) { return $"ERROR: {ex.Message}"; } }
private void LoadRegisteredCommanders() { if (File.Exists(_profilesFile)) { LoadCommanderProfiles(); return; } if (!File.Exists(_saveFile)) return; Dictionary<Guid, string> commanderGuids; try { string registeredCommanders = File.ReadAllText(_saveFile); commanderGuids = JsonSerializer.Deserialize<Dictionary<Guid, String>>(registeredCommanders); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show($"Failed to load commander registrations.{Environment.NewLine}{Environment.NewLine}{ex.Message}", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return; } // As we only had a commander Guid file, we need to generate blank profiles for all commanders _commanderProfiles = new Dictionary<Guid, EDRacerProfile>(); foreach (Guid commanderGuid in commanderGuids.Keys) { EDRacerProfile profile = new EDRacerProfile(); profile.CommanderName = commanderGuids[commanderGuid]; _commanderProfiles.Add(commanderGuid, profile); } SaveCommanderProfiles(); }
public string UpdateCommanderProfile(Guid commanderGuid, string commanderProfile) { if (!_commanderProfiles.ContainsKey(commanderGuid)) return "ERROR: Client Id not found"; try { EDRacerProfile profile = EDRacerProfile.FromJSON(commanderProfile); profile.CommanderName = _commanderProfiles[commanderGuid].CommanderName; // Ensure commander name doesn't change _commanderProfiles[commanderGuid] = profile; SaveCommanderProfiles(); return "SUCCESS"; } catch (Exception ex) { return $"ERROR: Unable to update profile ({ex.Message})"; } }