NetPacksLobbyServer.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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->camp->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->camp->conquerable(*pack.ourCampaign->currentMap);
  196. for(int i = 0; i < pack.ourCampaign->camp->scenarios.size(); i++)
  197. {
  198. if(pack.ourCampaign->camp->conquerable(i))
  199. {
  200. if(!isCurrentMapConquerable || (isCurrentMapConquerable && i == *pack.ourCampaign->currentMap))
  201. {
  202. srv.setCampaignMap(i);
  203. }
  204. }
  205. }
  206. result = true;
  207. }
  208. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignMap(LobbySetCampaignMap & pack)
  209. {
  210. srv.setCampaignMap(pack.mapId);
  211. result = true;
  212. }
  213. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignBonus(LobbySetCampaignBonus & pack)
  214. {
  215. srv.setCampaignBonus(pack.bonusId);
  216. result = true;
  217. }
  218. void ClientPermissionsCheckerNetPackVisitor::visitLobbyGuiAction(LobbyGuiAction & pack)
  219. {
  220. result = srv.isClientHost(pack.c->connectionID);
  221. }
  222. void ClientPermissionsCheckerNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  223. {
  224. result = srv.isClientHost(pack.c->connectionID);
  225. }
  226. void ApplyOnServerNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  227. {
  228. srv.prepareToRestart();
  229. result = true;
  230. }
  231. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  232. {
  233. boost::unique_lock<boost::mutex> stateLock(srv.stateMutex);
  234. for(auto & c : srv.connections)
  235. {
  236. c->enterLobbyConnectionMode();
  237. c->disableStackSendingByID();
  238. }
  239. }
  240. void ClientPermissionsCheckerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  241. {
  242. result = srv.isClientHost(pack.c->connectionID);
  243. }
  244. void ApplyOnServerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  245. {
  246. try
  247. {
  248. srv.verifyStateBeforeStart(true);
  249. }
  250. catch(...)
  251. {
  252. result = false;
  253. return;
  254. }
  255. // Server will prepare gamestate and we announce StartInfo to clients
  256. if(!srv.prepareToStartGame())
  257. {
  258. result = false;
  259. return;
  260. }
  261. pack.initializedStartInfo = std::make_shared<StartInfo>(*srv.gh->getStartInfo(true));
  262. pack.initializedGameState = srv.gh->gameState();
  263. result = true;
  264. }
  265. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  266. {
  267. if(pack.clientId == -1) //do not restart game for single client only
  268. srv.startGameImmidiately();
  269. else
  270. {
  271. for(auto & c : srv.connections)
  272. {
  273. if(c->connectionID == pack.clientId)
  274. {
  275. c->enterGameplayConnectionMode(srv.gh->gameState());
  276. srv.reconnectPlayer(pack.clientId);
  277. }
  278. }
  279. }
  280. }
  281. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  282. {
  283. result = srv.isClientHost(pack.c->connectionID);
  284. }
  285. void ApplyOnServerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  286. {
  287. result = true;
  288. }
  289. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  290. {
  291. auto result = srv.passHost(pack.newHostConnectionId);
  292. if(!result)
  293. {
  294. logGlobal->error("passHost returned false. What does it mean?");
  295. }
  296. }
  297. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  298. {
  299. if(srv.isClientHost(pack.c->connectionID))
  300. {
  301. result = true;
  302. return;
  303. }
  304. if(vstd::contains(srv.getAllClientPlayers(pack.c->connectionID), pack.color))
  305. {
  306. result = true;
  307. return;
  308. }
  309. result = false;
  310. }
  311. void ApplyOnServerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  312. {
  313. switch(pack.what)
  314. {
  315. case LobbyChangePlayerOption::TOWN:
  316. srv.optionNextCastle(pack.color, pack.direction);
  317. break;
  318. case LobbyChangePlayerOption::HERO:
  319. srv.optionNextHero(pack.color, pack.direction);
  320. break;
  321. case LobbyChangePlayerOption::BONUS:
  322. srv.optionNextBonus(pack.color, pack.direction);
  323. break;
  324. }
  325. result = true;
  326. }
  327. void ApplyOnServerNetPackVisitor::visitLobbySetPlayer(LobbySetPlayer & pack)
  328. {
  329. srv.setPlayer(pack.clickedColor);
  330. result = true;
  331. }
  332. void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack)
  333. {
  334. srv.si->turnTime = pack.turnTime;
  335. result = true;
  336. }
  337. void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack)
  338. {
  339. srv.si->difficulty = vstd::abetween(pack.difficulty, 0, 4);
  340. result = true;
  341. }
  342. void ApplyOnServerNetPackVisitor::visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack)
  343. {
  344. srv.si->playerInfos[pack.targetPlayerColor].connectedPlayerIDs.insert(pack.targetConnectedPlayer);
  345. result = true;
  346. }