NetPacksServer.cpp 10 KB

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