NetPacksServer.cpp 9.2 KB

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