NetPacksLobbyServer.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. // Server will prepare gamestate and we announce StartInfo to clients
  254. if(!srv.prepareToStartGame())
  255. {
  256. result = false;
  257. return;
  258. }
  259. pack.initializedStartInfo = std::make_shared<StartInfo>(*srv.gh->getStartInfo(true));
  260. pack.initializedGameState = srv.gh->gameState();
  261. result = true;
  262. }
  263. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  264. {
  265. if(pack.clientId == -1) //do not restart game for single client only
  266. srv.startGameImmidiately();
  267. else
  268. {
  269. for(auto & c : srv.connections)
  270. {
  271. if(c->connectionID == pack.clientId)
  272. {
  273. c->enterGameplayConnectionMode(srv.gh->gameState());
  274. srv.reconnectPlayer(pack.clientId);
  275. }
  276. }
  277. }
  278. }
  279. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  280. {
  281. result = srv.isClientHost(pack.c->connectionID);
  282. }
  283. void ApplyOnServerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  284. {
  285. result = true;
  286. }
  287. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  288. {
  289. auto result = srv.passHost(pack.newHostConnectionId);
  290. if(!result)
  291. {
  292. logGlobal->error("passHost returned false. What does it mean?");
  293. }
  294. }
  295. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  296. {
  297. if(srv.isClientHost(pack.c->connectionID))
  298. {
  299. result = true;
  300. return;
  301. }
  302. if(vstd::contains(srv.getAllClientPlayers(pack.c->connectionID), pack.color))
  303. {
  304. result = true;
  305. return;
  306. }
  307. result = false;
  308. }
  309. void ApplyOnServerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  310. {
  311. switch(pack.what)
  312. {
  313. case LobbyChangePlayerOption::TOWN_ID:
  314. srv.optionSetCastle(pack.color, pack.value);
  315. break;
  316. case LobbyChangePlayerOption::TOWN:
  317. srv.optionNextCastle(pack.color, pack.value);
  318. break;
  319. case LobbyChangePlayerOption::HERO_ID:
  320. srv.optionSetHero(pack.color, pack.value);
  321. break;
  322. case LobbyChangePlayerOption::HERO:
  323. srv.optionNextHero(pack.color, pack.value);
  324. break;
  325. case LobbyChangePlayerOption::BONUS_ID:
  326. srv.optionSetBonus(pack.color, pack.value);
  327. break;
  328. case LobbyChangePlayerOption::BONUS:
  329. srv.optionNextBonus(pack.color, pack.value);
  330. break;
  331. }
  332. result = true;
  333. }
  334. void ApplyOnServerNetPackVisitor::visitLobbySetPlayer(LobbySetPlayer & pack)
  335. {
  336. srv.setPlayer(pack.clickedColor);
  337. result = true;
  338. }
  339. void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack)
  340. {
  341. srv.si->turnTime = pack.turnTime;
  342. result = true;
  343. }
  344. void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack)
  345. {
  346. srv.si->difficulty = std::clamp<uint8_t>(pack.difficulty, 0, 4);
  347. result = true;
  348. }
  349. void ApplyOnServerNetPackVisitor::visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack)
  350. {
  351. srv.si->playerInfos[pack.targetPlayerColor].connectedPlayerIDs.insert(pack.targetConnectedPlayer);
  352. result = true;
  353. }