Etterna 0.74.4
Loading...
Searching...
No Matches
NetworkSyncManager.h
1#ifndef NetworkSyncManager_H
2#define NetworkSyncManager_H
3
4#include "Etterna/Models/Misc/Difficulty.h"
5#include "Etterna/Models/HighScore/HighScore.h"
6#include <queue>
7#include "rapidjson/fwd.h"
8#define ASIO_STANDALONE
9#define _WEBSOCKETPP_CPP11_INTERNAL_
10#include <websocketpp/client.hpp>
11#include <websocketpp/config/asio_client.hpp>
12typedef websocketpp::config::asio_tls_client::message_type::ptr wss_message_ptr;
13using wss_client = ::websocketpp::client<websocketpp::config::asio_tls_client>;
14#include <websocketpp/config/asio_no_tls_client.hpp>
15using ws_message_ptr = ::websocketpp::config::asio_client::message_type::ptr;
16using ws_client = ::websocketpp::client<websocketpp::config::asio_client>;
17
18class LoadingWindow;
19
20class RoomData;
23class Song;
24const int NETPROTOCOLVERSION = 4;
25const int ETTPCVERSION = 3;
26const int NETMAXBUFFERSIZE = 1020; // 1024 - 4 bytes for EzSockets
27const int NETNUMTAPSCORES = 8;
28
30// [SMLClientCommands name]
31enum NSCommand
32{
33 NSCPing = 0,
34 NSCPingR, // 1 [SMLC_PingR]
35 NSCHello, // 2 [SMLC_Hello]
36 NSCGSR, // 3 [SMLC_GameStart]
37 NSCGON, // 4 [SMLC_GameOver]
38 NSCGSU, // 5 [SMLC_GameStatusUpdate]
39 NSCSU, // 6 [SMLC_StyleUpdate]
40 NSCCM, // 7 [SMLC_Chat]
41 NSCRSG, // 8 [SMLC_RequestStart]
42 NSCUUL, // 9 [SMLC_Reserved1]
43 NSCSMS, // 10 [SMLC_MusicSelect]
44 NSCUPOpts, // 11 [SMLC_PlayerOpts]
45 NSCSMOnline, // 12 [SMLC_SMO]
46 NSCFormatted, // 13 [SMLC_RESERVED1]
47 NSCAttack, // 14 [SMLC_RESERVED2]
48 XML, // 15 [SMLC_RESERVED3]
49 FLU, // 16 [SMLC_FriendListUpdate]
50 NUM_NS_COMMANDS
51};
52
53enum SMOStepType
54{
55 SMOST_UNUSED = 0,
56 SMOST_HITMINE,
57 SMOST_AVOIDMINE,
58 SMOST_MISS,
59 SMOST_W5,
60 SMOST_W4,
61 SMOST_W3,
62 SMOST_W2,
63 SMOST_W1,
64 SMOST_LETGO,
65 SMOST_HELD
66 /*
67 ,SMOST_CHECKPOINTMISS,
68 SMOST_CHECKPOINTHIT
69 */
70};
71
72const NSCommand NSServerOffset = static_cast<NSCommand>(128);
73
74// TODO(Sam): Provide a Lua binding that gives access to this data. -aj
76{
77 public:
78 int name{0};
79 std::string nameStr;
80 int grade;
81 int score;
82 Difficulty difficulty;
83 int tapScores[NETNUMTAPSCORES]; // This will be a const soon enough
84 HighScore hs;
85 std::string playerOptions;
86};
87
88enum ETTServerMessageTypes
89{
90 ettps_hello = 0,
91 ettps_ping,
92 ettps_recievechat,
93 ettps_loginresponse,
94 ettps_roomlist,
95 ettps_lobbyuserlist,
96 ettps_lobbyuserlistupdate,
97 ettps_recievescore,
98 ettps_mpleaderboardupdate,
99 ettps_createroomresponse,
100 ettps_enterroomresponse,
101 ettps_selectchart,
102 ettps_startchart,
103 ettps_deleteroom,
104 ettps_newroom,
105 ettps_updateroom,
106 ettps_roomuserlist,
107 ettps_chartrequest,
108 ettps_roompacklist,
109 ettps_end
110};
111enum ETTClientMessageTypes
112{
113 ettpc_login = 0,
114 ettpc_ping,
115 ettpc_sendchat,
116 ettpc_sendscore,
117 ettpc_mpleaderboardupdate,
118 ettpc_createroom,
119 ettpc_enterroom,
120 ettpc_leaveroom,
121 ettpc_selectchart,
122 ettpc_startchart,
123 ettpc_gameover,
124 ettpc_haschart,
125 ettpc_missingchart,
126 ettpc_startingchart,
127 ettpc_notstartingchart,
128 ettpc_openoptions,
129 ettpc_closeoptions,
130 ettpc_openeval,
131 ettpc_closeeval,
132 ettpc_logout,
133 ettpc_hello,
134 ettpc_end
135};
137#define FOREACH_NSScoreBoardColumn(sc) FOREACH_ENUM(NSScoreBoardColumn, sc)
138
140{
141 std::string Name;
142 std::string Address;
143};
144
146{
147 public:
148 ChartRequest(const char* ck, const char* requester, int rate)
149 : chartkey(ck)
150 , user(requester)
151 , rate(rate)
152 {
153 }
154 const std::string chartkey;
155 const std::string user; // User that requested this chart
156 const int rate; // rate * 1000
157 void PushSelf(lua_State* L);
158};
159
160class EzSockets;
161class StepManiaLanServer;
163{
164 public:
165 float wife;
166 std::string jdgstr;
167};
168
170{
171 public:
172 unsigned char Data[NETMAXBUFFERSIZE]; // Data
173 int Position; // Other info (Used for following functions)
174 int size; // When sending these pacs, Position should
175 // be used; NOT size.
176
177 // Commands used to operate on NetPackets
178 auto Read1() -> uint8_t;
179 auto Read2() -> uint16_t;
180 auto Read4() -> uint32_t;
181 auto ReadNT() -> std::string;
182
183 void Write1(uint8_t Data);
184 void Write2(uint16_t Data);
185 void Write4(uint32_t Data);
186 void WriteNT(const std::string& Data);
187
188 void ClearPacket();
189};
190
192class ScreenNetRoom;
194{
195 public:
196 std::string serverName;
197 int serverVersion{ 0 }; // ServerVersion
198 virtual auto Connect(NetworkSyncManager* /*n*/,
199 unsigned short /*port*/,
200 std::string /*address*/) -> bool
201 {
202 return false;
203 }
204 virtual void close() {}
205 virtual void Update(NetworkSyncManager* n, float fDeltaTime) {}
206 virtual void CreateNewRoom(std::string name,
207 std::string desc,
208 std::string password)
209 {
210 }
211 virtual void SelectUserSong(NetworkSyncManager* n, Song* song) {}
212 virtual void EnterRoom(std::string name, std::string password) {}
213 virtual void LeaveRoom(NetworkSyncManager* n) {}
214 virtual void RequestRoomInfo(std::string name) {}
215 virtual void SendChat(const std::string& message, std::string tab, int type)
216 {
217 }
218 virtual void ReportNSSOnOff(int i) {}
219 virtual void ReportScore(NetworkSyncManager* n,
220 int playerID,
221 int step,
222 int score,
223 int combo,
224 float offset,
225 int numNotes)
226 {
227 }
228 virtual void ReportScore(NetworkSyncManager* n,
229 int playerID,
230 int step,
231 int score,
232 int combo,
233 float offset)
234 {
235 }
236 virtual void ReportHighScore(HighScore* hs, PlayerStageStats& pss) {}
237 virtual void ReportSongOver(NetworkSyncManager* n) {}
238 virtual void ReportStyle(NetworkSyncManager* n) {}
239 virtual void StartRequest(NetworkSyncManager* n, short position) {}
240 virtual void Login(std::string user, std::string pass) {}
241 virtual void Logout() {}
242 virtual void OnMusicSelect(){};
243 virtual void OffMusicSelect(){};
244 virtual void OnRoomSelect(){};
245 virtual void OffRoomSelect(){};
246 virtual void OnOptions(){};
247 virtual void OffOptions(){};
248 virtual void OnEval(){};
249 virtual void OffEval(){};
250 virtual void SendMPLeaderboardUpdate(float wife, std::string& jdgstr){};
251};
252
254{ // Websockets using websocketpp sending json
255 std::unique_ptr<std::thread> thread;
256 std::mutex messageBufferMutex;
257 std::vector<std::unique_ptr<rapidjson::Document>> newMessages;
258 unsigned int msgId{ 0 };
259 bool error{ false };
260 std::string errorMsg;
261 std::shared_ptr<ws_client> client{ nullptr };
262 std::shared_ptr<wss_client> secure_client{ nullptr };
263 std::shared_ptr<websocketpp::connection_hdl> hdl{ nullptr };
264 void FindJsonChart(NetworkSyncManager* n, rapidjson::Value& ch);
265 int state = 0; // 0 = ready, 1 = playing, 2 = evalScreen, 3 = options, 4 =
266 // notReady(unkown reason)
267 public:
268 ~ETTProtocol();
269 bool waitingForTimeout{ false };
270 bool creatingRoom{ false };
271 clock_t timeoutStart = 0;
272 double timeout = 0;
273 std::function<void(void)> onTimeout;
274 std::string roomName;
275 std::string roomDesc;
276 bool inRoom{ false };
277 auto Connect(NetworkSyncManager* n,
278 unsigned short port,
279 std::string address) -> bool override; // Connect and say hello
280 void close() override;
281 void Update(NetworkSyncManager* n, float fDeltaTime) override;
282 void Login(std::string user, std::string pass) override;
283 void Logout() override;
284 void SendChat(const std::string& message,
285 std::string tab,
286 int type) override;
287 void CreateNewRoom(std::string name,
288 std::string desc,
289 std::string password) override;
290 void EnterRoom(std::string name, std::string password) override;
291 void LeaveRoom(NetworkSyncManager* n) override;
292 void ReportSongOver(NetworkSyncManager* n) override;
293 void SelectUserSong(NetworkSyncManager* n, Song* song) override;
294 void OnMusicSelect() override;
295 void OnOptions() override;
296 void OffOptions() override;
297 void OnEval() override;
298 void OffEval() override;
299 void SendMPLeaderboardUpdate(float wife, std::string& jdgstr) override;
300 void ReportHighScore(HighScore* hs, PlayerStageStats& pss) override;
301 void Send(const char* msg);
302 /*
303 void ReportScore(NetworkSyncManager* n, int playerID, int step, int score,
304 int combo, float offset, int numNotes) override; void
305 ReportScore(NetworkSyncManager* n, int playerID, int step, int score, int
306 combo, float offset) override; void ReportStyle(NetworkSyncManager* n)
307 override;
308 */
309};
310// Regular pair map except [anyString, 0] is the same key
311class Chat
312{
313 public:
314 std::map<std::pair<std::string, int>, std::vector<std::string>> rawMap;
315
316 auto operator[](const std::pair<std::string, int>& p)
317 -> std::vector<std::string>&
318 {
319 if (p.second == 0) {
320 return rawMap.operator[](std::make_pair(std::string(""), 0));
321 }
322 {
323 return rawMap.operator[](p);
324 }
325 }
326};
329{
330 public:
331 NetworkSyncManager(LoadingWindow* ld = nullptr);
333 ETTProtocol ETTP;
334 NetProtocol* curProtocol{ nullptr };
335 // If "useSMserver" then send score to server
336 void ReportScore(int playerID,
337 int step,
338 int score,
339 int combo,
340 float offset);
341 void ReportScore(int playerID,
342 int step,
343 int score,
344 int combo,
345 float offset,
346 int numNotes);
347 void ReportHighScore(HighScore* hs, PlayerStageStats& pss);
348 void ReportSongOver();
349 void ReportStyle(); // Report style, players, and names
350 void ReportNSSOnOff(int i); // Report song selection screen on/off
351
352 void OnMusicSelect();
353 void OffMusicSelect();
354 void OnRoomSelect();
355 void OffRoomSelect();
356 void OnOptions();
357 void OffOptions();
358 void OnEval();
359 void OffEval();
360
361 void StartRequest(short position); // Request a start; Block until granted.
362 auto GetServerName() -> std::string;
363
364 void CreateNewRoom(std::string name,
365 std::string desc = "",
366 std::string password = "");
367 void EnterRoom(std::string name, std::string password = "");
368 void LeaveRoom();
369 void RequestRoomInfo(std::string name);
370
371 void PostStartUp(const std::string& ServerIP);
372
373 auto IsETTP() -> bool;
374
375 void CloseConnection();
376
377 void DisplayStartupStatus(); // Notify user if connect attempt was
378 // successful or not.
379
380 int m_playerLife; // Life (used for sending to server)
381
382 void Update(float fDeltaTime);
383
384 bool useSMserver;
385 bool isSMOnline;
386 bool loggedIn;
387 std::string loggedInUsername;
388 std::string loginResponse; // Failure reason
389
390 Chat chat; //[{Tabname, int}] = std::vector<line>
391
392 std::vector<int> m_PlayerStatus;
393 int m_ActivePlayers;
394 std::vector<int> m_ActivePlayer;
395 std::vector<std::string> m_PlayerNames;
396 std::vector<bool> m_PlayerReady;
397 std::vector<std::string> commonpacks;
398
399 // friendlist
400 std::vector<std::string> fl_PlayerNames;
401 std::vector<int> fl_PlayerStates;
402
403 // Used for ScreenNetEvaluation
404 std::vector<EndOfGame_PlayerData> m_EvalPlayerData;
405
406 // Used together:
407 auto ChangedScoreboard(int Column)
408 -> bool; // Returns true if scoreboard changed
409 // since function was last called.
410 std::string m_Scoreboard[NUM_NSScoreBoardColumn];
411
412 std::set<std::string> lobbyuserlist;
413
414 void SendMPLeaderboardUpdate(float wife, std::string& jdgstr);
415
416 // Used for chatting
417 void SendChat(const std::string& message,
418 std::string tab = "",
419 int type = 0); // 0=lobby (ettp only)
420 std::string m_WaitingChat;
421
422 // Used for song checking/changing
423 std::string m_sMainTitle;
424 std::string m_sArtist;
425 std::string m_sSubTitle;
426 std::string m_sFileHash;
427 std::string chartkey;
428 Song* song{ nullptr };
429 Steps* steps{ nullptr };
430 std::map<std::string, GameplayScore> mpleaderboard;
431 void PushMPLeaderboard(lua_State* L);
432 Difficulty difficulty;
433 int meter;
434 int rate;
435
436 int m_iSelectMode;
437 void SelectUserSong();
438
439 auto GetServerVersion() -> int;
440
441 std::string m_sChatText;
442
443 StepManiaLanServer* LANserver;
444
445 void GetListOfLANServers(std::vector<NetServerInfo>& AllServers);
446
447 // Aldo: Please move this method to a new class, I didn't want to create new
448 // files because I don't know how to properly update the files for each
449 // platform. I preferred to misplace code rather than cause unneeded
450 // headaches to non-windows users, although it would be nice to have in the
451 // wiki which files to update when adding new files.
452 static auto GetCurrentSMBuild(LoadingWindow* ld) -> unsigned long;
453
454 int m_startupStatus; // Used to see if attempt was successful or not.
455
456 void Login(std::string user, std::string pass);
457 void Logout();
458 std::vector<RoomData> m_Rooms;
459 std::vector<ChartRequest*> requests;
460 std::vector<ChartRequest*> staleRequests;
461
462 auto TranslateStepType(int score) -> SMOStepType;
463 std::vector<NetServerInfo> m_vAllLANServers;
464 bool m_scoreboardchange[NUM_NSScoreBoardColumn];
465
466 // Lua
467 void PushSelf(lua_State* L);
468
469 private:
470 void ProcessInput();
471 void StartUp();
472
473 int m_playerID; // Currently unused, but need to stay
474 int m_step;
475 int m_score;
476 int m_combo;
477};
478
479extern NetworkSyncManager* NSMAN;
480
481#endif
Definition NetworkSyncManager.h:146
Definition NetworkSyncManager.h:312
Definition NetworkSyncManager.h:254
Definition NetworkSyncManager.h:76
Definition NetworkSyncManager.h:163
Opens and displays the loading banner.
Definition LoadingWindow.h:9
Definition NetworkSyncManager.h:194
Uses ezsockets for primitive song syncing and score reporting.
Definition NetworkSyncManager.h:329
Definition NetworkSyncManager.h:170
Contains statistics for one stage of play - either one song, or a whole course.
Definition PlayerStageStats.h:19
Definition RoomWheel.h:10
Definition ScreenNetRoom.h:14
Definition ScreenNetSelectMusic.h:10
Definition ScreenSMOnlineLogin.h:7
Holds all music metadata and steps for one song.
Definition Song.h:65
Holds note information for a Song.
Definition Steps.h:42
The high score that is earned by a player.
Definition HighScore.h:18
Definition NetworkSyncManager.h:140