NetPacksLobby.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * NetPacksLobby.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "NetPacksBase.h"
  12. #include "StartInfo.h"
  13. class CLobbyScreen;
  14. class CServerHandler;
  15. class CVCMIServer;
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class CCampaignState;
  18. class CMapInfo;
  19. struct StartInfo;
  20. class CMapGenOptions;
  21. struct ClientPlayer;
  22. struct CPackForLobby : public CPack
  23. {
  24. bool checkClientPermissions(CVCMIServer * srv) const
  25. {
  26. return false;
  27. }
  28. bool applyOnServer(CVCMIServer * srv)
  29. {
  30. return true;
  31. }
  32. void applyOnServerAfterAnnounce(CVCMIServer * srv) {}
  33. bool applyOnLobbyHandler(CServerHandler * handler)
  34. {
  35. return true;
  36. }
  37. void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler) {}
  38. };
  39. struct CLobbyPackToPropagate : public CPackForLobby
  40. {
  41. };
  42. struct CLobbyPackToServer : public CPackForLobby
  43. {
  44. bool checkClientPermissions(CVCMIServer * srv) const;
  45. void applyOnServerAfterAnnounce(CVCMIServer * srv);
  46. };
  47. struct LobbyClientConnected : public CLobbyPackToPropagate
  48. {
  49. // Set by client before sending pack to server
  50. std::string uuid;
  51. std::vector<std::string> names;
  52. StartInfo::EMode mode;
  53. // Changed by server before announcing pack
  54. int clientId;
  55. int hostClientId;
  56. LobbyClientConnected()
  57. : mode(StartInfo::INVALID), clientId(-1), hostClientId(-1)
  58. {}
  59. bool checkClientPermissions(CVCMIServer * srv) const;
  60. bool applyOnLobbyHandler(CServerHandler * handler);
  61. void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
  62. bool applyOnServer(CVCMIServer * srv);
  63. void applyOnServerAfterAnnounce(CVCMIServer * srv);
  64. template <typename Handler> void serialize(Handler & h, const int version)
  65. {
  66. h & uuid;
  67. h & names;
  68. h & mode;
  69. h & clientId;
  70. h & hostClientId;
  71. }
  72. };
  73. struct LobbyClientDisconnected : public CLobbyPackToPropagate
  74. {
  75. int clientId;
  76. bool shutdownServer;
  77. LobbyClientDisconnected() : shutdownServer(false) {}
  78. bool checkClientPermissions(CVCMIServer * srv) const;
  79. bool applyOnServer(CVCMIServer * srv);
  80. void applyOnServerAfterAnnounce(CVCMIServer * srv);
  81. bool applyOnLobbyHandler(CServerHandler * handler);
  82. void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
  83. template <typename Handler> void serialize(Handler &h, const int version)
  84. {
  85. h & clientId;
  86. h & shutdownServer;
  87. }
  88. };
  89. struct LobbyChatMessage : public CLobbyPackToPropagate
  90. {
  91. std::string playerName, message;
  92. bool checkClientPermissions(CVCMIServer * srv) const;
  93. void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
  94. template <typename Handler> void serialize(Handler &h, const int version)
  95. {
  96. h & playerName;
  97. h & message;
  98. }
  99. };
  100. struct LobbyGuiAction : public CLobbyPackToPropagate
  101. {
  102. enum EAction : ui8 {
  103. NONE, NO_TAB, OPEN_OPTIONS, OPEN_SCENARIO_LIST, OPEN_RANDOM_MAP_OPTIONS
  104. } action;
  105. LobbyGuiAction() : action(NONE) {}
  106. bool checkClientPermissions(CVCMIServer * srv) const;
  107. void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
  108. template <typename Handler> void serialize(Handler &h, const int version)
  109. {
  110. h & action;
  111. }
  112. };
  113. struct LobbyEndGame : public CLobbyPackToPropagate
  114. {
  115. bool closeConnection = false, restart = false;
  116. bool checkClientPermissions(CVCMIServer * srv) const;
  117. bool applyOnServer(CVCMIServer * srv);
  118. void applyOnServerAfterAnnounce(CVCMIServer * srv);
  119. bool applyOnLobbyHandler(CServerHandler * handler);
  120. template <typename Handler> void serialize(Handler &h, const int version)
  121. {
  122. h & closeConnection;
  123. h & restart;
  124. }
  125. };
  126. struct LobbyStartGame : public CLobbyPackToPropagate
  127. {
  128. // Set by server
  129. std::shared_ptr<StartInfo> initializedStartInfo;
  130. CGameState * initializedGameState;
  131. LobbyStartGame() : initializedStartInfo(nullptr), initializedGameState(nullptr) {}
  132. bool checkClientPermissions(CVCMIServer * srv) const;
  133. bool applyOnServer(CVCMIServer * srv);
  134. void applyOnServerAfterAnnounce(CVCMIServer * srv);
  135. bool applyOnLobbyHandler(CServerHandler * handler);
  136. void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
  137. template <typename Handler> void serialize(Handler &h, const int version)
  138. {
  139. h & initializedStartInfo;
  140. bool sps = h.smartPointerSerialization;
  141. h.smartPointerSerialization = true;
  142. h & initializedGameState;
  143. h.smartPointerSerialization = sps;
  144. }
  145. };
  146. struct LobbyChangeHost : public CLobbyPackToPropagate
  147. {
  148. int newHostConnectionId;
  149. LobbyChangeHost() : newHostConnectionId(-1) {}
  150. bool checkClientPermissions(CVCMIServer * srv) const;
  151. bool applyOnServer(CVCMIServer * srv);
  152. bool applyOnServerAfterAnnounce(CVCMIServer * srv);
  153. template <typename Handler> void serialize(Handler & h, const int version)
  154. {
  155. h & newHostConnectionId;
  156. }
  157. };
  158. struct LobbyUpdateState : public CLobbyPackToPropagate
  159. {
  160. LobbyState state;
  161. bool hostChanged; // Used on client-side only
  162. LobbyUpdateState() : hostChanged(false) {}
  163. bool applyOnLobbyHandler(CServerHandler * handler);
  164. void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
  165. template <typename Handler> void serialize(Handler &h, const int version)
  166. {
  167. h & state;
  168. }
  169. };
  170. struct LobbySetMap : public CLobbyPackToServer
  171. {
  172. std::shared_ptr<CMapInfo> mapInfo;
  173. std::shared_ptr<CMapGenOptions> mapGenOpts;
  174. LobbySetMap() : mapInfo(nullptr), mapGenOpts(nullptr) {}
  175. bool applyOnServer(CVCMIServer * srv);
  176. template <typename Handler> void serialize(Handler &h, const int version)
  177. {
  178. h & mapInfo;
  179. h & mapGenOpts;
  180. }
  181. };
  182. struct LobbySetCampaign : public CLobbyPackToServer
  183. {
  184. std::shared_ptr<CCampaignState> ourCampaign;
  185. LobbySetCampaign() {}
  186. bool applyOnServer(CVCMIServer * srv);
  187. template <typename Handler> void serialize(Handler &h, const int version)
  188. {
  189. h & ourCampaign;
  190. }
  191. };
  192. struct LobbySetCampaignMap : public CLobbyPackToServer
  193. {
  194. int mapId;
  195. LobbySetCampaignMap() : mapId(-1) {}
  196. bool applyOnServer(CVCMIServer * srv);
  197. template <typename Handler> void serialize(Handler &h, const int version)
  198. {
  199. h & mapId;
  200. }
  201. };
  202. struct LobbySetCampaignBonus : public CLobbyPackToServer
  203. {
  204. int bonusId;
  205. LobbySetCampaignBonus() : bonusId(-1) {}
  206. bool applyOnServer(CVCMIServer * srv);
  207. template <typename Handler> void serialize(Handler &h, const int version)
  208. {
  209. h & bonusId;
  210. }
  211. };
  212. struct LobbyChangePlayerOption : public CLobbyPackToServer
  213. {
  214. enum EWhat : ui8 {UNKNOWN, TOWN, HERO, BONUS};
  215. ui8 what;
  216. si8 direction; //-1 or +1
  217. PlayerColor color;
  218. LobbyChangePlayerOption() : what(UNKNOWN), direction(0), color(PlayerColor::CANNOT_DETERMINE) {}
  219. bool checkClientPermissions(CVCMIServer * srv) const;
  220. bool applyOnServer(CVCMIServer * srv);
  221. template <typename Handler> void serialize(Handler &h, const int version)
  222. {
  223. h & what;
  224. h & direction;
  225. h & color;
  226. }
  227. };
  228. struct LobbySetPlayer : public CLobbyPackToServer
  229. {
  230. PlayerColor clickedColor;
  231. LobbySetPlayer() : clickedColor(PlayerColor::CANNOT_DETERMINE){}
  232. bool applyOnServer(CVCMIServer * srv);
  233. template <typename Handler> void serialize(Handler &h, const int version)
  234. {
  235. h & clickedColor;
  236. }
  237. };
  238. struct LobbySetTurnTime : public CLobbyPackToServer
  239. {
  240. ui8 turnTime;
  241. LobbySetTurnTime() : turnTime(0) {}
  242. bool applyOnServer(CVCMIServer * srv);
  243. template <typename Handler> void serialize(Handler &h, const int version)
  244. {
  245. h & turnTime;
  246. }
  247. };
  248. struct LobbySetDifficulty : public CLobbyPackToServer
  249. {
  250. ui8 difficulty;
  251. LobbySetDifficulty() : difficulty(0) {}
  252. bool applyOnServer(CVCMIServer * srv);
  253. template <typename Handler> void serialize(Handler &h, const int version)
  254. {
  255. h & difficulty;
  256. }
  257. };
  258. struct LobbyForceSetPlayer : public CLobbyPackToServer
  259. {
  260. ui8 targetConnectedPlayer;
  261. PlayerColor targetPlayerColor;
  262. LobbyForceSetPlayer() : targetConnectedPlayer(-1), targetPlayerColor(PlayerColor::CANNOT_DETERMINE) {}
  263. bool applyOnServer(CVCMIServer * srv);
  264. template <typename Handler> void serialize(Handler & h, const int version)
  265. {
  266. h & targetConnectedPlayer;
  267. h & targetPlayerColor;
  268. }
  269. };
  270. struct LobbyShowMessage : public CLobbyPackToPropagate
  271. {
  272. std::string message;
  273. void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
  274. template <typename Handler> void serialize(Handler & h, const int version)
  275. {
  276. h & message;
  277. }
  278. };
  279. VCMI_LIB_NAMESPACE_END