NetPacksLobbyServer.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/campaign/CampaignState.h"
  16. #include "../lib/serializer/Connection.h"
  17. void ClientPermissionsCheckerNetPackVisitor::visitForLobby(CPackForLobby & pack)
  18. {
  19. if(pack.isForServer())
  20. {
  21. result = srv.isClientHost(pack.c->connectionID);
  22. }
  23. }
  24. void ApplyOnServerAfterAnnounceNetPackVisitor::visitForLobby(CPackForLobby & pack)
  25. {
  26. // Propogate options after every CLobbyPackToServer
  27. if(pack.isForServer())
  28. {
  29. srv.updateAndPropagateLobbyState();
  30. }
  31. }
  32. void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  33. {
  34. if(srv.getState() == EServerState::LOBBY)
  35. {
  36. result = true;
  37. return;
  38. }
  39. result = false;
  40. return;
  41. }
  42. void ApplyOnServerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  43. {
  44. srv.clientConnected(pack.c, pack.names, pack.uuid, pack.mode);
  45. // Server need to pass some data to newly connected client
  46. pack.clientId = pack.c->connectionID;
  47. pack.mode = srv.si->mode;
  48. pack.hostClientId = srv.hostClientId;
  49. result = true;
  50. }
  51. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  52. {
  53. // FIXME: we need to avoid senting something to client that not yet get answer for LobbyClientConnected
  54. // Until UUID set we only pass LobbyClientConnected to this client
  55. pack.c->uuid = pack.uuid;
  56. srv.updateAndPropagateLobbyState();
  57. if(srv.getState() == EServerState::GAMEPLAY)
  58. {
  59. //immediately start game
  60. std::unique_ptr<LobbyStartGame> startGameForReconnectedPlayer(new LobbyStartGame);
  61. startGameForReconnectedPlayer->initializedStartInfo = srv.si;
  62. startGameForReconnectedPlayer->initializedGameState = srv.gh->gameState();
  63. startGameForReconnectedPlayer->clientId = pack.c->connectionID;
  64. srv.announcePack(std::move(startGameForReconnectedPlayer));
  65. }
  66. }
  67. void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  68. {
  69. if(pack.clientId != pack.c->connectionID)
  70. {
  71. result = false;
  72. return;
  73. }
  74. if(pack.shutdownServer)
  75. {
  76. if(!srv.cmdLineOptions.count("run-by-client"))
  77. {
  78. result = false;
  79. return;
  80. }
  81. if(pack.c->uuid != srv.cmdLineOptions["uuid"].as<std::string>())
  82. {
  83. result = false;
  84. return;
  85. }
  86. }
  87. result = true;
  88. }
  89. void ApplyOnServerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  90. {
  91. srv.clientDisconnected(pack.c);
  92. result = true;
  93. }
  94. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  95. {
  96. if(pack.shutdownServer)
  97. {
  98. logNetwork->info("Client requested shutdown, server will close itself...");
  99. srv.setState(EServerState::SHUTDOWN);
  100. return;
  101. }
  102. else if(srv.activeConnections.empty())
  103. {
  104. logNetwork->error("Last connection lost, server will close itself...");
  105. srv.setState(EServerState::SHUTDOWN);
  106. }
  107. else if(pack.c->connectionID == srv.hostClientId)
  108. {
  109. auto ph = std::make_unique<LobbyChangeHost>();
  110. auto newHost = srv.activeConnections.front();
  111. ph->newHostConnectionId = newHost->connectionID;
  112. srv.announcePack(std::move(ph));
  113. }
  114. srv.updateAndPropagateLobbyState();
  115. // if(srv.getState() != EServerState::SHUTDOWN && srv.remoteConnections.count(pack.c))
  116. // {
  117. // srv.remoteConnections -= pack.c;
  118. // srv.connectToRemote();
  119. // }
  120. }
  121. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChatMessage(LobbyChatMessage & pack)
  122. {
  123. result = true;
  124. }
  125. void ApplyOnServerNetPackVisitor::visitLobbySetMap(LobbySetMap & pack)
  126. {
  127. if(srv.getState() != EServerState::LOBBY)
  128. {
  129. result = false;
  130. return;
  131. }
  132. srv.updateStartInfoOnMapChange(pack.mapInfo, pack.mapGenOpts);
  133. result = true;
  134. }
  135. void ApplyOnServerNetPackVisitor::visitLobbySetCampaign(LobbySetCampaign & pack)
  136. {
  137. srv.si->mapname = pack.ourCampaign->getFilename();
  138. srv.si->mode = StartInfo::CAMPAIGN;
  139. srv.si->campState = pack.ourCampaign;
  140. srv.si->turnTimerInfo = TurnTimerInfo{};
  141. bool isCurrentMapConquerable = pack.ourCampaign->currentScenario() && pack.ourCampaign->isAvailable(*pack.ourCampaign->currentScenario());
  142. for(auto scenarioID : pack.ourCampaign->allScenarios())
  143. {
  144. if(pack.ourCampaign->isAvailable(scenarioID))
  145. {
  146. if(!isCurrentMapConquerable || (isCurrentMapConquerable && scenarioID == *pack.ourCampaign->currentScenario()))
  147. {
  148. srv.setCampaignMap(scenarioID);
  149. }
  150. }
  151. }
  152. result = true;
  153. }
  154. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignMap(LobbySetCampaignMap & pack)
  155. {
  156. srv.setCampaignMap(pack.mapId);
  157. result = true;
  158. }
  159. void ApplyOnServerNetPackVisitor::visitLobbySetCampaignBonus(LobbySetCampaignBonus & pack)
  160. {
  161. srv.setCampaignBonus(pack.bonusId);
  162. result = true;
  163. }
  164. void ClientPermissionsCheckerNetPackVisitor::visitLobbyGuiAction(LobbyGuiAction & pack)
  165. {
  166. result = srv.isClientHost(pack.c->connectionID);
  167. }
  168. void ClientPermissionsCheckerNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  169. {
  170. result = srv.isClientHost(pack.c->connectionID);
  171. }
  172. void ApplyOnServerNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  173. {
  174. srv.prepareToRestart();
  175. result = true;
  176. }
  177. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyEndGame(LobbyEndGame & pack)
  178. {
  179. for(auto & c : srv.activeConnections)
  180. c->enterLobbyConnectionMode();
  181. }
  182. void ClientPermissionsCheckerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  183. {
  184. result = srv.isClientHost(pack.c->connectionID);
  185. }
  186. void ApplyOnServerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  187. {
  188. try
  189. {
  190. srv.verifyStateBeforeStart(true);
  191. }
  192. catch(...)
  193. {
  194. result = false;
  195. return;
  196. }
  197. // Server will prepare gamestate and we announce StartInfo to clients
  198. if(!srv.prepareToStartGame())
  199. {
  200. result = false;
  201. return;
  202. }
  203. pack.initializedStartInfo = std::make_shared<StartInfo>(*srv.gh->getStartInfo(true));
  204. pack.initializedGameState = srv.gh->gameState();
  205. srv.setState(EServerState::GAMEPLAY_STARTING);
  206. result = true;
  207. }
  208. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  209. {
  210. if(pack.clientId == -1) //do not restart game for single client only
  211. srv.startGameImmediately();
  212. else
  213. {
  214. for(auto & c : srv.activeConnections)
  215. {
  216. if(c->connectionID == pack.clientId)
  217. {
  218. c->enterGameplayConnectionMode(srv.gh->gameState());
  219. srv.reconnectPlayer(pack.clientId);
  220. }
  221. }
  222. }
  223. }
  224. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  225. {
  226. result = srv.isClientHost(pack.c->connectionID);
  227. }
  228. void ApplyOnServerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  229. {
  230. result = true;
  231. }
  232. void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
  233. {
  234. auto result = srv.passHost(pack.newHostConnectionId);
  235. if(!result)
  236. {
  237. logGlobal->error("passHost returned false. What does it mean?");
  238. }
  239. }
  240. void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  241. {
  242. if(srv.isClientHost(pack.c->connectionID))
  243. {
  244. result = true;
  245. return;
  246. }
  247. if(vstd::contains(srv.getAllClientPlayers(pack.c->connectionID), pack.color))
  248. {
  249. result = true;
  250. return;
  251. }
  252. result = false;
  253. }
  254. void ApplyOnServerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
  255. {
  256. switch(pack.what)
  257. {
  258. case LobbyChangePlayerOption::TOWN_ID:
  259. srv.optionSetCastle(pack.color, FactionID(pack.value));
  260. break;
  261. case LobbyChangePlayerOption::TOWN:
  262. srv.optionNextCastle(pack.color, pack.value);
  263. break;
  264. case LobbyChangePlayerOption::HERO_ID:
  265. srv.optionSetHero(pack.color, HeroTypeID(pack.value));
  266. break;
  267. case LobbyChangePlayerOption::HERO:
  268. srv.optionNextHero(pack.color, pack.value);
  269. break;
  270. case LobbyChangePlayerOption::BONUS_ID:
  271. srv.optionSetBonus(pack.color, PlayerStartingBonus(pack.value));
  272. break;
  273. case LobbyChangePlayerOption::BONUS:
  274. srv.optionNextBonus(pack.color, pack.value);
  275. break;
  276. }
  277. result = true;
  278. }
  279. void ApplyOnServerNetPackVisitor::visitLobbySetPlayer(LobbySetPlayer & pack)
  280. {
  281. srv.setPlayer(pack.clickedColor);
  282. result = true;
  283. }
  284. void ApplyOnServerNetPackVisitor::visitLobbySetPlayerName(LobbySetPlayerName & pack)
  285. {
  286. srv.setPlayerName(pack.color, pack.name);
  287. result = true;
  288. }
  289. void ApplyOnServerNetPackVisitor::visitLobbySetSimturns(LobbySetSimturns & pack)
  290. {
  291. srv.si->simturnsInfo = pack.simturnsInfo;
  292. result = true;
  293. }
  294. void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack)
  295. {
  296. srv.si->turnTimerInfo = pack.turnTimerInfo;
  297. result = true;
  298. }
  299. void ApplyOnServerNetPackVisitor::visitLobbySetExtraOptions(LobbySetExtraOptions & pack)
  300. {
  301. srv.si->extraOptionsInfo = pack.extraOptionsInfo;
  302. result = true;
  303. }
  304. void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack)
  305. {
  306. srv.si->difficulty = std::clamp<uint8_t>(pack.difficulty, 0, 4);
  307. result = true;
  308. }
  309. void ApplyOnServerNetPackVisitor::visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack)
  310. {
  311. srv.si->playerInfos[pack.targetPlayerColor].connectedPlayerIDs.insert(pack.targetConnectedPlayer);
  312. result = true;
  313. }