Advertisement
Rochet2

Untitled

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