From aa88df78ad116cfdd9f99ca877cbc5e1e53fe7cb Mon Sep 17 00:00:00 2001 From: Yan Maniez Date: Mon, 30 Apr 2018 22:57:41 +0200 Subject: [PATCH] Ajout de la plupart des attributs d'un monstre --- AideDeJeu/AideDeJeu/Tools/Converters.cs | 54 ++++++++- .../AideDeJeu/Tools/FormatedTextHelpers.cs | 112 +++++++++++++----- .../ViewModels/MonsterDetailViewModel.cs | 57 ++++++++- .../ViewModels/SpellDetailViewModel.cs | 2 +- .../AideDeJeu/Views/MonsterDetailPage.xaml | 31 ++++- AideDeJeu/AideDeJeuLib/Monsters/Monster.cs | 79 +++++++----- 6 files changed, 264 insertions(+), 71 deletions(-) diff --git a/AideDeJeu/AideDeJeu/Tools/Converters.cs b/AideDeJeu/AideDeJeu/Tools/Converters.cs index 26ffec11..93fc2296 100644 --- a/AideDeJeu/AideDeJeu/Tools/Converters.cs +++ b/AideDeJeu/AideDeJeu/Tools/Converters.cs @@ -1,4 +1,5 @@ -using System; +using HtmlAgilityPack; +using System; using System.Collections.Generic; using System.Globalization; using System.Text; @@ -31,4 +32,55 @@ namespace AideDeJeu.Tools return null; } } + + public class HtmlNodeToFormattedStringConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + var node = value as HtmlNode; + if (node != null) + { + var fs = new FormattedString(); + FormatedTextHelpers.HtmlNodeToFormatedString(node, fs); + return fs; + } + else + { + return null; + } + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + return null; + } + } + + public class HtmlNodesToFormattedStringConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + var nodes = value as IEnumerable; + if (nodes != null) + { + var fs = new FormattedString(); + foreach (var node in nodes) + { + FormatedTextHelpers.HtmlNodeToFormatedString(node, fs); + fs.Spans.Add(new Span() { Text = "\r\n" }); + } + return fs; + } + else + { + return null; + } + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + return null; + } + } + } diff --git a/AideDeJeu/AideDeJeu/Tools/FormatedTextHelpers.cs b/AideDeJeu/AideDeJeu/Tools/FormatedTextHelpers.cs index 9202c9a6..f2074796 100644 --- a/AideDeJeu/AideDeJeu/Tools/FormatedTextHelpers.cs +++ b/AideDeJeu/AideDeJeu/Tools/FormatedTextHelpers.cs @@ -10,43 +10,95 @@ namespace AideDeJeu.Tools { public static class FormatedTextHelpers { - public static void HtmlToFormatedString(HtmlNode parentNode, FormattedString fs, FontAttributes attributes = FontAttributes.None) + public static void HtmlNodesToFormatedString(HtmlNodeCollection nodes, FormattedString fs, FontAttributes attributes = FontAttributes.None) { - foreach (var node in parentNode.ChildNodes) + if (nodes != null) { - if (node.NodeType == HtmlNodeType.Text) + foreach (var node in nodes) { - var resname = "content"; - if (attributes.HasFlag(FontAttributes.Bold)) - { - resname += "bold"; - } - if (attributes.HasFlag(FontAttributes.Italic)) - { - resname += "ital"; - } - var fd = FontData.FromResource(resname); - fs.Spans.Add(new Span() { FontFamily = fd.FontFamily, FontAttributes = attributes | fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor, Text = node.InnerText }); - } - else if (node.NodeType == HtmlNodeType.Element && node.Name == "br") - { - fs.Spans.Add(new Span() { Text = "\r\n" }); - } - else if (node.NodeType == HtmlNodeType.Element && node.Name == "strong") - { - HtmlToFormatedString(node, fs, attributes | FontAttributes.Bold); - } - else if (node.NodeType == HtmlNodeType.Element && node.Name == "em") - { - HtmlToFormatedString(node, fs, attributes | FontAttributes.Italic); - } - else if (node.NodeType == HtmlNodeType.Element) - { - HtmlToFormatedString(node, fs, attributes); + HtmlNodeToFormatedString(node, fs, attributes); } } } + public static void HtmlNodeToFormatedString(HtmlNode node, FormattedString fs, FontAttributes attributes = FontAttributes.None) + { + if (node.NodeType == HtmlNodeType.Text) + { + var resname = "content"; + if (attributes.HasFlag(FontAttributes.Bold)) + { + resname += "bold"; + } + if (attributes.HasFlag(FontAttributes.Italic)) + { + resname += "ital"; + } + var fd = FontData.FromResource(resname); + fs.Spans.Add(new Span() { FontFamily = fd.FontFamily, FontAttributes = attributes | fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor, Text = node.InnerText }); + } + else if (node.NodeType == HtmlNodeType.Element && node.Name == "br") + { + fs.Spans.Add(new Span() { Text = "\r\n" }); + } + else if (node.NodeType == HtmlNodeType.Element && node.Name == "strong") + { + HtmlNodesToFormatedString(node.ChildNodes, fs, attributes | FontAttributes.Bold); + } + else if (node.NodeType == HtmlNodeType.Element && node.Name == "em") + { + HtmlNodesToFormatedString(node.ChildNodes, fs, attributes | FontAttributes.Italic); + } + else if (node.NodeType == HtmlNodeType.Element) + { + HtmlNodesToFormatedString(node.ChildNodes, fs, attributes); + } + } + + //public static void HtmlToFormatedString(HtmlNode parentNode, FormattedString fs, FontAttributes attributes = FontAttributes.None) + //{ + // if (parentNode.NodeType == HtmlNodeType.Element) + // { + // foreach (var node in parentNode.ChildNodes) + // { + // if (node.NodeType == HtmlNodeType.Text) + // { + // var resname = "content"; + // if (attributes.HasFlag(FontAttributes.Bold)) + // { + // resname += "bold"; + // } + // if (attributes.HasFlag(FontAttributes.Italic)) + // { + // resname += "ital"; + // } + // var fd = FontData.FromResource(resname); + // fs.Spans.Add(new Span() { FontFamily = fd.FontFamily, FontAttributes = attributes | fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor, Text = node.InnerText }); + // } + // else if (node.NodeType == HtmlNodeType.Element && node.Name == "br") + // { + // fs.Spans.Add(new Span() { Text = "\r\n" }); + // } + // else if (node.NodeType == HtmlNodeType.Element && node.Name == "strong") + // { + // HtmlToFormatedString(node, fs, attributes | FontAttributes.Bold); + // } + // else if (node.NodeType == HtmlNodeType.Element && node.Name == "em") + // { + // HtmlToFormatedString(node, fs, attributes | FontAttributes.Italic); + // } + // else if (node.NodeType == HtmlNodeType.Element) + // { + // HtmlToFormatedString(node, fs, attributes); + // } + // } + // } + // else if (parentNode.NodeType == HtmlNodeType.Text) + // { + + // } + //} + public class FontData { public double FontSize { get; set; } diff --git a/AideDeJeu/AideDeJeu/ViewModels/MonsterDetailViewModel.cs b/AideDeJeu/AideDeJeu/ViewModels/MonsterDetailViewModel.cs index 66b21a4d..797fbe88 100644 --- a/AideDeJeu/AideDeJeu/ViewModels/MonsterDetailViewModel.cs +++ b/AideDeJeu/AideDeJeu/ViewModels/MonsterDetailViewModel.cs @@ -25,6 +25,9 @@ namespace AideDeJeu.ViewModels OnPropertyChanged(nameof(Speed)); OnPropertyChanged(nameof(SavingThrows)); OnPropertyChanged(nameof(Skills)); + OnPropertyChanged(nameof(DamageResistances)); + OnPropertyChanged(nameof(DamageImmunities)); + OnPropertyChanged(nameof(ConditionImmunities)); OnPropertyChanged(nameof(Senses)); OnPropertyChanged(nameof(Languages)); OnPropertyChanged(nameof(Challenge)); @@ -84,7 +87,7 @@ namespace AideDeJeu.ViewModels var fd = FormatedTextHelpers.FontData.FromResource("content"); var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); var fs = new FormattedString(); - fs.Spans.Add(new Span() { Text = "Jets de sauvegarde : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); + fs.Spans.Add(new Span() { Text = "Jets de sauvegarde ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); fs.Spans.Add(new Span() { Text = Item.SavingThrows, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); return fs; } @@ -97,12 +100,54 @@ namespace AideDeJeu.ViewModels var fd = FormatedTextHelpers.FontData.FromResource("content"); var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); var fs = new FormattedString(); - fs.Spans.Add(new Span() { Text = "Compétences : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); + fs.Spans.Add(new Span() { Text = "Compétence ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); fs.Spans.Add(new Span() { Text = Item.Skills, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); return fs; } } + + public FormattedString DamageImmunities + { + get + { + var fd = FormatedTextHelpers.FontData.FromResource("content"); + var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); + var fs = new FormattedString(); + fs.Spans.Add(new Span() { Text = "Immunité contre les dégâts ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); + fs.Spans.Add(new Span() { Text = Item.DamageImmunities, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); + return fs; + } + } + + public FormattedString ConditionImmunities + { + get + { + var fd = FormatedTextHelpers.FontData.FromResource("content"); + var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); + var fs = new FormattedString(); + fs.Spans.Add(new Span() { Text = "Immunité contre les états ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); + fs.Spans.Add(new Span() { Text = Item.ConditionImmunities, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); + return fs; + } + } + + public FormattedString DamageResistances + { + get + { + var fd = FormatedTextHelpers.FontData.FromResource("content"); + var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); + var fs = new FormattedString(); + fs.Spans.Add(new Span() { Text = "Résistance aux dégâts ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); + fs.Spans.Add(new Span() { Text = Item.DamageResistances, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); + return fs; + } + } + + + public FormattedString Senses { get @@ -110,7 +155,7 @@ namespace AideDeJeu.ViewModels var fd = FormatedTextHelpers.FontData.FromResource("content"); var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); var fs = new FormattedString(); - fs.Spans.Add(new Span() { Text = "Sens : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); + fs.Spans.Add(new Span() { Text = "Sens ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); fs.Spans.Add(new Span() { Text = Item.Senses, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); return fs; } @@ -123,7 +168,7 @@ namespace AideDeJeu.ViewModels var fd = FormatedTextHelpers.FontData.FromResource("content"); var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); var fs = new FormattedString(); - fs.Spans.Add(new Span() { Text = "Langues : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); + fs.Spans.Add(new Span() { Text = "Langues ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); fs.Spans.Add(new Span() { Text = Item.Languages, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); return fs; } @@ -136,12 +181,14 @@ namespace AideDeJeu.ViewModels var fd = FormatedTextHelpers.FontData.FromResource("content"); var fdb = FormatedTextHelpers.FontData.FromResource("contentbold"); var fs = new FormattedString(); - fs.Spans.Add(new Span() { Text = "Puissance : ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); + fs.Spans.Add(new Span() { Text = "Dangerosité ", FontFamily = fdb.FontFamily, FontAttributes = fdb.FontAttributes, FontSize = fdb.FontSize, ForegroundColor = fdb.TextColor }); fs.Spans.Add(new Span() { Text = Item.Challenge, FontFamily = fd.FontFamily, FontAttributes = fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor }); return fs; } } + + //public FormattedString Description //{ // get diff --git a/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs b/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs index 8710f445..f3fa78e2 100644 --- a/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs +++ b/AideDeJeu/AideDeJeu/ViewModels/SpellDetailViewModel.cs @@ -34,7 +34,7 @@ namespace AideDeJeu.ViewModels var fs = new FormattedString(); if (Item?.DescriptionDiv != null) { - FormatedTextHelpers.HtmlToFormatedString(Item?.DescriptionDiv, fs, FontAttributes.None); + FormatedTextHelpers.HtmlNodeToFormatedString(Item?.DescriptionDiv, fs, FontAttributes.None); } return fs; } diff --git a/AideDeJeu/AideDeJeu/Views/MonsterDetailPage.xaml b/AideDeJeu/AideDeJeu/Views/MonsterDetailPage.xaml index 7de018c2..a4056fcc 100644 --- a/AideDeJeu/AideDeJeu/Views/MonsterDetailPage.xaml +++ b/AideDeJeu/AideDeJeu/Views/MonsterDetailPage.xaml @@ -8,6 +8,8 @@ + + @@ -15,9 +17,9 @@ - diff --git a/AideDeJeu/AideDeJeuLib/Monsters/Monster.cs b/AideDeJeu/AideDeJeuLib/Monsters/Monster.cs index ea049ebb..c060b943 100644 --- a/AideDeJeu/AideDeJeuLib/Monsters/Monster.cs +++ b/AideDeJeu/AideDeJeuLib/Monsters/Monster.cs @@ -28,12 +28,19 @@ namespace AideDeJeuLib.Monsters public string Charisma { get; set; } public string SavingThrows { get; set; } public string Skills { get; set; } + public string DamageImmunities { get; set; } + public string ConditionImmunities { get; set; } + public string DamageResistances { get; set; } public string Senses { get; set; } public string Languages { get; set; } public string Challenge { get; set; } public string Description { get; set; } public string Picture { get; set; } + public List SpecialFeatures { get; set; } + public List Actions { get; set; } + public List LegendaryActions { get; set; } + public static Monster FromHtml(HtmlNode divBloc) @@ -66,51 +73,67 @@ namespace AideDeJeuLib.Monsters monster.SavingThrows = divRed?.SelectSingleNode("strong[contains(text(),'Jets de sauvegarde')]")?.NextSibling?.InnerText; monster.Skills = divRed?.SelectSingleNode("strong[contains(text(),'Compétences')]")?.NextSibling?.InnerText; + monster.DamageResistances = divRed?.SelectSingleNode("strong[contains(text(),'Résistances aux dégâts')]")?.NextSibling?.InnerText; + monster.DamageImmunities = divRed?.SelectSingleNode("strong[contains(text(),'Immunités aux dégâts')]")?.NextSibling?.InnerText; + monster.ConditionImmunities = divRed?.SelectSingleNode("strong[contains(text(),'Immunités aux conditions')]")?.NextSibling?.InnerText; monster.Senses = divRed?.SelectSingleNode("strong[contains(text(),'Sens')]")?.NextSibling?.InnerText; monster.Languages = divRed?.SelectSingleNode("strong[contains(text(),'Langues')]")?.NextSibling?.InnerText; - monster.Power = divRed?.SelectSingleNode("strong[contains(text(),'Puissance')]")?.NextSibling?.InnerText; + monster.Challenge = divRed?.SelectSingleNode("strong[contains(text(),'Puissance')]")?.NextSibling?.InnerText; - List actions = new List(); - List beforeActions = null; - List commonActions = null; - List legendaryActions = null; - var p = divSansSerif.SelectSingleNode("p"); - while(p != null) + List nodes = new List(); + List specialFeatures = null; + List actions = null; + List legendaryActions = null; + var node = divSansSerif.SelectSingleNode("p"); + while(node != null) { - if(p.NodeType == HtmlNodeType.Element && p.Name == "p") + if(node.NodeType == HtmlNodeType.Element && node.Name == "div") { - actions.Add(p.InnerText); - } - else if(p.NodeType == HtmlNodeType.Element && p.Name == "div") - { - if(p.InnerText == "ACTIONS") + if(node.InnerText == "ACTIONS") { - beforeActions = actions; - actions = new List(); + specialFeatures = nodes; + nodes = new List(); } - else if (p.InnerText == "ACTIONS LÉGENDAIRES") + else if (node.InnerText == "ACTIONS LÉGENDAIRES") { - commonActions = actions; - actions = new List(); + actions = nodes; + nodes = new List(); } } - p = p.NextSibling; - } - if(commonActions == null) - { - if(beforeActions == null) - { - beforeActions = actions; - } else { - commonActions = actions; + nodes.Add(node); + } + node = node.NextSibling; + } + if(actions == null) + { + if(specialFeatures == null) + { + specialFeatures = nodes; + } + else + { + actions = nodes; } } else { - legendaryActions = actions; + legendaryActions = nodes; } + + monster.SpecialFeatures = specialFeatures; + monster.Actions = actions; + monster.LegendaryActions = legendaryActions; + + var divDescription = divBloc?.SelectSingleNode("div[contains(@class,'description')]"); + monster.Description = divDescription?.InnerText; + + var divSource = divBloc?.SelectSingleNode("div[contains(@class,'source')]"); + monster.Source = divSource?.InnerText; + + var img = divBloc?.SelectSingleNode("div[contains(@class,'center')]/img[contains(@class,'picture')]"); + monster.Picture = img?.GetAttributeValue("src", null); return monster; }