NetPacksLobbyServer.cpp 10 KB

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