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

File diff suppressed because one or more lines are too long

@ -122,7 +122,7 @@ namespace QwirkleClassLibrary.Boards
return null;
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
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));
}

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

Loading…
Cancel
Save