💾 Persistance avec SQLite (création et bases)
continuous-integration/drone/push Build was killed Details
continuous-integration/drone/pr Build is failing Details

pull/111/head
Rémi LAVERGNE 11 months ago
parent 56c47829e8
commit 631fae3711

@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Models\Models.csproj" />
</ItemGroup>

@ -0,0 +1,69 @@
using SQLite;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Threading.Tasks;
namespace DataContractPersistence
{
using Models.Game;
using Models.Interfaces;
public class SqLitePersistence : IPersistence
{
private readonly SQLiteConnection _database;
private readonly string _databasePath;
public SqLitePersistence()
{
_databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Trek_12.db");
_database = new SQLiteConnection(_databasePath);
_database.CreateTable<Player>();
_database.CreateTable<Map>();
_database.CreateTable<Game>();
_database.CreateTable<BestScore>();
}
public (ObservableCollection<Player>, ObservableCollection<Game>, ObservableCollection<Map>, ObservableCollection<BestScore>) LoadData()
{
var players = new ObservableCollection<Player>(_database.Table<Player>());
var games = new ObservableCollection<Game>(_database.Table<Game>());
var maps = new ObservableCollection<Map>(_database.Table<Map>());
var bestScores = new ObservableCollection<BestScore>(_database.Table<BestScore>());
return (players, games, maps, bestScores);
}
public void SaveData(ObservableCollection<Player> players, ObservableCollection<Game> games, ObservableCollection<Map> maps, ObservableCollection<BestScore> bestScores)
{
_database.RunInTransaction(() =>
{
_database.DeleteAll<Player>();
_database.DeleteAll<Game>();
_database.DeleteAll<Map>();
_database.DeleteAll<BestScore>();
});
foreach (var player in players)
{
_database.Insert(player);
}
foreach (var game in games)
{
_database.Insert(game);
}
foreach (var map in maps)
{
_database.Insert(map);
}
foreach (var bestScore in bestScores)
{
_database.Insert(bestScore);
}
}
}
}

@ -5,15 +5,20 @@ using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using SQLite;
namespace Models.Game
{
/// <summary>
/// This class represents the best score of a player.
/// </summary>
[DataContract]
[DataContract, SQLite.Table("BestScores")]
public class BestScore
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
/// <summary>
/// Name of the map.
/// </summary>
@ -69,6 +74,11 @@ namespace Models.Game
Score = score;
}
/// <summary>
/// SQLite constructor
/// </summary>
public BestScore() { }
/// <summary>
/// Increment the number of games played by the user.
/// </summary>

@ -13,6 +13,7 @@ using System.Threading.Tasks.Dataflow;
using Models.Events;
using Models.Interfaces;
using Models.Rules;
using SQLite;
namespace Models.Game
{
@ -20,14 +21,19 @@ namespace Models.Game
/// The Game class represents a game session in the application.
/// It contains all the necessary properties and methods to manage a game, including the game loop, dice rolling, and use of the game rules.
/// </summary>
[DataContract]
[DataContract, SQLite.Table("Games")]
public class Game : INotifyPropertyChanged
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
/* Persistence Interface */
[Ignore]
public IPersistence PersistenceManager { get; set; }
/* List for the game and persistence */
private ObservableCollection<Player> _players;
[Ignore]
public ObservableCollection<Player> Players
{
get => _players;
@ -39,6 +45,7 @@ namespace Models.Game
}
private ObservableCollection<Game> _games;
[Ignore]
public ObservableCollection<Game> Games
{
get => _games;
@ -50,6 +57,7 @@ namespace Models.Game
}
private ObservableCollection<Map> _maps;
[Ignore]
public ObservableCollection<Map> Maps
{
get => _maps;
@ -61,6 +69,7 @@ namespace Models.Game
}
private ObservableCollection<BestScore> _bestScores;
[Ignore]
public ObservableCollection<BestScore> BestScores
{
get => _bestScores;
@ -94,17 +103,22 @@ namespace Models.Game
}
}
[Ignore]
public Dice Dice1 { get; private set;}
[Ignore]
public Dice Dice2 { get; private set; }
[DataMember]
public int Turn { get; private set; }
[Ignore]
public Operation PlayerOperation { get; set; }
[Ignore]
public Cell PlayerCell { get; set; }
[Ignore]
public Rules.Rules GameRules { get; }

@ -1,5 +1,6 @@
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using SQLite;
namespace Models.Game
{
@ -7,21 +8,22 @@ namespace Models.Game
/// <summary>
/// The Map class is the representation of the game map with the board and the operations table.
/// </summary>
[DataContract]
[DataContract, SQLite.Table("Maps")]
public class Map
{
/// <summary>
/// It is the list of cells on the map.
/// The displaying name of the map
/// </summary>
[DataMember]
public ReadOnlyObservableCollection<Cell> Boards { get; private set; }
ObservableCollection<Cell> board = new ObservableCollection<Cell>();
[DataMember, PrimaryKey]
public string Name { get; private set; }
/// <summary>
/// The displaying name of the map
/// It is the list of cells on the map.
/// </summary>
[DataMember]
public string Name { get; private set; }
[Ignore]
public ReadOnlyObservableCollection<Cell> Boards { get; private set; }
ObservableCollection<Cell> board = new ObservableCollection<Cell>();
/// <summary>
/// It is the backgrond image of the map
@ -64,6 +66,11 @@ namespace Models.Game
Zones = new List<List<Cell>>();
}
/// <summary>
/// SQLite constructor
/// </summary>
public Map() { }
/// <summary>
/// Initializes the boards of the map.
/// </summary>

@ -1,5 +1,7 @@
using System.Collections.ObjectModel;
using SQLite;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using Models.Interfaces;
@ -10,7 +12,7 @@ namespace Models.Game
/// <summary>
/// Represents a player in the game.
/// </summary>
[DataContract]
[DataContract, SQLite.Table("Players")]
public class Player : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
@ -23,7 +25,7 @@ namespace Models.Game
/// It is he pseudo of the player.
/// </summary>
private string _pseudo;
[DataMember]
[DataMember, PrimaryKey]
public string Pseudo
{
get => _pseudo;
@ -69,6 +71,11 @@ namespace Models.Game
CreationDate = DateTime.Now.ToString("dd/MM/yyyy");
}
/// <summary>
/// SQLite constructor
/// </summary>
public Player() { }
/// <summary>
/// Redefine the equal operation between player.
/// </summary>

@ -1,12 +1,12 @@
using Models.Game;
using System.Collections.ObjectModel;
using System.Collections.ObjectModel;
namespace Models.Interfaces
{
using Models.Game;
public interface IPersistence
{
(ObservableCollection<Player>, ObservableCollection<Game.Game>, ObservableCollection<Map>, ObservableCollection<BestScore>) LoadData();
(ObservableCollection<Player>, ObservableCollection<Game>, ObservableCollection<Map>, ObservableCollection<BestScore>) LoadData();
void SaveData(ObservableCollection<Player> players, ObservableCollection<Game.Game> games, ObservableCollection<Map> maps, ObservableCollection<BestScore> bestScores);
void SaveData(ObservableCollection<Player> players, ObservableCollection<Game> games, ObservableCollection<Map> maps, ObservableCollection<BestScore> bestScores);
}
}

@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls.Core" Version="8.0.3" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
</ItemGroup>
</Project>

@ -29,7 +29,7 @@ namespace Trek_12
Directory.CreateDirectory(FilePath);
}
File.Delete(Path.Combine(FilePath, FileName));
//File.Delete(Path.Combine(FilePath, FileName));
string fullPath = Path.Combine(FilePath, FileName);
if (File.Exists(fullPath))

Loading…
Cancel
Save