NetPacksLobbyServer.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * NetPacksLobbyServer.cpp, 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. #include "StdInc.h"
  11. #include "LobbyNetPackVisitors.h"
  12. #include "CVCMIServer.h"
  13. #include "CGameHandler.h"
  14. #include "../lib/StartInfo.h"
  15. #include "../lib/CRandomGenerator.h"
  16. #include "../lib/campaign/CampaignState.h"
  17. #include "../lib/serializer/Connection.h"
  18. void ClientPermissionsCheckerNetPackVisitor::visitForLobby(CPackForLobby & pack)
  19. {
  20. if(pack.isForServer())
  21. {
  22. result = srv.isClientHost(pack.c->connectionID);
  23. }
  24. }
  25. void ApplyOnServerAfterAnnounceNetPackVisitor::visitForLobby(CPackForLobby & pack)
  26. {
  27. // Propogate options after every CLobbyPackToServer
  28. if(pack.isForServer())
  29. {
  30. srv.updateAndPropagateLobbyState();
  31. }
  32. }
  33. void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  34. {
  35. result = srv.getState() == EServerState::LOBBY;
  36. }
  37. void ApplyOnServerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  38. {
  39. srv.clientConnected(pack.c, pack.names, pack.uuid, pack.mode);
  40. // Server need to pass some data to newly connected client
  41. pack.clientId = pack.c->connectionID;
  42. pack.mode = srv.si->mode;
  43. pack.hostClientId = srv.hostClientId;
  44. result = true;
  45. }
  46. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  47. {
  48. // FIXME: we need to avoid senting something to client that not yet get answer for LobbyClientConnected
  49. // Until UUID set we only pass LobbyClientConnected to this client
  50. pack.c->uuid = pack.uuid;
  51. srv.updateAndPropagateLobbyState();
  52. // FIXME: what is this??? We do NOT support reconnection into ongoing game - at the very least queries and battles are NOT serialized
  53. // if(srv.getState() == EServerState::GAMEPLAY)
  54. // {
  55. // //immediately start game
  56. // std::unique_ptr<LobbyStartGame> startGameForReconnectedPlayer(new LobbyStartGame);
  57. // startGameForReconnectedPlayer->initializedStartInfo = srv.si;
  58. // startGameForReconnectedPlayer->initializedGameState = srv.gh->gameState();
  59. // startGameForReconnectedPlayer->clientId = pack.c->connectionID;
  60. // srv.announcePack(std::move(startGameForReconnectedPlayer));
  61. // }
  62. }
  63. void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  64. {
  65. if(pack.clientId != pack.c->connectionID)
  66. {
  67. result = false;
  68. return;
  69. }
  70. if(pack.shutdownServer)
  71. {
  72. if(!srv.wasStartedByClient())
  73. {
  74. result = false;
  75. return;
  76. }
  77. if(pack.c->connectionID != srv.hostClientId)
  78. {
  79. result = false;
  80. return;
  81. }
  82. }
  83. result = true;
  84. }
  85. void ApplyOnServerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  86. {
  87. srv.clientDisconnected(pack.c);
  88. result = true;
  89. }
  90. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  91. {
  92. if(pack.shutdownServer)
  93. {
  94. logNetwork->info("Client requested shutdown, server will close itself...");
  95. srv.setState(EServerState::SHUTDOWN);
  96. return;
  97. }
  98. else if(srv.activeConnections.empty())
  99. {
  100. logNetwork->error("Last connection lost, server will close itself...");
  101. srv.setState(EServerState::SHUTDOWN);
  102. }
  103. else if(pack.c->connectionID == srv.hostClientId)
  104. {
  105. auto ph = std::make_unique<LobbyChangeHost>();
  106. auto newHost = srv.activeConnections.front();
  107. ph->newHostConnectionId = newHost->connectionID;
  108. srv.announcePack(std::move(ph));
  109. }
  110. srv.updateAndPropagateLobbyState();
  111. // if(srv.getState() != EServerState::SHUTDOWN && srv.remoteConnections.count(pack.c))
  112. // {
  113. // srv.remoteConnections -= pack.c;
  114. // srv.connectToRemote();
  115. // }
  116. }
  117. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChatMessage(LobbyChatMessage & pack)
  118. {
  119. result = true;
  120. }
  121. void ApplyOnServerNetPackVisitor::visitLobbySetMap(LobbySetMap & pack)
  122. {
  123. if(srv.getState() != EServerState::LOBBY)
  124. {
  125. result = false;
  126. return;
  127. }
  128. srv.updateStartInfoOnMapChange(pack.mapInfo, pack.mapGenOpts);
  129. result = true;
  130. }
  131. void ApplyOnServerNetPackVisitor::visitLobbySetCampaign(LobbySetCampaign & pack)
  132. {
  133. srv.si->mapname = pack.ourCampaign->getFilename();
  134. srv.si->mode = EStartMode::CAMPAIGN;
  135. srv.si->campState = pack.ourCampaign;
  136. srv.si->turnTimerInfo = TurnTimerInfo{};
  137. bool isCurrentMapConquerable = pack.ourCampaign->currentScenario() && pack.ourCampaign->isAvailable(*pack.ourCampaign->currentScenario());
  138. for(auto scenarioID : pack.ourCampaign->allScenarios())
  139. {
  140. if(pack.ourCampaign->isAvailable(scenarioID))
  141. {
  142. if(!isCurrentMapConquerable || (isCurrentMapConquerable && scenarioID == *pack.ourCampaign->currentScenario()))
  143. {
  144. srv.setCampaignMap(scenarioID);
  145. }
  146. }
  147. }
  148. result = true;
  149. }
  150. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignMap(LobbySetCampaignMap & pack)
  151. {
  152. srv.setCampaignMap(pack.mapId);
  153. result = true;
  154. }
  155. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignBonus(LobbySetCampaignBonus & pack)
  156. {
  157. srv.setCampaignBonus(pack.bonusId);
  158. result = true;
  159. }
  160. void ClientPermissionsCheckerNetPackVisitor::visitLobbyGuiAction(LobbyGuiAction & pack)
  161. {
  162. result = srv.isClientHost(pack.c->connectionID);
  163. }
  164. void ClientPermissionsCheckerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
  165. {
  166. result = srv.isClientHost(pack.c->connectionID);
  167. }
  168. void ApplyOnServerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
  169. {
  170. srv.prepareToRestart();
  171. result = true;
  172. }
  173. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
  174. {
  175. for(const auto & connection : srv.activeConnections)
  176. connection->enterLobbyConnectionMode();
  177. }
  178. void ClientPermissionsCheckerNetPackVisitor::visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack)
  179. {
  180. result = srv.isClientHost(pack.c->connectionID);
  181. }
  182. void ClientPermissionsCheckerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  183. {
  184. result = srv.isClientHost(pack.c->connectionID);
  185. }
  186. void ApplyOnServerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  187. {
  188. try
  189. {
  190. srv.verifyStateBeforeStart(true);
  191. }
  192. catch(...)
  193. {
  194. result = false;
  195. return;
  196. }
  197. // Server will prepare gamestate and we announce StartInfo to clients
  198. if(!srv.prepareToStartGame())
  199. {
  200. result = false;
  201. return;
  202. }
  203. pack.initializedStartInfo = std::make_shared<StartInfo>(*srv.gh->getStartInfo(true));
  204. pack.initializedGameState = srv.gh->gameState();
  205. result = true;
  206. }
  207. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  208. {
  209. if(pack.clientId == -1) //do not restart game for single client only
  210. srv.startGameImmediately();
  211. else
  212. {
  213. for(const auto & connection : srv.activeConnections)
  214. {
  215. if(connection->connectionID == pack.clientId)
  216. {
  217. connection->enterGameplayConnectionMode(srv.gh->gameState());
  218. srv.reconnectPlayer(pack.clientId);
  219. }
  220. }
  221. }
  222. }
  223. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  224. {
  225. result = srv.isClientHost(pack.c->connectionID);
  226. }
  227. void ApplyOnServerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  228. {
  229. result = true;
  230. }
  231. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  232. {
  233. auto result = srv.passHost(pack.newHostConnectionId);
  234. if(!result)
  235. {
  236. logGlobal->error("passHost returned false. What does it mean?");
  237. }
  238. }
  239. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  240. {
  241. if(srv.isClientHost(pack.c->connectionID))
  242. {
  243. result = true;
  244. return;
  245. }
  246. if(vstd::contains(srv.getAllClientPlayers(pack.c->connectionID), pack.color))
  247. {
  248. result = true;
  249. return;
  250. }
  251. result = false;
  252. }
  253. void ApplyOnServerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  254. {
  255. switch(pack.what)
  256. {
  257. case LobbyChangePlayerOption::TOWN_ID:
  258. srv.optionSetCastle(pack.color, FactionID(pack.value));
  259. break;
  260. case LobbyChangePlayerOption::TOWN:
  261. srv.optionNextCastle(pack.color, pack.value);
  262. break;
  263. case LobbyChangePlayerOption::HERO_ID:
  264. srv.optionSetHero(pack.color, HeroTypeID(pack.value));
  265. break;
  266. case LobbyChangePlayerOption::HERO:
  267. srv.optionNextHero(pack.color, pack.value);
  268. break;
  269. case LobbyChangePlayerOption::BONUS_ID:
  270. srv.optionSetBonus(pack.color, PlayerStartingBonus(pack.value));
  271. break;
  272. case LobbyChangePlayerOption::BONUS:
  273. srv.optionNextBonus(pack.color, pack.value);
  274. break;
  275. }
  276. result = true;
  277. }
  278. void ApplyOnServerNetPackVisitor::visitLobbySetPlayer(LobbySetPlayer & pack)
  279. {
  280. srv.setPlayer(pack.clickedColor);
  281. result = true;
  282. }
  283. void ApplyOnServerNetPackVisitor::visitLobbySetPlayerName(LobbySetPlayerName & pack)
  284. {
  285. srv.setPlayerName(pack.color, pack.name);
  286. result = true;
  287. }
  288. void ApplyOnServerNetPackVisitor::visitLobbySetSimturns(LobbySetSimturns & pack)
  289. {
  290. srv.si->simturnsInfo = pack.simturnsInfo;
  291. result = true;
  292. }
  293. void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack)
  294. {
  295. srv.si->turnTimerInfo = pack.turnTimerInfo;
  296. result = true;
  297. }
  298. void ApplyOnServerNetPackVisitor::visitLobbySetExtraOptions(LobbySetExtraOptions & pack)
  299. {
  300. srv.si->extraOptionsInfo = pack.extraOptionsInfo;
  301. result = true;
  302. }
  303. void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack)
  304. {
  305. srv.si->difficulty = std::clamp<uint8_t>(pack.difficulty, 0, 4);
  306. result = true;
  307. }
  308. void ApplyOnServerNetPackVisitor::visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack)
  309. {
  310. srv.si->playerInfos[pack.targetPlayerColor].connectedPlayerIDs.insert(pack.targetConnectedPlayer);
  311. result = true;
  312. }