1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-31 07:26:09 +00:00

Préparation multilingue

This commit is contained in:
Yan Maniez 2018-05-23 20:28:50 +02:00
parent ee98ddea9d
commit ac7c57a956
8 changed files with 123 additions and 78 deletions

View file

@ -53,6 +53,10 @@ namespace AideDeJeu.Tools
{ {
HtmlNodesToFormatedString(node.ChildNodes, fs, attributes); HtmlNodesToFormatedString(node.ChildNodes, fs, attributes);
} }
else if (node.NodeType == HtmlNodeType.Document)
{
HtmlNodesToFormatedString(node.ChildNodes, fs, attributes);
}
} }
//public static void HtmlToFormatedString(HtmlNode parentNode, FormattedString fs, FontAttributes attributes = FontAttributes.None) //public static void HtmlToFormatedString(HtmlNode parentNode, FormattedString fs, FontAttributes attributes = FontAttributes.None)

View file

@ -6,6 +6,21 @@
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Content Include="..\..\Data\monsters.json" Link="monsters.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="..\..\Data\spells.json" Link="spells.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="..\..\Data\database.db" Link="database.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="2.0.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="2.0.3" />

View file

@ -25,24 +25,24 @@ namespace AideDeJeu.Services
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
if (DatabasePath == null) //if (DatabasePath == null)
{ //{
switch (Device.RuntimePlatform) // switch (Device.RuntimePlatform)
{ // {
case Device.iOS: // case Device.iOS:
SQLitePCL.Batteries_V2.Init(); // SQLitePCL.Batteries_V2.Init();
DatabasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", DatabaseName); ; // DatabasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", DatabaseName); ;
break; // break;
case Device.Android: // case Device.Android:
DatabasePath = DependencyService.Get<INativeAPI>().GetDatabasePath(DatabaseName); // DatabasePath = DependencyService.Get<INativeAPI>().GetDatabasePath(DatabaseName);
break; // break;
case Device.UWP: // case Device.UWP:
DatabasePath = DependencyService.Get<INativeAPI>().GetDatabasePath(DatabaseName); // DatabasePath = DependencyService.Get<INativeAPI>().GetDatabasePath(DatabaseName);
break; // break;
default: // default:
throw new NotImplementedException("Platform not supported"); // throw new NotImplementedException("Platform not supported");
} // }
} //}
// Specify that we will use sqlite and the path of the database here // Specify that we will use sqlite and the path of the database here
optionsBuilder.UseSqlite($"Filename={DatabasePath}"); optionsBuilder.UseSqlite($"Filename={DatabasePath}");
} }

View file

@ -1,8 +1,11 @@
using AideDeJeu.Services; using AideDeJeu.Services;
using AideDeJeuLib.Monsters;
using AideDeJeuLib.Spells; using AideDeJeuLib.Spells;
using HtmlAgilityPack;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net.Http;
using System.Runtime.Serialization.Json; using System.Runtime.Serialization.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -12,21 +15,61 @@ namespace AideDeJeuCmd
{ {
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
var documentsDirectoryPath = @"C:\Users\yanma\Documents\Visual Studio 2017\Projects\AideDeJeu\Data\database.db"; // Windows.Storage.ApplicationData.Current.LocalFolder.Path; //var documentsDirectoryPath = @"database.db"; // Windows.Storage.ApplicationData.Current.LocalFolder.Path;
ItemDatabaseHelper helper = new ItemDatabaseHelper(documentsDirectoryPath); //ItemDatabaseHelper helper = new ItemDatabaseHelper(documentsDirectoryPath);
//var items = await helper.GetSpellsAsync(classe: "", niveauMin: "0", niveauMax: "9", ecole: "", rituel: "", source: "(SRD)"); //var spells = await helper.GetSpellsAsync(classe: "", niveauMin: "0", niveauMax: "9", ecole: "", rituel: "", source: "(SRD)");
var items = await helper.GetMonstersAsync(category: "", type: "", minPower: " 0 (0 PX)", maxPower: " 30 (155000 PX)", size: "", legendary: "", source: "(SRD)"); //var monsters = await helper.GetMonstersAsync(category: "", type: "", minPower: " 0 (0 PX)", maxPower: " 30 (155000 PX)", size: "", legendary: "", source: "(SRD)");
var pack = new HtmlDocument();
var client = new HttpClient();
foreach (var item in items) var spells = LoadJSon<IEnumerable<Spell>>("spells.json");
var monsters = LoadJSon<IEnumerable<Monster>>("monsters.json");
var spellsVO = new List<Spell>();
foreach(var spell in spells)
{ {
spell.ParseHtml();
var htmlVO = await client.GetStringAsync(string.Format("https://www.aidedd.org/dnd/sorts.php?vo={0}", spell.IdVO));
pack.LoadHtml(htmlVO);
var spellVO = Spell.FromHtml(pack.DocumentNode.SelectSingleNode("//div[contains(@class,'bloc')]"));
spellVO.IdVO = spell.IdVO;
spell.IdVF = spellVO.IdVF;
spellsVO.Add(spellVO);
Console.WriteLine(string.Format("{0} : {1} / {2} : {3}", spell.IdVF, spell.NamePHB, spellVO.IdVO, spellVO.NameVO));
} }
DataContractJsonSerializer serializer = new DataContractJsonSerializer(items.GetType());
MemoryStream stream = new MemoryStream(); foreach(var monster in monsters)
serializer.WriteObject(stream, items); {
stream.Seek(0, SeekOrigin.Begin); monster.ParseHtml();
string text = await new StreamReader(stream).ReadToEndAsync(); }
SaveJSon<IEnumerable<Monster>>("monsters_vf.json", monsters);
SaveJSon<IEnumerable<Spell>>("spells_vf.json", spells);
//SaveJSon<IEnumerable<Monster>>("monsters_fr.json", monsters);
SaveJSon<IEnumerable<Spell>>("spells_vo.json", spellsVO);
//DataContractJsonSerializer serializer = new DataContractJsonSerializer(items.GetType());
//MemoryStream stream = new MemoryStream();
//serializer.WriteObject(stream, items);
//stream.Seek(0, SeekOrigin.Begin);
//string text = await new StreamReader(stream).ReadToEndAsync();
Console.WriteLine("Hello World!"); Console.WriteLine("Hello World!");
} }
private static T LoadJSon<T>(string filename) where T : class
{
var serializer = new DataContractJsonSerializer(typeof(T));
using (var stream = new FileStream(filename, FileMode.Open))
{
return serializer.ReadObject(stream) as T;
}
}
private static void SaveJSon<T>(string filename, T objT) where T : class
{
var serializer = new DataContractJsonSerializer(typeof(T));
using (var stream = new FileStream(filename, FileMode.Create))
{
serializer.WriteObject(stream, objT);
}
}
} }
} }

View file

@ -8,10 +8,4 @@
<PackageReference Include="HtmlAgilityPack" Version="1.8.2" /> <PackageReference Include="HtmlAgilityPack" Version="1.8.2" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="System.ComponentModel.Annotations">
<HintPath>..\..\..\..\..\..\..\..\Program Files (x86)\Microsoft SDKs\UWPNuGetPackages\microsoft.netcore.universalwindowsplatform\6.1.4\ref\uap10.0.15138\System.ComponentModel.Annotations.dll</HintPath>
</Reference>
</ItemGroup>
</Project> </Project>

View file

@ -1,14 +1,10 @@
using System; namespace AideDeJeuLib
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace AideDeJeuLib
{ {
public class Item public class Item
{ {
[Key]
public string Id { get; set; } public string Id { get; set; }
public string IdVO { get; set; }
public string IdVF { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string NameVO { get; set; } public string NameVO { get; set; }
public string NamePHB { get; set; } public string NamePHB { get; set; }

View file

@ -1,10 +1,7 @@
using HtmlAgilityPack; using HtmlAgilityPack;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq; using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace AideDeJeuLib.Monsters namespace AideDeJeuLib.Monsters
@ -54,7 +51,6 @@ namespace AideDeJeuLib.Monsters
} }
[IgnoreDataMember] [IgnoreDataMember]
[NotMapped]
public List<HtmlNode> SpecialFeatures { get; set; } public List<HtmlNode> SpecialFeatures { get; set; }
//public List<string> SpecialFeaturesPersist //public List<string> SpecialFeaturesPersist
//{ //{
@ -76,10 +72,8 @@ namespace AideDeJeuLib.Monsters
//} //}
[IgnoreDataMember] [IgnoreDataMember]
[NotMapped]
public List<HtmlNode> Actions { get; set; } public List<HtmlNode> Actions { get; set; }
[IgnoreDataMember] [IgnoreDataMember]
[NotMapped]
public List<HtmlNode> LegendaryActions { get; set; } public List<HtmlNode> LegendaryActions { get; set; }

View file

@ -1,11 +1,7 @@
using HtmlAgilityPack; using HtmlAgilityPack;
using System; using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq; using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace AideDeJeuLib.Spells namespace AideDeJeuLib.Spells
@ -21,26 +17,7 @@ namespace AideDeJeuLib.Spells
public string Range { get; set; } public string Range { get; set; }
public string Components { get; set; } public string Components { get; set; }
public string Duration { get; set; } public string Duration { get; set; }
public string DescriptionHtml public string DescriptionHtml { get; set; }
{
get
{
return DescriptionDiv?.InnerHtml;
}
set
{
if (value != null)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(value);
DescriptionDiv = doc.DocumentNode;
}
else
{
DescriptionDiv = null;
}
}
}
public string DescriptionText public string DescriptionText
{ {
get get
@ -49,8 +26,23 @@ namespace AideDeJeuLib.Spells
} }
} }
[IgnoreDataMember] [IgnoreDataMember]
[NotMapped] public HtmlNode DescriptionDiv
public HtmlNode DescriptionDiv { get; set; } {
get
{
if(DescriptionHtml != null)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(DescriptionHtml);
return doc.DocumentNode;
}
return null;
}
set
{
DescriptionHtml = value?.OuterHtml;
}
}
public string Overflow { get; set; } public string Overflow { get; set; }
public string NoOverflow { get; set; } public string NoOverflow { get; set; }
@ -67,7 +59,14 @@ namespace AideDeJeuLib.Spells
public void ParseNode(HtmlNode nodeSpell) public void ParseNode(HtmlNode nodeSpell)
{ {
this.Name = nodeSpell.SelectSingleNode("h1").InnerText; this.Name = nodeSpell.SelectSingleNode("h1").InnerText;
var altNames = nodeSpell.SelectSingleNode("div[@class='trad']")?.InnerText; var divTrad = nodeSpell.SelectSingleNode("div[@class='trad']");
var linkVO = divTrad.SelectSingleNode("a").GetAttributeValue("href", "");
var matchIdVF = new Regex(@"\?vf=(?<idvf>.*)").Match(linkVO);
this.IdVF = matchIdVF?.Groups["idvf"]?.Value;
var matchIdVO = new Regex(@"\?vo=(?<idvo>.*)").Match(linkVO);
this.IdVO = matchIdVO?.Groups["idvo"]?.Value;
var altNames = divTrad?.InnerText;
if (altNames != null) if (altNames != null)
{ {
var matchNames = new Regex(@"\[ (?<vo>.*?) \](?: \[ (?<alt>.*?) \])?").Match(altNames); var matchNames = new Regex(@"\[ (?<vo>.*?) \](?: \[ (?<alt>.*?) \])?").Match(altNames);
@ -81,10 +80,10 @@ namespace AideDeJeuLib.Spells
this.LevelType = nodeSpell.SelectSingleNode("h2/em").InnerText; this.LevelType = nodeSpell.SelectSingleNode("h2/em").InnerText;
this.Level = this.LevelType.Split(new string[] { " - " }, StringSplitOptions.None)[0].Split(' ')[1]; this.Level = this.LevelType.Split(new string[] { " - " }, StringSplitOptions.None)[0].Split(' ')[1];
this.Type = this.LevelType.Split(new string[] { " - " }, StringSplitOptions.None)[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.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.Range = nodeSpell.SelectSingleNode("div[strong/text()='Portée' or strong/text()='Range']").InnerText.Split(new string[] { ": " }, StringSplitOptions.None)[1];
this.Components = nodeSpell.SelectSingleNode("div[strong/text()='Composantes']").InnerText.Split(new string[] { " : " }, StringSplitOptions.None)[1]; this.Components = nodeSpell.SelectSingleNode("div[strong/text()='Composantes' or strong/text()='Components']")?.InnerText?.Split(new string[] { ": " }, StringSplitOptions.None)?[1];
this.Duration = nodeSpell.SelectSingleNode("div[strong/text()='Durée']").InnerText.Split(new string[] { " : " }, StringSplitOptions.None)[1]; this.Duration = nodeSpell.SelectSingleNode("div[strong/text()='Durée' or strong/text()='Duration']").InnerText.Split(new string[] { ": " }, StringSplitOptions.None)[1];
this.DescriptionDiv = nodeSpell.SelectSingleNode("div[contains(@class,'description')]"); this.DescriptionDiv = nodeSpell.SelectSingleNode("div[contains(@class,'description')]");
this.Overflow = nodeSpell.SelectSingleNode("div[@class='overflow']")?.InnerText; this.Overflow = nodeSpell.SelectSingleNode("div[@class='overflow']")?.InnerText;
this.NoOverflow = nodeSpell.SelectSingleNode("div[@class='nooverflow']")?.InnerText; this.NoOverflow = nodeSpell.SelectSingleNode("div[@class='nooverflow']")?.InnerText;