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 4d0f6a7..64be955 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,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. /// - [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 _players; + [Ignore] public ObservableCollection Players { get => _players; @@ -39,6 +45,7 @@ namespace Models.Game } private ObservableCollection _games; + [Ignore] public ObservableCollection Games { get => _games; @@ -50,6 +57,7 @@ namespace Models.Game } private ObservableCollection _maps; + [Ignore] public ObservableCollection Maps { get => _maps; @@ -61,6 +69,7 @@ namespace Models.Game } private ObservableCollection _bestScores; + [Ignore] public ObservableCollection 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; } diff --git a/source/Trek-12/Models/Game/Map.cs b/source/Trek-12/Models/Game/Map.cs index 324d5fd..fbd229d 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 @@ -64,6 +66,11 @@ namespace Models.Game Zones = new List>(); } + /// + /// SQLite constructor + /// + public Map() { } + /// /// Initializes the boards of the map. /// diff --git a/source/Trek-12/Models/Game/Player.cs b/source/Trek-12/Models/Game/Player.cs index 1cbfcc2..934e82f 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; @@ -69,6 +71,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 @@ + diff --git a/source/Trek-12/Trek-12/App.xaml.cs b/source/Trek-12/Trek-12/App.xaml.cs index f9a7703..e4167f7 100644 --- a/source/Trek-12/Trek-12/App.xaml.cs +++ b/source/Trek-12/Trek-12/App.xaml.cs @@ -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))