NetPacksServer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. if(hero && (player != hero->tempOwner || hero->visitablePos() != market->visitablePos()))
  170. throwAndComplain(gh, "This hero can't use this marketplace!");
  171. throwOnWrongPlayer(gh, player);
  172. bool result = true;
  173. switch(mode)
  174. {
  175. case EMarketMode::RESOURCE_RESOURCE:
  176. for(int i = 0; i < r1.size(); ++i)
  177. result &= gh->tradeResources(m, val[i], player, r1[i], r2[i]);
  178. break;
  179. case EMarketMode::RESOURCE_PLAYER:
  180. for(int i = 0; i < r1.size(); ++i)
  181. result &= gh->sendResources(val[i], player, static_cast<Res::ERes>(r1[i]), PlayerColor(r2[i]));
  182. break;
  183. case EMarketMode::CREATURE_RESOURCE:
  184. for(int i = 0; i < r1.size(); ++i)
  185. result &= gh->sellCreatures(val[i], m, hero, SlotID(r1[i]), static_cast<Res::ERes>(r2[i]));
  186. break;
  187. case EMarketMode::RESOURCE_ARTIFACT:
  188. for(int i = 0; i < r1.size(); ++i)
  189. result &= gh->buyArtifact(m, hero, static_cast<Res::ERes>(r1[i]), ArtifactID(r2[i]));
  190. break;
  191. case EMarketMode::ARTIFACT_RESOURCE:
  192. for(int i = 0; i < r1.size(); ++i)
  193. result &= gh->sellArtifact(m, hero, ArtifactInstanceID(r1[i]), static_cast<Res::ERes>(r2[i]));
  194. break;
  195. case EMarketMode::CREATURE_UNDEAD:
  196. for(int i = 0; i < r1.size(); ++i)
  197. result &= gh->transformInUndead(m, hero, SlotID(r1[i]));
  198. break;
  199. case EMarketMode::RESOURCE_SKILL:
  200. for(int i = 0; i < r2.size(); ++i)
  201. result &= gh->buySecSkill(m, hero, SecondarySkill(r2[i]));
  202. break;
  203. case EMarketMode::CREATURE_EXP:
  204. {
  205. std::vector<SlotID> slotIDs(r1.begin(), r1.end());
  206. std::vector<ui32> count(val.begin(), val.end());
  207. return gh->sacrificeCreatures(m, hero, slotIDs, count);
  208. }
  209. case EMarketMode::ARTIFACT_EXP:
  210. {
  211. std::vector<ArtifactPosition> positions(r1.begin(), r1.end());
  212. return gh->sacrificeArtifact(m, hero, positions);
  213. }
  214. default:
  215. throwAndComplain(gh, "Unknown exchange mode!");
  216. }
  217. return result;
  218. }
  219. bool SetFormation::applyGh(CGameHandler * gh)
  220. {
  221. throwOnWrongOwner(gh, hid);
  222. return gh->setFormation(hid, formation);
  223. }
  224. bool HireHero::applyGh(CGameHandler * gh)
  225. {
  226. const CGObjectInstance * obj = gh->getObj(tid);
  227. const CGTownInstance * town = dynamic_ptr_cast<CGTownInstance>(obj);
  228. if(town && PlayerRelations::ENEMIES == gh->getPlayerRelations(obj->tempOwner, gh->getPlayerAt(c)))
  229. throwAndComplain(gh, "Can't buy hero in enemy town!");
  230. return gh->hireHero(obj, hid, player);
  231. }
  232. bool BuildBoat::applyGh(CGameHandler * gh)
  233. {
  234. if(gh->getPlayerRelations(gh->getOwner(objid), gh->getPlayerAt(c)) == PlayerRelations::ENEMIES)
  235. throwAndComplain(gh, "Can't build boat at enemy shipyard");
  236. return gh->buildBoat(objid);
  237. }
  238. bool QueryReply::applyGh(CGameHandler * gh)
  239. {
  240. auto playerToConnection = gh->connections.find(player);
  241. if(playerToConnection == gh->connections.end())
  242. throwAndComplain(gh, "No such player!");
  243. if(!vstd::contains(playerToConnection->second, c))
  244. throwAndComplain(gh, "Message came from wrong connection!");
  245. if(qid == QueryID(-1))
  246. throwAndComplain(gh, "Cannot answer the query with id -1!");
  247. assert(vstd::contains(gh->states.players, player));
  248. return gh->queryReply(qid, reply, player);
  249. }
  250. bool MakeAction::applyGh(CGameHandler * gh)
  251. {
  252. const BattleInfo * b = GS(gh)->curB;
  253. if(!b)
  254. throwNotAllowedAction();
  255. if(b->tacticDistance)
  256. {
  257. if(ba.actionType != EActionType::WALK && ba.actionType != EActionType::END_TACTIC_PHASE
  258. && ba.actionType != EActionType::RETREAT && ba.actionType != EActionType::SURRENDER)
  259. throwNotAllowedAction();
  260. if(!vstd::contains(gh->connections[b->sides[b->tacticsSide].color], c))
  261. throwNotAllowedAction();
  262. }
  263. else
  264. {
  265. auto active = b->battleActiveUnit();
  266. if(!active)
  267. throwNotAllowedAction();
  268. auto unitOwner = b->battleGetOwner(active);
  269. if(!vstd::contains(gh->connections[unitOwner], c))
  270. throwNotAllowedAction();
  271. }
  272. return gh->makeBattleAction(ba);
  273. }
  274. bool MakeCustomAction::applyGh(CGameHandler * gh)
  275. {
  276. const BattleInfo * b = GS(gh)->curB;
  277. if(!b)
  278. throwNotAllowedAction();
  279. if(b->tacticDistance)
  280. throwNotAllowedAction();
  281. auto active = b->battleActiveUnit();
  282. if(!active)
  283. throwNotAllowedAction();
  284. auto unitOwner = b->battleGetOwner(active);
  285. if(!vstd::contains(gh->connections[unitOwner], c))
  286. throwNotAllowedAction();
  287. if(ba.actionType != EActionType::HERO_SPELL)
  288. throwNotAllowedAction();
  289. return gh->makeCustomAction(ba);
  290. }
  291. bool DigWithHero::applyGh(CGameHandler * gh)
  292. {
  293. throwOnWrongOwner(gh, id);
  294. return gh->dig(gh->getHero(id));
  295. }
  296. bool CastAdvSpell::applyGh(CGameHandler * gh)
  297. {
  298. throwOnWrongOwner(gh, hid);
  299. const CSpell * s = sid.toSpell();
  300. if(!s)
  301. throwNotAllowedAction();
  302. const CGHeroInstance * h = gh->getHero(hid);
  303. if(!h)
  304. throwNotAllowedAction();
  305. AdventureSpellCastParameters p;
  306. p.caster = h;
  307. p.pos = pos;
  308. return s->adventureCast(gh->spellEnv, p);
  309. }
  310. bool PlayerMessage::applyGh(CGameHandler * gh)
  311. {
  312. if(!player.isSpectator()) // TODO: clearly not a great way to verify permissions
  313. {
  314. throwOnWrongPlayer(gh, player);
  315. if(gh->getPlayerAt(this->c) != player)
  316. throwNotAllowedAction();
  317. }
  318. gh->playerMessage(player, text, currObj);
  319. return true;
  320. }