-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConnectionMessage.cs
377 lines (356 loc) · 13.6 KB
/
ConnectionMessage.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System.Text.RegularExpressions;
using System;
using Newtonsoft.Json;
namespace Phi_MGUS
{
public static class ConnectionMessage
{
/// <summary>
/// client and server message | 客户端与服务器消息
/// </summary>
public class Message
{
public string action = "";
//public string token = "";
/// <summary>
/// Deserialize message | 反序列化消息
/// </summary>
/// <returns>Json字符串</returns>
public string Serialize()
{
return JsonConvert.SerializeObject(this);
}
}
/// <summary>
/// Server to client message | 服务器到客户端消息
/// </summary>
public static class Server
{
/// <summary>
/// Get client metadata message | 获取客户端元数据消息
/// </summary>
public class GetData : Message
{
public new readonly string action = "getData";
public bool needPassword = false;
}
/// <summary>
/// Join server failed message | 加入服务器失败消息
/// </summary>
public class JoinServerFailed : Message
{
public new readonly string action = "joinServerFailed";
public ReasonType reason = ReasonType.Unknown;
public enum ReasonType
{
AuthFailedByPwdIncorrect,// Will be kicked out | 会被踢出服务器
AuthFailedByPwdNull, // Will be kicked out | 会被踢出服务器
JoinFailedByIllegalClient,
JoinFailedByInvalidParameter,
Unknown
}
}
/// <summary>
/// Join server success message | 加入服务器成功消息
/// </summary>
public class JoinServerSuccess : Message
{
public new readonly string action = "joinServerSuccess";
}
/// <summary>
/// Room new failed message | 房间新建失败消息
/// </summary>
public class NewRoomFailed : Message
{
public new readonly string action = "newRoomFailed";
public ReasonType reason = ReasonType.Unknown;
public enum ReasonType
{
RoomAlreadyExists,
RoomIdentifierInvalid,
AlreadyInRoom,
Unknown
}
}
/// <summary>
/// Room new success message | 房间新建成功消息
/// </summary>
public class NewRoomSuccess : Message
{
public new readonly string action = "newRoomSuccess";
}
/// <summary>
/// Room join failed message | 房间加入成功消息
/// </summary>
public class JoinRoomFailed : Message
{
public new readonly string action = "joinRoomFailed";
public ReasonType reason = ReasonType.Unknown;
public enum ReasonType
{
RoomNotFound,
RoomIsFull,
AlreadyInRoom,
Unknown
}
}
/// <summary>
/// Room join success message | 房间加入成功消息
/// </summary>
public class JoinRoomSuccess : Message
{
public new readonly string action = "joinRoomSuccess";
public Data data = new Data();
public class Data
{
public string roomID = "";
public string roomOwner = "";
public string chartMD5 = "";// If no chart is selected, it is empty | 如果没有选择谱面,则为空
public string chartUrl = "";// If no chart is selected, it is empty | 如果没有选择谱面,则为空
public List<string> userList = new List<string>();
}
}
/// <summary>
/// Leave room failed message | 离开房间失败消息
/// </summary>
public class LeaveRoomFailed : Message
{
public new readonly string action = "leaveRoomFailed";
public ReasonType reason = ReasonType.Unknown;
public enum ReasonType
{
NotInRoom,
Unknown
}
}
/// <summary>
/// Leave room success message | 离开房间成功消息
/// </summary>
public class LeaveRoomSuccess : Message
{
public new readonly string action = "leaveRoomSuccess";
}
/// <summary>
/// User join room message | 新用户加入房间消息
/// </summary>
public class UserJoinRoom : Message
{
public readonly string action = "newUserJoinRoom";
public Data data = new Data();
public class Data
{
public string userName = null;
public bool isSpectator = false;
public string avatarUrl = "";
}
public UserJoinRoom(string userName, bool isSpectator, string avatarUrl)
{
data.userName = userName;
data.isSpectator = isSpectator;
data.avatarUrl = avatarUrl;
}
}
/// <summary>
/// User leave room message | 用户离开房间消息
/// </summary>
public class UserLeaveRoom : Message
{
public new readonly string action = "userLeaveRoom";
public Data data = new Data();
public class Data
{
public string userName = "";
public string roomID = "";
}
public UserLeaveRoom(string userName, string roomID)
{
data.userName = userName;
data.roomID = roomID;
}
}
/// <summary>
/// User selected chart message | 用户选择谱面消息
/// </summary>
public class UserSelectedChart : Message
{
public new readonly string action = "userSelectedChart";
public Data data = new Data();
public class Data
{
public string chartMD5 = "";
public string chartUrl = "";
}
public UserSelectedChart(string chartMD5, string chartUrl)
{
data.chartMD5 = chartMD5;
data.chartUrl = chartUrl;
}
}
/// <summary>
/// Select chart failed message | 选择谱面失败消息
/// </summary>
public class SelectChartFailed : Message
{
public new readonly string action = "selectChartFailed";
public ReasonType reason = ReasonType.Unknown;
public enum ReasonType
{
InsufficientPermissions,
NotInRoom,
Unknown
}
}
/// <summary>
/// Game start message | 游戏开始消息
/// </summary>
public class GameStart : Message
{
public new readonly string action = "gameStart";
public Data data = new Data();
public class Data
{
public string chartMD5 = "";
public string chartUrl = "";
private DateTime _startDate;
public double startDate
{
get => (_startDate - new DateTime(1970, 1, 1)).TotalMilliseconds;
set => _startDate = new DateTime(1970, 1, 1).AddMilliseconds(value);
}
}
public GameStart(string chartMD5, string chartUrl, DateTime startDate)
{
data.chartMD5 = chartMD5;
data.chartUrl = chartUrl;
data.startDate = (startDate - new DateTime(1970, 1, 1)).TotalMilliseconds;
}
}
}
/// <summary>
/// Client to server message | 客户端到服务器消息
/// </summary>
public static class Client
{
/// <summary>
/// Client meta data | 客户端元数据
/// </summary>
public class ClientMetaData : Message
{
public new readonly string action = "clientMetaData";
public Data data = new Data();
public class Data
{
public FeatureSupport features = new FeatureSupport();
public string clientName = "anonymous";
public int clientVersion = -1;
public string userName = null; // If it is an anonymous user, this value is null
public string password = null; // if server is private,this value is not null
public bool isDebugger = false;
public bool isSpectator = false;
}
}
/// <summary>
/// Feature support | 功能支持
/// </summary>
public class FeatureSupport
{
public bool RealTimeUpload = false;
public bool VotingSelection = false;
public bool RealTimeLeaderboard = false;
public bool RealTimeChat = false;
}
/// <summary>
/// New room | 新建房间
/// </summary>
public class NewRoom : Message
{
public readonly string action = "newRoom";
public Data data = new Data
{
//RoomID is a random string, length is 16 | 房间ID是随机字符串,长度为16
roomID = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16)
};
public class Data
{
public int maxUser = 8;
private string _roomID;
public string roomID
{
set
{
if (value.Length > 32)
{
throw new ArgumentException("RoomIdentifier cannot exceed 32 digits.");// 不能超过32位
}
if (!Regex.IsMatch(value, @"^[a-zA-Z0-9]+$"))
{
throw new ArgumentException("RoomIdentifier can only use English or numbers.");// 只能使用英文或数字
}
_roomID = value;
}
get
{
return _roomID;
}
}// Only English or numbers can be used, and cannot exceed 32 digits | 只能使用英文或数字,且不超过32位
}
public NewRoom(int? maxUser = 8, string roomID = null)
{
if (roomID == null)
{
roomID = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16);
}
else
{
data.roomID = roomID;
}
if (maxUser == null)
{
maxUser = 8;
}
else
{
data.maxUser = maxUser.Value;
}
}
}
/// <summary>
/// Join room | 加入房间
/// </summary>
public class JoinRoom : Message
{
public readonly string action = "joinRoom";
public Data data = new Data();
public class Data
{
public string roomID = "";
}
public JoinRoom(string roomID)
{
data.roomID = roomID;
}
}
/// <summary>
/// Leave room | 离开房间
/// </summary>
public class LeaveRoom : Message
{
public readonly string action = "leaveRoom";
}
public class SelectChart : Message
{
public readonly string action = "selectChart";
public Data data = new Data();
public class Data
{
public string chartMD5 = "";
public string chartUrl = "";
}
}
public class GameStart : Message
{
public readonly string action = "gameStart";
}
}
}
}