Last active
March 11, 2018 22:19
-
-
Save mindsear/28a5b617c07c756f9a04b360e2ac0388 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//updated | |
// added check for negative bets | |
#include "ScriptedCreature.h" | |
#include "ScriptedGossip.h" | |
#include "WorldSession.h" | |
#include "Player.h" | |
#include "ScriptMgr.h" | |
#include "Random.h" | |
#include "Chat.h" | |
/* config */ | |
#define AMOUNT_IN_GOLD true | |
#define WIN_PERCENTAGE 50 | |
/* config end */ | |
#define GOLD(b,f) f ? b/10000 : b*10000 | |
class Gamble_npc : public CreatureScript | |
{ | |
public: | |
Gamble_npc() : CreatureScript("Gambler") {} | |
struct Gamble_npc_AI : public ScriptedAI | |
{ | |
Gamble_npc_AI(Creature* creature) : ScriptedAI(creature) { } | |
bool GossipHello(Player* player) | |
{ | |
ClearGossipMenuFor(player); | |
std::string text = "Enter money amount " + std::string(AMOUNT_IN_GOLD ? "in gold" : "in copper"); | |
AddGossipItemFor(player, GOSSIP_ICON_MONEY_BAG, "|TInterface\\Icons\\INV_Misc_Coin_02:30|t Let's Gamble!", GOSSIP_SENDER_MAIN, 1, text, 0, true); | |
AddGossipItemFor(player, GOSSIP_ICON_CHAT, "|TInterface\\Icons\\Spell_Shadow_SacrificialShield:30|t Goodbye", GOSSIP_SENDER_MAIN, 500); | |
SendGossipMenuFor(player, DEFAULT_GOSSIP_MESSAGE, me->GetGUID()); | |
return true; | |
} | |
bool GossipSelect(Player* player, uint32 /*menuId*/, uint32 gossipListId) | |
{ | |
uint32 const sender = player->PlayerTalkClass->GetGossipOptionSender(gossipListId); | |
uint32 const action = player->PlayerTalkClass->GetGossipOptionAction(gossipListId); | |
ClearGossipMenuFor(player); | |
if (sender == GOSSIP_SENDER_MAIN) | |
{ | |
if (action == 500) | |
{ | |
CloseGossipMenuFor(player); | |
} | |
} | |
return true; | |
} | |
bool GossipSelectCode(Player* player, uint32 menuId, uint32 gossipListId, char const* code) | |
{ | |
uint32 const sender = player->PlayerTalkClass->GetGossipOptionSender(gossipListId); | |
uint32 const action = player->PlayerTalkClass->GetGossipOptionAction(gossipListId); | |
int32 betAmount = atoi(code); | |
bool isNegative = betAmount < 0; | |
if (!betAmount || isNegative) | |
{ | |
player->GetSession()->SendNotification("Invalid bet amount"); | |
GossipHello(player); | |
return false; | |
} | |
int32 money = AMOUNT_IN_GOLD ? GOLD(player->GetMoney(), true) : player->GetMoney(); | |
if (money < betAmount) | |
{ | |
player->GetSession()->SendNotification("You don't have enough money"); | |
GossipHello(player); | |
return false; | |
} | |
int32 doubledBetAmount = betAmount * 2; | |
std::stringstream ss; | |
ss << "|cffE68318[Gamble System]:|r |cff00ff00You won and doubled your bet! +" << doubledBetAmount << " gold"; | |
std::string winTextStr = ss.str(); | |
std::stringstream ss1; | |
ss1 << "|cffE68318[Gamble System]:|r |cffFF0000You lost.. better luck next time! -" << betAmount << " gold"; | |
std::string lossTextStr = ss1.str(); | |
int32 randVal = 100 / WIN_PERCENTAGE - 1; | |
if (!urand(0, randVal)) // player won | |
{ | |
player->ModifyMoney(AMOUNT_IN_GOLD ? GOLD(doubledBetAmount, false) : doubledBetAmount); | |
player->GetSession()->SendAreaTriggerMessage(winTextStr.c_str()); | |
ChatHandler(player->GetSession()).PSendSysMessage(winTextStr.c_str()); | |
} | |
else | |
{ | |
player->ModifyMoney(AMOUNT_IN_GOLD ? GOLD(-betAmount, false) : ~betAmount + 1u); | |
player->GetSession()->SendAreaTriggerMessage(lossTextStr.c_str()); | |
ChatHandler(player->GetSession()).PSendSysMessage(lossTextStr.c_str()); | |
} | |
GossipHello(player); | |
return true; | |
} | |
}; | |
CreatureAI* GetAI(Creature* creature) const override | |
{ | |
return new Gamble_npc_AI(creature); | |
} | |
}; | |
void AddSC_Gamble_npc() | |
{ | |
new Gamble_npc(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment