Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ScriptPCH.h"
- struct betInfo
- {
- uint32 GUID;
- uint32 Goal;
- uint32 RemainingRankPoints;
- float timeLeft;
- };
- class Bet_Npc : public CreatureScript, PlayerScript, WorldScript
- {
- public:
- Bet_Npc() : CreatureScript("Bet_Npc"), PlayerScript("Check"), WorldScript("Timer") {Timer = 0;}
- uint32 Timer;
- void OnRankPointsReceive(Player* obtainer, int32 amount)
- {
- if(amount <= 0 || betTracker.empty())
- return;
- for(std::vector<betInfo>::iterator itr = betTracker.begin(); itr != betTracker.end(); ++itr)
- {
- if(itr->GUID == obtainer->GetGUID())
- {
- if(itr->RemainingRankPoints <= amount )
- {
- itr->RemainingRankPoints = 0;
- ChatHandler(obtainer->GetSession()).SendSysMessage("[Goal System] : You have successfully reached your goal, please visit the Betting Npc to claim your reward");
- }
- else
- {
- itr->RemainingRankPoints -= amount;
- ChatHandler(obtainer->GetSession()).PSendSysMessage("[Goal System] : %u Rank Points remaining to complete your goal", itr->RemainingRankPoints);
- }
- CharacterDatabase.PExecute("UPDATE `Character_Bets` SET `RemainingGoal` = %u WHERE `GUID` = %u", itr->RemainingRankPoints, obtainer->GetGUIDLow());
- }
- }
- }
- void OnStartup()
- {
- QueryResult result = CharacterDatabase.Query("SELECT * FROM `Character_Bets`");
- if(!result)
- return;
- do{
- Field* fields = result->Fetch();
- betInfo info;
- info.GUID = fields[0].GetUInt64();
- info.Goal = fields[1].GetUInt32();
- info.RemainingRankPoints = fields[2].GetUInt32();
- info.timeLeft = fields[3].GetUInt64();
- betTracker.push_back(info);
- } while(result->NextRow());
- }
- void OnUpdate(uint32 diff)
- {
- if(Timer >= 300000)
- {
- Timer = 0;
- CharacterDatabase.PExecute("UPDATE `Character_Bets` SET `TimeLeft` = `TimeLeft` - 300 WHERE `Timeleft` > 300");
- }
- else
- Timer += diff;
- if(betTracker.empty())
- return;
- float DiffinSecs = diff / 1000;
- for(std::vector<betInfo>::iterator itr = betTracker.begin(); itr != betTracker.end(); )
- {
- if(itr->timeLeft < DiffinSecs && itr->RemainingRankPoints != 0)
- itr->timeLeft = 0;
- if(itr->RemainingRankPoints != 0)
- itr->timeLeft -= DiffinSecs;
- if(!itr->timeLeft)
- {
- if(Player* player = sObjectAccessor->FindPlayer(itr->GUID))
- player->GetSession()->SendNotification("[Goal System] : You have failed your goal");
- betTracker.erase(itr++);
- continue;
- }
- ++itr;
- }
- }
- bool OnGossipHello(Player* player, Creature* creature)
- {
- bool found = false;
- bool Completed = false;
- uint32 seconds;
- uint32 Reward;
- std::vector<betInfo>::iterator Pos = betTracker.begin();
- for(; Pos != betTracker.end(); ++Pos)
- {
- if(Pos->GUID == player->GetGUIDLow())
- {
- found = true;
- if(Pos->RemainingRankPoints == 0)
- Completed = true;
- seconds = Pos->timeLeft;
- Reward = Pos->Goal * 2;
- break;
- }
- }
- if(found && !Completed)
- {
- sLog->outError(LOG_FILTER_GENERAL, "seconds : %u", seconds);
- sLog->outError(LOG_FILTER_GENERAL, "itr->timeleft : %f", Pos->timeLeft);
- sLog->outError(LOG_FILTER_GENERAL, "Reward : %u", Reward);
- sLog->outError(LOG_FILTER_GENERAL, "Position : %i", std::distance(betTracker.begin(), Pos));
- std::ostringstream ss;
- ss << "Already set a goal, time left :";
- uint64 secs = seconds % 60;
- uint64 minutes = seconds % 3600 / 60;
- uint64 hours = seconds / 3600;
- if(hours)
- hours != 1 ? ss << hours << " Hours" : ss << hours << " Hour";
- if(minutes)
- minutes != 1 ? ss << minutes << " Minutes " : ss << minutes << " Minutes ";
- if(secs || (!hours && !minutes))
- secs != 1 ? ss << secs << " Seconds " : ss << secs << " Second ";
- player->ADD_GOSSIP_ITEM(0, ss.str().c_str(), GOSSIP_SENDER_MAIN, 0);
- }
- else if(found && Completed)
- {
- player->GetSession()->SendAreaTriggerMessage("You claimed your reward, %u Rank Points", Reward);
- player->ModifyRankPoints(Reward);
- betTracker.erase(Pos);
- player->PlayerTalkClass->SendCloseGossip();
- CharacterDatabase.PExecute("DELETE FROM `Character_Bets` WHERE `GUID` = %u", player->GetGUIDLow());
- }
- else
- player->ADD_GOSSIP_ITEM_EXTENDED(0, "Set a goal", GOSSIP_SENDER_MAIN, 1, "RankPoints to obtain in 24 hours : ", 0, true);
- player->ADD_GOSSIP_ITEM(0, "Nevermind", GOSSIP_SENDER_MAIN, 2);
- player->PlayerTalkClass->SendGossipMenu(907, creature->GetGUID());
- return true;
- }
- bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action)
- {
- if(action == 2)
- player->PlayerTalkClass->SendCloseGossip();
- return true;
- }
- bool OnGossipSelectCode(Player* player, Creature* creature, uint32 sender, uint32 action, const char* code)
- {
- if(action == 1)
- {
- if(uint32 rankPoints = (uint32)atoi(code))
- {
- if(player->GetRankPoints() < rankPoints)
- {
- player->GetSession()->SendNotification("Set goal is higher than current RankPoints, current Rankpoints : %u", player->GetRankPoints());
- return false;
- }
- // should be fine, add the guy, set the timer, and remove the set goal RP
- betInfo temp;
- temp.GUID = player->GetGUIDLow();
- temp.Goal = rankPoints;
- temp.RemainingRankPoints = rankPoints;
- temp.timeLeft = HOUR*24;
- betTracker.push_back(temp);
- player->ModifyRankPoints(-1*rankPoints);
- CharacterDatabase.PExecute("INSERT INTO `Character_Bets` VALUES(%u, %u, %u, %f)", temp.GUID, temp.Goal, temp.RemainingRankPoints, temp.timeLeft);
- }
- }
- return true;
- }
- private:
- std::vector<betInfo> betTracker;
- };
- void AddSC_Bet_Npc()
- {
- new Bet_Npc();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement