NetPacksServer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * NetPacksServer.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 "../lib/NetPacks.h"
  12. #include "CGameHandler.h"
  13. #include "../lib/IGameCallback.h"
  14. #include "../lib/mapping/CMap.h"
  15. #include "../lib/CGameState.h"
  16. #include "../lib/battle/BattleInfo.h"
  17. #include "../lib/battle/BattleAction.h"
  18. #include "../lib/battle/Unit.h"
  19. #include "../lib/serializer/Connection.h"
  20. #include "../lib/spells/CSpellHandler.h"
  21. #include "../lib/spells/ISpellMechanics.h"
  22. #include "../lib/serializer/Cast.h"
  23. bool CPackForServer::isPlayerOwns(CGameHandler * gh, ObjectInstanceID id)
  24. {
  25. return gh->getPlayerAt(c) == gh->getOwner(id);
  26. }
  27. void CPackForServer::throwNotAllowedAction()
  28. {
  29. if(c)
  30. {
  31. SystemMessage temp_message("You are not allowed to perform this action!");
  32. c->sendPack(&temp_message);
  33. }
  34. logNetwork->error("Player is not allowed to perform this action!");
  35. throw ExceptionNotAllowedAction();
  36. }
  37. void CPackForServer::wrongPlayerMessage(CGameHandler * gh, PlayerColor expectedplayer)
  38. {
  39. std::ostringstream oss;
  40. oss << "You were identified as player " << gh->getPlayerAt(c) << " while expecting " << expectedplayer;
  41. logNetwork->error(oss.str());
  42. if(c)
  43. {
  44. SystemMessage temp_message(oss.str());
  45. c->sendPack(&temp_message);
  46. }
  47. }
  48. void CPackForServer::throwOnWrongOwner(CGameHandler * gh, ObjectInstanceID id)
  49. {
  50. if(!isPlayerOwns(gh, id))
  51. {
  52. wrongPlayerMessage(gh, gh->getOwner(id));
  53. throwNotAllowedAction();
  54. }
  55. }
  56. void CPackForServer::throwOnWrongPlayer(CGameHandler * gh, PlayerColor player)
  57. {
  58. if(!gh->hasPlayerAt(player, c) && player != gh->getPlayerAt(c))
  59. {
  60. wrongPlayerMessage(gh, player);
  61. throwNotAllowedAction();
  62. }
  63. }
  64. void CPackForServer::throwAndComplain(CGameHandler * gh, std::string txt)
  65. {
  66. gh->complain(txt);
  67. throwNotAllowedAction();
  68. }
  69. CGameState * CPackForServer::GS(CGameHandler * gh)
  70. {
  71. return gh->gs;
  72. }
  73. bool SaveGame::applyGh(CGameHandler * gh)
  74. {
  75. gh->save(fname);
  76. logGlobal->info("Game has been saved as %s", fname);
  77. return true;
  78. }
  79. bool EndTurn::applyGh(CGameHandler * gh)
  80. {
  81. PlayerColor currentPlayer = GS(gh)->currentPlayer;
  82. if(player != currentPlayer)
  83. {
  84. if(gh->getPlayerStatus(player) == EPlayerStatus::INGAME)
  85. throwAndComplain(gh, "Player attempted to end turn for another player!");
  86. logGlobal->debug("Player attempted to end turn after game over. Ignoring this request.");
  87. return true;
  88. }
  89. throwOnWrongPlayer(gh, player);
  90. if(gh->queries.topQuery(player))
  91. throwAndComplain(gh, "Cannot end turn before resolving queries!");
  92. gh->states.setFlag(GS(gh)->currentPlayer, &PlayerStatus::makingTurn, false);
  93. return true;
  94. }
  95. bool DismissHero::applyGh(CGameHandler * gh)
  96. {
  97. throwOnWrongOwner(gh, hid);
  98. return gh->removeObject(gh->getObj(hid));
  99. }
  100. bool MoveHero::applyGh(CGameHandler * gh)
  101. {
  102. throwOnWrongOwner(gh, hid);
  103. return gh->moveHero(hid, dest, 0, transit, gh->getPlayerAt(c));
  104. }
  105. bool CastleTeleportHero::applyGh(CGameHandler * gh)
  106. {
  107. throwOnWrongOwner(gh, hid);
  108. return gh->teleportHero(hid, dest, source, gh->getPlayerAt(c));
  109. }
  110. bool ArrangeStacks::applyGh(CGameHandler * gh)
  111. {
  112. //checks for owning in the gh func
  113. return gh->arrangeStacks(id1, id2, what, p1, p2, val, gh->getPlayerAt(c));
  114. }
  115. bool BulkMoveArmy::applyGh(CGameHandler * gh)
  116. {
  117. return gh->bulkMoveArmy(srcArmy, destArmy, srcSlot);
  118. }
  119. bool BulkSplitStack::applyGh(CGameHandler * gh)
  120. {
  121. return gh->bulkSplitStack(src, srcOwner, amount);
  122. }
  123. bool BulkMergeStacks::applyGh(CGameHandler* gh)
  124. {
  125. return gh->bulkMergeStacks(src, srcOwner);
  126. }
  127. bool BulkSmartSplitStack::applyGh(CGameHandler * gh)
  128. {
  129. return gh->bulkSmartSplitStack(src, srcOwner);
  130. }
  131. bool DisbandCreature::applyGh(CGameHandler * gh)
  132. {
  133. throwOnWrongOwner(gh, id);
  134. return gh->disbandCreature(id, pos);
  135. }
  136. bool BuildStructure::applyGh(CGameHandler * gh)
  137. {
  138. throwOnWrongOwner(gh, tid);
  139. return gh->buildStructure(tid, bid);
  140. }
  141. bool RecruitCreatures::applyGh(CGameHandler * gh)
  142. {
  143. return gh->recruitCreatures(tid, dst, crid, amount, level);
  144. }
  145. bool UpgradeCreature::applyGh(CGameHandler * gh)
  146. {
  147. throwOnWrongOwner(gh, id);
  148. return gh->upgradeCreature(id, pos, cid);
  149. }
  150. bool GarrisonHeroSwap::applyGh(CGameHandler * gh)
  151. {
  152. const CGTownInstance * town = gh->getTown(tid);
  153. if(!isPlayerOwns(gh, tid) && !(town->garrisonHero && isPlayerOwns(gh, town->garrisonHero->id)))
  154. throwNotAllowedAction(); //neither town nor garrisoned hero (if present) is ours
  155. return gh->garrisonSwap(tid);
  156. }
  157. bool ExchangeArtifacts::applyGh(CGameHandler * gh)
  158. {
  159. throwOnWrongPlayer(gh, src.owningPlayer()); //second hero can be ally
  160. return gh->moveArtifact(src, dst);
  161. }
  162. bool BulkExchangeArtifacts::applyGh(CGameHandler * gh)
  163. {
  164. const CGHeroInstance * pSrcHero = gh->getHero(srcHero);
  165. throwOnWrongPlayer(gh, pSrcHero->getOwner());
  166. return gh->bulkMoveArtifacts(srcHero, dstHero, swap);
  167. }
  168. bool AssembleArtifacts::applyGh(CGameHandler * gh)
  169. {
  170. throwOnWrongOwner(gh, heroID);
  171. return gh->assembleArtifacts(heroID, artifactSlot, assemble, assembleTo);
  172. }
  173. bool BuyArtifact::applyGh(CGameHandler * gh)
  174. {
  175. throwOnWrongOwner(gh, hid);
  176. return gh->buyArtifact(hid, aid);
  177. }
  178. bool TradeOnMarketplace::applyGh(CGameHandler * gh)
  179. {
  180. const CGObjectInstance * market = gh->getObj(marketId);
  181. if(!market)
  182. throwAndComplain(gh, "Invalid market object");
  183. const CGHeroInstance * hero = gh->getHero(heroId);
  184. //market must be owned or visited
  185. const IMarket * m = IMarket::castFrom(market);
  186. if(!m)
  187. throwAndComplain(gh, "market is not-a-market! :/");
  188. PlayerColor player = market->tempOwner;
  189. if(player >= PlayerColor::PLAYER_LIMIT)
  190. player = gh->getTile(market->visitablePos())->visitableObjects.back()->tempOwner;
  191. if(player >= PlayerColor::PLAYER_LIMIT)
  192. throwAndComplain(gh, "No player can use this market!");
  193. bool allyTownSkillTrade = (mode == EMarketMode::RESOURCE_SKILL && gh->getPlayerRelations(player, hero->tempOwner) == PlayerRelations::ALLIES);
  194. if(hero && (!(player == hero->tempOwner || allyTownSkillTrade)
  195. || hero->visitablePos() != market->visitablePos()))
  196. throwAndComplain(gh, "This hero can't use this marketplace!");
  197. if(!allyTownSkillTrade)
  198. throwOnWrongPlayer(gh, player);
  199. bool result = true;
  200. switch(mode)
  201. {
  202. case EMarketMode::RESOURCE_RESOURCE:
  203. for(int i = 0; i < r1.size(); ++i)
  204. result &= gh->tradeResources(m, val[i], player, r1[i], r2[i]);
  205. break;
  206. case EMarketMode::RESOURCE_PLAYER:
  207. for(int i = 0; i < r1.size(); ++i)
  208. result &= gh->sendResources(val[i], player, static_cast<Res::ERes>(r1[i]), PlayerColor(r2[i]));
  209. break;
  210. case EMarketMode::CREATURE_RESOURCE:
  211. for(int i = 0; i < r1.size(); ++i)
  212. result &= gh->sellCreatures(val[i], m, hero, SlotID(r1[i]), static_cast<Res::ERes>(r2[i]));
  213. break;
  214. case EMarketMode::RESOURCE_ARTIFACT:
  215. for(int i = 0; i < r1.size(); ++i)
  216. result &= gh->buyArtifact(m, hero, static_cast<Res::ERes>(r1[i]), ArtifactID(r2[i]));
  217. break;
  218. case EMarketMode::ARTIFACT_RESOURCE:
  219. for(int i = 0; i < r1.size(); ++i)
  220. result &= gh->sellArtifact(m, hero, ArtifactInstanceID(r1[i]), static_cast<Res::ERes>(r2[i]));
  221. break;
  222. case EMarketMode::CREATURE_UNDEAD:
  223. for(int i = 0; i < r1.size(); ++i)
  224. result &= gh->transformInUndead(m, hero, SlotID(r1[i]));
  225. break;
  226. case EMarketMode::RESOURCE_SKILL:
  227. for(int i = 0; i < r2.size(); ++i)
  228. result &= gh->buySecSkill(m, hero, SecondarySkill(r2[i]));
  229. break;
  230. case EMarketMode::CREATURE_EXP:
  231. {
  232. std::vector<SlotID> slotIDs(r1.begin(), r1.end());
  233. std::vector<ui32> count(val.begin(), val.end());
  234. return gh->sacrificeCreatures(m, hero, slotIDs, count);
  235. }
  236. case EMarketMode::ARTIFACT_EXP:
  237. {
  238. std::vector<ArtifactPosition> positions(r1.begin(), r1.end());
  239. return gh->sacrificeArtifact(m, hero, positions);
  240. }
  241. default:
  242. throwAndComplain(gh, "Unknown exchange mode!");
  243. }
  244. return result;
  245. }
  246. bool SetFormation::applyGh(CGameHandler * gh)
  247. {
  248. throwOnWrongOwner(gh, hid);
  249. return gh->setFormation(hid, formation);
  250. }
  251. bool HireHero::applyGh(CGameHandler * gh)
  252. {
  253. const CGObjectInstance * obj = gh->getObj(tid);
  254. const CGTownInstance * town = dynamic_ptr_cast<CGTownInstance>(obj);
  255. if(town && PlayerRelations::ENEMIES == gh->getPlayerRelations(obj->tempOwner, gh->getPlayerAt(c)))
  256. throwAndComplain(gh, "Can't buy hero in enemy town!");
  257. return gh->hireHero(obj, hid, player);
  258. }
  259. bool BuildBoat::applyGh(CGameHandler * gh)
  260. {
  261. if(gh->getPlayerRelations(gh->getOwner(objid), gh->getPlayerAt(c)) == PlayerRelations::ENEMIES)
  262. throwAndComplain(gh, "Can't build boat at enemy shipyard");
  263. return gh->buildBoat(objid);
  264. }
  265. bool QueryReply::applyGh(CGameHandler * gh)
  266. {
  267. auto playerToConnection = gh->connections.find(player);
  268. if(playerToConnection == gh->connections.end())
  269. throwAndComplain(gh, "No such player!");
  270. if(!vstd::contains(playerToConnection->second, c))
  271. throwAndComplain(gh, "Message came from wrong connection!");
  272. if(qid == QueryID(-1))
  273. throwAndComplain(gh, "Cannot answer the query with id -1!");
  274. assert(vstd::contains(gh->states.players, player));
  275. return gh->queryReply(qid, reply, player);
  276. }
  277. bool MakeAction::applyGh(CGameHandler * gh)
  278. {
  279. const BattleInfo * b = GS(gh)->curB;
  280. if(!b)
  281. throwNotAllowedAction();
  282. if(b->tacticDistance)
  283. {
  284. if(ba.actionType != EActionType::WALK && ba.actionType != EActionType::END_TACTIC_PHASE
  285. && ba.actionType != EActionType::RETREAT && ba.actionType != EActionType::SURRENDER)
  286. throwNotAllowedAction();
  287. if(!vstd::contains(gh->connections[b->sides[b->tacticsSide].color], c))
  288. throwNotAllowedAction();
  289. }
  290. else
  291. {
  292. auto active = b->battleActiveUnit();
  293. if(!active)
  294. throwNotAllowedAction();
  295. auto unitOwner = b->battleGetOwner(active);
  296. if(!vstd::contains(gh->connections[unitOwner], c))
  297. throwNotAllowedAction();
  298. }
  299. return gh->makeBattleAction(ba);
  300. }
  301. bool MakeCustomAction::applyGh(CGameHandler * gh)
  302. {
  303. const BattleInfo * b = GS(gh)->curB;
  304. if(!b)
  305. throwNotAllowedAction();
  306. if(b->tacticDistance)
  307. throwNotAllowedAction();
  308. auto active = b->battleActiveUnit();
  309. if(!active)
  310. throwNotAllowedAction();
  311. auto unitOwner = b->battleGetOwner(active);
  312. if(!vstd::contains(gh->connections[unitOwner], c))
  313. throwNotAllowedAction();
  314. if(ba.actionType != EActionType::HERO_SPELL)
  315. throwNotAllowedAction();
  316. return gh->makeCustomAction(ba);
  317. }
  318. bool DigWithHero::applyGh(CGameHandler * gh)
  319. {
  320. throwOnWrongOwner(gh, id);
  321. return gh->dig(gh->getHero(id));
  322. }
  323. bool CastAdvSpell::applyGh(CGameHandler * gh)
  324. {
  325. throwOnWrongOwner(gh, hid);
  326. const CSpell * s = sid.toSpell();
  327. if(!s)
  328. throwNotAllowedAction();
  329. const CGHeroInstance * h = gh->getHero(hid);
  330. if(!h)
  331. throwNotAllowedAction();
  332. AdventureSpellCastParameters p;
  333. p.caster = h;
  334. p.pos = pos;
  335. return s->adventureCast(gh->spellEnv, p);
  336. }
  337. bool PlayerMessage::applyGh(CGameHandler * gh)
  338. {
  339. if(!player.isSpectator()) // TODO: clearly not a great way to verify permissions
  340. throwOnWrongPlayer(gh, player);
  341. gh->playerMessage(player, text, currObj);
  342. return true;
  343. }