Skip to content

Commit

Permalink
Large code cleanup to be less crashy.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnchyDev committed May 22, 2024
1 parent c5c6a7f commit 9b1a076
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 267 deletions.
24 changes: 8 additions & 16 deletions conf/ladyluck.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,20 @@
LadyLuck.Enable = 0

#
# LadyLuck.Currency
# Description: The currency used to enter the lottery room.
# Default: 37711
# LadyLuck.UnlockItem
# Description: The item used to open the lottery boxes.
# Default: 5518 (Tiny Iron Key)
#

LadyLuck.Currency = 37711
LadyLuck.UnlockItem = 5518

#
# LadyLuck.CurrencyCount
# Description: The amount of currency used to enter the lottery room.
# Default: 3
# LadyLuck.UnlockItemCount
# Description: The amount of item used to open the lottery boxes.
# Default: 1
#

LadyLuck.CurrencyCount = 3

#
# LadyLuck.Money
# Description: The amount of money used to enter the lottery room in copper.
# Default: 1000000
#

LadyLuck.Money = 1000000
LadyLuck.UnlockItemCount = 1

########################################
# Lottery Location
Expand Down
258 changes: 45 additions & 213 deletions src/LadyLuck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

bool LadyLuckCreatureScript::OnGossipHello(Player* player, Creature* creature)
{
if (!ladyLuckEnabled)
if (!sConfigMgr->GetOption<bool>("LadyLuck.Enable", false))
{
return true;
}
Expand All @@ -26,165 +26,48 @@ bool LadyLuckCreatureScript::OnGossipHello(Player* player, Creature* creature)

void LadyLuckCreatureScript::EnterLottery(Player* player)
{
if (!IsInLottery(player))
{
PlayerLotteryInfo playerInfo;

playerInfo.playerGuid = player->GetGUID();

playerInfo.previousLocation.Map = player->GetMapId();
playerInfo.previousLocation.X = player->GetPositionX();
playerInfo.previousLocation.Y = player->GetPositionY();
playerInfo.previousLocation.Z = player->GetPositionZ();
playerInfo.previousLocation.O = player->GetOrientation();

playerInfo.canLoot = true;
playerInfo.inLottery = true;

playerLotteryInfo.push_back(playerInfo);

player->TeleportTo(ladyLuckTele.Map, ladyLuckTele.X, ladyLuckTele.Y, ladyLuckTele.Z, ladyLuckTele.O);
}
else
{
UpdateCanLoot(player, true);
}
player->m_recallMap = player->GetMapId();
player->m_recallX = player->GetPositionX();
player->m_recallY = player->GetPositionY();
player->m_recallZ = player->GetPositionZ();
player->m_recallO = player->GetOrientation();

player->TeleportTo(sConfigMgr->GetOption<uint32>("LadyLuck.TeleMap", 449),
sConfigMgr->GetOption<float>("LadyLuck.TeleX", 0.072697),
sConfigMgr->GetOption<float>("LadyLuck.TeleY", 9.618815),
sConfigMgr->GetOption<float>("LadyLuck.TeleZ", -0.227239),
sConfigMgr->GetOption<float>("LadyLuck.TeleO", 1.584149));
}

void LadyLuckCreatureScript::SayGoodbye(Player* player, Creature* /*creature*/)
{
CloseGossipMenuFor(player);
}

void LadyLuckCreatureScript::DeductCurrency(Player* player, uint32 count)
{
Item* currency = player->GetItemByEntry(ladyLuckCurrency);
player->DestroyItemCount(currency, count, true);
}

void LadyLuckCreatureScript::ValidateCurrency(Player* player, Creature* creature)
{
uint32 currencyAmount = player->GetItemCount(ladyLuckCurrency, false);

if (currencyAmount >= ladyLuckCurrencyCount)
{
CloseGossipMenuFor(player);
DeductCurrency(player, ladyLuckCurrencyCount);
EnterLottery(player);
}
else
{
ClearGossipMenuFor(player);
AddGossipItemFor(player, GOSSIP_ICON_CHAT, "I see, goodbye.", GOSSIP_SENDER_MAIN, LADYLUCK_GOODBYE);
SendGossipMenuFor(player, LADYLUCK_GOSSIPTEXT_FAIL_CURRENCY, creature->GetGUID());
}
}

void LadyLuckCreatureScript::ValidateMoney(Player* player, Creature* creature)
{
uint32 playerMoney = player->GetMoney();

if (playerMoney >= ladyLuckMoney)
{
CloseGossipMenuFor(player);
DeductMoney(player, ladyLuckMoney);
EnterLottery(player);
}
else
{
ClearGossipMenuFor(player);
AddGossipItemFor(player, GOSSIP_ICON_CHAT, "I see, goodbye.", GOSSIP_SENDER_MAIN, LADYLUCK_GOODBYE);
SendGossipMenuFor(player, LADYLUCK_GOSSIPTEXT_FAIL_MONEY, creature->GetGUID());
}
}

void LadyLuckCreatureScript::DeductMoney(Player* player, uint32 amount)
bool LadyLuckCreatureScript::IsInLottery(Player* player)
{
player->ModifyMoney(-amount, true);
}

bool IsInLottery(Player* player)
{
for (auto it = playerLotteryInfo.begin(); it != playerLotteryInfo.end(); ++it)
{
if (it->playerGuid == player->GetGUID() && it->inLottery)
{
return true;
}
}

return false;
}

void UpdateCanLoot(Player* player, bool state)
{
for (auto it = playerLotteryInfo.begin(); it != playerLotteryInfo.end(); ++it)
{
if (it->playerGuid == player->GetGUID())
{
it->canLoot = state;
}
}
}

bool CanLoot(Player* player)
{
for (auto it = playerLotteryInfo.begin(); it != playerLotteryInfo.end(); ++it)
{
if (it->playerGuid == player->GetGUID())
{
return it->canLoot;
}
}

return false;
return player->GetMapId() == sConfigMgr->GetOption<uint32>("LadyLuck.TeleMap", 449);
}

void LadyLuckCreatureScript::PromptExit(Player* player, Creature* creature)
{
ClearGossipMenuFor(player);

AddGossipItemFor(player, GOSSIP_ICON_CHAT, "Yes, please.", GOSSIP_SENDER_MAIN, LADYLUCK_EXITLOTTERY);
AddGossipItemFor(player, GOSSIP_ICON_CHAT, "No, I would like to open another box.", GOSSIP_SENDER_MAIN, LADYLUCK_ENTERLOTTERY);
AddGossipItemFor(player, GOSSIP_ICON_CHAT, "No, I would like to open another box.", GOSSIP_SENDER_MAIN, LADYLUCK_GOODBYE);

SendGossipMenuFor(player, LADYLUCK_GOSSIPTEXT_EXIT, creature->GetGUID());
}

void LadyLuckCreatureScript::ExitLottery(Player* player)
{
CloseGossipMenuFor(player);

for (auto it = playerLotteryInfo.begin(); it != playerLotteryInfo.end(); ++it)
{
if (it->playerGuid == player->GetGUID())
{
RestorePlayer(player, &it->previousLocation);
it->inLottery = false;
break;
}
}
}

void LadyLuckCreatureScript::RestorePlayer(Player* player, TeleportInfo* teleInfo)
{
player->TeleportTo(teleInfo->Map, teleInfo->X, teleInfo->Y, teleInfo->Z, teleInfo->O);
RestorePlayerLocation(player);
}

void LadyLuckCreatureScript::DisplayLotteryOptions(Player* player, Creature* creature)
void LadyLuckCreatureScript::RestorePlayerLocation(Player* player)
{
ClearGossipMenuFor(player);

if (ladyLuckMoney > 0)
{
AddGossipItemFor(player, GOSSIP_ICON_CHAT, "I would like to use gold.", GOSSIP_SENDER_MAIN, LADYLUCK_ENTERLOTTERY_GOLD, "Are you sure you would like to use gold?", ladyLuckMoney, false);
}

if (ladyLuckCurrency > 0)
{
std::string itemName = sObjectMgr->GetItemTemplate(ladyLuckCurrency)->Name1;
ladyLuckCurrencyStr = Acore::StringFormatFmt("Are you sure you would like to use currency?|n|nThis will cost you :|n{}x[{}]", ladyLuckCurrencyCount, itemName);
AddGossipItemFor(player, GOSSIP_ICON_CHAT, "I would like to use currency.", GOSSIP_SENDER_MAIN, LADYLUCK_ENTERLOTTERY_CURRENCY, ladyLuckCurrencyStr, 0, false);
}

SendGossipMenuFor(player, LADYLUCK_GOSSIPTEXT_SELECT_COST, creature);
player->TeleportTo(player->m_recallMap, player->m_recallX, player->m_recallY, player->m_recallZ, player->m_recallO);
}

bool LadyLuckCreatureScript::OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action)
Expand All @@ -197,15 +80,7 @@ bool LadyLuckCreatureScript::OnGossipSelect(Player* player, Creature* creature,
switch (action)
{
case LADYLUCK_ENTERLOTTERY:
DisplayLotteryOptions(player, creature);
break;

case LADYLUCK_ENTERLOTTERY_CURRENCY:
ValidateCurrency(player, creature);
break;

case LADYLUCK_ENTERLOTTERY_GOLD:
ValidateMoney(player, creature);
EnterLottery(player);
break;

case LADYLUCK_EXITLOTTERY:
Expand All @@ -223,7 +98,7 @@ bool LadyLuckCreatureScript::OnGossipSelect(Player* player, Creature* creature,

bool LadyLuckGameObjectScript::OnGossipHello(Player* player, GameObject* go)
{
if (!ladyLuckEnabled)
if (!sConfigMgr->GetOption<bool>("LadyLuck.Enable", false))
{
return true;
}
Expand All @@ -232,8 +107,8 @@ bool LadyLuckGameObjectScript::OnGossipHello(Player* player, GameObject* go)

if (CanLoot(player))
{
AddGossipItemFor(player, GOSSIP_ICON_CHAT, "Open the box", GOSSIP_SENDER_MAIN, LOTTERYBOX_OPEN);
AddGossipItemFor(player, GOSSIP_ICON_CHAT, "I want to open a different box.", GOSSIP_SENDER_MAIN, LOTTERYBOX_GOODBYE);
AddGossipItemFor(player, GOSSIP_ICON_CHAT, "<Unlock the box>", GOSSIP_SENDER_MAIN, LOTTERYBOX_OPEN);
AddGossipItemFor(player, GOSSIP_ICON_CHAT, "<Open a different box>", GOSSIP_SENDER_MAIN, LOTTERYBOX_GOODBYE);
SendGossipMenuFor(player, LOTTERYBOX_GOSSIPTEXT, go->GetGUID());
}
else
Expand All @@ -244,6 +119,16 @@ bool LadyLuckGameObjectScript::OnGossipHello(Player* player, GameObject* go)
return true;
}

bool LadyLuckGameObjectScript::CanLoot(Player* player)
{
return player->HasItemCount(sConfigMgr->GetOption<uint32>("LadyLuck.UnlockItem", 5518), sConfigMgr->GetOption<uint32>("LadyLuck.UnlockItemCount", 1));
}

void LadyLuckGameObjectScript::DeductUnlockItem(Player* player)
{
player->DestroyItemCount(sConfigMgr->GetOption<uint32>("LadyLuck.UnlockItem", 5518), sConfigMgr->GetOption<uint32>("LadyLuck.UnlockItemCount", 1), true);
}

std::vector<LotteryLoot> GetLootForRoll(Player* player, uint32 roll)
{
std::vector<LotteryLoot> lootPool;
Expand All @@ -265,6 +150,17 @@ void LadyLuckGameObjectScript::OpenLotteryBox(Player* player)
{
CloseGossipMenuFor(player);

if (!CanLoot(player))
{
return;
}

DeductUnlockItem(player);
RewardLootItem(player);
}

void LadyLuckGameObjectScript::RewardLootItem(Player* player)
{
uint32 roll = urand(0, 100);
std::vector<LotteryLoot> lootPool = GetLootForRoll(player, roll);

Expand Down Expand Up @@ -297,7 +193,6 @@ void LadyLuckGameObjectScript::OpenLotteryBox(Player* player)
{
Item* item = player->StoreNewItem(dest, lootItem.itemId, true);
player->SendNewItem(item, lootItem.itemCount, true, false);
UpdateCanLoot(player, false);
}
else
{
Expand Down Expand Up @@ -327,44 +222,13 @@ bool LadyLuckGameObjectScript::OnGossipSelect(Player* player, GameObject* /*go*/
return true;
}

void LadyLuckWorldScript::OnShutdownInitiate(ShutdownExitCode /*code*/, ShutdownMask /*mask*/)
{
for (auto& pLotInfo : playerLotteryInfo)
{
if (pLotInfo.inLottery)
{
CharacterDatabase.Execute("REPLACE INTO `ladyluck_restore_info` (guid, canLoot, map, x, y, z, o) VALUES ({}, {}, {}, {}, {}, {}, {})",
pLotInfo.playerGuid.GetRawValue(), pLotInfo.canLoot,
pLotInfo.previousLocation.Map,
pLotInfo.previousLocation.X, pLotInfo.previousLocation.Y, pLotInfo.previousLocation.Z, pLotInfo.previousLocation.O);
}
else
{
CharacterDatabase.Execute("DELETE FROM `ladyluck_restore_info` WHERE guid = {}", pLotInfo.playerGuid.GetRawValue());
}
}
}

void LadyLuckWorldScript::OnAfterConfigLoad(bool reload)
{
if (reload)
{
lotteryLootPool.clear();
}

ladyLuckEnabled = sConfigMgr->GetOption<bool>("LadyLuck.Enable", false);

ladyLuckCurrency = sConfigMgr->GetOption<uint32>("LadyLuck.Currency", 37711);
ladyLuckCurrencyCount = sConfigMgr->GetOption<uint32>("LadyLuck.CurrencyCount", 3);

ladyLuckMoney = sConfigMgr->GetOption<uint32>("LadyLuck.Money", 1000000);

ladyLuckTele.Map = sConfigMgr->GetOption<uint32>("LadyLuck.TeleMap", 449);
ladyLuckTele.X = sConfigMgr->GetOption<float>("LadyLuck.TeleX", 0.072697);
ladyLuckTele.Y = sConfigMgr->GetOption<float>("LadyLuck.TeleY", 9.618815);
ladyLuckTele.Z = sConfigMgr->GetOption<float>("LadyLuck.TeleZ", -0.227239);
ladyLuckTele.O = sConfigMgr->GetOption<float>("LadyLuck.TeleO", 1.584149);

QueryResult result = WorldDatabase.Query("SELECT item_id, item_count, level_min, level_max, roll FROM ladyluck_lottery_loot");

if (!result)
Expand All @@ -386,40 +250,8 @@ void LadyLuckWorldScript::OnAfterConfigLoad(bool reload)

lotteryLootPool.push_back(lotteryLoot);
} while (result->NextRow());

result = CharacterDatabase.Query("SELECT guid, canLoot, map, x, y, z, o FROM ladyluck_restore_info");

if (!result)
{
LOG_ERROR("module", "Failed to load `ladyluck_restore_info` table!");
return;
}

do
{
Field* fields = result->Fetch();

TeleportInfo teleInfo =
{
fields[2].Get<uint32>(), //Map
fields[3].Get<float>(), //X
fields[4].Get<float>(), //Y
fields[5].Get<float>(), //Z
fields[6].Get<float>() //O
};
PlayerLotteryInfo pLotInfo =
{
teleInfo, //TeleportInfo
ObjectGuid(fields[0].Get<uint64>()), //Guid
fields[1].Get<bool>(), //CanLoot
true //InLottery
};

playerLotteryInfo.push_back(pLotInfo);
} while (result->NextRow());
}

// Add all scripts in one
void AddLadyLuckScripts()
{
new LadyLuckWorldScript();
Expand Down
Loading

0 comments on commit 9b1a076

Please sign in to comment.