-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession.cs
101 lines (88 loc) · 3.63 KB
/
Session.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
namespace CodeGame.Client;
using System.Text.Json;
using Directories.Net;
/// <summary>
/// Represents a CodeGame session.
/// </summary>
public class Session
{
private static readonly string gamesPath = Path.Combine(new BaseDirectories().DataDir, "codegame", "games");
/// <summary>
/// The URL of the game server.
/// </summary>
public string GameURL { get; internal set; }
/// <summary>
/// The username of the player.
/// </summary>
public string Username { get; internal set; }
/// <summary>
/// The ID of the game.
/// </summary>
public string GameId { get; internal set; }
/// <summary>
/// The ID of the player.
/// </summary>
public string PlayerId { get; internal set; }
/// <summary>
/// The player secret.
/// </summary>
public string PlayerSecret { get; internal set; }
/// <summary>
/// Creates a new session.
/// </summary>
public Session(string gameURL, string username, string gameId, string playerId, string playerSecret)
{
this.GameURL = gameURL;
this.Username = username;
this.GameId = gameId;
this.PlayerId = playerId;
this.PlayerSecret = playerSecret;
}
/// <summary>
/// Loads a session from disk.
/// </summary>
/// <param name="gameURL">The URL of the game.</param>
/// <param name="username">The username of the player.</param>
/// <returns>The loaded session.</returns>
/// <exception cref="JsonException">Thrown when the session file is invalid.</exception>
/// <exception cref="IOException">Thrown when the session file cannot be read.</exception>
public static Session Load(string gameURL, string username)
{
using var file = File.Open(Path.Combine(gamesPath, Uri.EscapeDataString(gameURL), username + ".json"), FileMode.Open);
var data = JsonSerializer.Deserialize<Dictionary<string, string>>(file);
if (data == null || !data.ContainsKey("game_id") || !data.ContainsKey("player_id") || !data.ContainsKey("player_secret"))
{
throw new JsonException("Invalid session file.");
}
return new Session(gameURL, username, data["game_id"], data["player_id"], data["player_secret"]);
}
/// <summary>
/// Writes the session to disk.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when one or more fields of the session are empty.</exception>
/// <exception cref="IOException">Thrown when the session file cannot be written.</exception>
public void Save()
{
if (GameURL == "" || Username == "" || GameId == "" || PlayerId == "" || PlayerSecret == "")
throw new InvalidOperationException("The session is not complete.");
var dir = Path.Combine(gamesPath, Uri.EscapeDataString(this.GameURL));
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
var data = new Dictionary<string, string>(3);
data.Add("game_id", GameId);
data.Add("player_id", PlayerId);
data.Add("player_secret", PlayerSecret);
using var file = File.Create(Path.Combine(dir, this.Username + ".json"));
JsonSerializer.Serialize<Dictionary<string, string>>(file, data);
}
/// <summary>
/// Deletes the session file.
/// </summary>
/// <exception cref="IOException">Thrown when the session file cannot be deleted.</exception>
public void Remove()
{
if (GameURL == "") return;
var dir = Path.Combine(gamesPath, Uri.EscapeDataString(GameURL));
File.Delete(Path.Combine(dir, Username + ".json"));
if (Directory.GetFiles(dir).Length == 0) Directory.Delete(dir);
}
}