NetPacksLobbyServer.cpp 10 KB

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