NetPacksLobbyServer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 "CVCMIServer.h"
  12. #include "CGameHandler.h"
  13. #include "../lib/NetPacksLobby.h"
  14. #include "../lib/serializer/Connection.h"
  15. #include "../lib/StartInfo.h"
  16. // Campaigns
  17. #include "../lib/mapping/CCampaignHandler.h"
  18. #include "../lib/mapping/CMapService.h"
  19. #include "../lib/mapping/CMapInfo.h"
  20. bool CLobbyPackToServer::checkClientPermissions(CVCMIServer * srv) const
  21. {
  22. return srv->isClientHost(c->connectionID);
  23. }
  24. void CLobbyPackToServer::applyOnServerAfterAnnounce(CVCMIServer * srv)
  25. {
  26. // Propogate options after every CLobbyPackToServer
  27. srv->updateAndPropagateLobbyState();
  28. }
  29. bool LobbyClientConnected::checkClientPermissions(CVCMIServer * srv) const
  30. {
  31. if(srv->gh)
  32. {
  33. for(auto & connection : srv->hangingConnections)
  34. {
  35. if(connection->uuid == uuid)
  36. {
  37. return true;
  38. }
  39. }
  40. }
  41. if(srv->state == EServerState::LOBBY)
  42. return true;
  43. //disconnect immediately and ignore this client
  44. srv->connections.erase(c);
  45. if(c && c->isOpen())
  46. {
  47. c->close();
  48. }
  49. return false;
  50. }
  51. bool LobbyClientConnected::applyOnServer(CVCMIServer * srv)
  52. {
  53. if(srv->gh)
  54. {
  55. for(auto & connection : srv->hangingConnections)
  56. {
  57. if(connection->uuid == uuid)
  58. {
  59. logNetwork->info("Reconnection player");
  60. c->connectionID = connection->connectionID;
  61. for(auto & playerConnection : srv->gh->connections)
  62. {
  63. for(auto & existingConnection : playerConnection.second)
  64. {
  65. if(existingConnection == connection)
  66. {
  67. playerConnection.second.erase(existingConnection);
  68. playerConnection.second.insert(c);
  69. break;
  70. }
  71. }
  72. }
  73. srv->hangingConnections.erase(connection);
  74. break;
  75. }
  76. }
  77. }
  78. srv->clientConnected(c, names, uuid, mode);
  79. // Server need to pass some data to newly connected client
  80. clientId = c->connectionID;
  81. mode = srv->si->mode;
  82. hostClientId = srv->hostClientId;
  83. return true;
  84. }
  85. void LobbyClientConnected::applyOnServerAfterAnnounce(CVCMIServer * srv)
  86. {
  87. // FIXME: we need to avoid senting something to client that not yet get answer for LobbyClientConnected
  88. // Until UUID set we only pass LobbyClientConnected to this client
  89. c->uuid = uuid;
  90. srv->updateAndPropagateLobbyState();
  91. if(srv->state == EServerState::GAMEPLAY)
  92. {
  93. //immediately start game
  94. std::unique_ptr<LobbyStartGame> startGameForReconnectedPlayer(new LobbyStartGame);
  95. startGameForReconnectedPlayer->initializedStartInfo = srv->si;
  96. startGameForReconnectedPlayer->initializedGameState = srv->gh->gameState();
  97. startGameForReconnectedPlayer->clientId = c->connectionID;
  98. srv->addToAnnounceQueue(std::move(startGameForReconnectedPlayer));
  99. }
  100. }
  101. bool LobbyClientDisconnected::checkClientPermissions(CVCMIServer * srv) const
  102. {
  103. if(clientId != c->connectionID)
  104. return false;
  105. if(shutdownServer)
  106. {
  107. if(!srv->cmdLineOptions.count("run-by-client"))
  108. return false;
  109. if(c->uuid != srv->cmdLineOptions["uuid"].as<std::string>())
  110. return false;
  111. }
  112. return true;
  113. }
  114. bool LobbyClientDisconnected::applyOnServer(CVCMIServer * srv)
  115. {
  116. c->close();
  117. return true;
  118. }
  119. void LobbyClientDisconnected::applyOnServerAfterAnnounce(CVCMIServer * srv)
  120. {
  121. if(c && c->isOpen())
  122. {
  123. c->close();
  124. }
  125. if(shutdownServer)
  126. {
  127. logNetwork->info("Client requested shutdown, server will close itself...");
  128. srv->state = EServerState::SHUTDOWN;
  129. return;
  130. }
  131. else if(srv->connections.empty())
  132. {
  133. logNetwork->error("Last connection lost, server will close itself...");
  134. srv->state = EServerState::SHUTDOWN;
  135. }
  136. else if(c == srv->hostClient)
  137. {
  138. auto ph = std::make_unique<LobbyChangeHost>();
  139. auto newHost = *RandomGeneratorUtil::nextItem(srv->connections, CRandomGenerator::getDefault());
  140. ph->newHostConnectionId = newHost->connectionID;
  141. srv->addToAnnounceQueue(std::move(ph));
  142. }
  143. srv->updateAndPropagateLobbyState();
  144. }
  145. bool LobbyChatMessage::checkClientPermissions(CVCMIServer * srv) const
  146. {
  147. return true;
  148. }
  149. bool LobbySetMap::applyOnServer(CVCMIServer * srv)
  150. {
  151. if(srv->state != EServerState::LOBBY)
  152. return false;
  153. srv->updateStartInfoOnMapChange(mapInfo, mapGenOpts);
  154. return true;
  155. }
  156. bool LobbySetCampaign::applyOnServer(CVCMIServer * srv)
  157. {
  158. srv->si->mapname = ourCampaign->camp->header.filename;
  159. srv->si->mode = StartInfo::CAMPAIGN;
  160. srv->si->campState = ourCampaign;
  161. srv->si->turnTime = 0;
  162. bool isCurrentMapConquerable = ourCampaign->currentMap && ourCampaign->camp->conquerable(*ourCampaign->currentMap);
  163. for(int i = 0; i < ourCampaign->camp->scenarios.size(); i++)
  164. {
  165. if(ourCampaign->camp->conquerable(i))
  166. {
  167. if(!isCurrentMapConquerable || (isCurrentMapConquerable && i == *ourCampaign->currentMap))
  168. {
  169. srv->setCampaignMap(i);
  170. }
  171. }
  172. }
  173. return true;
  174. }
  175. bool LobbySetCampaignMap::applyOnServer(CVCMIServer * srv)
  176. {
  177. srv->setCampaignMap(mapId);
  178. return true;
  179. }
  180. bool LobbySetCampaignBonus::applyOnServer(CVCMIServer * srv)
  181. {
  182. srv->setCampaignBonus(bonusId);
  183. return true;
  184. }
  185. bool LobbyGuiAction::checkClientPermissions(CVCMIServer * srv) const
  186. {
  187. return srv->isClientHost(c->connectionID);
  188. }
  189. bool LobbyEndGame::checkClientPermissions(CVCMIServer * srv) const
  190. {
  191. return srv->isClientHost(c->connectionID);
  192. }
  193. bool LobbyEndGame::applyOnServer(CVCMIServer * srv)
  194. {
  195. srv->prepareToRestart();
  196. return true;
  197. }
  198. void LobbyEndGame::applyOnServerAfterAnnounce(CVCMIServer * srv)
  199. {
  200. boost::unique_lock<boost::mutex> stateLock(srv->stateMutex);
  201. for(auto & c : srv->connections)
  202. {
  203. c->enterLobbyConnectionMode();
  204. c->disableStackSendingByID();
  205. }
  206. }
  207. bool LobbyStartGame::checkClientPermissions(CVCMIServer * srv) const
  208. {
  209. return srv->isClientHost(c->connectionID);
  210. }
  211. bool LobbyStartGame::applyOnServer(CVCMIServer * srv)
  212. {
  213. try
  214. {
  215. srv->verifyStateBeforeStart(true);
  216. }
  217. catch(...)
  218. {
  219. return false;
  220. }
  221. // Server will prepare gamestate and we announce StartInfo to clients
  222. if(!srv->prepareToStartGame())
  223. return false;
  224. initializedStartInfo = std::make_shared<StartInfo>(*srv->gh->getStartInfo(true));
  225. initializedGameState = srv->gh->gameState();
  226. return true;
  227. }
  228. void LobbyStartGame::applyOnServerAfterAnnounce(CVCMIServer * srv)
  229. {
  230. if(clientId == -1) //do not restart game for single client only
  231. srv->startGameImmidiately();
  232. else
  233. {
  234. for(auto & c : srv->connections)
  235. {
  236. if(c->connectionID == clientId)
  237. {
  238. c->enterGameplayConnectionMode(srv->gh->gameState());
  239. srv->reconnectPlayer(clientId);
  240. }
  241. }
  242. }
  243. }
  244. bool LobbyChangeHost::checkClientPermissions(CVCMIServer * srv) const
  245. {
  246. return srv->isClientHost(c->connectionID);
  247. }
  248. bool LobbyChangeHost::applyOnServer(CVCMIServer * srv)
  249. {
  250. return true;
  251. }
  252. bool LobbyChangeHost::applyOnServerAfterAnnounce(CVCMIServer * srv)
  253. {
  254. return srv->passHost(newHostConnectionId);
  255. }
  256. bool LobbyChangePlayerOption::checkClientPermissions(CVCMIServer * srv) const
  257. {
  258. if(srv->isClientHost(c->connectionID))
  259. return true;
  260. if(vstd::contains(srv->getAllClientPlayers(c->connectionID), color))
  261. return true;
  262. return false;
  263. }
  264. bool LobbyChangePlayerOption::applyOnServer(CVCMIServer * srv)
  265. {
  266. switch(what)
  267. {
  268. case TOWN:
  269. srv->optionNextCastle(color, direction);
  270. break;
  271. case HERO:
  272. srv->optionNextHero(color, direction);
  273. break;
  274. case BONUS:
  275. srv->optionNextBonus(color, direction);
  276. break;
  277. }
  278. return true;
  279. }
  280. bool LobbySetPlayer::applyOnServer(CVCMIServer * srv)
  281. {
  282. srv->setPlayer(clickedColor);
  283. return true;
  284. }
  285. bool LobbySetTurnTime::applyOnServer(CVCMIServer * srv)
  286. {
  287. srv->si->turnTime = turnTime;
  288. return true;
  289. }
  290. bool LobbySetDifficulty::applyOnServer(CVCMIServer * srv)
  291. {
  292. srv->si->difficulty = vstd::abetween(difficulty, 0, 4);
  293. return true;
  294. }
  295. bool LobbyForceSetPlayer::applyOnServer(CVCMIServer * srv)
  296. {
  297. srv->si->playerInfos[targetPlayerColor].connectedPlayerIDs.insert(targetConnectedPlayer);
  298. return true;
  299. }