NetPacksLobbyServer.cpp 9.1 KB

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