Skip to content

Commit

Permalink
Fix some npcs bugs and change behaviour (#396)
Browse files Browse the repository at this point in the history
Fixed:
Total cost for custom currencys
Bank npcs depot, transfer and withdraw logic

Changed:
MsgContains behaviour, added new function "MsgFind" so that we avoid some bugs in messages that have the same words
Added debug log for assistant with debug build
  • Loading branch information
dudantas authored Jun 30, 2022
1 parent 0b65754 commit 5b25b43
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 22 deletions.
9 changes: 9 additions & 0 deletions data/npclib/npc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ function MsgContains(message, keyword)
return true
end

return lowerMessage:find(lowerKeyword) and not lowerMessage:find('(%w+)' .. lowerKeyword)
end

function MsgFind(message, keyword)
local lowerMessage, lowerKeyword = message:lower(), keyword:lower()
if lowerMessage == lowerKeyword then
return true
end

return string.find(lowerMessage, lowerKeyword)
and string.find(lowerMessage, lowerKeyword.. '(%w+)')
and string.find(lowerMessage, '(%w+)' .. lowerKeyword)
Expand Down
80 changes: 63 additions & 17 deletions data/npclib/npc_system/bank_system.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,26 @@ function Npc:parseBank(message, npc, creature, npcHandler)
return true
end
-- Deposit
elseif MsgContains(message, "deposit") then
elseif MsgFind(message, "deposit all") then
count[playerId] = player:getMoney()
if count[playerId] < 1 then
npcHandler:say("You do not have enough gold.", npc, creature)
npcHandler:setTopic(playerId, 0)
return false
end
if not isValidMoney(count[playerId]) then
npcHandler:say("Sorry, but you can't deposit that much.", npc, creature)
npcHandler:setTopic(playerId, 0)
return false
npcHandler:say(string.format("Would you really like to deposit %d gold?", count[playerId]), npc, creature)
npcHandler:setTopic(playerId, 2)
elseif MsgContains(message, "deposit") then
if string.match(message, "%d+") then
count[playerId] = getMoneyCount(message)
if isValidMoney(count[playerId]) then
npcHandler:say(string.format("Would you really like to deposit %d gold?",
count[playerId]), npc, creature)
npcHandler:setTopic(playerId, 2)
else
npcHandler:say("You do not have enough gold.", npc, creature)
npcHandler:setTopic(playerId, 0)
end
return true
end
count[playerId] = player:getMoney()
npcHandler:say("Please tell me how much gold it is you would like to deposit.", npc, creature)
npcHandler:setTopic(playerId, 1)
elseif MsgContains(message, "deposit all") then
count[playerId] = player:getMoney()
npcHandler:say(string.format("Would you really like to deposit %d gold?", count[playerId]), npc, creature)
npcHandler:setTopic(playerId, 2)
elseif MsgContains(message, "all") then
if npcHandler:getTopic(playerId) == 1 then
count[playerId] = player:getMoney()
Expand Down Expand Up @@ -154,6 +156,50 @@ function Npc:parseBank(message, npc, creature, npcHandler)
return true
-- Transfer
elseif MsgContains(message, "transfer") then
count[playerId] = getMoneyCount(message)
transfer[playerId] = string.match(message, "[^transfer %d+ to ].+")
if string.match(message,"%d+") then
if player:getBankBalance() < count[playerId] then
npcHandler:say("There is not enough gold on your account.", npc, creature)
npcHandler:setTopic(playerId, 0)
return false
end
if MsgContains(message,"to") then
if player:getName():lower() == transfer[playerId]:lower() then
npcHandler:say("Fill in this field with person who receives your gold!", npc, creature)
npcHandler:setTopic(playerId, 0)
return true
end
if playerExists(transfer[playerId]) then
local arrayDenied = {
"accountmanager",
"rooksample",
"druidsample",
"sorcerersample",
"knightsample",
"paladinsample"
}
if table.contains(arrayDenied, string.gsub(transfer[playerId], " ", "")) then
npcHandler:say("This player does not exist.", npc, creature)
npcHandler:setTopic(playerId, 0)
return true
end
npcHandler:say(string.format("So you would like to transfer %d gold to %s?",
count[playerId], string.titleCase(transfer[playerId])), npc, creature)
npcHandler:setTopic(playerId, 13)
return true
else
npcHandler:say("This player does not exist.", npc, creature)
npcHandler:setTopic(playerId, 0)
return false
end
end
if isValidMoney(count[playerId]) then
npcHandler:say(string.format("Who would you like transfer %d gold to?", count[playerId]), npc, creature)
npcHandler:setTopic(playerId, 12)
return true
end
end
npcHandler:say("Please tell me the amount of gold you would like to transfer.", npc, creature)
npcHandler:setTopic(playerId, 11)
elseif npcHandler:getTopic(playerId) == 11 then
Expand All @@ -164,15 +210,15 @@ function Npc:parseBank(message, npc, creature, npcHandler)
return true
end
if isValidMoney(count[playerId]) then
npcHandler:say("Who would you like transfer %d gold to?", npc, creature)
npcHandler:say(string.format("Who would you like transfer %d gold to?", count[playerId]), npc, creature)
npcHandler:setTopic(playerId, 12)
else
npcHandler:say("There is not enough gold on your account.", npc, creature)
npcHandler:setTopic(playerId, 0)
end
elseif npcHandler:getTopic(playerId) == 12 then
transfer[playerId] = message
if player:getName() == transfer[playerId] then
if player:getName():lower() == transfer[playerId]:lower() then
npcHandler:say("Fill in this field with person who receives your gold!", npc, creature)
npcHandler:setTopic(playerId, 0)
return true
Expand Down Expand Up @@ -335,7 +381,7 @@ function Npc:parseGuildBank(message, npc, creature, playerId, npcHandler)
npcHandler:say(string.format("Your guild account balance is %d gold.", player:getGuild():getBankBalance()), npc, creature)
return true
-- Guild deposit
elseif MsgContains(message, "guild deposit") then
elseif MsgFind(message, "guild deposit") then
if not player:getGuild() then
npcHandler:say("You are not a member of a guild.", npc, creature)
npcHandler:setTopic(playerId, 0)
Expand Down
7 changes: 4 additions & 3 deletions src/creatures/npcs/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "creatures/combat/spells.h"
#include "lua/creature/events.h"


int32_t Npc::despawnRange;
int32_t Npc::despawnRadius;

Expand Down Expand Up @@ -244,14 +243,16 @@ void Npc::onPlayerBuyItem(Player* player, uint16_t itemId,
}
}

int64_t totalCost = buyPrice * amount;
uint32_t totalCost = buyPrice * amount;
if (getCurrency() == ITEM_GOLD_COIN) {
if (!g_game().removeMoney(player, totalCost, 0, true)) {
SPDLOG_ERROR("[Npc::onPlayerBuyItem (removeMoney)] - Player {} have a problem for buy item {} on shop for npc {}", player->getName(), itemId, getName());
SPDLOG_DEBUG("[Information] Player {} buyed item {} on shop for npc {}, at position {}", player->getName(), itemId, getName(), player->getPosition().toString());
return;
}
} else if(!player->removeItemOfType(getCurrency(), buyPrice, -1, false)) {
} else if(!player->removeItemOfType(getCurrency(), totalCost, -1, false)) {
SPDLOG_ERROR("[Npc::onPlayerBuyItem (removeItemOfType)] - Player {} have a problem for buy item {} on shop for npc {}", player->getName(), itemId, getName());
SPDLOG_DEBUG("[Information] Player {} buyed item {} on shop for npc {}, at position {}", player->getName(), itemId, getName(), player->getPosition().toString());
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ void ProtocolGame::parsePacket(NetworkMessage& msg)
return;
}

//a dead player can not performs actions
// A dead player can not performs actions
if (player->isDead() || player->getHealth() <= 0) {
if (recvbyte == 0x14) {
disconnect();
Expand Down Expand Up @@ -641,7 +641,7 @@ void ProtocolGame::parsePacket(NetworkMessage& msg)
}

// Modules system
if(player && recvbyte != 0xD3){
if (player && recvbyte != 0xD3) {
g_dispatcher().addTask(createTask(std::bind(&Modules::executeOnRecvbyte, &g_modules(), player->getID(), msg, recvbyte)));
}

Expand Down

0 comments on commit 5b25b43

Please sign in to comment.