NetPacksLobbyServer.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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.state == 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.state == 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.state = EServerState::SHUTDOWN;
  157. return;
  158. }
  159. else if(srv.connections.empty())
  160. {
  161. logNetwork->error("Last connection lost, server will close itself...");
  162. srv.state = 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. }
  173. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChatMessage(LobbyChatMessage & pack)
  174. {
  175. result = true;
  176. }
  177. void ApplyOnServerNetPackVisitor::visitLobbySetMap(LobbySetMap & pack)
  178. {
  179. if(srv.state != EServerState::LOBBY)
  180. {
  181. result = false;
  182. return;
  183. }
  184. srv.updateStartInfoOnMapChange(pack.mapInfo, pack.mapGenOpts);
  185. result = true;
  186. }
  187. void ApplyOnServerNetPackVisitor::visitLobbySetCampaign(LobbySetCampaign & pack)
  188. {
  189. srv.si->mapname = pack.ourCampaign->getFilename();
  190. srv.si->mode = StartInfo::CAMPAIGN;
  191. srv.si->campState = pack.ourCampaign;
  192. srv.si->turnTime = 0;
  193. bool isCurrentMapConquerable = pack.ourCampaign->currentScenario() && pack.ourCampaign->isAvailable(*pack.ourCampaign->currentScenario());
  194. for(auto scenarioID : pack.ourCampaign->allScenarios())
  195. {
  196. if(pack.ourCampaign->isAvailable(scenarioID))
  197. {
  198. if(!isCurrentMapConquerable || (isCurrentMapConquerable && scenarioID == *pack.ourCampaign->currentScenario()))
  199. {
  200. srv.setCampaignMap(scenarioID);
  201. }
  202. }
  203. }
  204. result = true;
  205. }
  206. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignMap(LobbySetCampaignMap & pack)
  207. {
  208. srv.setCampaignMap(pack.mapId);
  209. result = true;
  210. }
  211. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignBonus(LobbySetCampaignBonus & pack)
  212. {
  213. srv.setCampaignBonus(pack.bonusId);
  214. result = true;
  215. }
  216. void ClientPermissionsCheckerNetPackVisitor::visitLobbyGuiAction(LobbyGuiAction & pack)
  217. {
  218. result = srv.isClientHost(pack.c->connectionID);
  219. }
  220. void ClientPermissionsCheckerNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  221. {
  222. result = srv.isClientHost(pack.c->connectionID);
  223. }
  224. void ApplyOnServerNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  225. {
  226. srv.prepareToRestart();
  227. result = true;
  228. }
  229. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  230. {
  231. boost::unique_lock<boost::mutex> stateLock(srv.stateMutex);
  232. for(auto & c : srv.connections)
  233. {
  234. c->enterLobbyConnectionMode();
  235. c->disableStackSendingByID();
  236. }
  237. }
  238. void ClientPermissionsCheckerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  239. {
  240. result = srv.isClientHost(pack.c->connectionID);
  241. }
  242. void ApplyOnServerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  243. {
  244. try
  245. {
  246. srv.verifyStateBeforeStart(true);
  247. }
  248. catch(...)
  249. {
  250. result = false;
  251. return;
  252. }
  253. // Announce loading
  254. std::unique_ptr<LobbyLoadProgress> loadProgress(new LobbyLoadProgress);
  255. srv.addToAnnounceQueue(std::move(loadProgress));
  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_ID:
  317. srv.optionSetCastle(pack.color, pack.value);
  318. break;
  319. case LobbyChangePlayerOption::TOWN:
  320. srv.optionNextCastle(pack.color, pack.value);
  321. break;
  322. case LobbyChangePlayerOption::HERO_ID:
  323. srv.optionSetHero(pack.color, pack.value);
  324. break;
  325. case LobbyChangePlayerOption::HERO:
  326. srv.optionNextHero(pack.color, pack.value);
  327. break;
  328. case LobbyChangePlayerOption::BONUS_ID:
  329. srv.optionSetBonus(pack.color, pack.value);
  330. break;
  331. case LobbyChangePlayerOption::BONUS:
  332. srv.optionNextBonus(pack.color, pack.value);
  333. break;
  334. }
  335. result = true;
  336. }
  337. void ApplyOnServerNetPackVisitor::visitLobbySetPlayer(LobbySetPlayer & pack)
  338. {
  339. srv.setPlayer(pack.clickedColor);
  340. result = true;
  341. }
  342. void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack)
  343. {
  344. srv.si->turnTime = pack.turnTime;
  345. result = true;
  346. }
  347. void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack)
  348. {
  349. srv.si->difficulty = std::clamp<uint8_t>(pack.difficulty, 0, 4);
  350. result = true;
  351. }
  352. void ApplyOnServerNetPackVisitor::visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack)
  353. {
  354. srv.si->playerInfos[pack.targetPlayerColor].connectedPlayerIDs.insert(pack.targetConnectedPlayer);
  355. result = true;
  356. }