NetPacksServer.cpp 10 KB

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