Skip to content

Commit

Permalink
feat: cyclopedia item summary (#2601)
Browse files Browse the repository at this point in the history
  • Loading branch information
phacUFPE committed May 9, 2024
1 parent 94f178c commit 09765f8
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 16 deletions.
54 changes: 51 additions & 3 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3930,16 +3930,22 @@ bool Player::removeItemCountById(uint16_t itemId, uint32_t itemAmount, bool remo
return false;
}

ItemsTierCountList Player::getInventoryItemsId() const {
ItemsTierCountList Player::getInventoryItemsId(bool ignoreStoreInbox /* false */) const {
ItemsTierCountList itemMap;
for (int32_t i = CONST_SLOT_FIRST; i <= CONST_SLOT_LAST; i++) {
std::shared_ptr<Item> item = inventory[i];
if (!item) {
continue;
}

(itemMap[item->getID()])[item->getTier()] += Item::countByType(item, -1);
if (std::shared_ptr<Container> container = item->getContainer()) {
const bool isStoreInbox = item->getID() == ITEM_STORE_INBOX;

if (!isStoreInbox) {
(itemMap[item->getID()])[item->getTier()] += Item::countByType(item, -1);
}

const auto &container = item->getContainer();
if (container && (!isStoreInbox || !ignoreStoreInbox)) {
for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) {
auto containerItem = *it;
(itemMap[containerItem->getID()])[containerItem->getTier()] += Item::countByType(containerItem, -1);
Expand Down Expand Up @@ -4038,6 +4044,48 @@ double_t Player::calculateDamageReduction(double_t currentTotal, int16_t resista
return (100 - currentTotal) / 100.0 * resistance + currentTotal;
}

ItemsTierCountList Player::getStoreInboxItemsId() const {
ItemsTierCountList itemMap;
const auto &container = getStoreInbox();
if (container) {
for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) {
std::shared_ptr<Item> item = *it;
(itemMap[item->getID()])[item->getTier()] += Item::countByType(item, -1);
}
}

return itemMap;
}

ItemsTierCountList Player::getDepotChestItemsId() const {
ItemsTierCountList itemMap;

for (const auto &[index, depot] : depotChests) {
const std::shared_ptr<Container> &container = depot->getContainer();
for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) {
std::shared_ptr<Item> item = *it;
(itemMap[item->getID()])[item->getTier()] += Item::countByType(item, -1);
}
}

return itemMap;
}

ItemsTierCountList Player::getDepotInboxItemsId() const {
ItemsTierCountList itemMap;

const std::shared_ptr<Inbox> &inbox = getInbox();
const std::shared_ptr<Container> &container = inbox->getContainer();
if (container) {
for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) {
const auto &item = *it;
(itemMap[item->getID()])[item->getTier()] += Item::countByType(item, -1);
}
}

return itemMap;
}

std::vector<std::shared_ptr<Item>> Player::getAllInventoryItems(bool ignoreEquiped /*= false*/, bool ignoreItemWithTier /* false*/) const {
std::vector<std::shared_ptr<Item>> itemVector;
for (int i = CONST_SLOT_FIRST; i <= CONST_SLOT_LAST; ++i) {
Expand Down
14 changes: 10 additions & 4 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,9 +1641,9 @@ class Player final : public Creature, public Cylinder, public Bankable {
}
}
void sendCyclopediaCharacterAchievements(uint16_t secretsUnlocked, std::vector<std::pair<Achievement, uint32_t>> achievementsUnlocked);
void sendCyclopediaCharacterItemSummary() {
void sendCyclopediaCharacterItemSummary(const ItemsTierCountList &inventoryItems, const ItemsTierCountList &storeInboxItems, const StashItemList &supplyStashItems, const ItemsTierCountList &depotBoxItems, const ItemsTierCountList &inboxItems) {
if (client) {
client->sendCyclopediaCharacterItemSummary();
client->sendCyclopediaCharacterItemSummary(inventoryItems, storeInboxItems, supplyStashItems, depotBoxItems, inboxItems);
}
}
void sendCyclopediaCharacterOutfitsMounts() {
Expand Down Expand Up @@ -2581,6 +2581,13 @@ class Player final : public Creature, public Cylinder, public Bankable {
// Get specific inventory item from itemid
std::vector<std::shared_ptr<Item>> getInventoryItemsFromId(uint16_t itemId, bool ignore = true) const;

// this get all player store inbox items and return as ItemsTierCountList
ItemsTierCountList getStoreInboxItemsId() const;
// this get all player depot chest items and return as ItemsTierCountList
ItemsTierCountList getDepotChestItemsId() const;
// this get all player depot inbox items and return as ItemsTierCountList
ItemsTierCountList getDepotInboxItemsId() const;

// This get all player inventory items
std::vector<std::shared_ptr<Item>> getAllInventoryItems(bool ignoreEquiped = false, bool ignoreItemWithTier = false) const;

Expand Down Expand Up @@ -2677,8 +2684,7 @@ class Player final : public Creature, public Cylinder, public Bankable {
size_t getLastIndex() const override;
uint32_t getItemTypeCount(uint16_t itemId, int32_t subType = -1) const override;
void stashContainer(StashContainerList itemDict);
ItemsTierCountList getInventoryItemsId() const;
// todo ItemsTierCountList getStoreInboxItemsId() const;
ItemsTierCountList getInventoryItemsId(bool ignoreStoreInbox = false) const;

// This function is a override function of base class
std::map<uint32_t, uint32_t> &getAllItemTypeCount(std::map<uint32_t, uint32_t> &countMap) const override;
Expand Down
11 changes: 9 additions & 2 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8307,9 +8307,16 @@ void Game::playerCyclopediaCharacterInfo(std::shared_ptr<Player> player, uint32_
case CYCLOPEDIA_CHARACTERINFO_ACHIEVEMENTS:
player->achiev()->sendUnlockedSecretAchievements();
break;
case CYCLOPEDIA_CHARACTERINFO_ITEMSUMMARY:
player->sendCyclopediaCharacterItemSummary();
case CYCLOPEDIA_CHARACTERINFO_ITEMSUMMARY: {
const ItemsTierCountList &inventoryItems = player->getInventoryItemsId(true);
const ItemsTierCountList &storeInboxItems = player->getStoreInboxItemsId();
const StashItemList &supplyStashItems = player->getStashItems();
const ItemsTierCountList &depotBoxItems = player->getDepotChestItemsId();
const ItemsTierCountList &inboxItems = player->getDepotInboxItemsId();

player->sendCyclopediaCharacterItemSummary(inventoryItems, storeInboxItems, supplyStashItems, depotBoxItems, inboxItems);
break;
}
case CYCLOPEDIA_CHARACTERINFO_OUTFITSMOUNTS:
player->sendCyclopediaCharacterOutfitsMounts();
break;
Expand Down
105 changes: 99 additions & 6 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3699,7 +3699,7 @@ void ProtocolGame::sendCyclopediaCharacterAchievements(uint16_t secretsUnlocked,
writeToOutputBuffer(msg);
}

void ProtocolGame::sendCyclopediaCharacterItemSummary() {
void ProtocolGame::sendCyclopediaCharacterItemSummary(const ItemsTierCountList &inventoryItems, const ItemsTierCountList &storeInboxItems, const StashItemList &supplyStashItems, const ItemsTierCountList &depotBoxItems, const ItemsTierCountList &inboxItems) {
if (!player || oldProtocol) {
return;
}
Expand All @@ -3709,11 +3709,104 @@ void ProtocolGame::sendCyclopediaCharacterItemSummary() {
msg.addByte(CYCLOPEDIA_CHARACTERINFO_ITEMSUMMARY);
msg.addByte(0x00); // 0x00 Here means 'no error'

msg.add<uint16_t>(0); // inventoryItems.size()
msg.add<uint16_t>(0); // storeInboxItems.size()
msg.add<uint16_t>(0); // supplyStashItems.size()
msg.add<uint16_t>(0); // depotBoxItems.size()
msg.add<uint16_t>(0); // inboxItems.size()
uint16_t inventoryItemsCount = 0;
const auto startInventory = msg.getBufferPosition();
msg.skipBytes(2);

for (const auto &inventoryItems_it : inventoryItems) {
for (const auto &[itemTier, itemCount] : inventoryItems_it.second) {
const ItemType &it = Item::items[inventoryItems_it.first];
msg.add<uint16_t>(inventoryItems_it.first); // Item ID
if (it.upgradeClassification > 0) {
msg.addByte(itemTier);
}
msg.add<uint32_t>(itemCount);

++inventoryItemsCount;
}
}

const auto endInventory = msg.getBufferPosition();

msg.setBufferPosition(startInventory);
msg.add<uint16_t>(inventoryItemsCount);

msg.setBufferPosition(endInventory);

uint16_t storeInboxItemsCount = 0;
const auto startStoreInbox = msg.getBufferPosition();
msg.skipBytes(2);

for (const auto &storeInboxItems_it : storeInboxItems) {
for (const auto &[itemTier, itemCount] : storeInboxItems_it.second) {
const ItemType &it = Item::items[storeInboxItems_it.first];
msg.add<uint16_t>(storeInboxItems_it.first); // Item ID
if (it.upgradeClassification > 0) {
msg.addByte(itemTier);
}
msg.add<uint32_t>(itemCount);

++storeInboxItemsCount;
}
}

const auto endStoreInbox = msg.getBufferPosition();

msg.setBufferPosition(startStoreInbox);
msg.add<uint16_t>(storeInboxItemsCount);

msg.setBufferPosition(endStoreInbox);

msg.add<uint16_t>(supplyStashItems.size());

for (const auto &[itemId, itemCount] : supplyStashItems) {
msg.add<uint16_t>(itemId);
msg.add<uint32_t>(itemCount);
}

uint16_t depotBoxItemsCount = 0;
const auto startDepotBox = msg.getBufferPosition();
msg.skipBytes(2);

for (const auto &depotBoxItems_it : depotBoxItems) {
for (const auto &[itemTier, itemCount] : depotBoxItems_it.second) {
const ItemType &it = Item::items[depotBoxItems_it.first];
msg.add<uint16_t>(depotBoxItems_it.first); // Item ID
if (it.upgradeClassification > 0) {
msg.addByte(itemTier);
}
msg.add<uint32_t>(itemCount);

++depotBoxItemsCount;
}
}

const auto endDepotBox = msg.getBufferPosition();

msg.setBufferPosition(startDepotBox);
msg.add<uint16_t>(depotBoxItemsCount);

msg.setBufferPosition(endDepotBox);

uint16_t inboxItemsCount = 0;
const auto startInbox = msg.getBufferPosition();
msg.skipBytes(2);

for (const auto &inboxItems_it : inboxItems) {
for (const auto &[itemTier, itemCount] : inboxItems_it.second) {
const ItemType &it = Item::items[inboxItems_it.first];
msg.add<uint16_t>(inboxItems_it.first); // Item ID
if (it.upgradeClassification > 0) {
msg.addByte(itemTier);
}
msg.add<uint32_t>(itemCount);

++inboxItemsCount;
}
}

msg.setBufferPosition(startInbox);
msg.add<uint16_t>(inboxItemsCount);

writeToOutputBuffer(msg);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/network/protocol/protocolgame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class ProtocolGame final : public Protocol {
void sendCyclopediaCharacterRecentDeaths(uint16_t page, uint16_t pages, const std::vector<RecentDeathEntry> &entries);
void sendCyclopediaCharacterRecentPvPKills(uint16_t page, uint16_t pages, const std::vector<RecentPvPKillEntry> &entries);
void sendCyclopediaCharacterAchievements(uint16_t secretsUnlocked, std::vector<std::pair<Achievement, uint32_t>> achievementsUnlocked);
void sendCyclopediaCharacterItemSummary();
void sendCyclopediaCharacterItemSummary(const ItemsTierCountList &inventoryItems, const ItemsTierCountList &storeInboxItems, const StashItemList &supplyStashItems, const ItemsTierCountList &depotBoxItems, const ItemsTierCountList &inboxItems);
void sendCyclopediaCharacterOutfitsMounts();
void sendCyclopediaCharacterStoreSummary();
void sendCyclopediaCharacterInspection();
Expand Down

0 comments on commit 09765f8

Please sign in to comment.