forked from opentibiabr/canary
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: badge system (opentibiabr#2533)
- Loading branch information
1 parent
9f74b27
commit 9efbe4c
Showing
19 changed files
with
573 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
local addBadge = TalkAction("/addbadge") | ||
|
||
function addBadge.onSay(player, words, param) | ||
-- create log | ||
logCommand(player, words, param) | ||
|
||
if param == "" then | ||
player:sendCancelMessage("Command param required.") | ||
return true | ||
end | ||
|
||
local split = param:split(",") | ||
if not split[2] then | ||
player:sendCancelMessage("Insufficient parameters. Usage: /addbadge playerName, badgeID") | ||
return true | ||
end | ||
|
||
local target = Player(split[1]) | ||
if not target then | ||
player:sendCancelMessage("A player with that name is not online.") | ||
return true | ||
end | ||
|
||
-- Trim left | ||
split[2] = split[2]:gsub("^%s*(.-)$", "%1") | ||
local id = tonumber(split[2]) | ||
target:addBadge(id) | ||
return true | ||
end | ||
|
||
addBadge:separator(" ") | ||
addBadge:groupType("god") | ||
addBadge:register() |
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,156 @@ | ||
/** | ||
* 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.m_id == 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.emplace_back(badge, toSaveTimeStamp); | ||
m_badgesUnlocked.shrink_to_fit(); | ||
return true; | ||
} | ||
|
||
void PlayerBadge::checkAndUpdateNewBadges() { | ||
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; | ||
} | ||
} | ||
|
||
loadUnlockedBadges(); | ||
} | ||
|
||
void PlayerBadge::loadUnlockedBadges() { | ||
const auto &unlockedBadges = getUnlockedKV()->keys(); | ||
g_logger().debug("[{}] - Loading unlocked badges: {}", __FUNCTION__, unlockedBadges.size()); | ||
for (const auto &badgeName : unlockedBadges) { | ||
const Badge &badge = g_game().getBadgeByName(badgeName); | ||
if (badge.m_id == 0) { | ||
g_logger().error("[{}] - Badge {} not found.", __FUNCTION__, badgeName); | ||
continue; | ||
} | ||
|
||
g_logger().debug("[{}] - Badge {} found for player {}.", __FUNCTION__, badge.m_name, m_player.getName()); | ||
|
||
m_badgesUnlocked.emplace_back(badge, getUnlockedKV()->get(badgeName)->getNumber()); | ||
} | ||
} | ||
|
||
const std::shared_ptr<KV> &PlayerBadge::getUnlockedKV() { | ||
if (m_badgeUnlockedKV == nullptr) { | ||
m_badgeUnlockedKV = m_player.kv()->scoped("badges")->scoped("unlocked"); | ||
} | ||
|
||
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) { | ||
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; | ||
} | ||
|
||
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; | ||
} |
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,65 @@ | ||
/** | ||
* 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 | ||
|
||
#include "game/game_definitions.hpp" | ||
|
||
class Player; | ||
class KV; | ||
|
||
struct Badge { | ||
uint8_t m_id = 0; | ||
CyclopediaBadge_t m_type; | ||
std::string m_name; | ||
uint16_t m_amount = 0; | ||
|
||
Badge() = default; | ||
|
||
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; | ||
} | ||
}; | ||
|
||
namespace std { | ||
template <> | ||
struct hash<Badge> { | ||
std::size_t operator()(const Badge &b) const { | ||
return hash<uint8_t>()(b.m_id); | ||
} | ||
}; | ||
} | ||
|
||
class PlayerBadge { | ||
public: | ||
explicit PlayerBadge(Player &player); | ||
|
||
[[nodiscard]] bool hasBadge(uint8_t id) const; | ||
bool add(uint8_t id, uint32_t timestamp = 0); | ||
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); | ||
[[nodiscard]] bool tournamentParticipation(uint8_t skill); | ||
[[nodiscard]] bool tournamentPoints(uint8_t race); | ||
|
||
private: | ||
// {badge ID, time when it was unlocked} | ||
std::shared_ptr<KV> m_badgeUnlockedKV; | ||
std::vector<std::pair<Badge, 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
Oops, something went wrong.