/// <summary> /// Download one Skill by name and update it or save if it doesn't exist. /// </summary> /// <param name="id">Name of skill.</param>s public override async Task Download(string id) { Status = Statuses.InProgress; List <SkillJsonWrapper> wrappedSkills; try { string responseJson = await GetData("ship_skills, ships", SkillFields, "ship_skills._pageName=ships._pageName", "ship_skills.Name=\'" + id + "\'"); wrappedSkills = JsonConvert.DeserializeObject <List <SkillJsonWrapper> >(responseJson); } catch (JsonException) { Status = Statuses.ErrorInDeserialization; Logger.Write($"Failed to desirialize skill. ID: {id}", this.GetType().ToString()); return; } catch { Status = Statuses.DownloadError; Logger.Write($"Failed to get data for skill from server. ID: {id}", this.GetType().ToString()); return; } SkillJsonWrapper wrappedSkill = wrappedSkills.FirstOrDefault(); if (wrappedSkill == null) { Status = Statuses.EmptyResponse; return; } TotalImageCount = wrappedSkills.Count; using (CargoContext cargoContext = new CargoContext()) { downloadBlock.Post(wrappedSkill.Skill.Icon); downloadBlock.Complete(); wrappedSkill.Skill.FK_ShipGirl = await cargoContext.ShipGirls.FindAsync(wrappedSkill.Skill.ShipID); wrappedSkill.Skill.Detail = RemoveHTML(wrappedSkill.Skill.Detail); SavePaths(wrappedSkill.Skill); if (await cargoContext.Skills .CountAsync(e => e.Name == wrappedSkill.Skill.Name) == 0) { cargoContext.Skills.Add(wrappedSkill.Skill); await cargoContext.SaveChangesAsync(); } else { await cargoContext.Update(wrappedSkill.Skill); } } //downloadBlock.Completion.Wait(); Status = Statuses.DownloadComplete; }
/// <summary> /// Download all Skills of ShipGirl and update them or save if they doesn't exist. /// </summary> /// <param name="shipGirl">ShipGirl which skills need update.</param> /// <param name="cargoContext">DB context</param> public async Task Download(ShipGirl shipGirl, CargoContext cargoContext) { Status = Statuses.InProgress; List <SkillJsonWrapper> wrappedSkills; try { string responseJson = await GetData("ship_skills, ships", SkillFields, "ship_skills._pageName=ships._pageName", "ships.ShipID=\'" + shipGirl.ShipID + "\'"); wrappedSkills = JsonConvert.DeserializeObject <List <SkillJsonWrapper> >(responseJson); } catch (JsonException) { Status = Statuses.ErrorInDeserialization; Logger.Write($"Failed to desirialize skills for shipgirl. Shipirl ID: {shipGirl.ShipID}", this.GetType().ToString()); return; } catch { Status = Statuses.DownloadError; Logger.Write($"Failed to get skills data for shipgir from server. Shipgirl ID: {shipGirl.ShipID}", this.GetType().ToString()); return; } if (wrappedSkills.Count == 0) { Status = Statuses.EmptyResponse; return; } TotalImageCount = wrappedSkills.Count; foreach (Skill skill in shipGirl.Skills) { string imagePath = GetImageFolder(skill.Icon) + "/" + skill.Icon; if (File.Exists(imagePath)) { File.Delete(imagePath); } } cargoContext.Skills.RemoveRange(shipGirl.Skills); await cargoContext.SaveChangesAsync(); foreach (SkillJsonWrapper wrappedSkill in wrappedSkills) { downloadBlock.Post(wrappedSkill.Skill.Icon); } downloadBlock.Complete(); foreach (SkillJsonWrapper wrappedSkill in wrappedSkills) { wrappedSkill.Skill.FK_ShipGirl = shipGirl; wrappedSkill.Skill.Detail = RemoveHTML(wrappedSkill.Skill.Detail); SavePaths(wrappedSkill.Skill); if (await cargoContext.Skills .CountAsync(e => e.Name == wrappedSkill.Skill.Name) == 0) { cargoContext.Skills.Add(wrappedSkill.Skill); await cargoContext.SaveChangesAsync(); } else { await cargoContext.Update(wrappedSkill.Skill); } } downloadBlock.Completion.Wait(); Status = Statuses.DownloadComplete; }
/// <summary> /// Download one piece of Equipment by name and update it or save if it doesn't exist. /// </summary> /// <param name="id">Name of equipment.</param> public override async Task Download(string id) { Status = Statuses.InProgress; List <EquipmentJsonWrapper> wrappedEquipment; try { string responseJson = await GetData("equipment", EquipmentFields, "equipment.Name=\'" + id + "\'"); wrappedEquipment = JsonConvert.DeserializeObject <List <EquipmentJsonWrapper> >(responseJson); } catch (JsonException) { Status = Statuses.ErrorInDeserialization; Logger.Write($"Failed to desirialize equipment. ID: {id}", this.GetType().ToString()); return; } catch { Status = Statuses.DownloadError; Logger.Write($"Failed to get data for equipment from server. ID: {id}", this.GetType().ToString()); return; } EquipmentJsonWrapper wrpEquipment = wrappedEquipment.FirstOrDefault(); if (wrpEquipment == null) { Status = Statuses.EmptyResponse; return; } TotalImageCount = wrappedEquipment.Count; using (CargoContext cargoContext = new CargoContext()) { downloadBlock.Post(wrpEquipment.Equipment.Image); downloadBlock.Complete(); Nationality nationality = cargoContext.Nationalities.Find(wrpEquipment.Equipment.Nationality); if (nationality == null) { nationality = new Nationality { Name = wrpEquipment.Equipment.Nationality, FK_Icon = cargoContext.Icons.Find(wrpEquipment.Equipment.Nationality) }; cargoContext.Nationalities.Add(nationality); cargoContext.SaveChanges(); } wrpEquipment.Equipment.FK_Nationality = nationality; CreateRelationships(wrpEquipment.Equipment, cargoContext); wrpEquipment.Equipment.DropLocation = Refactor(wrpEquipment.Equipment.DropLocation); wrpEquipment.Equipment.Notes = Refactor(wrpEquipment.Equipment.Notes); wrpEquipment.Equipment.Name = Refactor(wrpEquipment.Equipment.Name); SavePaths(wrpEquipment.Equipment); if (await cargoContext.ShipGirlsEquipment .CountAsync(e => e.Name == wrpEquipment.Equipment.Name) == 0) { cargoContext.ShipGirlsEquipment.Add(wrpEquipment.Equipment); await cargoContext.SaveChangesAsync(); } else { await cargoContext.Update(wrpEquipment.Equipment); } } //downloadBlock.Completion.Wait(); Status = Statuses.DownloadComplete; }