-
Notifications
You must be signed in to change notification settings - Fork 1
/
GlobalProperties.cs
187 lines (180 loc) · 5.69 KB
/
GlobalProperties.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#nullable enable
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StackExchange.Redis;
using Team123it.Arcaea.MarveCube.LinkPlay.Core;
using static Team123it.Arcaea.MarveCube.LinkPlay.GlobalProperties;
namespace Team123it.Arcaea.MarveCube.LinkPlay
{
public static class RoomManager
{
private static Dictionary<string, Room> _rooms = new();
public static void RegisterRoom(Room room, string roomId) { _rooms.Add(roomId, room); }
public static void UnRegisterRoom(string roomId) { _rooms.Remove(roomId); }
public static Room? FetchRoomById(string roomId)
{
if (_rooms.TryGetValue(roomId, out var room)) { return room; }
else { return null; }
}
public static string FetchRoomIdByToken(byte[] data)
{
var mDatabaseConnectUrl = $"{RedisServerUrl}:{RedisServerPort},password={RedisServerPassword}";
var conn = ConnectionMultiplexer.Connect(mDatabaseConnectUrl);
var db = conn.GetDatabase();
var roomData = JObject.Parse(db.StringGet($"Arcaea-LinkPlayToken-{BitConverter.ToUInt64(data)}"));
var roomId = roomData.Value<string>("roomId");
conn.Close();
return roomId!;
}
}
/// <summary>
/// 提供适用于 <see cref="LinkPlay"/> 的全局属性的类。无法继承此类。
/// </summary>
public static class GlobalProperties
{
/// <summary>
/// 获取当前服务器是否在维护中的标志。
/// <para>注:为防止出现数据丢失或损坏或发生意外情况,获取过程中发生任何异常都将视为正在维护中。</para>
/// </summary>
public static bool Maintaining
{
get
{
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "data", "config.json")))
{
try
{
var settings = JObject.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "data", "config.json"), Encoding.UTF8));
if (settings.Value<JObject>("settings")!.Value<bool>("isMaintaining")) { return true; }
else { return false; }
}
catch { return true; }
}
else { return true; }
}
}
public static string MultiplayerServerUrl
{
get
{
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "data", "config.json")))
{
try
{
var settings = JObject.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "data", "config.json"), Encoding.UTF8));
var config = settings.Value<JObject>("config");
string prefix = config!.Value<string>("multiplayerServerUrl")!;
return prefix;
}
catch
{
return null;
}
}
else
{
throw new FileNotFoundException($"找不到配置文件(config.json): {Path.Combine(AppContext.BaseDirectory, "data", "config.json")}");
}
}
}
public static int MultiplayerServerPort
{
get
{
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "data", "config.json")))
{
try
{
var settings = JObject.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "data", "config.json"), Encoding.UTF8));
var config = settings.Value<JObject>("config");
int key = config!.Value<int>("multiplayerServerPort");
return key;
}
catch
{
return 0;
}
}
else
{
throw new FileNotFoundException($"找不到配置文件(config.json): {Path.Combine(AppContext.BaseDirectory, "data", "config.json")}");
}
}
}
/// <summary>
/// 获取API的监听端口。
/// </summary>
/// <exception cref="FileNotFoundException" />
/// <exception cref="JsonException" />
public static string RedisServerUrl
{
get
{
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "data", "config.json")))
{
try
{
var settings = JObject.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "data", "config.json"), Encoding.UTF8));
var config = settings.Value<JObject>("config");
return config!.Value<string>("redisServerUrl")!;
}
catch (Exception ex)
{
throw new JsonException($"配置文件 {Path.Combine(AppContext.BaseDirectory, "data", "config.json")} 读取失败: {ex.Message}");
}
}
else
{
throw new FileNotFoundException($"找不到配置文件(config.json): {Path.Combine(AppContext.BaseDirectory, "data", "config.json")}");
}
}
}
public static int RedisServerPort
{
get
{
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "data", "config.json")))
{
try
{
var settings = JObject.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "data", "config.json"), Encoding.UTF8));
var config = settings.Value<JObject>("config");
return config!.Value<int>("redisServerPort");
}
catch (Exception ex)
{
throw new JsonException($"配置文件 {Path.Combine(AppContext.BaseDirectory, "data", "config.json")} 读取失败: {ex.Message}");
}
}
else
{
throw new FileNotFoundException($"找不到配置文件(config.json): {Path.Combine(AppContext.BaseDirectory, "data", "config.json")}");
}
}
}
public static string RedisServerPassword
{
get
{
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "data", "config.json")))
{
try
{
var settings = JObject.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "data", "config.json"), Encoding.UTF8));
var config = settings.Value<JObject>("config");
return config!.Value<string>("redisServerPassword")!;
}
catch (Exception ex)
{
throw new JsonException($"配置文件 {Path.Combine(AppContext.BaseDirectory, "data", "config.json")} 读取失败: {ex.Message}");
}
}
else
{
throw new FileNotFoundException($"找不到配置文件(config.json): {Path.Combine(AppContext.BaseDirectory, "data", "config.json")}");
}
}
}
}
}