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]
private ObservableCollection<Cell> cells = new ObservableCollection<Cell>();
public ObservableCollection<Cell> ReadCells
{
get { return cells; }
@ -32,8 +33,11 @@ namespace QwirkleClassLibrary.Boards
}
}
public int Rows { get; }
public int Columns { get; }
[DataMember]
public int Rows { get; set; }
[DataMember]
public int Columns { get; set; }
/// <summary>
/// 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]
public class Game : IPlayer, IRules
{
[DataMember]
private TileBag? bag = null;
[DataMember]
@ -27,6 +28,7 @@ namespace QwirkleClassLibrary.Games
[DataMember]
private Board board = new Board(15, 12);
public Board Board => board;
public ReadOnlyCollection<Player> PlayerList => players.AsReadOnly();
@ -39,8 +41,10 @@ namespace QwirkleClassLibrary.Games
public ReadOnlyDictionary<Player, int> ScoreBoard => scoreBoard.AsReadOnly();
public ReadOnlyCollection<Cell> CellsUsed => cellUsed.AsReadOnly();
[DataMember]
private readonly List<Cell> cellUsed = new();
public ReadOnlyCollection<Cell> CellsUsed => cellUsed.AsReadOnly();
public event EventHandler<AddPlayerNotifiedEventArgs>? PlayerAddNotified;
@ -337,14 +341,9 @@ namespace QwirkleClassLibrary.Games
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 false;
return player.Tiles.Any(t => ReferenceEquals(t, tile));
}

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

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

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

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

@ -394,8 +394,14 @@ static void MainGame()
game.StartGame();
MainMenu(game);
leaderboard.AddScoreInLead(game.ScoreBoard);
foreach (var player in game.PlayerList)
{
if (game.CheckGameOver(player))
{
leaderboard.AddScoreInLead(game.ScoreBoard);
}
}
ILeaderboardPersistence leaderboardSave = new LeaderboardPersistenceJson();
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