NetPacksServer.cpp 8.8 KB

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