enhanced persistence with interfaces + fixed some code smells
continuous-integration/drone/push Build is passing Details

test_old_branch
Jules LASCRET 11 months ago
parent 8ab9642c9c
commit 88f7dc08c3

@ -1,16 +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": [
{
"Date": "\/Date(1717106400000+0200)\/",
"PlayerName": "Jérémy",
"Points": 0,
"Victories": 1
},
{
"Date": "\/Date(1717106400000+0200)\/",
"PlayerName": "Jules",
"Points": 0,
"Victories": 0
}
]
}

File diff suppressed because one or more lines are too long

@ -122,7 +122,7 @@ namespace QwirkleClassLibrary.Boards
return null; return null;
} }
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{ {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} }

@ -85,7 +85,7 @@ public class Cell : INotifyPropertyChanged
} }
} }
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{ {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} }

@ -1,7 +1,12 @@
namespace QwirkleClassLibrary.Events namespace QwirkleClassLibrary.Events
{ {
public class SwapTilesNotifiedEventArgs(string reason) public class SwapTilesNotifiedEventArgs
{ {
public string Reason { get; private set; } = reason; public string Reason { get; private set; }
public SwapTilesNotifiedEventArgs(string reason)
{
Reason = reason;
}
} }
} }

@ -337,16 +337,14 @@ namespace QwirkleClassLibrary.Games
return false; return false;
} }
public bool TileInbag(Player player, Tile tile) private bool TileInbag(Player player, Tile tile)
{ {
for (int i = 0; i < player.Tiles.Count; i++) foreach (var t in player.Tiles)
{ {
Tile? t = player.Tiles[i]; if (ReferenceEquals(t, tile)) return true;
if (Object.ReferenceEquals(t, tile)) return true;
} }
return false;
return false;
} }

@ -1,6 +0,0 @@
namespace QwirkleClassLibrary.Persistences;
public class GamePersistence
{
}

@ -0,0 +1,27 @@
using System.Runtime.Serialization;
using QwirkleClassLibrary.Games;
namespace QwirkleClassLibrary.Persistences;
public class GamePersistenceJson : IGamePersistence
{
public void SaveGame(Game game)
{
var serializer = new DataContractSerializer(typeof(Game));
using (Stream writer = File.Create("Game.json"))
{
serializer.WriteObject(writer, game);
}
}
public Game LoadGame()
{
var serializer = new DataContractSerializer(typeof(Game));
using (Stream reader = File.OpenRead("Game.json"))
{
return serializer.ReadObject(reader) as Game ?? throw new InvalidOperationException();
}
}
}

@ -0,0 +1,10 @@
using QwirkleClassLibrary.Games;
namespace QwirkleClassLibrary.Persistences;
public interface IGamePersistence
{
void SaveGame(Game game);
Game LoadGame();
}

@ -0,0 +1,10 @@
using QwirkleClassLibrary.Players;
namespace QwirkleClassLibrary.Persistences;
public interface ILeaderboardPersistence
{
void SaveLeaderboard(Leaderboard leaderboard);
Leaderboard LoadLeaderboard();
}

@ -1,27 +0,0 @@
using System.Runtime.Serialization;
using QwirkleClassLibrary.Players;
namespace QwirkleClassLibrary.Persistences;
public class LeaderboardPersistence
{
public void SaveLeaderboard(Leaderboard leaderboard)
{
var leaderboardSerializer = new DataContractSerializer(typeof(Leaderboard));
using (Stream s = File.Create("LeaderBoard.json "))
{
leaderboardSerializer.WriteObject(s, leaderboard);
}
}
public Leaderboard LoadLeaderboard()
{
var leaderboardSerializer = new DataContractSerializer(typeof(Leaderboard));
using (Stream s = File.OpenRead("LeaderBoard.json"))
{
return leaderboardSerializer.ReadObject(s) as Leaderboard ?? throw new NullReferenceException();
}
}
}

@ -0,0 +1,27 @@
using System.Runtime.Serialization;
using QwirkleClassLibrary.Players;
namespace QwirkleClassLibrary.Persistences;
public class LeaderboardPersistenceJson : ILeaderboardPersistence
{
public void SaveLeaderboard(Leaderboard leaderboard)
{
var serializer = new DataContractSerializer(typeof(Leaderboard));
using (Stream writer = File.Create("Leaderboard.json"))
{
serializer.WriteObject(writer, leaderboard);
}
}
public Leaderboard LoadLeaderboard()
{
var serializer = new DataContractSerializer(typeof(Leaderboard));
using (Stream reader = File.OpenRead("Leaderboard.json"))
{
return serializer.ReadObject(reader) as Leaderboard ?? throw new InvalidOperationException();
}
}
}

@ -218,19 +218,13 @@ static void MenuSwitch(Game game)
game.DrawTiles(game.GetPlayingPlayer()); game.DrawTiles(game.GetPlayingPlayer());
game.CheckGameOver(game.GetPlayingPlayer()); game.CheckGameOver(game.GetPlayingPlayer());
var inGameSerializer = new DataContractJsonSerializer(typeof(Game)); IGamePersistence gameSave = new GamePersistenceJson();
using (Stream s = File.Create("game.json")) gameSave.SaveGame(game);
{
inGameSerializer.WriteObject(s, game);
}
return; return;
case 4: case 4:
var postGameSerializer = new DataContractJsonSerializer(typeof(Game)); IGamePersistence endGameSave = new GamePersistenceJson();
using (Stream s = File.Create("game.json")) endGameSave.SaveGame(game);
{
postGameSerializer.WriteObject(s, game);
}
game.GameRunning = false; game.GameRunning = false;
break; break;
@ -403,25 +397,19 @@ static void MainGame()
leaderboard.AddScoreInLead(game.ScoreBoard); leaderboard.AddScoreInLead(game.ScoreBoard);
LeaderboardPersistence lp = new LeaderboardPersistence(); ILeaderboardPersistence leaderboardSave = new LeaderboardPersistenceJson();
lp.SaveLeaderboard(leaderboard); leaderboardSave.SaveLeaderboard(leaderboard);
break; break;
case 2: case 2:
Game loadedGame; Game loadedGame;
var serializer = new DataContractJsonSerializer(typeof(Game)); IGamePersistence gameLoad = new GamePersistenceJson();
using (Stream s = File.OpenRead("game.json")) loadedGame = gameLoad.LoadGame();
{
loadedGame = (serializer.ReadObject(s) as Game)!;
}
var loadedLeaderboardSerializer = new DataContractJsonSerializer(typeof(Leaderboard)); ILeaderboardPersistence leaderboardLoad = new LeaderboardPersistenceJson();
using (Stream s = File.OpenRead("leaderboard.json")) leaderboard = leaderboardLoad.LoadLeaderboard();
{
leaderboard = (loadedLeaderboardSerializer.ReadObject(s) as Leaderboard)!;
}
MainMenuContinue(loadedGame); MainMenuContinue(loadedGame);
leaderboard.AddScoreInLead(loadedGame.ScoreBoard); leaderboard.AddScoreInLead(loadedGame.ScoreBoard);

Loading…
Cancel
Save