diff --git a/source/Trek-12/DataContractPersistence/DataContractPersistence.csproj b/source/Trek-12/DataContractPersistence/DataContractPersistence.csproj index fbf8a46..f70f862 100644 --- a/source/Trek-12/DataContractPersistence/DataContractPersistence.csproj +++ b/source/Trek-12/DataContractPersistence/DataContractPersistence.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/source/Trek-12/DataContractPersistence/SqLitePersistence.cs b/source/Trek-12/DataContractPersistence/SqLitePersistence.cs new file mode 100644 index 0000000..f48c0d6 --- /dev/null +++ b/source/Trek-12/DataContractPersistence/SqLitePersistence.cs @@ -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(); + _database.CreateTable(); + _database.CreateTable(); + _database.CreateTable(); + } + + public (ObservableCollection, ObservableCollection, ObservableCollection, ObservableCollection) LoadData() + { + var players = new ObservableCollection(_database.Table()); + var games = new ObservableCollection(_database.Table()); + var maps = new ObservableCollection(_database.Table()); + var bestScores = new ObservableCollection(_database.Table()); + + return (players, games, maps, bestScores); + } + + public void SaveData(ObservableCollection players, ObservableCollection games, ObservableCollection maps, ObservableCollection bestScores) + { + _database.RunInTransaction(() => + { + _database.DeleteAll(); + _database.DeleteAll(); + _database.DeleteAll(); + _database.DeleteAll(); + }); + + 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); + } + } + } +} diff --git a/source/Trek-12/Models/Game/BestScore.cs b/source/Trek-12/Models/Game/BestScore.cs index e61a5c8..aba67ca 100644 --- a/source/Trek-12/Models/Game/BestScore.cs +++ b/source/Trek-12/Models/Game/BestScore.cs @@ -5,15 +5,20 @@ using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; +using SQLite; namespace Models.Game { /// /// This class represents the best score of a player. /// - [DataContract] + [DataContract, SQLite.Table("BestScores")] public class BestScore { + + [PrimaryKey, AutoIncrement] + public int Id { get; set; } + /// /// Name of the map. /// @@ -69,6 +74,11 @@ namespace Models.Game Score = score; } + /// + /// SQLite constructor + /// + public BestScore() { } + /// /// Increment the number of games played by the user. /// diff --git a/source/Trek-12/Models/Game/Game.cs b/source/Trek-12/Models/Game/Game.cs index 0022425..558878f 100644 --- a/source/Trek-12/Models/Game/Game.cs +++ b/source/Trek-12/Models/Game/Game.cs @@ -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. /// - [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 _players; + [Ignore] public ObservableCollection Players { get => _players; @@ -41,6 +47,7 @@ namespace Models.Game } private ObservableCollection _games; + [Ignore] public ObservableCollection Games { get => _games; @@ -52,6 +59,7 @@ namespace Models.Game } private ObservableCollection _maps; + [Ignore] public ObservableCollection Maps { get => _maps; @@ -63,6 +71,7 @@ namespace Models.Game } private ObservableCollection _bestScores; + [Ignore] public ObservableCollection 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 { playerChoice, item }); return; - } + } if (!GameRules.AsValue(playerChoice, UsedMap.RopePaths, index)) { UsedMap.RopePaths[index].Add(playerChoice); return; - } + } } } diff --git a/source/Trek-12/Models/Game/Map.cs b/source/Trek-12/Models/Game/Map.cs index 9d18df9..737e0b2 100644 --- a/source/Trek-12/Models/Game/Map.cs +++ b/source/Trek-12/Models/Game/Map.cs @@ -1,5 +1,6 @@ using System.Collections.ObjectModel; using System.Runtime.Serialization; +using SQLite; namespace Models.Game { @@ -7,21 +8,22 @@ namespace Models.Game /// /// The Map class is the representation of the game map with the board and the operations table. /// - [DataContract] + [DataContract, SQLite.Table("Maps")] public class Map { /// - /// It is the list of cells on the map. + /// The displaying name of the map /// - [DataMember] - public ReadOnlyObservableCollection Boards { get; private set; } - ObservableCollection board = new ObservableCollection(); + [DataMember, PrimaryKey] + public string Name { get; private set; } /// - /// The displaying name of the map + /// It is the list of cells on the map. /// [DataMember] - public string Name { get; private set; } + [Ignore] + public ReadOnlyObservableCollection Boards { get; private set; } + ObservableCollection board = new ObservableCollection(); /// /// It is the backgrond image of the map @@ -63,6 +65,11 @@ namespace Models.Game RopePaths = new List>(); Zones = new List>(); } + + /// + /// SQLite constructor + /// + public Map() { } /// /// Clone the map to avoid reference problems. diff --git a/source/Trek-12/Models/Game/Player.cs b/source/Trek-12/Models/Game/Player.cs index 0acad39..cdf06e8 100644 --- a/source/Trek-12/Models/Game/Player.cs +++ b/source/Trek-12/Models/Game/Player.cs @@ -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 /// /// Represents a player in the game. /// - [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. /// private string _pseudo; - [DataMember] + [DataMember, PrimaryKey] public string Pseudo { get => _pseudo; @@ -66,6 +68,11 @@ namespace Models.Game CreationDate = DateTime.Now.ToString("dd/MM/yyyy"); } + /// + /// SQLite constructor + /// + public Player() { } + /// /// Redefine the equal operation between player. /// diff --git a/source/Trek-12/Models/Interfaces/IPersistence.cs b/source/Trek-12/Models/Interfaces/IPersistence.cs index fa0a645..3a5c55b 100644 --- a/source/Trek-12/Models/Interfaces/IPersistence.cs +++ b/source/Trek-12/Models/Interfaces/IPersistence.cs @@ -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, ObservableCollection, ObservableCollection, ObservableCollection) LoadData(); + (ObservableCollection, ObservableCollection, ObservableCollection, ObservableCollection) LoadData(); - void SaveData(ObservableCollection players, ObservableCollection games, ObservableCollection maps, ObservableCollection bestScores); + void SaveData(ObservableCollection players, ObservableCollection games, ObservableCollection maps, ObservableCollection bestScores); } } \ No newline at end of file diff --git a/source/Trek-12/Models/Models.csproj b/source/Trek-12/Models/Models.csproj index 0215eb2..2506c52 100644 --- a/source/Trek-12/Models/Models.csproj +++ b/source/Trek-12/Models/Models.csproj @@ -14,6 +14,7 @@ +