NetPacksLobbyServer.cpp 7.9 KB

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