Skip to content

Commit

Permalink
improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
elsongabriel committed Apr 27, 2024
1 parent b59dbc7 commit ea1daa0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 78 deletions.
62 changes: 29 additions & 33 deletions src/creatures/players/cyclopedia/player_badge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,32 @@ bool PlayerBadge::add(uint8_t id, uint32_t timestamp /* = 0*/) {
return true;
}

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);
for (const auto &badge : g_game().getBadges()) {
switch (badge.m_type) {
case CyclopediaBadge_t::ACCOUNT_AGE:
if (accountAge(badge.m_amount)) {
add(badge.m_id);
}
break;
case CyclopediaBadge_t::LOYALTY:
if (loyalty(badge.m_amount)) {
add(badge.m_id);
}
break;
case CyclopediaBadge_t::ACCOUNT_ALL_LEVEL:
if (accountAllLevel(badge.m_amount)) {
add(badge.m_id);
}
break;
case CyclopediaBadge_t::ACCOUNT_ALL_VOCATIONS:
if (accountAllVocations(badge.m_amount)) {
add(badge.m_id);
}
break;
case CyclopediaBadge_t::TOURNAMENT_PARTICIPATION:
case CyclopediaBadge_t::TOURNAMENT_POINTS:
break;
}
}

Expand Down Expand Up @@ -120,10 +116,10 @@ bool PlayerBadge::loyalty(uint8_t 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();
}
const auto &players = g_game().getPlayersByAccount(m_player.getAccount(), true);
uint16_t total = std::accumulate(players.begin(), players.end(), 0, [](uint16_t sum, const std::shared_ptr<Player> &player) {
return sum + player->getLevel();
});
return total >= amount;
}

Expand Down
13 changes: 6 additions & 7 deletions src/creatures/players/cyclopedia/player_badge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class KV;

struct Badge {
uint8_t m_id = 0;
CyclopediaBadgeType_t m_type;
CyclopediaBadge_t m_type;
std::string m_name;
uint16_t m_amount = 0;

Badge() = default;

Badge(uint8_t id, CyclopediaBadgeType_t type, const std::string &name, uint16_t amount) :
m_id(id), m_type(type), m_name(name), m_amount(amount) { }
Badge(uint8_t id, CyclopediaBadge_t type, std::string name, uint16_t amount) :
m_id(id), m_type(type), m_name(std::move(name)), m_amount(amount) { }

bool operator==(const Badge &other) const {
return m_id == other.m_id;
Expand All @@ -32,7 +32,7 @@ namespace std {
template <>
struct hash<Badge> {
std::size_t operator()(const Badge &b) const {
return hash<uint8_t>()(b.m_id); // Use o ID como base para o hash
return hash<uint8_t>()(b.m_id);
}
};
}
Expand All @@ -43,7 +43,6 @@ 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();
Expand All @@ -53,8 +52,8 @@ class PlayerBadge {
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);
[[nodiscard]] bool tournamentParticipation(uint8_t skill);
[[nodiscard]] bool tournamentPoints(uint8_t race);

private:
// {badge ID, time when it was unlocked}
Expand Down
62 changes: 26 additions & 36 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,32 +194,32 @@ Game::Game() {
wildcardTree = std::make_shared<WildcardTreeNode>(false);

m_badges = {
Badge(1, CyclopediaBadgeType_t::ACCOUNT_AGE, "Fledegeling Hero", 1),
Badge(2, CyclopediaBadgeType_t::ACCOUNT_AGE, "Veteran Hero", 5),
Badge(3, CyclopediaBadgeType_t::ACCOUNT_AGE, "Senior Hero", 10),
Badge(4, CyclopediaBadgeType_t::ACCOUNT_AGE, "Ancient Hero", 15),
Badge(5, CyclopediaBadgeType_t::ACCOUNT_AGE, "Exalted Hero", 20),

Badge(6, CyclopediaBadgeType_t::LOYALTY, "Tibia Loyalist (Grade 1)", 100),
Badge(7, CyclopediaBadgeType_t::LOYALTY, "Tibia Loyalist (Grade 2)", 1000),
Badge(8, CyclopediaBadgeType_t::LOYALTY, "Tibia Loyalist (Grade 3)", 5000),

Badge(9, CyclopediaBadgeType_t::ACCOUNT_ALL_LEVEL, "Global Player (Grade 1)", 500),
Badge(10, CyclopediaBadgeType_t::ACCOUNT_ALL_LEVEL, "Global Player (Grade 2)", 1000),
Badge(11, CyclopediaBadgeType_t::ACCOUNT_ALL_LEVEL, "Global Player (Grade 3)", 2000),

Badge(12, CyclopediaBadgeType_t::ACCOUNT_ALL_VOCATIONS, "Master Class (Grade 1)", 100),
Badge(13, CyclopediaBadgeType_t::ACCOUNT_ALL_VOCATIONS, "Master Class (Grade 2)", 250),
Badge(14, CyclopediaBadgeType_t::ACCOUNT_ALL_VOCATIONS, "Master Class (Grade 3)", 500),

Badge(15, CyclopediaBadgeType_t::TOURNAMENT_PARTICIPATION, "Freshman of the Tournament", 1),
Badge(16, CyclopediaBadgeType_t::TOURNAMENT_PARTICIPATION, "Regular of the Tournament", 5),
Badge(17, CyclopediaBadgeType_t::TOURNAMENT_PARTICIPATION, "Hero of the Tournament", 10),

Badge(18, CyclopediaBadgeType_t::TOURNAMENT_POINTS, "Tournament Competitor", 1000),
Badge(19, CyclopediaBadgeType_t::TOURNAMENT_POINTS, "Tournament Challenger", 2500),
Badge(20, CyclopediaBadgeType_t::TOURNAMENT_POINTS, "Tournament Master", 5000),
Badge(21, CyclopediaBadgeType_t::TOURNAMENT_POINTS, "Tournament Champion", 10000),
Badge(1, CyclopediaBadge_t::ACCOUNT_AGE, "Fledegeling Hero", 1),
Badge(2, CyclopediaBadge_t::ACCOUNT_AGE, "Veteran Hero", 5),
Badge(3, CyclopediaBadge_t::ACCOUNT_AGE, "Senior Hero", 10),
Badge(4, CyclopediaBadge_t::ACCOUNT_AGE, "Ancient Hero", 15),
Badge(5, CyclopediaBadge_t::ACCOUNT_AGE, "Exalted Hero", 20),

Badge(6, CyclopediaBadge_t::LOYALTY, "Tibia Loyalist (Grade 1)", 100),
Badge(7, CyclopediaBadge_t::LOYALTY, "Tibia Loyalist (Grade 2)", 1000),
Badge(8, CyclopediaBadge_t::LOYALTY, "Tibia Loyalist (Grade 3)", 5000),

Badge(9, CyclopediaBadge_t::ACCOUNT_ALL_LEVEL, "Global Player (Grade 1)", 500),
Badge(10, CyclopediaBadge_t::ACCOUNT_ALL_LEVEL, "Global Player (Grade 2)", 1000),
Badge(11, CyclopediaBadge_t::ACCOUNT_ALL_LEVEL, "Global Player (Grade 3)", 2000),

Badge(12, CyclopediaBadge_t::ACCOUNT_ALL_VOCATIONS, "Master Class (Grade 1)", 100),
Badge(13, CyclopediaBadge_t::ACCOUNT_ALL_VOCATIONS, "Master Class (Grade 2)", 250),
Badge(14, CyclopediaBadge_t::ACCOUNT_ALL_VOCATIONS, "Master Class (Grade 3)", 500),

Badge(15, CyclopediaBadge_t::TOURNAMENT_PARTICIPATION, "Freshman of the Tournament", 1),
Badge(16, CyclopediaBadge_t::TOURNAMENT_PARTICIPATION, "Regular of the Tournament", 5),
Badge(17, CyclopediaBadge_t::TOURNAMENT_PARTICIPATION, "Hero of the Tournament", 10),

Badge(18, CyclopediaBadge_t::TOURNAMENT_POINTS, "Tournament Competitor", 1000),
Badge(19, CyclopediaBadge_t::TOURNAMENT_POINTS, "Tournament Challenger", 2500),
Badge(20, CyclopediaBadge_t::TOURNAMENT_POINTS, "Tournament Master", 5000),
Badge(21, CyclopediaBadge_t::TOURNAMENT_POINTS, "Tournament Champion", 10000),
};

m_highscoreCategoriesNames = {
Expand Down Expand Up @@ -10578,13 +10578,3 @@ Badge Game::getBadgeByIdOrName(uint8_t id, const std::string &name /*= ""*/) {
}
return {};
}

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: 0 additions & 1 deletion src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,6 @@ 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/game/game_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ enum LightState_t {
LIGHT_STATE_SUNRISE,
};

enum class CyclopediaBadgeType_t : uint8_t {
enum class CyclopediaBadge_t : uint8_t {
ACCOUNT_AGE = 1,
LOYALTY,
ACCOUNT_ALL_LEVEL,
Expand Down

0 comments on commit ea1daa0

Please sign in to comment.