NetPacksLobbyServer.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/NetPacksLobby.h"
  15. #include "../lib/serializer/Connection.h"
  16. #include "../lib/StartInfo.h"
  17. // Campaigns
  18. #include "../lib/mapping/CCampaignHandler.h"
  19. #include "../lib/mapping/CMapService.h"
  20. #include "../lib/mapping/CMapInfo.h"
  21. void ClientPermissionsCheckerNetPackVisitor::visitForLobby(CPackForLobby & pack)
  22. {
  23. if(pack.isForServer())
  24. {
  25. result = srv.isClientHost(pack.c->connectionID);
  26. }
  27. }
  28. void ApplyOnServerAfterAnnounceNetPackVisitor::visitForLobby(CPackForLobby & pack)
  29. {
  30. // Propogate options after every CLobbyPackToServer
  31. if(pack.isForServer())
  32. {
  33. srv.updateAndPropagateLobbyState();
  34. }
  35. }
  36. void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  37. {
  38. if(srv.gh)
  39. {
  40. for(auto & connection : srv.hangingConnections)
  41. {
  42. if(connection->uuid == pack.uuid)
  43. {
  44. {
  45. result = true;
  46. return;
  47. }
  48. }
  49. }
  50. }
  51. if(srv.state == EServerState::LOBBY)
  52. {
  53. result = true;
  54. return;
  55. }
  56. //disconnect immediately and ignore this client
  57. srv.connections.erase(pack.c);
  58. if(pack.c && pack.c->isOpen())
  59. {
  60. pack.c->close();
  61. pack.c->connected = false;
  62. }
  63. {
  64. result = false;
  65. return;
  66. }
  67. }
  68. void ApplyOnServerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  69. {
  70. if(srv.gh)
  71. {
  72. for(auto & connection : srv.hangingConnections)
  73. {
  74. if(connection->uuid == pack.uuid)
  75. {
  76. logNetwork->info("Reconnection player");
  77. pack.c->connectionID = connection->connectionID;
  78. for(auto & playerConnection : srv.gh->connections)
  79. {
  80. for(auto & existingConnection : playerConnection.second)
  81. {
  82. if(existingConnection == connection)
  83. {
  84. playerConnection.second.erase(existingConnection);
  85. playerConnection.second.insert(pack.c);
  86. break;
  87. }
  88. }
  89. }
  90. srv.hangingConnections.erase(connection);
  91. break;
  92. }
  93. }
  94. }
  95. srv.clientConnected(pack.c, pack.names, pack.uuid, pack.mode);
  96. // Server need to pass some data to newly connected client
  97. pack.clientId = pack.c->connectionID;
  98. pack.mode = srv.si->mode;
  99. pack.hostClientId = srv.hostClientId;
  100. result = true;
  101. }
  102. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  103. {
  104. // FIXME: we need to avoid senting something to client that not yet get answer for LobbyClientConnected
  105. // Until UUID set we only pass LobbyClientConnected to this client
  106. pack.c->uuid = pack.uuid;
  107. srv.updateAndPropagateLobbyState();
  108. if(srv.state == EServerState::GAMEPLAY)
  109. {
  110. //immediately start game
  111. std::unique_ptr<LobbyStartGame> startGameForReconnectedPlayer(new LobbyStartGame);
  112. startGameForReconnectedPlayer->initializedStartInfo = srv.si;
  113. startGameForReconnectedPlayer->initializedGameState = srv.gh->gameState();
  114. startGameForReconnectedPlayer->clientId = pack.c->connectionID;
  115. srv.addToAnnounceQueue(std::move(startGameForReconnectedPlayer));
  116. }
  117. }
  118. void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  119. {
  120. if(pack.clientId != pack.c->connectionID)
  121. {
  122. result = false;
  123. return;
  124. }
  125. if(pack.shutdownServer)
  126. {
  127. if(!srv.cmdLineOptions.count("run-by-client"))
  128. {
  129. result = false;
  130. return;
  131. }
  132. if(pack.c->uuid != srv.cmdLineOptions["uuid"].as<std::string>())
  133. {
  134. result = false;
  135. return;
  136. }
  137. }
  138. result = true;
  139. }
  140. void ApplyOnServerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  141. {
  142. srv.clientDisconnected(pack.c);
  143. pack.c->close();
  144. pack.c->connected = false;
  145. result = true;
  146. }
  147. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  148. {
  149. if(pack.c && pack.c->isOpen())
  150. {
  151. boost::unique_lock<boost::mutex> lock(*pack.c->mutexWrite);
  152. pack.c->close();
  153. pack.c->connected = false;
  154. }
  155. if(pack.shutdownServer)
  156. {
  157. logNetwork->info("Client requested shutdown, server will close itself...");
  158. srv.state = EServerState::SHUTDOWN;
  159. return;
  160. }
  161. else if(srv.connections.empty())
  162. {
  163. logNetwork->error("Last connection lost, server will close itself...");
  164. srv.state = EServerState::SHUTDOWN;
  165. }
  166. else if(pack.c == srv.hostClient)
  167. {
  168. auto ph = std::make_unique<LobbyChangeHost>();
  169. auto newHost = *RandomGeneratorUtil::nextItem(srv.connections, CRandomGenerator::getDefault());
  170. ph->newHostConnectionId = newHost->connectionID;
  171. srv.addToAnnounceQueue(std::move(ph));
  172. }
  173. srv.updateAndPropagateLobbyState();
  174. }
  175. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChatMessage(LobbyChatMessage & pack)
  176. {
  177. result = true;
  178. }
  179. void ApplyOnServerNetPackVisitor::visitLobbySetMap(LobbySetMap & pack)
  180. {
  181. if(srv.state != EServerState::LOBBY)
  182. {
  183. result = false;
  184. return;
  185. }
  186. srv.updateStartInfoOnMapChange(pack.mapInfo, pack.mapGenOpts);
  187. result = true;
  188. }
  189. void ApplyOnServerNetPackVisitor::visitLobbySetCampaign(LobbySetCampaign & pack)
  190. {
  191. srv.si->mapname = pack.ourCampaign->header.filename;
  192. srv.si->mode = StartInfo::CAMPAIGN;
  193. srv.si->campState = pack.ourCampaign;
  194. srv.si->turnTime = 0;
  195. bool isCurrentMapConquerable = pack.ourCampaign->currentMap && pack.ourCampaign->conquerable(*pack.ourCampaign->currentMap);
  196. for(int i = 0; i < pack.ourCampaign->scenarios.size(); i++)
  197. {
  198. auto scenarioID = static_cast<CampaignScenarioID>(i);
  199. if(pack.ourCampaign->conquerable(scenarioID))
  200. {
  201. if(!isCurrentMapConquerable || (isCurrentMapConquerable && scenarioID == *pack.ourCampaign->currentMap))
  202. {
  203. srv.setCampaignMap(scenarioID);
  204. }
  205. }
  206. }
  207. result = true;
  208. }
  209. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignMap(LobbySetCampaignMap & pack)
  210. {
  211. srv.setCampaignMap(pack.mapId);
  212. result = true;
  213. }
  214. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignBonus(LobbySetCampaignBonus & pack)
  215. {
  216. srv.setCampaignBonus(pack.bonusId);
  217. result = true;
  218. }
  219. void ClientPermissionsCheckerNetPackVisitor::visitLobbyGuiAction(LobbyGuiAction & pack)
  220. {
  221. result = srv.isClientHost(pack.c->connectionID);
  222. }
  223. void ClientPermissionsCheckerNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  224. {
  225. result = srv.isClientHost(pack.c->connectionID);
  226. }
  227. void ApplyOnServerNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  228. {
  229. srv.prepareToRestart();
  230. result = true;
  231. }
  232. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  233. {
  234. boost::unique_lock<boost::mutex> stateLock(srv.stateMutex);
  235. for(auto & c : srv.connections)
  236. {
  237. c->enterLobbyConnectionMode();
  238. c->disableStackSendingByID();
  239. }
  240. }
  241. void ClientPermissionsCheckerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  242. {
  243. result = srv.isClientHost(pack.c->connectionID);
  244. }
  245. void ApplyOnServerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  246. {
  247. try
  248. {
  249. srv.verifyStateBeforeStart(true);
  250. }
  251. catch(...)
  252. {
  253. result = false;
  254. return;
  255. }
  256. // Server will prepare gamestate and we announce StartInfo to clients
  257. if(!srv.prepareToStartGame())
  258. {
  259. result = false;
  260. return;
  261. }
  262. pack.initializedStartInfo = std::make_shared<StartInfo>(*srv.gh->getStartInfo(true));
  263. pack.initializedGameState = srv.gh->gameState();
  264. result = true;
  265. }
  266. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  267. {
  268. if(pack.clientId == -1) //do not restart game for single client only
  269. srv.startGameImmidiately();
  270. else
  271. {
  272. for(auto & c : srv.connections)
  273. {
  274. if(c->connectionID == pack.clientId)
  275. {
  276. c->enterGameplayConnectionMode(srv.gh->gameState());
  277. srv.reconnectPlayer(pack.clientId);
  278. }
  279. }
  280. }
  281. }
  282. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  283. {
  284. result = srv.isClientHost(pack.c->connectionID);
  285. }
  286. void ApplyOnServerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  287. {
  288. result = true;
  289. }
  290. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  291. {
  292. auto result = srv.passHost(pack.newHostConnectionId);
  293. if(!result)
  294. {
  295. logGlobal->error("passHost returned false. What does it mean?");
  296. }
  297. }
  298. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  299. {
  300. if(srv.isClientHost(pack.c->connectionID))
  301. {
  302. result = true;
  303. return;
  304. }
  305. if(vstd::contains(srv.getAllClientPlayers(pack.c->connectionID), pack.color))
  306. {
  307. result = true;
  308. return;
  309. }
  310. result = false;
  311. }
  312. void ApplyOnServerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  313. {
  314. switch(pack.what)
  315. {
  316. case LobbyChangePlayerOption::TOWN:
  317. srv.optionNextCastle(pack.color, pack.direction);
  318. break;
  319. case LobbyChangePlayerOption::HERO:
  320. srv.optionNextHero(pack.color, pack.direction);
  321. break;
  322. case LobbyChangePlayerOption::BONUS:
  323. srv.optionNextBonus(pack.color, pack.direction);
  324. break;
  325. }
  326. result = true;
  327. }
  328. void ApplyOnServerNetPackVisitor::visitLobbySetPlayer(LobbySetPlayer & pack)
  329. {
  330. srv.setPlayer(pack.clickedColor);
  331. result = true;
  332. }
  333. void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack)
  334. {
  335. srv.si->turnTime = pack.turnTime;
  336. result = true;
  337. }
  338. void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack)
  339. {
  340. srv.si->difficulty = std::clamp<uint8_t>(pack.difficulty, 0, 4);
  341. result = true;
  342. }
  343. void ApplyOnServerNetPackVisitor::visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack)
  344. {
  345. srv.si->playerInfos[pack.targetPlayerColor].connectedPlayerIDs.insert(pack.targetConnectedPlayer);
  346. result = true;
  347. }