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>
</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,16 +21,21 @@ 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
{
public bool IsPreviousGameNotFinished { get; private set; }
[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;
@ -41,6 +47,7 @@ namespace Models.Game
}
private ObservableCollection<Game> _games;
[Ignore]
public ObservableCollection<Game> Games
{
get => _games;
@ -52,6 +59,7 @@ namespace Models.Game
}
private ObservableCollection<Map> _maps;
[Ignore]
public ObservableCollection<Map> Maps
{
get => _maps;
@ -63,6 +71,7 @@ namespace Models.Game
}
private ObservableCollection<BestScore> _bestScores;
[Ignore]
public ObservableCollection<BestScore> BestScores
{
get => _bestScores;
@ -96,16 +105,20 @@ namespace Models.Game
}
}
[Ignore]
public Dice Dice1 { get; private set;}
[Ignore]
public Dice Dice2 { get; private set; }
[DataMember]
public int Turn { get; set; }
[Ignore]
public Operation PlayerOperation { get; set; }
private Cell _playerCell;
[Ignore]
public Cell PlayerCell {
get => _playerCell;
set
@ -128,6 +141,7 @@ namespace Models.Game
public bool DiceRolledFlag { get; private set; }
public bool OperationChosenFlag { get; private set; }
[Ignore]
public Rules.Rules GameRules { get; }
@ -424,22 +438,22 @@ namespace Models.Game
where cell.Value != null && cell.Valid == true && cell != playerChoice
select cell;
foreach (var item in ValidCell)
{
foreach (var item in ValidCell)
{
if (!GameRules.IsCellAdjacent(playerChoice, item))
continue;
if (!((playerChoice.Value - item.Value) == 1 || (playerChoice.Value - item.Value) == -1))
continue;
continue;
if (!((playerChoice.Value - item.Value) == 1 || (playerChoice.Value - item.Value) == -1))
continue;
if (!GameRules.IsInRopePaths(item,UsedMap.RopePaths,index))
{
UsedMap.RopePaths.Add(new List<Cell> { playerChoice, item });
return;
}
}
if (!GameRules.AsValue(playerChoice, UsedMap.RopePaths, index))
{
UsedMap.RopePaths[index].Add(playerChoice);
return;
}
}
}
}

@ -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
@ -63,6 +65,11 @@ namespace Models.Game
RopePaths = new List<List<Cell>>();
Zones = new List<List<Cell>>();
}
/// <summary>
/// SQLite constructor
/// </summary>
public Map() { }
/// <summary>
/// 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.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>

Loading…
Cancel
Save