NetPacksServer.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. bool result = true;
  148. switch(mode)
  149. {
  150. case EMarketMode::RESOURCE_RESOURCE:
  151. for (int i = 0; i < r1.size(); ++i)
  152. result &= gh->tradeResources(m, val[i], player, r1[i], r2[i]);
  153. break;
  154. case EMarketMode::RESOURCE_PLAYER:
  155. for (int i = 0; i < r1.size(); ++i)
  156. result &= gh->sendResources(val[i], player, static_cast<Res::ERes>(r1[i]), PlayerColor(r2[i]));
  157. break;
  158. case EMarketMode::CREATURE_RESOURCE:
  159. for (int i = 0; i < r1.size(); ++i)
  160. result &= gh->sellCreatures(val[i], m, hero, SlotID(r1[i]), static_cast<Res::ERes>(r2[i]));
  161. break;
  162. case EMarketMode::RESOURCE_ARTIFACT:
  163. for (int i = 0; i < r1.size(); ++i)
  164. result &= gh->buyArtifact(m, hero, static_cast<Res::ERes>(r1[i]), ArtifactID(r2[i]));
  165. break;
  166. case EMarketMode::ARTIFACT_RESOURCE:
  167. for (int i = 0; i < r1.size(); ++i)
  168. result &= gh->sellArtifact(m, hero, ArtifactInstanceID(r1[i]), static_cast<Res::ERes>(r2[i]));
  169. break;
  170. case EMarketMode::CREATURE_UNDEAD:
  171. for (int i = 0; i < r1.size(); ++i)
  172. result &= gh->transformInUndead(m, hero, SlotID(r1[i]));
  173. break;
  174. case EMarketMode::RESOURCE_SKILL:
  175. for (int i = 0; i < r2.size(); ++i)
  176. result &= gh->buySecSkill(m, hero, SecondarySkill(r2[i]));
  177. break;
  178. case EMarketMode::CREATURE_EXP:
  179. {
  180. std::vector<SlotID> slotIDs(r1.begin(), r1.end());
  181. std::vector<ui32> count(val.begin(), val.end());
  182. return gh->sacrificeCreatures(m, hero, slotIDs, count);
  183. }
  184. case EMarketMode::ARTIFACT_EXP:
  185. {
  186. std::vector<ArtifactPosition> positions(r1.begin(), r1.end());
  187. return gh->sacrificeArtifact(m, hero, positions);
  188. }
  189. default:
  190. COMPLAIN_AND_RETURN("Unknown exchange mode!");
  191. }
  192. return result;
  193. }
  194. bool SetFormation::applyGh( CGameHandler *gh )
  195. {
  196. ERROR_IF_NOT_OWNS(hid);
  197. return gh->setFormation(hid,formation);
  198. }
  199. bool HireHero::applyGh( CGameHandler *gh )
  200. {
  201. const CGObjectInstance *obj = gh->getObj(tid);
  202. const CGTownInstance *town = dynamic_ptr_cast<CGTownInstance>(obj);
  203. if(town && PlayerRelations::ENEMIES == gh->getPlayerRelations(obj->tempOwner, gh->getPlayerAt(c)))
  204. COMPLAIN_AND_RETURN("Can't buy hero in enemy town!");
  205. return gh->hireHero(obj, hid,player);
  206. }
  207. bool BuildBoat::applyGh( CGameHandler *gh )
  208. {
  209. ERROR_IF_NOT_OWNS(objid);
  210. return gh->buildBoat(objid);
  211. }
  212. bool QueryReply::applyGh( CGameHandler *gh )
  213. {
  214. auto playerToConnection = gh->connections.find(player);
  215. if(playerToConnection == gh->connections.end())
  216. COMPLAIN_AND_RETURN("No such player!");
  217. if(playerToConnection->second != c)
  218. COMPLAIN_AND_RETURN("Message came from wrong connection!");
  219. if(qid == QueryID(-1))
  220. COMPLAIN_AND_RETURN("Cannot answer the query with id -1!");
  221. assert(vstd::contains(gh->states.players, player));
  222. return gh->queryReply(qid, reply, player);
  223. }
  224. bool MakeAction::applyGh( CGameHandler *gh )
  225. {
  226. const BattleInfo *b = GS(gh)->curB;
  227. if(!b) ERROR_AND_RETURN;
  228. if(b->tacticDistance)
  229. {
  230. if(ba.actionType != Battle::WALK && ba.actionType != Battle::END_TACTIC_PHASE
  231. && ba.actionType != Battle::RETREAT && ba.actionType != Battle::SURRENDER)
  232. ERROR_AND_RETURN;
  233. if(gh->connections[b->sides[b->tacticsSide].color] != c)
  234. ERROR_AND_RETURN;
  235. }
  236. else if(gh->connections[b->battleGetStackByID(b->activeStack)->owner] != c)
  237. ERROR_AND_RETURN;
  238. return gh->makeBattleAction(ba);
  239. }
  240. bool MakeCustomAction::applyGh( CGameHandler *gh )
  241. {
  242. const BattleInfo *b = GS(gh)->curB;
  243. if(!b) ERROR_AND_RETURN;
  244. if(b->tacticDistance) ERROR_AND_RETURN;
  245. const CStack *active = GS(gh)->curB->battleGetStackByID(GS(gh)->curB->activeStack);
  246. if(!active) ERROR_AND_RETURN;
  247. if(gh->connections[active->owner] != c) ERROR_AND_RETURN;
  248. if(ba.actionType != Battle::HERO_SPELL) ERROR_AND_RETURN;
  249. return gh->makeCustomAction(ba);
  250. }
  251. bool DigWithHero::applyGh( CGameHandler *gh )
  252. {
  253. ERROR_IF_NOT_OWNS(id);
  254. return gh->dig(gh->getHero(id));
  255. }
  256. bool CastAdvSpell::applyGh(CGameHandler * gh)
  257. {
  258. ERROR_IF_NOT_OWNS(hid);
  259. const CSpell * s = sid.toSpell();
  260. if(!s)
  261. ERROR_AND_RETURN;
  262. const CGHeroInstance * h = gh->getHero(hid);
  263. if(!h)
  264. ERROR_AND_RETURN;
  265. AdventureSpellCastParameters p;
  266. p.caster = h;
  267. p.pos = pos;
  268. return s->adventureCast(gh->spellEnv, p);
  269. }
  270. bool PlayerMessage::applyGh( CGameHandler *gh )
  271. {
  272. if(!player.isSpectator()) // TODO: clearly not a great way to verify permissions
  273. {
  274. ERROR_IF_NOT(player);
  275. if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
  276. }
  277. gh->playerMessage(player,text, currObj);
  278. return true;
  279. }