NetPacksLobbyServer.cpp 9.2 KB

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