Skip to content

Commit

Permalink
badge & title system: registering badges and titles with cpp (removed…
Browse files Browse the repository at this point in the history
… from lua).
  • Loading branch information
elsongabriel committed Apr 12, 2024
1 parent 0ba3ee9 commit 45ed0c6
Show file tree
Hide file tree
Showing 24 changed files with 707 additions and 221 deletions.
8 changes: 4 additions & 4 deletions data/libs/functions/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1025,10 +1025,10 @@ do
-- end
-- end
--
-- return true
--end
--
--function Player.initializeTitleSystem(self)
return true
end

function Player.initializeTitleSystem(self)
-- if not TitleSystem.enable then
-- return false
-- end
Expand Down
4 changes: 2 additions & 2 deletions data/scripts/creaturescripts/player/login.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ function playerLoginGlobal.onLogin(player)
end

player:initializeLoyaltySystem()
player:initializeTitleSystem()
player:initializeBadgeSystem()
--player:initializeTitleSystem()
--player:initializeBadgeSystem()
player:registerEvent("PlayerDeath")
player:registerEvent("DropLoot")
player:registerEvent("BossParticipation")
Expand Down
20 changes: 10 additions & 10 deletions data/scripts/lib/register_badges.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ Functions:
BadgeSystem:tournamentPointsFunction(player, race)
]]

for _, badgeTable in pairs(BADGES) do
if type(badgeTable) == "table" and #badgeTable > 0 then
for __, badge in pairs(badgeTable) do
if badge.id then
logger.trace("[Badge System registration] - Registering badge '{}' with id '{}'", badge.name, badge.id)
Game.registerBadge(badge.id, badge.name, badge.amount)
end
end
end
end
--for _, badgeTable in pairs(BADGES) do
-- if type(badgeTable) == "table" and #badgeTable > 0 then
-- for __, badge in pairs(badgeTable) do
-- if badge.id then
-- logger.trace("[Badge System registration] - Registering badge '{}' with id '{}'", badge.name, badge.id)
-- Game.registerBadge(badge.id, badge.name, badge.amount)
-- end
-- end
-- end
--end

---- Account age section
--BadgeSystem.accountAgeFunction = function(player, amount)
Expand Down
3 changes: 2 additions & 1 deletion src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ void CanaryServer::loadModules() {
modulesLoadHelper((g_npcs().load(false, true)), "npc");

g_game().loadBoostedCreature();
// TODO g_game().initializeGameWorldHighscores();
// TODO: g_game().initializeCyclopedia();
// TODO: verify g_game().initializeGameWorldHighscores();
g_ioBosstiary().loadBoostedBoss();
g_ioprey().initializeTaskHuntOptions();
}
Expand Down
2 changes: 2 additions & 0 deletions src/creatures/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ target_sources(${PROJECT_NAME}_lib PRIVATE
players/storages/storages.cpp
players/player.cpp
players/achievement/player_achievement.cpp
players/cyclopedia/player_badge.cpp
players/cyclopedia/player_cyclopedia.cpp
players/cyclopedia/player_title.cpp
players/wheel/player_wheel.cpp
players/wheel/wheel_gems.cpp
players/vocations/vocation.cpp
Expand Down
80 changes: 80 additions & 0 deletions src/creatures/players/cyclopedia/player_badge.cpp
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

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] reported by reviewdog 🐶 The function 'getUnlockedBadges' is never used. Raw Output: src/creatures/players/cyclopedia/player_badge.cpp:53:The function 'getUnlockedBadges' is never used.
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;
}
55 changes: 55 additions & 0 deletions src/creatures/players/cyclopedia/player_badge.hpp
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;
};
70 changes: 0 additions & 70 deletions src/creatures/players/cyclopedia/player_cyclopedia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,76 +18,6 @@
PlayerCyclopedia::PlayerCyclopedia(Player &player) :
m_player(player) { }

// Badge
bool PlayerCyclopedia::hasBadge(uint8_t id) {
if (auto it = std::find_if(m_badges.begin(), m_badges.end(), [id](uint16_t it) {
return it == id;
});
it != m_badges.end()) {
return true;
}

return false;
}

void PlayerCyclopedia::addBadge(uint8_t id) {
if (hasBadge(id)) {
return;
}

m_badges.push_back(id);
}

// Title
std::vector<uint8_t> PlayerCyclopedia::getTitles() const {
return m_titles;
}

uint8_t PlayerCyclopedia::getCurrentTitle() const {
return m_currentTitle;
}

std::string PlayerCyclopedia::getCurrentTitleName() const {
auto currentTitle = getCurrentTitle();
if (currentTitle == 0) {
return "";
}

auto title = g_game().getTitleById(currentTitle);
return (m_player.getSex() == PLAYERSEX_MALE) ? title.maleName : title.femaleName;
}

void PlayerCyclopedia::setCurrentTitle(uint8_t id) {
if (id != 0 && !isTitleUnlocked(id)) {
return;
}

m_currentTitle = id;
}

void PlayerCyclopedia::addTitle(uint8_t id) {
if (isTitleUnlocked(id)) {
return;
}

m_titles.push_back(id);
}

bool PlayerCyclopedia::isTitleUnlocked(uint8_t id) const {
if (id == 0) {
return false;
}

if (auto it = std::find_if(m_titles.begin(), m_titles.end(), [id](auto title_it) {
return title_it == id;
});
it != m_titles.end()) {
return true;
}

return false;
}

// Death History
std::vector<RecentDeathEntry> PlayerCyclopedia::getDeathHistory() const {

Check warning on line 22 in src/creatures/players/cyclopedia/player_cyclopedia.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] reported by reviewdog 🐶 The function 'getDeathHistory' is never used. Raw Output: src/creatures/players/cyclopedia/player_cyclopedia.cpp:22:The function 'getDeathHistory' is never used.
return m_deathHistory;
Expand Down
32 changes: 0 additions & 32 deletions src/creatures/players/cyclopedia/player_cyclopedia.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,9 @@
class Player;
class KV;

struct Badge {
Badge() { }

uint8_t id = 0;
std::string name;
uint16_t amount = 0;
};

struct Title {
Title() { }

uint8_t id = 0;
std::string maleName;
std::string femaleName;
std::string type;
std::string description;
bool permanent = false;
};

class PlayerCyclopedia {
public:
explicit PlayerCyclopedia(Player &player);
bool hasBadge(uint8_t id);
void addBadge(uint8_t id);

std::vector<uint8_t> getTitles() const;
uint8_t getCurrentTitle() const;
std::string getCurrentTitleName() const;
void setCurrentTitle(uint8_t id);
void addTitle(uint8_t id);
bool isTitleUnlocked(uint8_t id) const;

std::vector<RecentDeathEntry> getDeathHistory() const;
std::vector<RecentPvPKillEntry> getPvpKillsHistory() const;
Expand All @@ -52,10 +24,6 @@ class PlayerCyclopedia {
private:
Player &m_player;

std::vector<uint8_t> m_badges;
std::vector<uint8_t> m_titles;
uint8_t m_currentTitle;

// std::vector<uint16_t> hirelingOutfitsObtained;
// std::vector<uint8_t> hirelingJobsObtained;
// std::map<Blessings_t, uint16_t> blessingsObtained;
Expand Down
Loading

0 comments on commit 45ed0c6

Please sign in to comment.