/// <summary> /// Result = 0 -> Error /// Result = 1 -> Success /// Result = 2 -> Character not found, but no errors. /// </summary> /// <param name="character"></param> /// <param name="realm"></param> /// <returns></returns> private int RetrieveCharacter(string character, string realm, int roster, string charRole = "") { int result = 0; JSONCharacter charFromWeb = JSONBase.GetCharacterJSON(character, realm); ViewBag.CharacterName = character; ViewBag.CharacterRealm = realm; if (charFromWeb == null) { if (JSONBase.CharacterNotFound) { ModelState.AddModelError(string.Empty, $"Character {character} from {realm} not found."); result = 2; } else { foreach (string e in JSONBase.Errors) { ModelState.AddModelError(string.Empty, $"Error: {e}"); } } } else { string spec = string.Empty; string role = string.Empty; if (charFromWeb.Talents != null && charFromWeb.Talents.Count > 0) { foreach (JSONCharacterTalent talent in charFromWeb.Talents) { if (talent.Selected != null && talent.Selected == "true") { spec = talent.Spec.Name; role = talent.Spec.Role; } } } List <Character> search = null; if (roster == WGOConstants.GuildRoster) { search = db.Characters.Where(s => s.Roster == 1 && s.Name == charFromWeb.Name && s.Realm == charFromWeb.Realm).ToList(); } else if (roster == WGOConstants.RaidRoster) { search = db.Characters.Where(s => s.Roster == 2 && s.Name == charFromWeb.Name && s.Realm == charFromWeb.Realm).ToList(); } else if (roster == WGOConstants.MyRoster) { search = db.Characters.Where(s => s.Roster == 3 && s.Name == charFromWeb.Name && s.Realm == charFromWeb.Realm).ToList(); } if (search.Count() > 0) { // Already exists in the DB - update it foreach (Character searchChar in search) { // Check for differences if (searchChar.Level != charFromWeb.Level) { searchChar.Modified_Level = GetCentralTime(); } if (searchChar.AchievementPoints != charFromWeb.AchievementPoints) { searchChar.Modified_AchievementPoints = GetCentralTime(); } if (searchChar.Max_iLevel != charFromWeb.Items.AverageItemLevel) { searchChar.Modified_Max_iLevel = GetCentralTime(); } // Update Character searchChar.Level = charFromWeb.Level; searchChar.Class = WoWConverter.ConvertClass(charFromWeb.Class); searchChar.Race = WoWConverter.ConvertRace(charFromWeb.Race); searchChar.AchievementPoints = charFromWeb.AchievementPoints; // Only check for updates on these fields if it's the guild roster or it's the raid roster and the role is the same if (roster == WGOConstants.GuildRoster || (roster == WGOConstants.RaidRoster && role == search[0].Role) || (roster == WGOConstants.MyRoster && role == search[0].Role)) { // This is a different Role than we are tracking... so don't update these fields if (searchChar.Equipped_iLevel != charFromWeb.Items.AverageItemLevelEquipped) { searchChar.Modified_Equipped_iLevel = GetCentralTime(); } searchChar.Equipped_iLevel = charFromWeb.Items.AverageItemLevelEquipped; searchChar.Spec = spec; searchChar.Role = role; searchChar.Items = JsonConvert.SerializeObject(charFromWeb.Items); } searchChar.Max_iLevel = charFromWeb.Items.AverageItemLevel; searchChar.LastUpdated = GetCentralTime(); if (roster == WGOConstants.GuildRoster) { searchChar.Roster = WGOConstants.GuildRoster; } else if (roster == WGOConstants.RaidRoster) { searchChar.Roster = WGOConstants.RaidRoster; } else if (roster == WGOConstants.MyRoster) { searchChar.Roster = WGOConstants.MyRoster; } } try { db.SaveChanges(); result = 1; } catch (Exception ex) { // no op ModelState.AddModelError(string.Empty, $"Error saving character: {ex.Message}"); } } else if ((roster == WGOConstants.GuildRoster) || (roster == WGOConstants.RaidRoster && charRole == role) || (roster == WGOConstants.MyRoster && charRole == role)) { // Now insert the data inot the database Character charToDB = new Character(); charToDB.Name = charFromWeb.Name; charToDB.Level = charFromWeb.Level; charToDB.Class = WoWConverter.ConvertClass(charFromWeb.Class); charToDB.Race = WoWConverter.ConvertRace(charFromWeb.Race); charToDB.AchievementPoints = charFromWeb.AchievementPoints; charToDB.Max_iLevel = charFromWeb.Items.AverageItemLevel; charToDB.Equipped_iLevel = charFromWeb.Items.AverageItemLevelEquipped; charToDB.LastUpdated = GetCentralTime(); charToDB.Realm = charFromWeb.Realm; charToDB.Spec = spec; charToDB.Role = role; charToDB.Modified_AchievementPoints = GetCentralTime(); charToDB.Modified_Equipped_iLevel = GetCentralTime(); charToDB.Modified_Max_iLevel = GetCentralTime(); charToDB.Modified_Level = GetCentralTime(); if (roster == WGOConstants.GuildRoster) { charToDB.Roster = WGOConstants.GuildRoster; } else if (roster == WGOConstants.RaidRoster) { charToDB.Roster = WGOConstants.RaidRoster; } else if (roster == WGOConstants.MyRoster) { charToDB.Roster = WGOConstants.MyRoster; } // Items = List<CharacterItems> charToDB.Items = JsonConvert.SerializeObject(charFromWeb.Items); // does it exist in the db? then add it! db.Characters.Add(charToDB); try { db.SaveChanges(); result = 1; } catch (Exception ex) { ModelState.AddModelError(string.Empty, $"Error saving character: {ex.Message}"); } } } return(result); }