-
-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
badge & title system: registering badges and titles with cpp (removed…
… from lua).
- Loading branch information
1 parent
0ba3ee9
commit 45ed0c6
Showing
24 changed files
with
707 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Canary - A free and open-source MMORPG server emulator | ||
* Copyright (©) 2019-2024 OpenTibiaBR <[email protected]> | ||
* Repository: https://github.com/opentibiabr/canary | ||
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE | ||
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors | ||
* Website: https://docs.opentibiabr.com/ | ||
*/ | ||
|
||
#include "pch.hpp" | ||
|
||
#include "player_badge.hpp" | ||
|
||
#include "creatures/players/player.hpp" | ||
#include "game/game.hpp" | ||
#include "kv/kv.hpp" | ||
|
||
PlayerBadge::PlayerBadge(Player &player) : | ||
m_player(player) { } | ||
|
||
bool PlayerBadge::hasBadge(uint8_t id) const { | ||
if (id == 0) { | ||
return false; | ||
} | ||
|
||
if (auto it = std::find_if(m_badgesUnlocked.begin(), m_badgesUnlocked.end(), [id](auto badge_it) { | ||
return badge_it.first == id; | ||
}); | ||
it != m_badgesUnlocked.end()) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool PlayerBadge::add(uint8_t id, uint32_t timestamp /* = 0*/) { | ||
if (hasBadge(id)) { | ||
return false; | ||
} | ||
|
||
const Badge &badge = g_game().getBadgeById(id); | ||
if (badge.m_id == 0) { | ||
return false; | ||
} | ||
|
||
int toSaveTimeStamp = timestamp != 0 ? timestamp : (OTSYS_TIME() / 1000); | ||
getUnlockedKV()->set(badge.m_name, toSaveTimeStamp); | ||
m_badgesUnlocked.push_back({badge.m_id, toSaveTimeStamp}); | ||
m_badgesUnlocked.shrink_to_fit(); | ||
return true; | ||
} | ||
|
||
std::vector<std::pair<uint8_t, uint32_t>> PlayerBadge::getUnlockedBadges() const { | ||
Check warning on line 53 in src/creatures/players/cyclopedia/player_badge.cpp GitHub Actions / cppcheck
|
||
return m_badgesUnlocked; | ||
} | ||
|
||
void PlayerBadge::loadUnlockedBadges() { | ||
const auto &unlockedBadges = getUnlockedKV()->keys(); | ||
g_logger().debug("[{}] - Loading unlocked badges: {}", __FUNCTION__, unlockedBadges.size()); | ||
for (const auto &uBadge: unlockedBadges) { | ||
auto id = static_cast<uint8_t>(std::stoul(uBadge)); | ||
const Badge &badge = g_game().getBadgeById(static_cast<uint8_t>(id)); | ||
if (badge.m_id == 0) { | ||
g_logger().error("[{}] - Badge {} not found.", __FUNCTION__, uBadge); | ||
continue; | ||
} | ||
|
||
g_logger().debug("[{}] - Badge {} found for player {}.", __FUNCTION__, badge.m_name, m_player.getName()); | ||
|
||
m_badgesUnlocked.push_back({badge.m_id, getUnlockedKV()->get(uBadge)->getNumber()}); | ||
} | ||
} | ||
|
||
const std::shared_ptr<KV> &PlayerBadge::getUnlockedKV() { | ||
if (m_badgeUnlockedKV == nullptr) { | ||
m_badgeUnlockedKV = m_player.kv()->scoped("badges")->scoped("unlocked"); | ||
} | ||
|
||
return m_badgeUnlockedKV; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Canary - A free and open-source MMORPG server emulator | ||
* Copyright (©) 2019-2024 OpenTibiaBR <[email protected]> | ||
* Repository: https://github.com/opentibiabr/canary | ||
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE | ||
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors | ||
* Website: https://docs.opentibiabr.com/ | ||
*/ | ||
|
||
#pragma once | ||
|
||
class Player; | ||
class KV; | ||
|
||
struct Badge { | ||
uint8_t m_id = 0; | ||
CyclopediaBadgeType_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) {} | ||
|
||
bool operator==(const Badge &other) const { | ||
return m_id == other.m_id; | ||
} | ||
}; | ||
|
||
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 | ||
} | ||
}; | ||
} | ||
|
||
class PlayerBadge { | ||
public: | ||
explicit PlayerBadge(Player &player); | ||
|
||
bool hasBadge(uint8_t id) const; | ||
bool add(uint8_t id, uint32_t timestamp = 0); | ||
std::vector<std::pair<uint8_t, uint32_t>> getUnlockedBadges() const; | ||
void loadUnlockedBadges(); | ||
const std::shared_ptr<KV> &getUnlockedKV(); | ||
|
||
private: | ||
// {badge ID, time when it was unlocked} | ||
std::shared_ptr<KV> m_badgeUnlockedKV; | ||
std::vector<std::pair<uint8_t, uint32_t>> m_badgesUnlocked; | ||
Player &m_player; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.