NetPacksLobby.h 7.5 KB

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