public async Task <LocationCreationResult> AddLocation(string locationName, string faction, int level) { var normalized = locationName.Normalise(); var system = await _db.Systems.FirstOrDefaultAsync(x => x.NormalizedName == normalized); if (system != null) { return(LocationCreationResult.Duplicate); } system = new Data.Models.System { Faction = faction, Level = level, Name = locationName, NormalizedName = normalized }; _db.Systems.Add(system); await _db.SaveChangesAsync(); return(LocationCreationResult.OK); }
public async Task <UpdatePlayerResult> TryUpdatePlayerLocation(string playerName, string location) { var normalized = playerName.Normalise(); var player = await _db.Players.Include(a => a.System).FirstOrDefaultAsync(x => x.NormalizedName == normalized); if (player == null) { return(UpdatePlayerResult.CantFindPlayer); } Data.Models.System system = null; if (location != null) { var normalisedSystemName = location.Normalise(); system = await _db.Systems.AsQueryable().SingleOrDefaultAsync(a => a.NormalizedName.StartsWith(normalisedSystemName)); if (system == null) { return(UpdatePlayerResult.CantFindSystem); } } UpdatePlayer(player, player.Name, player.Alliance, system, player.Level); await _db.SaveChangesAsync(); return(UpdatePlayerResult.OK); }
public async Task <PlayerCreationResult> AddPlayer(string playerName, string alliance, string location, string addedByUsername, int?level) { var normalized = playerName.Normalise(); var player = await _db.Players.Include(a => a.System).FirstOrDefaultAsync(x => x.NormalizedName == normalized); if (player != null) { return(PlayerCreationResult.Duplicate); } player = new Player(); player.AddedDate = DateTime.Now; player.AddedBy = addedByUsername; _db.Players.Add(player); Data.Models.System system = null; if (string.IsNullOrWhiteSpace(location) == false) { var systemName = location.Normalise(); system = await _db.Systems.AsQueryable().FirstOrDefaultAsync(a => a.NormalizedName == systemName); if (system == null) { return(PlayerCreationResult.CantFindSystem); } } UpdatePlayer(player, playerName, alliance, system, level); await _db.SaveChangesAsync(); return(PlayerCreationResult.OK); }
private static void UpdatePlayer(Player player, string name, string alliance, Data.Models.System system, int?level) { name = name.UnifyApostrophe(); player.Name = name; player.NormalizedName = name.Normalise(); player.Level = level; if (!string.IsNullOrWhiteSpace(alliance)) { alliance = alliance.UnifyApostrophe(); player.Alliance = alliance.ToUpperInvariant(); player.NormalizedAlliance = alliance.Normalise(); } player.SystemLogs.Add(new SystemLog { Player = player, System = system, DateUpdated = DateTime.Now }); player.System = system; player.UpdatedDate = DateTime.Now; }