1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-30 06:56:10 +00:00

Ajout database et début utilisation pour les spells

This commit is contained in:
Yan Maniez 2018-05-13 01:10:03 +02:00
parent da08a4d387
commit 1f23facbb7
14 changed files with 197 additions and 10 deletions

View file

@ -66,7 +66,7 @@
<Compile Include="MainActivity.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Version.cs" />
<Compile Include="NativeAPI.cs" />
</ItemGroup>
<ItemGroup>
<AndroidAsset Include="Assets\LinLibertine_R.ttf" />

View file

@ -1,10 +1,12 @@
using AideDeJeu.Tools;
using Android.Content.PM;
using System;
using System.IO;
[assembly: Xamarin.Forms.Dependency(typeof(AideDeJeu.Droid.Version_Android))]
namespace AideDeJeu.Droid
{
public class Version_Android : IAppVersion
public class Version_Android : INativeAPI
{
public string GetVersion()
{
@ -24,5 +26,10 @@ namespace AideDeJeu.Droid
return info.VersionCode;
}
public string GetDatabasePath(string databaseName)
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), databaseName);
}
}
}

View file

@ -95,6 +95,7 @@
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="NativeAPI.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

View file

@ -0,0 +1,24 @@
using AideDeJeu.Tools;
using System.IO;
[assembly: Xamarin.Forms.Dependency(typeof(AideDeJeu.Droid.Version_Android))]
namespace AideDeJeu.Droid
{
public class Version_Android : INativeAPI
{
public string GetVersion()
{
return "";
}
public int GetBuild()
{
return 0;
}
public string GetDatabasePath(string databaseName)
{
return Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, databaseName);
}
}
}

View file

@ -4,11 +4,18 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Remove="Resources\AdJTheme.xaml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.3" />
<PackageReference Include="SkiaSharp.Views.Forms" Version="1.60.0" />
<PackageReference Include="Xamarin.Forms" Version="3.0.0.446417" />
</ItemGroup>
@ -34,7 +41,6 @@
<ItemGroup>
<Folder Include="Models\" />
<Folder Include="Services\" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,43 @@
using AideDeJeu.Tools;
using AideDeJeuLib.Monsters;
using AideDeJeuLib.Spells;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Xamarin.Forms;
namespace AideDeJeu.Services
{
public class ItemDatabaseContext : DbContext
{
private const string databaseName = "database.db";
public DbSet<Spell> Spells { get; set; }
public DbSet<Monster> Monsters { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
String databasePath = "";
switch (Device.RuntimePlatform)
{
case Device.iOS:
SQLitePCL.Batteries_V2.Init();
databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", databaseName); ;
break;
case Device.Android:
databasePath = DependencyService.Get<INativeAPI>().GetDatabasePath(databaseName);
break;
case Device.UWP:
databasePath = DependencyService.Get<INativeAPI>().GetDatabasePath(databaseName);
break;
default:
throw new NotImplementedException("Platform not supported");
}
// Specify that we will use sqlite and the path of the database here
optionsBuilder.UseSqlite($"Filename={databasePath}");
}
}
}

View file

@ -0,0 +1,46 @@
using AideDeJeuLib.Spells;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AideDeJeu.Services
{
public class ItemDatabaseHelper<T> where T : ItemDatabaseContext
{
protected T CreateContext()
{
T postDatabaseContext = (T)Activator.CreateInstance(typeof(T));
postDatabaseContext.Database.EnsureCreated();
postDatabaseContext.Database.Migrate();
return postDatabaseContext;
}
public async Task<IEnumerable<Spell>> GetSpellsAsync()
{
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)
.ToListAsync();
}
}
public async Task AddOrUpdateSpellsAsync(IEnumerable<Spell> spells)
{
using (var context = CreateContext())
{
// add posts that do not exist in the database
var newSpells = spells.Where(
spell => context.Spells.Any(dbSpell => dbSpell.Id == spell.Id) == false
);
await context.Spells.AddRangeAsync(newSpells);
await context.SaveChangesAsync();
}
}
}
}

View file

@ -4,9 +4,10 @@ using System.Text;
namespace AideDeJeu.Tools
{
public interface IAppVersion
public interface INativeAPI
{
string GetVersion();
int GetBuild();
string GetDatabasePath(string databaseName);
}
}

View file

@ -20,7 +20,7 @@ namespace AideDeJeu.ViewModels
public string Version {
get
{
return DependencyService.Get<IAppVersion>().GetVersion();
return DependencyService.Get<INativeAPI>().GetVersion();
}
}
}

View file

@ -1,4 +1,5 @@
using AideDeJeu.Tools;
using AideDeJeu.Services;
using AideDeJeu.Tools;
using AideDeJeuLib;
using AideDeJeuLib.Spells;
using System;
@ -170,7 +171,18 @@ namespace AideDeJeu.ViewModels
{
AllItems.Clear();
var 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);
var aitems = items.ToArray();
//try
//{
ItemDatabaseHelper<ItemDatabaseContext> helper = new ItemDatabaseHelper<ItemDatabaseContext>();
await helper.AddOrUpdateSpellsAsync(items);
var items2 = await helper.GetSpellsAsync();
//}
//catch(Exception ex)
//{
// Debug.WriteLine(ex);
//}
var aitems = items2.ToArray();
Array.Sort(aitems, new ItemComparer());
foreach (var item in aitems)
{

View file

@ -8,4 +8,10 @@
<PackageReference Include="HtmlAgilityPack" Version="1.8.1" />
</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>

View file

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

View file

@ -1,6 +1,9 @@
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
@ -35,8 +38,33 @@ namespace AideDeJeuLib.Monsters
public string Description { get; set; }
public string Picture { get; set; }
[IgnoreDataMember]
[NotMapped]
public List<HtmlNode> SpecialFeatures { get; set; }
//public List<string> SpecialFeaturesPersist
//{
// get
// {
// return SpecialFeatures.Select(node => node.OuterHtml).ToList();
// }
// set
// {
// List<HtmlNode> nodes = new List<HtmlNode>();
// foreach (var str in value)
// {
// HtmlDocument doc = new HtmlDocument();
// doc.LoadHtml(str);
// nodes.Add(doc.DocumentNode);
// }
// SpecialFeatures = nodes;
// }
//}
[IgnoreDataMember]
[NotMapped]
public List<HtmlNode> Actions { get; set; }
[IgnoreDataMember]
[NotMapped]
public List<HtmlNode> LegendaryActions { get; set; }

View file

@ -1,6 +1,8 @@
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
@ -26,9 +28,16 @@ namespace AideDeJeuLib.Spells
}
set
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(value);
DescriptionDiv = doc.DocumentNode;
if (value != null)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(value);
DescriptionDiv = doc.DocumentNode;
}
else
{
DescriptionDiv = null;
}
}
}
public string DescriptionText
@ -39,7 +48,9 @@ namespace AideDeJeuLib.Spells
}
}
[IgnoreDataMember]
[NotMapped]
public HtmlNode DescriptionDiv { get; set; }
public string Overflow { get; set; }
public string NoOverflow { get; set; }
public string Source { get; set; }