Skip to content

Commit

Permalink
Created function to check badges of player.
Browse files Browse the repository at this point in the history
Moved and adjusted function 'getPlayerVocationEnum' used in player_wheel to player class.
  • Loading branch information
elsongabriel committed Apr 26, 2024
1 parent cbd9d44 commit b54710c
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 30 deletions.
81 changes: 81 additions & 0 deletions src/creatures/players/cyclopedia/player_badge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,38 @@ std::vector<std::pair<Badge, uint32_t>> PlayerBadge::getUnlockedBadges() const {
return m_badgesUnlocked;
}

void PlayerBadge::checkAndUpdateNewBadges() {
// CyclopediaBadgeType_t::ACCOUNT_AGE
for (const auto &badge : g_game().getBadgesByType(CyclopediaBadgeType_t::ACCOUNT_AGE)) {
if (accountAge(badge.m_amount)) {
add(badge.m_id);
}
}

// CyclopediaBadgeType_t::LOYALTY
for (const auto &badge : g_game().getBadgesByType(CyclopediaBadgeType_t::LOYALTY)) {
if (loyalty(badge.m_amount)) {
add(badge.m_id);
}
}

// CyclopediaBadgeType_t::ACCOUNT_ALL_LEVEL
for (const auto &badge : g_game().getBadgesByType(CyclopediaBadgeType_t::ACCOUNT_ALL_LEVEL)) {
if (accountAllLevel(badge.m_amount)) {
add(badge.m_id);
}
}

// CyclopediaBadgeType_t::ACCOUNT_ALL_VOCATIONS
for (const auto &badge : g_game().getBadgesByType(CyclopediaBadgeType_t::ACCOUNT_ALL_VOCATIONS)) {
if (accountAllVocations(badge.m_amount)) {
add(badge.m_id);
}
}

loadUnlockedBadges();
}

void PlayerBadge::loadUnlockedBadges() {
const auto &unlockedBadges = getUnlockedKV()->keys();
g_logger().debug("[{}] - Loading unlocked badges: {}", __FUNCTION__, unlockedBadges.size());
Expand All @@ -77,3 +109,52 @@ const std::shared_ptr<KV> &PlayerBadge::getUnlockedKV() {

return m_badgeUnlockedKV;
}

// Badge Calculate Functions
bool PlayerBadge::accountAge(uint8_t amount) {
return std::floor(m_player.getLoyaltyPoints() / 365) >= amount;
}

bool PlayerBadge::loyalty(uint8_t amount) {
return m_player.getLoyaltyPoints() >= amount;
}

bool PlayerBadge::accountAllLevel(uint8_t amount) {
uint8_t total = 0;
for (const auto &player : g_game().getPlayersByAccount(m_player.getAccount(), true)) {
total = total + player->getLevel();
}
return total >= amount;
}

bool PlayerBadge::accountAllVocations(uint8_t amount) {
auto knight = false;
auto paladin = false;
auto druid = false;
auto sorcerer = false;
for (const auto &player : g_game().getPlayersByAccount(m_player.getAccount(), true)) {
if (player->getLevel() >= amount) {
auto vocationEnum = player->getPlayerVocationEnum();
if (vocationEnum == Vocation_t::VOCATION_KNIGHT_CIP) {
knight = true;
} else if (vocationEnum == Vocation_t::VOCATION_SORCERER_CIP) {
sorcerer = true;
} else if (vocationEnum == Vocation_t::VOCATION_PALADIN_CIP) {
paladin = true;
} else if (vocationEnum == Vocation_t::VOCATION_DRUID_CIP) {
druid = true;
}
}
}
return knight && paladin && druid && sorcerer;
}

bool PlayerBadge::tournamentParticipation(uint8_t skill) {
// todo check if is used
return false;
}

bool PlayerBadge::tournamentPoints(uint8_t race) {
// todo check if is used
return false;
}
9 changes: 9 additions & 0 deletions src/creatures/players/cyclopedia/player_badge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ class PlayerBadge {
[[nodiscard]] bool hasBadge(uint8_t id) const;
bool add(uint8_t id, uint32_t timestamp = 0);
[[nodiscard]] std::vector<std::pair<Badge, uint32_t>> getUnlockedBadges() const;
void checkAndUpdateNewBadges();
void loadUnlockedBadges();
const std::shared_ptr<KV> &getUnlockedKV();

// Badge Calculate Functions
bool accountAge(uint8_t amount);
bool loyalty(uint8_t amount);
bool accountAllLevel(uint8_t amount);
bool accountAllVocations(uint8_t amount);
bool tournamentParticipation(uint8_t skill);
bool tournamentPoints(uint8_t race);

private:
// {badge ID, time when it was unlocked}
std::shared_ptr<KV> m_badgeUnlockedKV;
Expand Down
15 changes: 15 additions & 0 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8085,3 +8085,18 @@ bool Player::canSpeakWithHireling(uint8_t speechbubble) {

return true;
}

uint16_t Player::getPlayerVocationEnum() const {
int cipTibiaId = getVocation()->getClientId();
if (cipTibiaId == 1 || cipTibiaId == 11) {
return Vocation_t::VOCATION_KNIGHT_CIP; // Knight
} else if (cipTibiaId == 2 || cipTibiaId == 12) {
return Vocation_t::VOCATION_PALADIN_CIP; // Paladin
} else if (cipTibiaId == 3 || cipTibiaId == 13) {
return Vocation_t::VOCATION_SORCERER_CIP; // Sorcerer
} else if (cipTibiaId == 4 || cipTibiaId == 14) {
return Vocation_t::VOCATION_DRUID_CIP; // Druid
}

return Vocation_t::VOCATION_NONE;
}
2 changes: 2 additions & 0 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2619,6 +2619,8 @@ class Player final : public Creature, public Cylinder, public Bankable {

bool canSpeakWithHireling(uint8_t speechbubble);

uint16_t getPlayerVocationEnum() const;

private:
friend class PlayerLock;
std::mutex mutex;
Expand Down
33 changes: 9 additions & 24 deletions src/creatures/players/wheel/player_wheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ bool PlayerWheel::getSpellAdditionalArea(const std::string &spellName) const {
return false;
}

auto vocationEnum = getPlayerVocationEnum();
auto vocationEnum = m_player.getPlayerVocationEnum();
if (vocationEnum == Vocation_t::VOCATION_KNIGHT_CIP) {
return checkSpellArea(g_game().getIOWheel()->getWheelBonusData().spells.knight, spellName, stage);
} else if (vocationEnum == Vocation_t::VOCATION_PALADIN_CIP) {
Expand All @@ -709,7 +709,7 @@ int PlayerWheel::getSpellAdditionalTarget(const std::string &spellName) const {
return 0;
}

auto vocationEnum = getPlayerVocationEnum();
auto vocationEnum = m_player.getPlayerVocationEnum();
if (vocationEnum == Vocation_t::VOCATION_KNIGHT_CIP) {
return checkSpellAdditionalTarget(g_game().getIOWheel()->getWheelBonusData().spells.knight, spellName, stage);
} else if (vocationEnum == Vocation_t::VOCATION_PALADIN_CIP) {
Expand All @@ -729,7 +729,7 @@ int PlayerWheel::getSpellAdditionalDuration(const std::string &spellName) const
return 0;
}

auto vocationEnum = getPlayerVocationEnum();
auto vocationEnum = m_player.getPlayerVocationEnum();
if (vocationEnum == Vocation_t::VOCATION_KNIGHT_CIP) {
return checkSpellAdditionalDuration(g_game().getIOWheel()->getWheelBonusData().spells.knight, spellName, stage);
} else if (vocationEnum == Vocation_t::VOCATION_PALADIN_CIP) {
Expand Down Expand Up @@ -1016,7 +1016,7 @@ void PlayerWheel::sendOpenWheelWindow(NetworkMessage &msg, uint32_t ownerId) con
}

msg.addByte(getOptions(ownerId)); // Options
msg.addByte(getPlayerVocationEnum()); // Vocation id
msg.addByte(m_player.getPlayerVocationEnum()); // Vocation id

msg.add<uint16_t>(getWheelPoints(false)); // Points (false param for not send extra points)
msg.add<uint16_t>(getExtraPoints()); // Extra points
Expand Down Expand Up @@ -1262,7 +1262,7 @@ uint16_t PlayerWheel::getWheelPoints(bool includeExtraPoints /* = true*/) const

bool PlayerWheel::canOpenWheel() const {
// Vocation check
if (getPlayerVocationEnum() == Vocation_t::VOCATION_NONE) {
if (m_player.getPlayerVocationEnum() == Vocation_t::VOCATION_NONE) {
return false;
}

Expand Down Expand Up @@ -1304,21 +1304,6 @@ uint8_t PlayerWheel::getOptions(uint32_t ownerId) const {
return 2;
}

uint8_t PlayerWheel::getPlayerVocationEnum() const {
int cipTibiaId = m_player.getVocation()->getClientId();
if (cipTibiaId == 1 || cipTibiaId == 11) {
return Vocation_t::VOCATION_KNIGHT_CIP; // Knight
} else if (cipTibiaId == 2 || cipTibiaId == 12) {
return Vocation_t::VOCATION_PALADIN_CIP; // Paladin
} else if (cipTibiaId == 3 || cipTibiaId == 13) {
return Vocation_t::VOCATION_SORCERER_CIP; // Sorcerer
} else if (cipTibiaId == 4 || cipTibiaId == 14) {
return Vocation_t::VOCATION_DRUID_CIP; // Druid
}

return Vocation_t::VOCATION_NONE;
}

bool PlayerWheel::canSelectSlotFullOrPartial(WheelSlots_t slot) const {
if (getPointsBySlotType(slot) == getMaxPointsPerSlot(slot)) {
g_logger().debug("[{}] points on slot {}, max points {}", __FUNCTION__, getPointsBySlotType(slot), getMaxPointsPerSlot(slot));
Expand Down Expand Up @@ -1785,7 +1770,7 @@ void PlayerWheel::printPlayerWheelMethodsBonusData(const PlayerWheelMethodsBonus
void PlayerWheel::loadDedicationAndConvictionPerks() {
using VocationBonusFunction = std::function<void(const std::shared_ptr<Player> &, uint16_t, uint8_t, PlayerWheelMethodsBonusData &)>;
auto wheelFunctions = g_game().getIOWheel()->getWheelMapFunctions();
auto vocationCipId = getPlayerVocationEnum();
auto vocationCipId = m_player.getPlayerVocationEnum();
if (vocationCipId < VOCATION_KNIGHT_CIP || vocationCipId > VOCATION_DRUID_CIP) {
return;
}
Expand Down Expand Up @@ -1826,7 +1811,7 @@ void PlayerWheel::loadRevelationPerks() {
m_playerBonusData.stats.healing += statsHealing;

auto redStageValue = static_cast<uint8_t>(redStageEnum);
auto vocationEnum = getPlayerVocationEnum();
auto vocationEnum = m_player.getPlayerVocationEnum();
if (vocationEnum == Vocation_t::VOCATION_DRUID_CIP) {
m_playerBonusData.stages.blessingOfTheGrove = redStageValue;
} else if (vocationEnum == Vocation_t::VOCATION_KNIGHT_CIP) {
Expand Down Expand Up @@ -1854,7 +1839,7 @@ void PlayerWheel::loadRevelationPerks() {
m_playerBonusData.stats.healing += statsHealing;

auto purpleStage = static_cast<uint8_t>(purpleStageEnum);
auto vocationEnum = getPlayerVocationEnum();
auto vocationEnum = m_player.getPlayerVocationEnum();
if (vocationEnum == Vocation_t::VOCATION_KNIGHT_CIP) {
m_playerBonusData.avatar.steel = purpleStage;
for (uint8_t i = 0; i < purpleStage; ++i) {
Expand Down Expand Up @@ -1885,7 +1870,7 @@ void PlayerWheel::loadRevelationPerks() {
m_playerBonusData.stats.healing += statsHealing;

auto blueStage = static_cast<uint8_t>(blueStageEnum);
auto vocationEnum = getPlayerVocationEnum();
auto vocationEnum = m_player.getPlayerVocationEnum();
if (vocationEnum == Vocation_t::VOCATION_KNIGHT_CIP) {
m_playerBonusData.stages.combatMastery = blueStage;
} else if (vocationEnum == Vocation_t::VOCATION_SORCERER_CIP) {
Expand Down
1 change: 0 additions & 1 deletion src/creatures/players/wheel/player_wheel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ class PlayerWheel {
* indicating that the player can increase points but cannot decrease the ID.
*/
uint8_t getOptions(uint32_t ownerId) const;
uint8_t getPlayerVocationEnum() const;

std::shared_ptr<KV> gemsKV() const;

Expand Down
14 changes: 12 additions & 2 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10562,6 +10562,10 @@ void Game::getCyclopediaStatistics() {
// todo in title system: g_logger().info("Loaded {} titles from Title system", m_titles.size());
}

std::unordered_set<Badge> Game::getBadges() {
return m_badges;
}

Badge Game::getBadgeByIdOrName(uint8_t id, const std::string &name /*= ""*/) {
if (id == 0 && name.empty()) {
return {};
Expand All @@ -10575,6 +10579,12 @@ Badge Game::getBadgeByIdOrName(uint8_t id, const std::string &name /*= ""*/) {
return {};
}

std::unordered_set<Badge> Game::getBadges() {
return m_badges;
std::vector<Badge> Game::getBadgesByType(CyclopediaBadgeType_t type) {
std::vector<Badge> badgesFound;
for (const auto &badge : getBadges()) {
if (badge.m_type == type) {
badgesFound.push_back(badge);
}
}
return badgesFound;
}
1 change: 1 addition & 0 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ class Game {

std::unordered_set<Badge> getBadges();
Badge getBadgeByIdOrName(uint8_t id, const std::string &name = "");
std::vector<Badge> getBadgesByType(CyclopediaBadgeType_t type);

private:
std::map<uint16_t, Achievement> m_achievements;
Expand Down
2 changes: 1 addition & 1 deletion src/io/functions/iologindata_load_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ void IOLoginDataLoad::loadPlayerInitializeSystem(std::shared_ptr<Player> player)
player->wheel()->initializePlayerData();

player->achiev()->loadUnlockedAchievements();
player->badge()->loadUnlockedBadges();
player->badge()->checkAndUpdateNewBadges();

player->initializePrey();
player->initializeTaskHunting();
Expand Down
3 changes: 1 addition & 2 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3380,8 +3380,7 @@ void ProtocolGame::sendCyclopediaCharacterBaseInformation() {
msg.add<uint16_t>(player->getLevel());
AddOutfit(msg, player->getDefaultOutfit(), false);

msg.addByte(g_configManager().getBoolean(STAMINA_SYSTEM, __FUNCTION__) ? 0x01 : 0x00); // hide stamina
// msg.addByte(0x00); // Store summary & Character titles (will be 0x01)
msg.addByte(0x00); // Store summary & Character titles
msg.addString("", "ProtocolGame::sendCyclopediaCharacterBaseInformation - empty"); // character title
// msg.addString(player->title()->getCurrentTitleName(), "ProtocolGame::sendCyclopediaCharacterBaseInformation - player->title()->getCurrentTitleName()"); // character title
writeToOutputBuffer(msg);
Expand Down

0 comments on commit b54710c

Please sign in to comment.