NetPacksLobby.h 7.4 KB

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