NetPacksLobbyServer.cpp 11 KB

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