Merge SQLite into dev (#111)
continuous-integration/drone/push Build is passing Details

pull/112/head
Rémi LAVERGNE 11 months ago
commit b98b2172ce

@ -6,6 +6,10 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Models\Models.csproj" /> <ProjectReference Include="..\Models\Models.csproj" />
</ItemGroup> </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.Runtime.Serialization;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using SQLite;
namespace Models.Game namespace Models.Game
{ {
/// <summary> /// <summary>
/// This class represents the best score of a player. /// This class represents the best score of a player.
/// </summary> /// </summary>
[DataContract] [DataContract, SQLite.Table("BestScores")]
public class BestScore public class BestScore
{ {
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
/// <summary> /// <summary>
/// Name of the map. /// Name of the map.
/// </summary> /// </summary>
@ -69,6 +74,11 @@ namespace Models.Game
Score = score; Score = score;
} }
/// <summary>
/// SQLite constructor
/// </summary>
public BestScore() { }
/// <summary> /// <summary>
/// Increment the number of games played by the user. /// Increment the number of games played by the user.
/// </summary> /// </summary>

@ -13,6 +13,7 @@ using System.Threading.Tasks.Dataflow;
using Models.Events; using Models.Events;
using Models.Interfaces; using Models.Interfaces;
using Models.Rules; using Models.Rules;
using SQLite;
namespace Models.Game namespace Models.Game
{ {
@ -20,16 +21,21 @@ namespace Models.Game
/// The Game class represents a game session in the application. /// 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. /// 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> /// </summary>
[DataContract] [DataContract, SQLite.Table("Games")]
public class Game : INotifyPropertyChanged public class Game : INotifyPropertyChanged
{ {
public bool IsPreviousGameNotFinished { get; private set; } public bool IsPreviousGameNotFinished { get; private set; }
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
/* Persistence Interface */ /* Persistence Interface */
[Ignore]
public IPersistence PersistenceManager { get; set; } public IPersistence PersistenceManager { get; set; }
/* List for the game and persistence */ /* List for the game and persistence */
private ObservableCollection<Player> _players; private ObservableCollection<Player> _players;
[Ignore]
public ObservableCollection<Player> Players public ObservableCollection<Player> Players
{ {
get => _players; get => _players;
@ -41,6 +47,7 @@ namespace Models.Game
} }
private ObservableCollection<Game> _games; private ObservableCollection<Game> _games;
[Ignore]
public ObservableCollection<Game> Games public ObservableCollection<Game> Games
{ {
get => _games; get => _games;
@ -52,6 +59,7 @@ namespace Models.Game
} }
private ObservableCollection<Map> _maps; private ObservableCollection<Map> _maps;
[Ignore]
public ObservableCollection<Map> Maps public ObservableCollection<Map> Maps
{ {
get => _maps; get => _maps;
@ -63,6 +71,7 @@ namespace Models.Game
} }
private ObservableCollection<BestScore> _bestScores; private ObservableCollection<BestScore> _bestScores;
[Ignore]
public ObservableCollection<BestScore> BestScores public ObservableCollection<BestScore> BestScores
{ {
get => _bestScores; get => _bestScores;
@ -96,16 +105,20 @@ namespace Models.Game
} }
} }
[Ignore]
public Dice Dice1 { get; private set;} public Dice Dice1 { get; private set;}
[Ignore]
public Dice Dice2 { get; private set; } public Dice Dice2 { get; private set; }
[DataMember] [DataMember]
public int Turn { get; set; } public int Turn { get; set; }
[Ignore]
public Operation PlayerOperation { get; set; } public Operation PlayerOperation { get; set; }
private Cell _playerCell; private Cell _playerCell;
[Ignore]
public Cell PlayerCell { public Cell PlayerCell {
get => _playerCell; get => _playerCell;
set set
@ -128,6 +141,7 @@ namespace Models.Game
public bool DiceRolledFlag { get; private set; } public bool DiceRolledFlag { get; private set; }
public bool OperationChosenFlag { get; private set; } public bool OperationChosenFlag { get; private set; }
[Ignore]
public Rules.Rules GameRules { get; } public Rules.Rules GameRules { get; }
@ -424,22 +438,22 @@ namespace Models.Game
where cell.Value != null && cell.Valid == true && cell != playerChoice where cell.Value != null && cell.Valid == true && cell != playerChoice
select cell; select cell;
foreach (var item in ValidCell) foreach (var item in ValidCell)
{ {
if (!GameRules.IsCellAdjacent(playerChoice, item)) if (!GameRules.IsCellAdjacent(playerChoice, item))
continue; continue;
if (!((playerChoice.Value - item.Value) == 1 || (playerChoice.Value - item.Value) == -1)) if (!((playerChoice.Value - item.Value) == 1 || (playerChoice.Value - item.Value) == -1))
continue; continue;
if (!GameRules.IsInRopePaths(item,UsedMap.RopePaths,index)) if (!GameRules.IsInRopePaths(item,UsedMap.RopePaths,index))
{ {
UsedMap.RopePaths.Add(new List<Cell> { playerChoice, item }); UsedMap.RopePaths.Add(new List<Cell> { playerChoice, item });
return; return;
} }
if (!GameRules.AsValue(playerChoice, UsedMap.RopePaths, index)) if (!GameRules.AsValue(playerChoice, UsedMap.RopePaths, index))
{ {
UsedMap.RopePaths[index].Add(playerChoice); UsedMap.RopePaths[index].Add(playerChoice);
return; return;
} }
} }
} }

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

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

@ -1,12 +1,12 @@
using Models.Game; using System.Collections.ObjectModel;
using System.Collections.ObjectModel;
namespace Models.Interfaces namespace Models.Interfaces
{ {
using Models.Game;
public interface IPersistence 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> <ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls.Core" Version="8.0.3" /> <PackageReference Include="Microsoft.Maui.Controls.Core" Version="8.0.3" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
</ItemGroup> </ItemGroup>
</Project> </Project>

Loading…
Cancel
Save