-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameFile.cs
54 lines (38 loc) · 1.49 KB
/
GameFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.Text.Json;
namespace Treblecross
{
public class GameFile : IDisposable
{
public void Dispose()
{
// Dispose(true);
GC.SuppressFinalize(this);
}
public void Save(GameData gameData) {
//DateTime now = DateTime.Now;
File.WriteAllText("Gamedata.json", JsonSerializer.Serialize(gameData));
Log.Info("File Saved.");
}
public (GameMode?, Player[], Board) Load() {
try {
GameMode mode;
string filePath;
do {
Log.Info("Menu", "Enter your file path: ");
filePath = Console.ReadLine();
} while (!File.Exists(filePath));
string jsonData = File.ReadAllText(filePath);
GameData gameData = JsonSerializer.Deserialize<GameData>(jsonData);
if (gameData == null) throw new Exception("Unable to read GameData (=null) object.");
mode = gameData.Game.Mode;
Player[] players = GameData.ParsePlayer(gameData);
Player playerTurn = players.Where(x => x.Id == gameData.Board.Player).FirstOrDefault();
Board board = GameData.ParseBoard(gameData, playerTurn);
return (mode, players, board);
} catch (Exception ex) {
Log.Error("Failed at loading file.", ex);
}
return (null, null, null);
}
}
}