NetPacksServer.cpp 9.9 KB

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