NetPacksLobbyServer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/StartInfo.h"
  15. #include "../lib/CRandomGenerator.h"
  16. #include "../lib/campaign/CampaignState.h"
  17. #include "../lib/entities/faction/CTownHandler.h"
  18. #include "../lib/entities/faction/CFaction.h"
  19. #include "../lib/serializer/Connection.h"
  20. #include "../lib/mapping/CMapInfo.h"
  21. #include "../lib/mapping/CMapHeader.h"
  22. void ClientPermissionsCheckerNetPackVisitor::visitForLobby(CPackForLobby & pack)
  23. {
  24. if(pack.isForServer())
  25. {
  26. result = srv.isClientHost(pack.c->connectionID);
  27. }
  28. }
  29. void ApplyOnServerAfterAnnounceNetPackVisitor::visitForLobby(CPackForLobby & pack)
  30. {
  31. // Propagate options after every CLobbyPackToServer
  32. if(pack.isForServer())
  33. {
  34. srv.updateAndPropagateLobbyState();
  35. }
  36. }
  37. void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  38. {
  39. result = srv.getState() == EServerState::LOBBY;
  40. }
  41. void ApplyOnServerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  42. {
  43. auto compatibleVersion = std::min(pack.version, ESerializationVersion::CURRENT);
  44. pack.c->setSerializationVersion(compatibleVersion);
  45. srv.clientConnected(pack.c, pack.names, pack.uuid, pack.mode);
  46. // Server need to pass some data to newly connected client
  47. pack.clientId = pack.c->connectionID;
  48. pack.mode = srv.si->mode;
  49. pack.hostClientId = srv.hostClientId;
  50. pack.version = compatibleVersion;
  51. result = true;
  52. }
  53. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  54. {
  55. // FIXME: we need to avoid senting something to client that not yet get answer for LobbyClientConnected
  56. // Until UUID set we only pass LobbyClientConnected to this client
  57. pack.c->uuid = pack.uuid;
  58. srv.updateAndPropagateLobbyState();
  59. // FIXME: what is this??? We do NOT support reconnection into ongoing game - at the very least queries and battles are NOT serialized
  60. // if(srv.getState() == EServerState::GAMEPLAY)
  61. // {
  62. // //immediately start game
  63. // std::unique_ptr<LobbyStartGame> startGameForReconnectedPlayer(new LobbyStartGame);
  64. // startGameForReconnectedPlayer->initializedStartInfo = srv.si;
  65. // startGameForReconnectedPlayer->initializedGameState = srv.gh->gameState();
  66. // startGameForReconnectedPlayer->clientId = pack.c->connectionID;
  67. // srv.announcePack(std::move(startGameForReconnectedPlayer));
  68. // }
  69. }
  70. void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  71. {
  72. if(pack.clientId != pack.c->connectionID)
  73. {
  74. result = false;
  75. return;
  76. }
  77. if(pack.shutdownServer)
  78. {
  79. if(!srv.wasStartedByClient())
  80. {
  81. result = false;
  82. return;
  83. }
  84. if(pack.c->connectionID != srv.hostClientId)
  85. {
  86. result = false;
  87. return;
  88. }
  89. }
  90. result = true;
  91. }
  92. void ApplyOnServerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  93. {
  94. pack.c->getConnection()->close();
  95. srv.clientDisconnected(pack.c);
  96. result = true;
  97. }
  98. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  99. {
  100. if(pack.shutdownServer)
  101. {
  102. logNetwork->info("Client requested shutdown, server will close itself...");
  103. srv.setState(EServerState::SHUTDOWN);
  104. return;
  105. }
  106. else if(srv.activeConnections.empty())
  107. {
  108. logNetwork->error("Last connection lost, server will close itself...");
  109. srv.setState(EServerState::SHUTDOWN);
  110. }
  111. else if(pack.c->connectionID == srv.hostClientId)
  112. {
  113. LobbyChangeHost ph;
  114. auto newHost = srv.activeConnections.front();
  115. ph.newHostConnectionId = newHost->connectionID;
  116. srv.announcePack(ph);
  117. }
  118. srv.updateAndPropagateLobbyState();
  119. // if(srv.getState() != EServerState::SHUTDOWN && srv.remoteConnections.count(pack.c))
  120. // {
  121. // srv.remoteConnections -= pack.c;
  122. // srv.connectToRemote();
  123. // }
  124. }
  125. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChatMessage(LobbyChatMessage & pack)
  126. {
  127. result = true;
  128. }
  129. void ApplyOnServerNetPackVisitor::visitLobbySetMap(LobbySetMap & pack)
  130. {
  131. if(srv.getState() != EServerState::LOBBY)
  132. {
  133. result = false;
  134. return;
  135. }
  136. srv.updateStartInfoOnMapChange(pack.mapInfo, pack.mapGenOpts);
  137. result = true;
  138. }
  139. void ApplyOnServerNetPackVisitor::visitLobbySetCampaign(LobbySetCampaign & pack)
  140. {
  141. srv.si->mapname = pack.ourCampaign->getFilename();
  142. srv.si->mode = EStartMode::CAMPAIGN;
  143. srv.si->campState = pack.ourCampaign;
  144. srv.si->turnTimerInfo = TurnTimerInfo{};
  145. bool isCurrentMapConquerable = pack.ourCampaign->currentScenario() && pack.ourCampaign->isAvailable(*pack.ourCampaign->currentScenario());
  146. for(auto scenarioID : pack.ourCampaign->allScenarios())
  147. {
  148. if(pack.ourCampaign->isAvailable(scenarioID))
  149. {
  150. if(!isCurrentMapConquerable || (isCurrentMapConquerable && scenarioID == *pack.ourCampaign->currentScenario()))
  151. {
  152. srv.setCampaignMap(scenarioID);
  153. }
  154. }
  155. }
  156. result = true;
  157. }
  158. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignMap(LobbySetCampaignMap & pack)
  159. {
  160. srv.setCampaignMap(pack.mapId);
  161. result = true;
  162. }
  163. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignBonus(LobbySetCampaignBonus & pack)
  164. {
  165. srv.setCampaignBonus(pack.bonusId);
  166. result = true;
  167. }
  168. void ClientPermissionsCheckerNetPackVisitor::visitLobbyGuiAction(LobbyGuiAction & pack)
  169. {
  170. result = srv.isClientHost(pack.c->connectionID);
  171. }
  172. void ClientPermissionsCheckerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
  173. {
  174. result = srv.isClientHost(pack.c->connectionID);
  175. }
  176. void ApplyOnServerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
  177. {
  178. srv.prepareToRestart();
  179. result = true;
  180. }
  181. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
  182. {
  183. for(const auto & connection : srv.activeConnections)
  184. connection->enterLobbyConnectionMode();
  185. }
  186. void ClientPermissionsCheckerNetPackVisitor::visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack)
  187. {
  188. result = srv.isClientHost(pack.c->connectionID);
  189. }
  190. void ClientPermissionsCheckerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  191. {
  192. result = srv.isClientHost(pack.c->connectionID);
  193. }
  194. void ApplyOnServerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  195. {
  196. try
  197. {
  198. srv.verifyStateBeforeStart(true);
  199. }
  200. catch(...)
  201. {
  202. result = false;
  203. return;
  204. }
  205. // Server will prepare gamestate and we announce StartInfo to clients
  206. if(!srv.prepareToStartGame())
  207. {
  208. result = false;
  209. return;
  210. }
  211. pack.initializedStartInfo = std::make_shared<StartInfo>(*srv.gh->getStartInfo(true));
  212. pack.initializedGameState = srv.gh->gameState();
  213. result = true;
  214. }
  215. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  216. {
  217. if(pack.clientId == -1) //do not restart game for single client only
  218. srv.startGameImmediately();
  219. else
  220. {
  221. for(const auto & connection : srv.activeConnections)
  222. {
  223. if(connection->connectionID == pack.clientId)
  224. {
  225. connection->enterGameplayConnectionMode(srv.gh->gameState());
  226. srv.reconnectPlayer(pack.clientId);
  227. }
  228. }
  229. }
  230. }
  231. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  232. {
  233. result = srv.isClientHost(pack.c->connectionID);
  234. }
  235. void ApplyOnServerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  236. {
  237. result = true;
  238. }
  239. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  240. {
  241. auto result = srv.passHost(pack.newHostConnectionId);
  242. if(!result)
  243. {
  244. logGlobal->error("passHost returned false. What does it mean?");
  245. }
  246. }
  247. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  248. {
  249. if(srv.isClientHost(pack.c->connectionID))
  250. {
  251. result = true;
  252. return;
  253. }
  254. if(vstd::contains(srv.getAllClientPlayers(pack.c->connectionID), pack.color))
  255. {
  256. result = true;
  257. return;
  258. }
  259. result = false;
  260. }
  261. void ApplyOnServerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  262. {
  263. switch(pack.what)
  264. {
  265. case LobbyChangePlayerOption::TOWN_ID:
  266. srv.optionSetCastle(pack.color, FactionID(pack.value));
  267. break;
  268. case LobbyChangePlayerOption::TOWN:
  269. srv.optionNextCastle(pack.color, pack.value);
  270. break;
  271. case LobbyChangePlayerOption::HERO_ID:
  272. srv.optionSetHero(pack.color, HeroTypeID(pack.value));
  273. break;
  274. case LobbyChangePlayerOption::HERO:
  275. srv.optionNextHero(pack.color, pack.value);
  276. break;
  277. case LobbyChangePlayerOption::BONUS_ID:
  278. srv.optionSetBonus(pack.color, PlayerStartingBonus(pack.value));
  279. break;
  280. case LobbyChangePlayerOption::BONUS:
  281. srv.optionNextBonus(pack.color, pack.value);
  282. break;
  283. }
  284. result = true;
  285. }
  286. void ApplyOnServerNetPackVisitor::visitLobbySetPlayer(LobbySetPlayer & pack)
  287. {
  288. srv.setPlayer(pack.clickedColor);
  289. result = true;
  290. }
  291. void ApplyOnServerNetPackVisitor::visitLobbySetPlayerName(LobbySetPlayerName & pack)
  292. {
  293. srv.setPlayerName(pack.color, pack.name);
  294. result = true;
  295. }
  296. void ApplyOnServerNetPackVisitor::visitLobbySetPlayerHandicap(LobbySetPlayerHandicap & pack)
  297. {
  298. srv.setPlayerHandicap(pack.color, pack.handicap);
  299. result = true;
  300. }
  301. void ApplyOnServerNetPackVisitor::visitLobbySetSimturns(LobbySetSimturns & pack)
  302. {
  303. srv.si->simturnsInfo = pack.simturnsInfo;
  304. result = true;
  305. }
  306. void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack)
  307. {
  308. srv.si->turnTimerInfo = pack.turnTimerInfo;
  309. result = true;
  310. }
  311. void ApplyOnServerNetPackVisitor::visitLobbySetExtraOptions(LobbySetExtraOptions & pack)
  312. {
  313. srv.si->extraOptionsInfo = pack.extraOptionsInfo;
  314. result = true;
  315. }
  316. void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack)
  317. {
  318. srv.si->difficulty = std::clamp<uint8_t>(pack.difficulty, 0, 4);
  319. result = true;
  320. }
  321. void ApplyOnServerNetPackVisitor::visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack)
  322. {
  323. srv.si->playerInfos[pack.targetPlayerColor].connectedPlayerIDs.insert(pack.targetConnectedPlayer);
  324. result = true;
  325. }
  326. void ClientPermissionsCheckerNetPackVisitor::visitLobbyPvPAction(LobbyPvPAction & pack)
  327. {
  328. result = true;
  329. }
  330. void ApplyOnServerNetPackVisitor::visitLobbyPvPAction(LobbyPvPAction & pack)
  331. {
  332. std::vector<FactionID> allowedTowns;
  333. for (auto const & factionID : VLC->townh->getDefaultAllowed())
  334. if(std::find(pack.bannedTowns.begin(), pack.bannedTowns.end(), factionID) == pack.bannedTowns.end())
  335. allowedTowns.push_back(factionID);
  336. std::vector<FactionID> randomFaction1;
  337. std::sample(allowedTowns.begin(), allowedTowns.end(), std::back_inserter(randomFaction1), 1, std::mt19937{std::random_device{}()});
  338. std::vector<FactionID> randomFaction2;
  339. std::sample(allowedTowns.begin(), allowedTowns.end(), std::back_inserter(randomFaction2), 1, std::mt19937{std::random_device{}()});
  340. MetaString txt;
  341. switch(pack.action) {
  342. case LobbyPvPAction::COIN:
  343. txt.appendTextID("vcmi.lobby.pvp.coin.hover");
  344. txt.appendRawString(" - " + std::to_string(std::rand()%2));
  345. srv.announceTxt(txt);
  346. break;
  347. case LobbyPvPAction::RANDOM_TOWN:
  348. if(!allowedTowns.size())
  349. break;
  350. txt.appendTextID("core.overview.3");
  351. txt.appendRawString(" - ");
  352. txt.appendTextID(VLC->townh->getById(randomFaction1[0])->getNameTextID());
  353. srv.announceTxt(txt);
  354. break;
  355. case LobbyPvPAction::RANDOM_TOWN_VS:
  356. if(!allowedTowns.size())
  357. break;
  358. txt.appendTextID("core.overview.3");
  359. txt.appendRawString(" - ");
  360. txt.appendTextID(VLC->townh->getById(randomFaction1[0])->getNameTextID());
  361. txt.appendRawString(" ");
  362. txt.appendTextID("vcmi.lobby.pvp.versus");
  363. txt.appendRawString(" ");
  364. txt.appendTextID(VLC->townh->getById(randomFaction2[0])->getNameTextID());
  365. srv.announceTxt(txt);
  366. break;
  367. }
  368. result = true;
  369. }