NetPacksLobbyServer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. srv.clientDisconnected(pack.c);
  95. result = true;
  96. }
  97. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  98. {
  99. if(pack.shutdownServer)
  100. {
  101. logNetwork->info("Client requested shutdown, server will close itself...");
  102. srv.setState(EServerState::SHUTDOWN);
  103. return;
  104. }
  105. else if(srv.activeConnections.empty())
  106. {
  107. logNetwork->error("Last connection lost, server will close itself...");
  108. srv.setState(EServerState::SHUTDOWN);
  109. }
  110. else if(pack.c->connectionID == srv.hostClientId)
  111. {
  112. auto ph = std::make_unique<LobbyChangeHost>();
  113. auto newHost = srv.activeConnections.front();
  114. ph->newHostConnectionId = newHost->connectionID;
  115. srv.announcePack(std::move(ph));
  116. }
  117. srv.updateAndPropagateLobbyState();
  118. // if(srv.getState() != EServerState::SHUTDOWN && srv.remoteConnections.count(pack.c))
  119. // {
  120. // srv.remoteConnections -= pack.c;
  121. // srv.connectToRemote();
  122. // }
  123. }
  124. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChatMessage(LobbyChatMessage & pack)
  125. {
  126. result = true;
  127. }
  128. void ApplyOnServerNetPackVisitor::visitLobbySetMap(LobbySetMap & pack)
  129. {
  130. if(srv.getState() != EServerState::LOBBY)
  131. {
  132. result = false;
  133. return;
  134. }
  135. srv.updateStartInfoOnMapChange(pack.mapInfo, pack.mapGenOpts);
  136. result = true;
  137. }
  138. void ApplyOnServerNetPackVisitor::visitLobbySetCampaign(LobbySetCampaign & pack)
  139. {
  140. srv.si->mapname = pack.ourCampaign->getFilename();
  141. srv.si->mode = EStartMode::CAMPAIGN;
  142. srv.si->campState = pack.ourCampaign;
  143. srv.si->turnTimerInfo = TurnTimerInfo{};
  144. bool isCurrentMapConquerable = pack.ourCampaign->currentScenario() && pack.ourCampaign->isAvailable(*pack.ourCampaign->currentScenario());
  145. for(auto scenarioID : pack.ourCampaign->allScenarios())
  146. {
  147. if(pack.ourCampaign->isAvailable(scenarioID))
  148. {
  149. if(!isCurrentMapConquerable || (isCurrentMapConquerable && scenarioID == *pack.ourCampaign->currentScenario()))
  150. {
  151. srv.setCampaignMap(scenarioID);
  152. }
  153. }
  154. }
  155. result = true;
  156. }
  157. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignMap(LobbySetCampaignMap & pack)
  158. {
  159. srv.setCampaignMap(pack.mapId);
  160. result = true;
  161. }
  162. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignBonus(LobbySetCampaignBonus & pack)
  163. {
  164. srv.setCampaignBonus(pack.bonusId);
  165. result = true;
  166. }
  167. void ClientPermissionsCheckerNetPackVisitor::visitLobbyGuiAction(LobbyGuiAction & pack)
  168. {
  169. result = srv.isClientHost(pack.c->connectionID);
  170. }
  171. void ClientPermissionsCheckerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
  172. {
  173. result = srv.isClientHost(pack.c->connectionID);
  174. }
  175. void ApplyOnServerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
  176. {
  177. srv.prepareToRestart();
  178. result = true;
  179. }
  180. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
  181. {
  182. for(const auto & connection : srv.activeConnections)
  183. connection->enterLobbyConnectionMode();
  184. }
  185. void ClientPermissionsCheckerNetPackVisitor::visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack)
  186. {
  187. result = srv.isClientHost(pack.c->connectionID);
  188. }
  189. void ClientPermissionsCheckerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  190. {
  191. result = srv.isClientHost(pack.c->connectionID);
  192. }
  193. void ApplyOnServerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  194. {
  195. try
  196. {
  197. srv.verifyStateBeforeStart(true);
  198. }
  199. catch(...)
  200. {
  201. result = false;
  202. return;
  203. }
  204. // Server will prepare gamestate and we announce StartInfo to clients
  205. if(!srv.prepareToStartGame())
  206. {
  207. result = false;
  208. return;
  209. }
  210. pack.initializedStartInfo = std::make_shared<StartInfo>(*srv.gh->getStartInfo(true));
  211. pack.initializedGameState = srv.gh->gameState();
  212. result = true;
  213. }
  214. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  215. {
  216. if(pack.clientId == -1) //do not restart game for single client only
  217. srv.startGameImmediately();
  218. else
  219. {
  220. for(const auto & connection : srv.activeConnections)
  221. {
  222. if(connection->connectionID == pack.clientId)
  223. {
  224. connection->enterGameplayConnectionMode(srv.gh->gameState());
  225. srv.reconnectPlayer(pack.clientId);
  226. }
  227. }
  228. }
  229. }
  230. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  231. {
  232. result = srv.isClientHost(pack.c->connectionID);
  233. }
  234. void ApplyOnServerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  235. {
  236. result = true;
  237. }
  238. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  239. {
  240. auto result = srv.passHost(pack.newHostConnectionId);
  241. if(!result)
  242. {
  243. logGlobal->error("passHost returned false. What does it mean?");
  244. }
  245. }
  246. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  247. {
  248. if(srv.isClientHost(pack.c->connectionID))
  249. {
  250. result = true;
  251. return;
  252. }
  253. if(vstd::contains(srv.getAllClientPlayers(pack.c->connectionID), pack.color))
  254. {
  255. result = true;
  256. return;
  257. }
  258. result = false;
  259. }
  260. void ApplyOnServerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  261. {
  262. switch(pack.what)
  263. {
  264. case LobbyChangePlayerOption::TOWN_ID:
  265. srv.optionSetCastle(pack.color, FactionID(pack.value));
  266. break;
  267. case LobbyChangePlayerOption::TOWN:
  268. srv.optionNextCastle(pack.color, pack.value);
  269. break;
  270. case LobbyChangePlayerOption::HERO_ID:
  271. srv.optionSetHero(pack.color, HeroTypeID(pack.value));
  272. break;
  273. case LobbyChangePlayerOption::HERO:
  274. srv.optionNextHero(pack.color, pack.value);
  275. break;
  276. case LobbyChangePlayerOption::BONUS_ID:
  277. srv.optionSetBonus(pack.color, PlayerStartingBonus(pack.value));
  278. break;
  279. case LobbyChangePlayerOption::BONUS:
  280. srv.optionNextBonus(pack.color, pack.value);
  281. break;
  282. }
  283. result = true;
  284. }
  285. void ApplyOnServerNetPackVisitor::visitLobbySetPlayer(LobbySetPlayer & pack)
  286. {
  287. srv.setPlayer(pack.clickedColor);
  288. result = true;
  289. }
  290. void ApplyOnServerNetPackVisitor::visitLobbySetPlayerName(LobbySetPlayerName & pack)
  291. {
  292. srv.setPlayerName(pack.color, pack.name);
  293. result = true;
  294. }
  295. void ApplyOnServerNetPackVisitor::visitLobbySetPlayerHandicap(LobbySetPlayerHandicap & pack)
  296. {
  297. srv.setPlayerHandicap(pack.color, pack.handicap);
  298. result = true;
  299. }
  300. void ApplyOnServerNetPackVisitor::visitLobbySetSimturns(LobbySetSimturns & pack)
  301. {
  302. srv.si->simturnsInfo = pack.simturnsInfo;
  303. result = true;
  304. }
  305. void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack)
  306. {
  307. srv.si->turnTimerInfo = pack.turnTimerInfo;
  308. result = true;
  309. }
  310. void ApplyOnServerNetPackVisitor::visitLobbySetExtraOptions(LobbySetExtraOptions & pack)
  311. {
  312. srv.si->extraOptionsInfo = pack.extraOptionsInfo;
  313. result = true;
  314. }
  315. void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack)
  316. {
  317. srv.si->difficulty = std::clamp<uint8_t>(pack.difficulty, 0, 4);
  318. result = true;
  319. }
  320. void ApplyOnServerNetPackVisitor::visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack)
  321. {
  322. srv.si->playerInfos[pack.targetPlayerColor].connectedPlayerIDs.insert(pack.targetConnectedPlayer);
  323. result = true;
  324. }
  325. void ClientPermissionsCheckerNetPackVisitor::visitLobbyPvPAction(LobbyPvPAction & pack)
  326. {
  327. result = true;
  328. }
  329. void ApplyOnServerNetPackVisitor::visitLobbyPvPAction(LobbyPvPAction & pack)
  330. {
  331. std::vector<FactionID> allowedTowns;
  332. for (auto const & factionID : VLC->townh->getDefaultAllowed())
  333. if(std::find(pack.bannedTowns.begin(), pack.bannedTowns.end(), factionID) == pack.bannedTowns.end())
  334. allowedTowns.push_back(factionID);
  335. std::vector<FactionID> randomFaction1;
  336. std::sample(allowedTowns.begin(), allowedTowns.end(), std::back_inserter(randomFaction1), 1, std::mt19937{std::random_device{}()});
  337. std::vector<FactionID> randomFaction2;
  338. std::sample(allowedTowns.begin(), allowedTowns.end(), std::back_inserter(randomFaction2), 1, std::mt19937{std::random_device{}()});
  339. MetaString txt;
  340. switch(pack.action) {
  341. case LobbyPvPAction::COIN:
  342. txt.appendTextID("vcmi.lobby.pvp.coin.hover");
  343. txt.appendRawString(" - " + std::to_string(std::rand()%2));
  344. srv.announceTxt(txt);
  345. break;
  346. case LobbyPvPAction::RANDOM_TOWN:
  347. if(!allowedTowns.size())
  348. break;
  349. txt.appendTextID("core.overview.3");
  350. txt.appendRawString(" - ");
  351. txt.appendTextID(VLC->townh->getById(randomFaction1[0])->getNameTextID());
  352. srv.announceTxt(txt);
  353. break;
  354. case LobbyPvPAction::RANDOM_TOWN_VS:
  355. if(!allowedTowns.size())
  356. break;
  357. txt.appendTextID("core.overview.3");
  358. txt.appendRawString(" - ");
  359. txt.appendTextID(VLC->townh->getById(randomFaction1[0])->getNameTextID());
  360. txt.appendRawString(" ");
  361. txt.appendTextID("vcmi.lobby.pvp.versus");
  362. txt.appendRawString(" ");
  363. txt.appendTextID(VLC->townh->getById(randomFaction2[0])->getNameTextID());
  364. srv.announceTxt(txt);
  365. break;
  366. }
  367. result = true;
  368. }