Skip to content

Commit

Permalink
improve: refactor sendSaleItemList (opentibiabr#2665)
Browse files Browse the repository at this point in the history
  • Loading branch information
luanluciano93 authored Jul 18, 2024
1 parent 07df814 commit 165a908
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion data-otservbr-global/npc/obi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ npcConfig.shop = {
{ itemName = "hatchet", clientId = 3276, sell = 25 },
{ itemName = "katana", clientId = 3300, sell = 35 },
{ itemName = "mace", clientId = 3286, sell = 30 },
{ itemName = "machete", clientId = 3308, sell = 30 },
{ itemName = "machete", clientId = 3308, sell = 6 },
{ itemName = "rapier", clientId = 3272, buy = 15, sell = 5 },
{ itemName = "sabre", clientId = 3273, buy = 25, sell = 12 },
{ itemName = "scythe", clientId = 3453, buy = 12, sell = 3 },
Expand Down
2 changes: 1 addition & 1 deletion data-otservbr-global/npc/ramina.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ npcConfig.shop = {
{ itemName = "orange", clientId = 3586, buy = 12 },
{ itemName = "peas", clientId = 11683, buy = 5 },
{ itemName = "vial of fruit juice", clientId = 2874, buy = 10, count = 14 },
{ itemName = "vial of water", clientId = 2874, buy = 2, count = 1 },
{ itemName = "vial of water", clientId = 2874, buy = 6, count = 1 },
}
-- On buy npc shop message
npcType.onBuyItem = function(npc, player, itemId, subType, amount, ignore, inBackpacks, totalCost)
Expand Down
8 changes: 4 additions & 4 deletions data-otservbr-global/npc/sessek.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ npcConfig.shop = {
{ itemName = "peas", clientId = 11683, buy = 3 },
{ itemName = "pineapple", clientId = 11459, buy = 14 },
{ itemName = "roll", clientId = 3601, buy = 2 },
{ itemName = "vial of coconut milk", clientId = 2874, buy = 2, count = 15 },
{ itemName = "vial of coconut milk", clientId = 2874, buy = 6, count = 15 },
{ itemName = "vial of fruit juice", clientId = 2874, buy = 6, count = 14 },
{ itemName = "vial of tea", clientId = 2874, buy = 3, count = 17 },
{ itemName = "vial of water", clientId = 2874, buy = 2, count = 1 },
{ itemName = "vial of wine", clientId = 2874, buy = 3, count = 2 },
{ itemName = "vial of tea", clientId = 2874, buy = 6, count = 17 },
{ itemName = "vial of water", clientId = 2874, buy = 6, count = 1 },
{ itemName = "vial of wine", clientId = 2874, buy = 6, count = 2 },
}
-- On buy npc shop message
npcType.onBuyItem = function(npc, player, itemId, subType, amount, ignore, inBackpacks, totalCost)
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/players/wheel/player_wheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ void PlayerWheel::sendOpenWheelWindow(NetworkMessage &msg, uint32_t ownerId) con
// TODO: read items from inventory
auto voc = m_player.getVocation();
m_player.client->sendResourceBalance(RESOURCE_BANK, m_player.getBankBalance());
m_player.client->sendResourceBalance(RESOURCE_INVENTORY, m_player.getMoney());
m_player.client->sendResourceBalance(RESOURCE_INVENTORY_MONEY, m_player.getMoney());
m_player.client->sendResourceBalance(RESOURCE_LESSER_GEMS, m_player.getItemTypeCount(voc->getWheelGemId(WheelGemQuality_t::Lesser)));
m_player.client->sendResourceBalance(RESOURCE_REGULAR_GEMS, m_player.getItemTypeCount(voc->getWheelGemId(WheelGemQuality_t::Regular)));
m_player.client->sendResourceBalance(RESOURCE_GREATER_GEMS, m_player.getItemTypeCount(voc->getWheelGemId(WheelGemQuality_t::Greater)));
Expand Down
49 changes: 22 additions & 27 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4804,7 +4804,7 @@ void ProtocolGame::sendGameNews() {

void ProtocolGame::sendResourcesBalance(uint64_t money /*= 0*/, uint64_t bank /*= 0*/, uint64_t preyCards /*= 0*/, uint64_t taskHunting /*= 0*/, uint64_t forgeDust /*= 0*/, uint64_t forgeSliver /*= 0*/, uint64_t forgeCores /*= 0*/) {
sendResourceBalance(RESOURCE_BANK, bank);
sendResourceBalance(RESOURCE_INVENTORY, money);
sendResourceBalance(RESOURCE_INVENTORY_MONEY, money);
sendResourceBalance(RESOURCE_PREY_CARDS, preyCards);
sendResourceBalance(RESOURCE_TASK_HUNTING, taskHunting);
sendResourceBalance(RESOURCE_FORGE_DUST, forgeDust);
Expand All @@ -4825,40 +4825,35 @@ void ProtocolGame::sendResourceBalance(Resource_t resourceType, uint64_t value)
}

void ProtocolGame::sendSaleItemList(const std::vector<ShopBlock> &shopVector, const std::map<uint16_t, uint16_t> &inventoryMap) {
// Since we already have full inventory map we shouldn't call getMoney here - it is simply wasting cpu power
uint64_t playerMoney = 0;
auto it = inventoryMap.find(ITEM_CRYSTAL_COIN);
if (it != inventoryMap.end()) {
playerMoney += static_cast<uint64_t>(it->second) * 10000;
}
it = inventoryMap.find(ITEM_PLATINUM_COIN);
if (it != inventoryMap.end()) {
playerMoney += static_cast<uint64_t>(it->second) * 100;
}
it = inventoryMap.find(ITEM_GOLD_COIN);
if (it != inventoryMap.end()) {
playerMoney += static_cast<uint64_t>(it->second);
}
sendResourceBalance(RESOURCE_BANK, player->getBankBalance());

NetworkMessage msg;
msg.addByte(0xEE);
msg.addByte(0x00);
msg.add<uint64_t>(player->getBankBalance());
uint16_t currency = player->getShopOwner() ? player->getShopOwner()->getCurrency() : static_cast<uint16_t>(ITEM_GOLD_COIN);
msg.addByte(0xEE);
if (currency == ITEM_GOLD_COIN) {
msg.addByte(0x01);
msg.add<uint64_t>(playerMoney);
// Since we already have full inventory map we shouldn't call getMoney here - it is simply wasting cpu power
uint64_t playerMoney = 0;
auto it = inventoryMap.find(ITEM_CRYSTAL_COIN);
if (it != inventoryMap.end()) {
playerMoney += static_cast<uint64_t>(it->second) * 10000;
}
it = inventoryMap.find(ITEM_PLATINUM_COIN);
if (it != inventoryMap.end()) {
playerMoney += static_cast<uint64_t>(it->second) * 100;
}
it = inventoryMap.find(ITEM_GOLD_COIN);
if (it != inventoryMap.end()) {
playerMoney += static_cast<uint64_t>(it->second);
}
sendResourceBalance(RESOURCE_INVENTORY_MONEY, playerMoney);
} else {
msg.addByte(oldProtocol ? 0x01 : 0x02);
uint64_t newCurrency = 0;
uint64_t customCurrencyValue = 0;
auto search = inventoryMap.find(currency);
if (search != inventoryMap.end()) {
newCurrency += static_cast<uint64_t>(search->second);
customCurrencyValue += static_cast<uint64_t>(search->second);
}
msg.add<uint64_t>(newCurrency);
sendResourceBalance(oldProtocol ? RESOURCE_INVENTORY_MONEY : RESOURCE_INVENTORY_CURRENCY_CUSTOM, customCurrencyValue);
}

NetworkMessage msg;
msg.addByte(0x7B);

if (oldProtocol) {
Expand All @@ -4874,7 +4869,7 @@ void ProtocolGame::sendSaleItemList(const std::vector<ShopBlock> &shopVector, co
continue;
}

it = inventoryMap.find(shopBlock.itemId);
auto it = inventoryMap.find(shopBlock.itemId);
if (it != inventoryMap.end()) {
msg.add<uint16_t>(shopBlock.itemId);
if (oldProtocol) {
Expand Down
3 changes: 2 additions & 1 deletion src/server/server_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ enum SessionEndInformations : uint8_t {

enum Resource_t : uint8_t {
RESOURCE_BANK = 0x00,
RESOURCE_INVENTORY = 0x01,
RESOURCE_INVENTORY_MONEY = 0x01,
RESOURCE_INVENTORY_CURRENCY_CUSTOM = 0x02,
RESOURCE_PREY_CARDS = 0x0A,
RESOURCE_TASK_HUNTING = 0x32,
RESOURCE_FORGE_DUST = 0x46,
Expand Down

0 comments on commit 165a908

Please sign in to comment.