Completed Persistance + BAG NOT WORKING
continuous-integration/drone/push Build is failing Details

test_old_branch
Jules LASCRET 11 months ago
parent c446099974
commit 68021750fe

@ -1 +1 @@
<Leaderboard xmlns="http://schemas.datacontract.org/2004/07/QwirkleClassLibrary.Players" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><leaderboard><Score><Date>2024-05-31T00:00:00+02:00</Date><PlayerName>Jérémy</PlayerName><Points>0</Points><Victories>1</Victories></Score><Score><Date>2024-05-31T00:00:00+02:00</Date><PlayerName>Jules</PlayerName><Points>0</Points><Victories>0</Victories></Score></leaderboard></Leaderboard> {"leaderboard":[]}

File diff suppressed because one or more lines are too long

@ -22,6 +22,7 @@ namespace QwirkleClassLibrary.Boards
[DataMember] [DataMember]
private ObservableCollection<Cell> cells = new ObservableCollection<Cell>(); private ObservableCollection<Cell> cells = new ObservableCollection<Cell>();
public ObservableCollection<Cell> ReadCells public ObservableCollection<Cell> ReadCells
{ {
get { return cells; } get { return cells; }
@ -32,8 +33,11 @@ namespace QwirkleClassLibrary.Boards
} }
} }
public int Rows { get; } [DataMember]
public int Columns { get; } public int Rows { get; set; }
[DataMember]
public int Columns { get; set; }
/// <summary> /// <summary>
/// This is the constructor for the board. The parameters 'rows' and 'cols' are used to calculate the size of the board. /// This is the constructor for the board. The parameters 'rows' and 'cols' are used to calculate the size of the board.

@ -20,6 +20,7 @@ namespace QwirkleClassLibrary.Games
[DataContract] [DataContract]
public class Game : IPlayer, IRules public class Game : IPlayer, IRules
{ {
[DataMember]
private TileBag? bag = null; private TileBag? bag = null;
[DataMember] [DataMember]
@ -27,6 +28,7 @@ namespace QwirkleClassLibrary.Games
[DataMember] [DataMember]
private Board board = new Board(15, 12); private Board board = new Board(15, 12);
public Board Board => board; public Board Board => board;
public ReadOnlyCollection<Player> PlayerList => players.AsReadOnly(); public ReadOnlyCollection<Player> PlayerList => players.AsReadOnly();
@ -39,9 +41,11 @@ namespace QwirkleClassLibrary.Games
public ReadOnlyDictionary<Player, int> ScoreBoard => scoreBoard.AsReadOnly(); public ReadOnlyDictionary<Player, int> ScoreBoard => scoreBoard.AsReadOnly();
public ReadOnlyCollection<Cell> CellsUsed => cellUsed.AsReadOnly(); [DataMember]
private readonly List<Cell> cellUsed = new(); private readonly List<Cell> cellUsed = new();
public ReadOnlyCollection<Cell> CellsUsed => cellUsed.AsReadOnly();
public event EventHandler<AddPlayerNotifiedEventArgs>? PlayerAddNotified; public event EventHandler<AddPlayerNotifiedEventArgs>? PlayerAddNotified;
protected virtual void OnPlayerNotified(AddPlayerNotifiedEventArgs args) protected virtual void OnPlayerNotified(AddPlayerNotifiedEventArgs args)
@ -337,14 +341,9 @@ namespace QwirkleClassLibrary.Games
return false; return false;
} }
private bool TileInbag(Player player, Tile tile) private static bool TileInbag(Player player, Tile tile)
{
foreach (var t in player.Tiles)
{ {
if (ReferenceEquals(t, tile)) return true; return player.Tiles.Any(t => ReferenceEquals(t, tile));
}
return false;
} }

@ -1,4 +1,4 @@
using System.Runtime.Serialization; using System.Runtime.Serialization.Json;
using QwirkleClassLibrary.Games; using QwirkleClassLibrary.Games;
namespace QwirkleClassLibrary.Persistences; namespace QwirkleClassLibrary.Persistences;
@ -7,7 +7,7 @@ public class GamePersistenceJson : IGamePersistence
{ {
public void SaveGame(Game game) public void SaveGame(Game game)
{ {
var serializer = new DataContractSerializer(typeof(Game)); var serializer = new DataContractJsonSerializer(typeof(Game));
using (Stream writer = File.Create("Game.json")) using (Stream writer = File.Create("Game.json"))
{ {
@ -17,7 +17,7 @@ public class GamePersistenceJson : IGamePersistence
public Game LoadGame() public Game LoadGame()
{ {
var serializer = new DataContractSerializer(typeof(Game)); var serializer = new DataContractJsonSerializer(typeof(Game));
using (Stream reader = File.OpenRead("Game.json")) using (Stream reader = File.OpenRead("Game.json"))
{ {

@ -1,4 +1,4 @@
using System.Runtime.Serialization; using System.Runtime.Serialization.Json;
using QwirkleClassLibrary.Players; using QwirkleClassLibrary.Players;
namespace QwirkleClassLibrary.Persistences; namespace QwirkleClassLibrary.Persistences;
@ -7,7 +7,7 @@ public class LeaderboardPersistenceJson : ILeaderboardPersistence
{ {
public void SaveLeaderboard(Leaderboard leaderboard) public void SaveLeaderboard(Leaderboard leaderboard)
{ {
var serializer = new DataContractSerializer(typeof(Leaderboard)); var serializer = new DataContractJsonSerializer(typeof(Leaderboard));
using (Stream writer = File.Create("Leaderboard.json")) using (Stream writer = File.Create("Leaderboard.json"))
{ {
@ -17,7 +17,7 @@ public class LeaderboardPersistenceJson : ILeaderboardPersistence
public Leaderboard LoadLeaderboard() public Leaderboard LoadLeaderboard()
{ {
var serializer = new DataContractSerializer(typeof(Leaderboard)); var serializer = new DataContractJsonSerializer(typeof(Leaderboard));
using (Stream reader = File.OpenRead("Leaderboard.json")) using (Stream reader = File.OpenRead("Leaderboard.json"))
{ {

@ -16,7 +16,7 @@ namespace QwirkleClassLibrary.Players
public class Player : INotifyPropertyChanged public class Player : INotifyPropertyChanged
{ {
[DataMember] [DataMember]
public ObservableCollection<Tile> Tiles { get; private set; } public ObservableCollection<Tile> Tiles { get; set; }
public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler? PropertyChanged;

@ -2,14 +2,18 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Runtime.Serialization;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace QwirkleClassLibrary.Tiles namespace QwirkleClassLibrary.Tiles
{ {
[DataContract]
public class TileBag public class TileBag
{ {
public ReadOnlyCollection<Tile> TilesBag { get; private set; } public ReadOnlyCollection<Tile> TilesBag { get; private set; }
[DataMember]
private readonly List<Tile> tiles = new List<Tile>(); private readonly List<Tile> tiles = new List<Tile>();
/// <summary> /// <summary>

@ -395,7 +395,13 @@ static void MainGame()
game.StartGame(); game.StartGame();
MainMenu(game); MainMenu(game);
foreach (var player in game.PlayerList)
{
if (game.CheckGameOver(player))
{
leaderboard.AddScoreInLead(game.ScoreBoard); leaderboard.AddScoreInLead(game.ScoreBoard);
}
}
ILeaderboardPersistence leaderboardSave = new LeaderboardPersistenceJson(); ILeaderboardPersistence leaderboardSave = new LeaderboardPersistenceJson();
leaderboardSave.SaveLeaderboard(leaderboard); leaderboardSave.SaveLeaderboard(leaderboard);

@ -0,0 +1,42 @@
using Xunit;
using QwirkleClassLibrary.Games;
using QwirkleClassLibrary.Persistences;
using System.IO;
namespace TestBase
{
public class TestPersistence
{
[Fact]
public void Test_SaveGame()
{
var game = new Game();
IGamePersistence gamePersistence = new GamePersistenceJson();
gamePersistence.SaveGame(game);
Assert.True(File.Exists("Game.json"));
}
[Fact]
public void Test_LoadGame()
{
Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\..\\..\\Files"));
var game = new Game();
game.AddPlayerInGame(["Jules", "Jérémy"]);
game.StartGame();
IGamePersistence gamePersistence = new GamePersistenceJson();
gamePersistence.SaveGame(game);
var loadedGame = gamePersistence.LoadGame();
Assert.True(game.Board.ReadCells.All(cell =>
cell.GetX == loadedGame.Board.GetCell(cell.GetX, cell.GetY)!.GetX &&
cell.GetY == loadedGame.Board.GetCell(cell.GetX, cell.GetY)!.GetY &&
cell.IsFree == loadedGame.Board.GetCell(cell.GetX, cell.GetY)!.IsFree));
}
}
}
Loading…
Cancel
Save