CBattleCallback.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #define VCMI_DLL
  2. #include "CBattleCallback.h"
  3. #include "NetPacks.h"
  4. #include "Connection.h"
  5. #include "CGameState.h"
  6. #include <boost/thread/shared_mutex.hpp>
  7. #include "BattleState.h"
  8. CBattleCallback::CBattleCallback(CGameState *GS, int Player, IConnectionHandler *C )
  9. {
  10. gs = GS;
  11. player = Player;
  12. connHandler = C;
  13. }
  14. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  15. {
  16. assert(gs->curB->tacticDistance);
  17. MakeAction ma;
  18. ma.ba = *action;
  19. sendRequest(&ma);
  20. return true;
  21. }
  22. int CBattleCallback::battleMakeAction(BattleAction* action)
  23. {
  24. assert(action->actionType == BattleAction::HERO_SPELL);
  25. MakeCustomAction mca(*action);
  26. sendRequest(&mca);
  27. return 0;
  28. }
  29. void CBattleCallback::sendRequest(const CPack* request)
  30. {
  31. //TODO should be part of CClient (client owns connection, not CB)
  32. //but it would have to be very tricky cause template/serialization issues
  33. if(waitTillRealize)
  34. connHandler->waitingRequest.set(typeList.getTypeID(request));
  35. connHandler->serv->sendPack(*request);
  36. if(waitTillRealize)
  37. {
  38. if(unlockGsWhenWaiting)
  39. gs->mx->unlock_shared();
  40. connHandler->waitingRequest.waitWhileTrue();
  41. if(unlockGsWhenWaiting)
  42. gs->mx->lock_shared();
  43. }
  44. }
  45. IConnectionHandler::IConnectionHandler()
  46. : waitingRequest(0)
  47. {
  48. }
  49. IConnectionHandler::~IConnectionHandler()
  50. {
  51. }