NetPacksServer.cpp 9.9 KB

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