NetPacksLobby.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. int clientId; //-1 means to all clients
  132. LobbyStartGame() : initializedStartInfo(nullptr), initializedGameState(nullptr), clientId(-1) {}
  133. bool checkClientPermissions(CVCMIServer * srv) const;
  134. bool applyOnServer(CVCMIServer * srv);
  135. void applyOnServerAfterAnnounce(CVCMIServer * srv);
  136. bool applyOnLobbyHandler(CServerHandler * handler);
  137. void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
  138. template <typename Handler> void serialize(Handler &h, const int version)
  139. {
  140. h & clientId;
  141. h & initializedStartInfo;
  142. bool sps = h.smartPointerSerialization;
  143. h.smartPointerSerialization = true;
  144. h & initializedGameState;
  145. h.smartPointerSerialization = sps;
  146. }
  147. };
  148. struct LobbyChangeHost : public CLobbyPackToPropagate
  149. {
  150. int newHostConnectionId;
  151. LobbyChangeHost() : newHostConnectionId(-1) {}
  152. bool checkClientPermissions(CVCMIServer * srv) const;
  153. bool applyOnServer(CVCMIServer * srv);
  154. bool applyOnServerAfterAnnounce(CVCMIServer * srv);
  155. template <typename Handler> void serialize(Handler & h, const int version)
  156. {
  157. h & newHostConnectionId;
  158. }
  159. };
  160. struct LobbyUpdateState : public CLobbyPackToPropagate
  161. {
  162. LobbyState state;
  163. bool hostChanged; // Used on client-side only
  164. LobbyUpdateState() : hostChanged(false) {}
  165. bool applyOnLobbyHandler(CServerHandler * handler);
  166. void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
  167. template <typename Handler> void serialize(Handler &h, const int version)
  168. {
  169. h & state;
  170. }
  171. };
  172. struct LobbySetMap : public CLobbyPackToServer
  173. {
  174. std::shared_ptr<CMapInfo> mapInfo;
  175. std::shared_ptr<CMapGenOptions> mapGenOpts;
  176. LobbySetMap() : mapInfo(nullptr), mapGenOpts(nullptr) {}
  177. bool applyOnServer(CVCMIServer * srv);
  178. template <typename Handler> void serialize(Handler &h, const int version)
  179. {
  180. h & mapInfo;
  181. h & mapGenOpts;
  182. }
  183. };
  184. struct LobbySetCampaign : public CLobbyPackToServer
  185. {
  186. std::shared_ptr<CCampaignState> ourCampaign;
  187. LobbySetCampaign() {}
  188. bool applyOnServer(CVCMIServer * srv);
  189. template <typename Handler> void serialize(Handler &h, const int version)
  190. {
  191. h & ourCampaign;
  192. }
  193. };
  194. struct LobbySetCampaignMap : public CLobbyPackToServer
  195. {
  196. int mapId;
  197. LobbySetCampaignMap() : mapId(-1) {}
  198. bool applyOnServer(CVCMIServer * srv);
  199. template <typename Handler> void serialize(Handler &h, const int version)
  200. {
  201. h & mapId;
  202. }
  203. };
  204. struct LobbySetCampaignBonus : public CLobbyPackToServer
  205. {
  206. int bonusId;
  207. LobbySetCampaignBonus() : bonusId(-1) {}
  208. bool applyOnServer(CVCMIServer * srv);
  209. template <typename Handler> void serialize(Handler &h, const int version)
  210. {
  211. h & bonusId;
  212. }
  213. };
  214. struct LobbyChangePlayerOption : public CLobbyPackToServer
  215. {
  216. enum EWhat : ui8 {UNKNOWN, TOWN, HERO, BONUS};
  217. ui8 what;
  218. si8 direction; //-1 or +1
  219. PlayerColor color;
  220. LobbyChangePlayerOption() : what(UNKNOWN), direction(0), color(PlayerColor::CANNOT_DETERMINE) {}
  221. bool checkClientPermissions(CVCMIServer * srv) const;
  222. bool applyOnServer(CVCMIServer * srv);
  223. template <typename Handler> void serialize(Handler &h, const int version)
  224. {
  225. h & what;
  226. h & direction;
  227. h & color;
  228. }
  229. };
  230. struct LobbySetPlayer : public CLobbyPackToServer
  231. {
  232. PlayerColor clickedColor;
  233. LobbySetPlayer() : clickedColor(PlayerColor::CANNOT_DETERMINE){}
  234. bool applyOnServer(CVCMIServer * srv);
  235. template <typename Handler> void serialize(Handler &h, const int version)
  236. {
  237. h & clickedColor;
  238. }
  239. };
  240. struct LobbySetTurnTime : public CLobbyPackToServer
  241. {
  242. ui8 turnTime;
  243. LobbySetTurnTime() : turnTime(0) {}
  244. bool applyOnServer(CVCMIServer * srv);
  245. template <typename Handler> void serialize(Handler &h, const int version)
  246. {
  247. h & turnTime;
  248. }
  249. };
  250. struct LobbySetDifficulty : public CLobbyPackToServer
  251. {
  252. ui8 difficulty;
  253. LobbySetDifficulty() : difficulty(0) {}
  254. bool applyOnServer(CVCMIServer * srv);
  255. template <typename Handler> void serialize(Handler &h, const int version)
  256. {
  257. h & difficulty;
  258. }
  259. };
  260. struct LobbyForceSetPlayer : public CLobbyPackToServer
  261. {
  262. ui8 targetConnectedPlayer;
  263. PlayerColor targetPlayerColor;
  264. LobbyForceSetPlayer() : targetConnectedPlayer(-1), targetPlayerColor(PlayerColor::CANNOT_DETERMINE) {}
  265. bool applyOnServer(CVCMIServer * srv);
  266. template <typename Handler> void serialize(Handler & h, const int version)
  267. {
  268. h & targetConnectedPlayer;
  269. h & targetPlayerColor;
  270. }
  271. };
  272. struct LobbyShowMessage : public CLobbyPackToPropagate
  273. {
  274. std::string message;
  275. void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
  276. template <typename Handler> void serialize(Handler & h, const int version)
  277. {
  278. h & message;
  279. }
  280. };
  281. VCMI_LIB_NAMESPACE_END