Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. #include "ScriptPCH.h"
  2.  
  3. struct betInfo
  4. {
  5. uint64 GUID;
  6. uint32 Goal;
  7. int32 RemainingRankPoints;
  8. float timeLeft;
  9. };
  10.  
  11. class Bet_Npc : public CreatureScript, PlayerScript, WorldScript
  12. {
  13. public:
  14. Bet_Npc() : CreatureScript("Bet_Npc"), PlayerScript("Check"), WorldScript("Timer") {Timer = 0;}
  15. uint32 Timer;
  16.  
  17. void OnRankPointsReceive(Player* obtainer, int32 amount)
  18. {
  19. if(amount <= 0 || betTracker.empty())
  20. return;
  21. for(std::vector<betInfo>::iterator itr = betTracker.begin(); itr != betTracker.end(); ++itr)
  22. {
  23. if(itr->GUID == obtainer->GetGUID())
  24. {
  25. if(itr->RemainingRankPoints <= amount )
  26. {
  27. itr->RemainingRankPoints = 0;
  28. ChatHandler(obtainer->GetSession()).SendSysMessage("[Goal System] : You have successfully reached your goal, please visit the Betting Npc to claim your reward");
  29. }
  30. else
  31. {
  32. itr->RemainingRankPoints -= amount;
  33. ChatHandler(obtainer->GetSession()).PSendSysMessage("[Goal System] : %u Rank Points remaining to complete your goal", itr->RemainingRankPoints);
  34. }
  35. CharacterDatabase.PExecute("UPDATE `Character_Bets` SET `RemainingGoal` = %u WHERE `GUID` = %u", itr->RemainingRankPoints, obtainer->GetGUID());
  36. return;
  37. }
  38. }
  39. }
  40.  
  41. void OnStartup()
  42. {
  43. QueryResult result = CharacterDatabase.Query("SELECT * FROM `Character_Bets`");
  44. if(result)
  45. {
  46.  
  47. do{
  48. Field* fields = result->Fetch();
  49. betInfo info;
  50. info.GUID = fields[0].GetUInt64();
  51. info.Goal = fields[1].GetUInt32();
  52. info.RemainingRankPoints = fields[2].GetUInt32();
  53. info.timeLeft = fields[3].GetUInt64();
  54. betTracker.push_back(info);
  55. }while(result->NextRow());
  56. }
  57.  
  58. }
  59.  
  60. void OnUpdate(uint32 diff)
  61. {
  62. if(Timer >= 300000)
  63. {
  64. Timer = 0;
  65. CharacterDatabase.PExecute("UPDATE `Character_Bets` SET `TimeLeft` = `TimeLeft` - 300 WHERE `Timeleft` > 300");
  66. }
  67. else
  68. Timer += diff;
  69. if(betTracker.empty())
  70. return;
  71. float DiffinSecs = diff / 1000;
  72. for(std::vector<betInfo>::iterator itr = betTracker.begin(); itr != betTracker.end(); ++itr)
  73. {
  74. if(itr->timeLeft < DiffinSecs && itr->RemainingRankPoints != 0)
  75. itr->timeLeft = 0;
  76. if(itr->timeLeft == 0)
  77. {
  78. if(Player* player = sObjectAccessor->FindPlayer(itr->GUID))
  79. player->GetSession()->SendNotification("[Goal System] : You have failed your goal");
  80. betTracker.erase(itr);
  81. continue;
  82. }
  83. if(itr->RemainingRankPoints != 0)
  84. itr->timeLeft -= DiffinSecs;
  85. }
  86. }
  87.  
  88. bool OnGossipHello(Player* player, Creature* creature)
  89. {
  90. bool found = false;
  91. bool Completed = false;
  92.  
  93. uint32 seconds;
  94. uint32 Reward;
  95. std::vector<betInfo>::iterator Pos;
  96. for(std::vector<betInfo>::iterator itr = betTracker.begin(); itr != betTracker.end(); ++itr)
  97. {
  98. if(itr->GUID == player->GetGUID())
  99. {
  100. found = true;
  101. if(itr->RemainingRankPoints == 0) Completed = true;
  102. seconds = itr->timeLeft;
  103. Reward = itr->Goal * 2;
  104. Pos = itr;
  105. break;
  106. }
  107. }
  108. if(found && !Completed)
  109. {
  110. sLog->outError(LOG_FILTER_GENERAL, "seconds : %u", seconds);
  111. sLog->outError(LOG_FILTER_GENERAL, "itr->timeleft : %f", Pos->timeLeft);
  112. sLog->outError(LOG_FILTER_GENERAL, "Reward : %u", Reward);
  113. sLog->outError(LOG_FILTER_GENERAL, "Position : %i", std::distance(betTracker.begin(), Pos));
  114. std::ostringstream ss;
  115. ss << "Already set a goal, time left :";
  116. uint64 secs = seconds % 60;
  117. uint64 minutes = seconds % 3600 / 60;
  118. uint64 hours = seconds / 3600;
  119. if(hours)
  120. hours != 1 ? ss << hours << " Hours" : ss << hours << " Hour";
  121. if(minutes)
  122. minutes != 1 ? ss << minutes << " Minutes " : ss << minutes << " Minutes ";
  123. if(secs || (!hours && !minutes))
  124. secs != 1 ? ss << secs << " Seconds " : ss << secs << " Second ";
  125. player->ADD_GOSSIP_ITEM(0, ss.str().c_str(), GOSSIP_SENDER_MAIN, 0);
  126. }
  127. else if(found && Completed)
  128. {
  129. player->GetSession()->SendAreaTriggerMessage("You claimed your reward, %u Rank Points", Reward);
  130. player->ModifyRankPoints(Reward);
  131. betTracker.erase(Pos);
  132. player->PlayerTalkClass->SendCloseGossip();
  133. CharacterDatabase.PExecute("DELETE FROM `Character_Bets` WHERE `GUID` = %u", player->GetGUID());
  134. }
  135. else
  136. player->ADD_GOSSIP_ITEM_EXTENDED(0, "Set a goal", GOSSIP_SENDER_MAIN, 1, "RankPoints to obtain in 24 hours : ", 0, true);
  137.  
  138. player->ADD_GOSSIP_ITEM(0, "Nevermind", GOSSIP_SENDER_MAIN, 2);
  139. player->PlayerTalkClass->SendGossipMenu(907, creature->GetGUID());
  140. return true;
  141. }
  142.  
  143. bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action)
  144. {
  145. if(action == 2)
  146. player->PlayerTalkClass->SendCloseGossip();
  147. return true;
  148. }
  149.  
  150. bool OnGossipSelectCode(Player* player, Creature* creature, uint32 sender, uint32 action, const char* code)
  151. {
  152. if(action == 1)
  153. {
  154.  
  155. if(uint32 rankPoints = atoi(code))
  156. {
  157. if(player->GetRankPoints() < rankPoints)
  158. {
  159. player->GetSession()->SendNotification("Set goal is higher than current RankPoints, current Rankpoints : %u", player->GetRankPoints());
  160. return false;
  161. }
  162. // should be fine, add the guy, set the timer, and remove the set goal RP
  163. betInfo temp;
  164. temp.GUID = player->GetGUID();
  165. temp.Goal = rankPoints;
  166. temp.RemainingRankPoints = rankPoints;
  167. temp.timeLeft = HOUR*24;
  168. betTracker.push_back(temp);
  169. player->ModifyRankPoints(-rankPoints);
  170. CharacterDatabase.PExecute("INSERT INTO `Character_Bets` VALUES(%u, %u, %u, %f)", temp.GUID, temp.Goal, temp.RemainingRankPoints, temp.timeLeft);
  171. }
  172. }
  173. return true;
  174. }
  175. private:
  176. std::vector<betInfo> betTracker;
  177. };
  178.  
  179. void AddSC_Bet_Npc()
  180. {
  181. new Bet_Npc();
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement