From 3ac3afc3aad50c9eaad3162602d3917c7d3f3fc1 Mon Sep 17 00:00:00 2001 From: Yan Maniez Date: Mon, 14 May 2018 21:40:18 +0200 Subject: [PATCH] =?UTF-8?q?Recablage=20des=20filtres=20pour=20les=20sorts?= =?UTF-8?q?=20en=20local=20+=20reparsing=20=C3=A0=20la=20vol=C3=A9e=20du?= =?UTF-8?q?=20html=20en=20vue=20d=C3=A9tail=20en=20attendant=20que=20la=20?= =?UTF-8?q?bdd=20soit=20carr=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AideDeJeu.Android.csproj | 3 ++ AideDeJeu/AideDeJeu.Android/NativeAPI.cs | 28 ++++++++++- AideDeJeu/AideDeJeu.UWP/AideDeJeu.UWP.csproj | 3 ++ AideDeJeu/AideDeJeu.UWP/Package.appxmanifest | 4 +- .../AideDeJeu/Services/ItemDatabaseHelper.cs | 12 ++++- .../ViewModels/SpellDetailViewModel.cs | 12 +++-- .../AideDeJeu/ViewModels/SpellsViewModel.cs | 40 ++++++++------- AideDeJeu/AideDeJeuLib/Spells/Spell.cs | 50 ++++++++++++------- 8 files changed, 106 insertions(+), 46 deletions(-) diff --git a/AideDeJeu/AideDeJeu.Android/AideDeJeu.Android.csproj b/AideDeJeu/AideDeJeu.Android/AideDeJeu.Android.csproj index 604ac510..c0000769 100644 --- a/AideDeJeu/AideDeJeu.Android/AideDeJeu.Android.csproj +++ b/AideDeJeu/AideDeJeu.Android/AideDeJeu.Android.csproj @@ -85,6 +85,9 @@ + + Assets\database.db + diff --git a/AideDeJeu/AideDeJeu.Android/NativeAPI.cs b/AideDeJeu/AideDeJeu.Android/NativeAPI.cs index 606132e7..dc4c2503 100644 --- a/AideDeJeu/AideDeJeu.Android/NativeAPI.cs +++ b/AideDeJeu/AideDeJeu.Android/NativeAPI.cs @@ -29,7 +29,33 @@ namespace AideDeJeu.Droid public string GetDatabasePath(string databaseName) { - return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), databaseName); + var documentsDirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); + var path = Path.Combine(documentsDirectoryPath, databaseName); + + // This is where we copy in our pre-created database + if (!File.Exists(path)) + { + using (var inStream = Android.App.Application.Context.Assets.Open(databaseName)) + { + using (var outStream = new FileStream(path, FileMode.Create)) + { + inStream.CopyTo(outStream); + } + } + //using (var binaryReader = new BinaryReader(Android.App.Application.Context.Assets.Open(databaseName))) + //{ + // using (var binaryWriter = new BinaryWriter(new FileStream(path, FileMode.Create))) + // { + // byte[] buffer = new byte[2048]; + // int length = 0; + // while ((length = binaryReader.Read(buffer, 0, buffer.Length)) > 0) + // { + // binaryWriter.Write(buffer, 0, length); + // } + // } + //} + } + return path; } } } \ No newline at end of file diff --git a/AideDeJeu/AideDeJeu.UWP/AideDeJeu.UWP.csproj b/AideDeJeu/AideDeJeu.UWP/AideDeJeu.UWP.csproj index f2299d1d..210c9124 100644 --- a/AideDeJeu/AideDeJeu.UWP/AideDeJeu.UWP.csproj +++ b/AideDeJeu/AideDeJeu.UWP/AideDeJeu.UWP.csproj @@ -102,6 +102,9 @@ Designer + + Assets\database.db + diff --git a/AideDeJeu/AideDeJeu.UWP/Package.appxmanifest b/AideDeJeu/AideDeJeu.UWP/Package.appxmanifest index bd2a2ac0..bd6a88ee 100644 --- a/AideDeJeu/AideDeJeu.UWP/Package.appxmanifest +++ b/AideDeJeu/AideDeJeu.UWP/Package.appxmanifest @@ -1,10 +1,10 @@  - + AideDeJeu.UWP - 899f1516-2a42-438b-8163-40d8e0e659aa + Nioux-899f1516-2a42-438b-8163-40d8e0e659aa Assets\StoreLogo.png diff --git a/AideDeJeu/AideDeJeu/Services/ItemDatabaseHelper.cs b/AideDeJeu/AideDeJeu/Services/ItemDatabaseHelper.cs index 16081fb1..4c573899 100644 --- a/AideDeJeu/AideDeJeu/Services/ItemDatabaseHelper.cs +++ b/AideDeJeu/AideDeJeu/Services/ItemDatabaseHelper.cs @@ -19,14 +19,22 @@ namespace AideDeJeu.Services return postDatabaseContext; } - public async Task> GetSpellsAsync() + public async Task> GetSpellsAsync(string classe, string niveauMin, string niveauMax, string ecole, string rituel, string source) { using (var context = CreateContext()) { //We use OrderByDescending because Posts are generally displayed from most recent to oldest return await context.Spells .AsNoTracking() - .OrderByDescending(spell => spell.Id) + .Where(spell => + (int.Parse(spell.Level) >= int.Parse(niveauMin)) && + (int.Parse(spell.Level) <= int.Parse(niveauMax)) && + spell.Type.Contains(ecole) && + spell.Source.Contains(source) && + spell.Type.Contains(classe) && + spell.Type.Contains(rituel) + ) + .OrderByDescending(spell => spell.NamePHB) .ToListAsync(); } } diff --git a/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs b/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs index 1d947c24..65f4d0a1 100644 --- a/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs +++ b/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs @@ -120,11 +120,13 @@ namespace AideDeJeu.ViewModels try { - using (var spellsScrappers = new SpellsScrappers()) - { - var item = await spellsScrappers.GetSpell(Item.Id); - Item = item; - } + Item.ParseHtml(); + Item = _Item; + //using (var spellsScrappers = new SpellsScrappers()) + //{ + // var item = await spellsScrappers.GetSpell(Item.Id); + // Item = item; + //} } catch (Exception ex) { diff --git a/AideDeJeu/AideDeJeu/ViewModels/SpellsViewModel.cs b/AideDeJeu/AideDeJeu/ViewModels/SpellsViewModel.cs index 827db6ca..e135bc7e 100644 --- a/AideDeJeu/AideDeJeu/ViewModels/SpellsViewModel.cs +++ b/AideDeJeu/AideDeJeu/ViewModels/SpellsViewModel.cs @@ -16,19 +16,19 @@ namespace AideDeJeu.ViewModels public List> Classes { get; set; } = new List>() { new KeyValuePair("", "Toutes" ), - new KeyValuePair("b", "Barde" ), - new KeyValuePair("c", "Clerc" ), - new KeyValuePair("d", "Druide" ), - new KeyValuePair("s", "Ensorceleur" ), - new KeyValuePair("w", "Magicien" ), - new KeyValuePair("p", "Paladin" ), - new KeyValuePair("r", "Rôdeur" ), - new KeyValuePair("k", "Sorcier" ), + new KeyValuePair("Barde", "Barde" ), + new KeyValuePair("Clerc", "Clerc" ), + new KeyValuePair("Druide", "Druide" ), + new KeyValuePair("Ensorceleur", "Ensorceleur" ), + new KeyValuePair("Magicien", "Magicien" ), + new KeyValuePair("Paladin", "Paladin" ), + new KeyValuePair("Rôdeur", "Rôdeur" ), + new KeyValuePair("Sorcier", "Sorcier" ), }; public List> Niveaux { get; set; } = new List>() { - new KeyValuePair("Z", "Sorts mineurs"), + new KeyValuePair("0", "Sorts mineurs"), new KeyValuePair("1", "Niveau 1"), new KeyValuePair("2", "Niveau 2"), new KeyValuePair("3", "Niveau 3"), @@ -46,24 +46,24 @@ namespace AideDeJeu.ViewModels new KeyValuePair("abjuration", "Abjuration"), new KeyValuePair("divination", "Divination"), new KeyValuePair("enchantement", "Enchantement"), - new KeyValuePair("evocation", "Évocation"), + new KeyValuePair("vocation", "Évocation"), new KeyValuePair("illusion", "Illusion"), new KeyValuePair("invocation", "Invocation"), - new KeyValuePair("necromancie", "Nécromancie"), + new KeyValuePair("cromancie", "Nécromancie"), new KeyValuePair("transmutation", "Transmutation"), }; public List> Rituels { get; set; } = new List>() { new KeyValuePair("", "Tous"), - new KeyValuePair("1", "Rituel"), + new KeyValuePair("(rituel)", "Rituel"), }; public List> Sources { get; set; } = new List>() { new KeyValuePair("", "Toutes"), - new KeyValuePair("srd", "SRD"), - new KeyValuePair("ph", "PHB"), + new KeyValuePair("(SRD)", "SRD"), + new KeyValuePair("Player's Handbook", "PHB"), new KeyValuePair("sup", "SCAG, XGtE"), }; @@ -171,11 +171,15 @@ namespace AideDeJeu.ViewModels { AllItems.Clear(); IEnumerable items = null; - using (var spellsScrappers = new SpellsScrappers()) - { - items = await spellsScrappers.GetSpells(classe: Classes[Classe].Key, niveauMin: Niveaux[NiveauMin].Key, niveauMax: Niveaux[NiveauMax].Key, ecole: Ecoles[Ecole].Key, rituel: Rituels[Rituel].Key, source: Sources[Source].Key); - } + //using (var spellsScrappers = new SpellsScrappers()) + //{ + // items = await spellsScrappers.GetSpells(classe: Classes[Classe].Key, niveauMin: Niveaux[NiveauMin].Key, niveauMax: Niveaux[NiveauMax].Key, ecole: Ecoles[Ecole].Key, rituel: Rituels[Rituel].Key, source: Sources[Source].Key); + //} + ItemDatabaseHelper helper = new ItemDatabaseHelper(); + items = await helper.GetSpellsAsync(classe: Classes[Classe].Key, niveauMin: Niveaux[NiveauMin].Key, niveauMax: Niveaux[NiveauMax].Key, ecole: Ecoles[Ecole].Key, rituel: Rituels[Rituel].Key, source: Sources[Source].Key); + + //try //{ //ItemDatabaseHelper helper = new ItemDatabaseHelper(); diff --git a/AideDeJeu/AideDeJeuLib/Spells/Spell.cs b/AideDeJeu/AideDeJeuLib/Spells/Spell.cs index 8b24c65f..400aabec 100644 --- a/AideDeJeu/AideDeJeuLib/Spells/Spell.cs +++ b/AideDeJeu/AideDeJeuLib/Spells/Spell.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Text.RegularExpressions; @@ -55,33 +56,46 @@ namespace AideDeJeuLib.Spells public string NoOverflow { get; set; } public string Source { get; set; } - public static Spell FromHtml(HtmlNode nodeSpell) + public void ParseHtml() { - var spell = new Spell(); - spell.Html = nodeSpell.OuterHtml; - spell.Name = nodeSpell.SelectSingleNode("h1").InnerText; + var pack = new HtmlDocument(); + pack.LoadHtml(this.Html); + var divSpell = pack.DocumentNode.SelectNodes("//div[contains(@class,'bloc')]").FirstOrDefault(); + ParseNode(divSpell); + } + + public void ParseNode(HtmlNode nodeSpell) + { + this.Name = nodeSpell.SelectSingleNode("h1").InnerText; var altNames = nodeSpell.SelectSingleNode("div[@class='trad']")?.InnerText; if (altNames != null) { var matchNames = new Regex(@"\[ (?.*?) \](?: \[ (?.*?) \])?").Match(altNames); - spell.NameVO = matchNames.Groups["vo"].Value; - spell.NamePHB = string.IsNullOrEmpty(matchNames.Groups["alt"].Value) ? spell.Name : matchNames.Groups["alt"].Value; + this.NameVO = matchNames.Groups["vo"].Value; + this.NamePHB = string.IsNullOrEmpty(matchNames.Groups["alt"].Value) ? this.Name : matchNames.Groups["alt"].Value; } else { - spell.NamePHB = spell.Name; + this.NamePHB = this.Name; } - spell.LevelType = nodeSpell.SelectSingleNode("h2/em").InnerText; - spell.Level = spell.LevelType.Split(new string[] { " - " }, StringSplitOptions.None)[0].Split(' ')[1]; - spell.Type = spell.LevelType.Split(new string[] { " - " }, StringSplitOptions.None)[1]; - spell.CastingTime = nodeSpell.SelectSingleNode("div[@class='paragraphe']").InnerText.Split(new string[] { " : " }, StringSplitOptions.None)[1]; - spell.Range = nodeSpell.SelectSingleNode("div[strong/text()='Portée']").InnerText.Split(new string[] { " : " }, StringSplitOptions.None)[1]; - spell.Components = nodeSpell.SelectSingleNode("div[strong/text()='Composantes']").InnerText.Split(new string[] { " : " }, StringSplitOptions.None)[1]; - spell.Duration = nodeSpell.SelectSingleNode("div[strong/text()='Durée']").InnerText.Split(new string[] { " : " }, StringSplitOptions.None)[1]; - spell.DescriptionDiv = nodeSpell.SelectSingleNode("div[contains(@class,'description')]"); - spell.Overflow = nodeSpell.SelectSingleNode("div[@class='overflow']")?.InnerText; - spell.NoOverflow = nodeSpell.SelectSingleNode("div[@class='nooverflow']")?.InnerText; - spell.Source = nodeSpell.SelectSingleNode("div[@class='source']").InnerText; + this.LevelType = nodeSpell.SelectSingleNode("h2/em").InnerText; + this.Level = this.LevelType.Split(new string[] { " - " }, StringSplitOptions.None)[0].Split(' ')[1]; + this.Type = this.LevelType.Split(new string[] { " - " }, StringSplitOptions.None)[1]; + this.CastingTime = nodeSpell.SelectSingleNode("div[@class='paragraphe']").InnerText.Split(new string[] { " : " }, StringSplitOptions.None)[1]; + this.Range = nodeSpell.SelectSingleNode("div[strong/text()='Portée']").InnerText.Split(new string[] { " : " }, StringSplitOptions.None)[1]; + this.Components = nodeSpell.SelectSingleNode("div[strong/text()='Composantes']").InnerText.Split(new string[] { " : " }, StringSplitOptions.None)[1]; + this.Duration = nodeSpell.SelectSingleNode("div[strong/text()='Durée']").InnerText.Split(new string[] { " : " }, StringSplitOptions.None)[1]; + this.DescriptionDiv = nodeSpell.SelectSingleNode("div[contains(@class,'description')]"); + this.Overflow = nodeSpell.SelectSingleNode("div[@class='overflow']")?.InnerText; + this.NoOverflow = nodeSpell.SelectSingleNode("div[@class='nooverflow']")?.InnerText; + this.Source = nodeSpell.SelectSingleNode("div[@class='source']").InnerText; + } + + public static Spell FromHtml(HtmlNode nodeSpell) + { + var spell = new Spell(); + spell.Html = nodeSpell.OuterHtml; + spell.ParseNode(nodeSpell); return spell; } }