2
0

NetPacksLobbyServer.cpp 12 KB

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