NetPacksServer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 player = GS(gh)->currentPlayer;
  82. throwOnWrongPlayer(gh, player);
  83. if(gh->queries.topQuery(player))
  84. throwAndComplain(gh, "Cannot end turn before resolving queries!");
  85. gh->states.setFlag(GS(gh)->currentPlayer, &PlayerStatus::makingTurn, false);
  86. return true;
  87. }
  88. bool DismissHero::applyGh(CGameHandler * gh)
  89. {
  90. throwOnWrongOwner(gh, hid);
  91. return gh->removeObject(gh->getObj(hid));
  92. }
  93. bool MoveHero::applyGh(CGameHandler * gh)
  94. {
  95. throwOnWrongOwner(gh, hid);
  96. return gh->moveHero(hid, dest, 0, transit, gh->getPlayerAt(c));
  97. }
  98. bool CastleTeleportHero::applyGh(CGameHandler * gh)
  99. {
  100. throwOnWrongOwner(gh, hid);
  101. return gh->teleportHero(hid, dest, source, gh->getPlayerAt(c));
  102. }
  103. bool ArrangeStacks::applyGh(CGameHandler * gh)
  104. {
  105. //checks for owning in the gh func
  106. return gh->arrangeStacks(id1, id2, what, p1, p2, val, gh->getPlayerAt(c));
  107. }
  108. bool BulkMoveArmy::applyGh(CGameHandler * gh)
  109. {
  110. return gh->bulkMoveArmy(srcArmy, destArmy, srcSlot);
  111. }
  112. bool BulkSplitStack::applyGh(CGameHandler * gh)
  113. {
  114. return gh->bulkSplitStack(src, srcOwner, amount);
  115. }
  116. bool BulkMergeStacks::applyGh(CGameHandler* gh)
  117. {
  118. return gh->bulkMergeStacks(src, srcOwner);
  119. }
  120. bool BulkSmartSplitStack::applyGh(CGameHandler * gh)
  121. {
  122. return gh->bulkSmartSplitStack(src, srcOwner);
  123. }
  124. bool DisbandCreature::applyGh(CGameHandler * gh)
  125. {
  126. throwOnWrongOwner(gh, id);
  127. return gh->disbandCreature(id, pos);
  128. }
  129. bool BuildStructure::applyGh(CGameHandler * gh)
  130. {
  131. throwOnWrongOwner(gh, tid);
  132. return gh->buildStructure(tid, bid);
  133. }
  134. bool RecruitCreatures::applyGh(CGameHandler * gh)
  135. {
  136. return gh->recruitCreatures(tid, dst, crid, amount, level);
  137. }
  138. bool UpgradeCreature::applyGh(CGameHandler * gh)
  139. {
  140. throwOnWrongOwner(gh, id);
  141. return gh->upgradeCreature(id, pos, cid);
  142. }
  143. bool GarrisonHeroSwap::applyGh(CGameHandler * gh)
  144. {
  145. const CGTownInstance * town = gh->getTown(tid);
  146. if(!isPlayerOwns(gh, tid) && !(town->garrisonHero && isPlayerOwns(gh, town->garrisonHero->id)))
  147. throwNotAllowedAction(); //neither town nor garrisoned hero (if present) is ours
  148. return gh->garrisonSwap(tid);
  149. }
  150. bool ExchangeArtifacts::applyGh(CGameHandler * gh)
  151. {
  152. throwOnWrongPlayer(gh, src.owningPlayer()); //second hero can be ally
  153. return gh->moveArtifact(src, dst);
  154. }
  155. bool AssembleArtifacts::applyGh(CGameHandler * gh)
  156. {
  157. throwOnWrongOwner(gh, heroID);
  158. return gh->assembleArtifacts(heroID, artifactSlot, assemble, assembleTo);
  159. }
  160. bool BuyArtifact::applyGh(CGameHandler * gh)
  161. {
  162. throwOnWrongOwner(gh, hid);
  163. return gh->buyArtifact(hid, aid);
  164. }
  165. bool TradeOnMarketplace::applyGh(CGameHandler * gh)
  166. {
  167. const CGObjectInstance * market = gh->getObj(marketId);
  168. if(!market)
  169. throwAndComplain(gh, "Invalid market object");
  170. const CGHeroInstance * hero = gh->getHero(heroId);
  171. //market must be owned or visited
  172. const IMarket * m = IMarket::castFrom(market);
  173. if(!m)
  174. throwAndComplain(gh, "market is not-a-market! :/");
  175. PlayerColor player = market->tempOwner;
  176. if(player >= PlayerColor::PLAYER_LIMIT)
  177. player = gh->getTile(market->visitablePos())->visitableObjects.back()->tempOwner;
  178. if(player >= PlayerColor::PLAYER_LIMIT)
  179. throwAndComplain(gh, "No player can use this market!");
  180. bool allyTownSkillTrade = (mode == EMarketMode::RESOURCE_SKILL && gh->getPlayerRelations(player, hero->tempOwner) == PlayerRelations::ALLIES);
  181. if(hero && (!(player == hero->tempOwner || allyTownSkillTrade)
  182. || hero->visitablePos() != market->visitablePos()))
  183. throwAndComplain(gh, "This hero can't use this marketplace!");
  184. if(!allyTownSkillTrade)
  185. throwOnWrongPlayer(gh, player);
  186. bool result = true;
  187. switch(mode)
  188. {
  189. case EMarketMode::RESOURCE_RESOURCE:
  190. for(int i = 0; i < r1.size(); ++i)
  191. result &= gh->tradeResources(m, val[i], player, r1[i], r2[i]);
  192. break;
  193. case EMarketMode::RESOURCE_PLAYER:
  194. for(int i = 0; i < r1.size(); ++i)
  195. result &= gh->sendResources(val[i], player, static_cast<Res::ERes>(r1[i]), PlayerColor(r2[i]));
  196. break;
  197. case EMarketMode::CREATURE_RESOURCE:
  198. for(int i = 0; i < r1.size(); ++i)
  199. result &= gh->sellCreatures(val[i], m, hero, SlotID(r1[i]), static_cast<Res::ERes>(r2[i]));
  200. break;
  201. case EMarketMode::RESOURCE_ARTIFACT:
  202. for(int i = 0; i < r1.size(); ++i)
  203. result &= gh->buyArtifact(m, hero, static_cast<Res::ERes>(r1[i]), ArtifactID(r2[i]));
  204. break;
  205. case EMarketMode::ARTIFACT_RESOURCE:
  206. for(int i = 0; i < r1.size(); ++i)
  207. result &= gh->sellArtifact(m, hero, ArtifactInstanceID(r1[i]), static_cast<Res::ERes>(r2[i]));
  208. break;
  209. case EMarketMode::CREATURE_UNDEAD:
  210. for(int i = 0; i < r1.size(); ++i)
  211. result &= gh->transformInUndead(m, hero, SlotID(r1[i]));
  212. break;
  213. case EMarketMode::RESOURCE_SKILL:
  214. for(int i = 0; i < r2.size(); ++i)
  215. result &= gh->buySecSkill(m, hero, SecondarySkill(r2[i]));
  216. break;
  217. case EMarketMode::CREATURE_EXP:
  218. {
  219. std::vector<SlotID> slotIDs(r1.begin(), r1.end());
  220. std::vector<ui32> count(val.begin(), val.end());
  221. return gh->sacrificeCreatures(m, hero, slotIDs, count);
  222. }
  223. case EMarketMode::ARTIFACT_EXP:
  224. {
  225. std::vector<ArtifactPosition> positions(r1.begin(), r1.end());
  226. return gh->sacrificeArtifact(m, hero, positions);
  227. }
  228. default:
  229. throwAndComplain(gh, "Unknown exchange mode!");
  230. }
  231. return result;
  232. }
  233. bool SetFormation::applyGh(CGameHandler * gh)
  234. {
  235. throwOnWrongOwner(gh, hid);
  236. return gh->setFormation(hid, formation);
  237. }
  238. bool HireHero::applyGh(CGameHandler * gh)
  239. {
  240. const CGObjectInstance * obj = gh->getObj(tid);
  241. const CGTownInstance * town = dynamic_ptr_cast<CGTownInstance>(obj);
  242. if(town && PlayerRelations::ENEMIES == gh->getPlayerRelations(obj->tempOwner, gh->getPlayerAt(c)))
  243. throwAndComplain(gh, "Can't buy hero in enemy town!");
  244. return gh->hireHero(obj, hid, player);
  245. }
  246. bool BuildBoat::applyGh(CGameHandler * gh)
  247. {
  248. if(gh->getPlayerRelations(gh->getOwner(objid), gh->getPlayerAt(c)) == PlayerRelations::ENEMIES)
  249. throwAndComplain(gh, "Can't build boat at enemy shipyard");
  250. return gh->buildBoat(objid);
  251. }
  252. bool QueryReply::applyGh(CGameHandler * gh)
  253. {
  254. auto playerToConnection = gh->connections.find(player);
  255. if(playerToConnection == gh->connections.end())
  256. throwAndComplain(gh, "No such player!");
  257. if(!vstd::contains(playerToConnection->second, c))
  258. throwAndComplain(gh, "Message came from wrong connection!");
  259. if(qid == QueryID(-1))
  260. throwAndComplain(gh, "Cannot answer the query with id -1!");
  261. assert(vstd::contains(gh->states.players, player));
  262. return gh->queryReply(qid, reply, player);
  263. }
  264. bool MakeAction::applyGh(CGameHandler * gh)
  265. {
  266. const BattleInfo * b = GS(gh)->curB;
  267. if(!b)
  268. throwNotAllowedAction();
  269. if(b->tacticDistance)
  270. {
  271. if(ba.actionType != EActionType::WALK && ba.actionType != EActionType::END_TACTIC_PHASE
  272. && ba.actionType != EActionType::RETREAT && ba.actionType != EActionType::SURRENDER)
  273. throwNotAllowedAction();
  274. if(!vstd::contains(gh->connections[b->sides[b->tacticsSide].color], c))
  275. throwNotAllowedAction();
  276. }
  277. else
  278. {
  279. auto active = b->battleActiveUnit();
  280. if(!active)
  281. throwNotAllowedAction();
  282. auto unitOwner = b->battleGetOwner(active);
  283. if(!vstd::contains(gh->connections[unitOwner], c))
  284. throwNotAllowedAction();
  285. }
  286. return gh->makeBattleAction(ba);
  287. }
  288. bool MakeCustomAction::applyGh(CGameHandler * gh)
  289. {
  290. const BattleInfo * b = GS(gh)->curB;
  291. if(!b)
  292. throwNotAllowedAction();
  293. if(b->tacticDistance)
  294. throwNotAllowedAction();
  295. auto active = b->battleActiveUnit();
  296. if(!active)
  297. throwNotAllowedAction();
  298. auto unitOwner = b->battleGetOwner(active);
  299. if(!vstd::contains(gh->connections[unitOwner], c))
  300. throwNotAllowedAction();
  301. if(ba.actionType != EActionType::HERO_SPELL)
  302. throwNotAllowedAction();
  303. return gh->makeCustomAction(ba);
  304. }
  305. bool DigWithHero::applyGh(CGameHandler * gh)
  306. {
  307. throwOnWrongOwner(gh, id);
  308. return gh->dig(gh->getHero(id));
  309. }
  310. bool CastAdvSpell::applyGh(CGameHandler * gh)
  311. {
  312. throwOnWrongOwner(gh, hid);
  313. const CSpell * s = sid.toSpell();
  314. if(!s)
  315. throwNotAllowedAction();
  316. const CGHeroInstance * h = gh->getHero(hid);
  317. if(!h)
  318. throwNotAllowedAction();
  319. AdventureSpellCastParameters p;
  320. p.caster = h;
  321. p.pos = pos;
  322. return s->adventureCast(gh->spellEnv, p);
  323. }
  324. bool PlayerMessage::applyGh(CGameHandler * gh)
  325. {
  326. if(!player.isSpectator()) // TODO: clearly not a great way to verify permissions
  327. throwOnWrongPlayer(gh, player);
  328. gh->playerMessage(player, text, currObj);
  329. return true;
  330. }