Skip to content

Commit

Permalink
Merge branch 'main' into fix-task-context
Browse files Browse the repository at this point in the history
  • Loading branch information
Luan Luciano authored Sep 24, 2024
2 parents 7f28f73 + 10823e3 commit d681263
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 8 deletions.
4 changes: 2 additions & 2 deletions data-otservbr-global/lib/quests/soul_war.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1112,9 +1112,9 @@ function MonsterType:calculateBagYouDesireChance(player, itemChance)
itemChance = itemChance + (playerTaintLevel * SoulWarQuest.bagYouDesireChancePerTaint)
end

logger.info("Player {} killed {} with {} taints, loot chance {}", player:getName(), monsterName, playerTaintLevel, itemChance)
logger.debug("Player {} killed {} with {} taints, loot chance {}", player:getName(), monsterName, playerTaintLevel, itemChance)

if math.random(1, 100000) <= totalChance then
if math.random(1, 100000) <= itemChance then
logger.debug("Player {} killed {} and got a bag you desire with drop chance {}", player:getName(), monsterName, itemChance)
if monsterName == "Goshnar's Megalomania" then
-- Reset kill count on successful drop
Expand Down
33 changes: 33 additions & 0 deletions data/items/items.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4027,6 +4027,9 @@
<attribute key="count" value="2"/>
<attribute key="damage" value="20"/>
</attribute>
<attribute key="script" value="moveevent">
<attribute key="eventType" value="stepin"/>
</attribute>
</item>
<item id="2001" article="an" name="unlit campfire">
<attribute key="primarytype" value="illumination"/>
Expand Down Expand Up @@ -4342,6 +4345,9 @@
</attribute>
<attribute key="duration" value="120"/>
<attribute key="decayTo" value="2132"/>
<attribute key="script" value="moveevent">
<attribute key="eventType" value="stepin"/>
</attribute>
</item>
<item id="2132" article="a" name="fire field">
<attribute key="type" value="magicfield"/>
Expand Down Expand Up @@ -20590,6 +20596,9 @@
<attribute key="field" value="fire">
<attribute key="damage" value="300"/>
</attribute>
<attribute key="script" value="moveevent">
<attribute key="eventType" value="stepin"/>
</attribute>
<attribute key="replaceable" value="0"/>
</item>
<item fromid="7474" toid="7475" article="a" name="sarcophagus">
Expand Down Expand Up @@ -26310,6 +26319,9 @@
<attribute key="count" value="7"/>
<attribute key="damage" value="10"/>
</attribute>
<attribute key="script" value="moveevent">
<attribute key="eventType" value="stepin"/>
</attribute>
<attribute key="replaceable" value="0"/>
</item>
<item id="10070" article="a" name="fire field">
Expand All @@ -26319,6 +26331,9 @@
<attribute key="count" value="7"/>
<attribute key="damage" value="10"/>
</attribute>
<attribute key="script" value="moveevent">
<attribute key="eventType" value="stepin"/>
</attribute>
<attribute key="replaceable" value="0"/>
</item>
<item id="10071" article="a" name="fire field">
Expand All @@ -26328,6 +26343,9 @@
<attribute key="count" value="7"/>
<attribute key="damage" value="10"/>
</attribute>
<attribute key="script" value="moveevent">
<attribute key="eventType" value="stepin"/>
</attribute>
<attribute key="replaceable" value="0"/>
</item>
<item fromid="10072" toid="10081" name="zaoan decoration"/>
Expand Down Expand Up @@ -45774,20 +45792,29 @@ hands of its owner. Granted by TibiaRoyal.com"/>
<attribute key="ticks" value="10000"/>
<attribute key="damage" value="25"/>
</attribute>
<attribute key="script" value="moveevent">
<attribute key="eventType" value="stepin"/>
</attribute>
</item>
<item id="23366" article="an" name="energy trap">
<attribute key="type" value="magicfield"/>
<attribute key="field" value="energy">
<attribute key="ticks" value="10000"/>
<attribute key="damage" value="25"/>
</attribute>
<attribute key="script" value="moveevent">
<attribute key="eventType" value="stepin"/>
</attribute>
</item>
<item id="23367" article="a" name="toxic trap">
<attribute key="type" value="magicfield"/>
<attribute key="field" value="poison">
<attribute key="ticks" value="10000"/>
<attribute key="damage" value="25"/>
</attribute>
<attribute key="script" value="moveevent">
<attribute key="eventType" value="stepin"/>
</attribute>
</item>
<item id="23368" article="a" name="poison trap">
<attribute key="type" value="magicfield"/>
Expand All @@ -45796,6 +45823,9 @@ hands of its owner. Granted by TibiaRoyal.com"/>
<attribute key="damage" value="100"/>
<attribute key="start" value="5"/>
</attribute>
<attribute key="script" value="moveevent">
<attribute key="eventType" value="stepin"/>
</attribute>
</item>
<item id="23369" article="a" name="blade trap"/>
<item id="23370" article="a" name="spike trap"/>
Expand Down Expand Up @@ -58245,6 +58275,9 @@ hands of its owner. Granted by TibiaRoyal.com"/>
<attribute key="ticks" value="10000"/>
<attribute key="damage" value="25"/>
</attribute>
<attribute key="script" value="moveevent">
<attribute key="eventType" value="stepin"/>
</attribute>
</item>
<item id="31381" name="nothing">
<attribute key="primarytype" value="natural tiles"/>
Expand Down
2 changes: 2 additions & 0 deletions qodana.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ version: "1.0"
profile:
name: qodana.recommended

linter: jetbrains/qodana-clang:latest

bootstrap: |
set -e
sudo apt-get update && sudo apt-get -y dist-upgrade
Expand Down
2 changes: 2 additions & 0 deletions src/account/account_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class AccountRepository {
virtual bool loadBySession(const std::string &email, AccountInfo &acc) = 0;
virtual bool save(const AccountInfo &accInfo) = 0;

virtual bool getCharacterByAccountIdAndName(const uint32_t &id, const std::string &name) = 0;

virtual bool getPassword(const uint32_t &id, std::string &password) = 0;

virtual bool getCoins(const uint32_t &id, const uint8_t &type, uint32_t &coins) = 0;
Expand Down
10 changes: 10 additions & 0 deletions src/account/account_repository_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ bool AccountRepositoryDB::save(const AccountInfo &accInfo) {
return successful;
};

bool AccountRepositoryDB::getCharacterByAccountIdAndName(const uint32_t &id, const std::string &name) {
auto result = g_database().storeQuery(fmt::format("SELECT `id` FROM `players` WHERE `account_id` = {} AND `name` = {}", id, g_database().escapeString(name)));
if (!result) {
g_logger().error("Failed to get character: [{}] from account: [{}]!", name, id);
return false;
}

return result->countResults() == 1;
}

bool AccountRepositoryDB::getPassword(const uint32_t &id, std::string &password) {
auto result = g_database().storeQuery(fmt::format("SELECT * FROM `accounts` WHERE `id` = {}", id));
if (!result) {
Expand Down
2 changes: 2 additions & 0 deletions src/account/account_repository_db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class AccountRepositoryDB final : public AccountRepository {
bool loadBySession(const std::string &esseionKey, AccountInfo &acc) override;
bool save(const AccountInfo &accInfo) override;

bool getCharacterByAccountIdAndName(const uint32_t &id, const std::string &name) override;

bool getPassword(const uint32_t &id, std::string &password) override;

bool getCoins(const uint32_t &id, const uint8_t &type, uint32_t &coins) override;
Expand Down
2 changes: 1 addition & 1 deletion src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3932,7 +3932,7 @@ void Game::playerUseWithCreature(uint32_t playerId, const Position &fromPos, uin
}

const std::shared_ptr<Monster> monster = creature->getMonster();
if (monster && monster->isFamiliar() && creature->getMaster()->getPlayer() == player && (it.isRune() || it.type == ITEM_TYPE_POTION)) {
if (monster && monster->isFamiliar() && creature->getMaster() && creature->getMaster()->getPlayer() == player && (it.isRune() || it.type == ITEM_TYPE_POTION)) {
player->setNextPotionAction(OTSYS_TIME() + g_configManager().getNumber(EX_ACTIONS_DELAY_INTERVAL, __FUNCTION__));

if (it.isMultiUse()) {
Expand Down
7 changes: 6 additions & 1 deletion src/io/iologindata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "enums/account_type.hpp"
#include "enums/account_errors.hpp"

bool IOLoginData::gameWorldAuthentication(const std::string &accountDescriptor, const std::string &password, std::string &characterName, uint32_t &accountId, bool oldProtocol) {
bool IOLoginData::gameWorldAuthentication(const std::string &accountDescriptor, const std::string &password, std::string &characterName, uint32_t &accountId, bool oldProtocol, const uint32_t ip) {
Account account(accountDescriptor);
account.setProtocolCompat(oldProtocol);

Expand All @@ -38,6 +38,11 @@ bool IOLoginData::gameWorldAuthentication(const std::string &accountDescriptor,
}
}

if (!g_accountRepository().getCharacterByAccountIdAndName(account.getID(), characterName)) {
g_logger().warn("IP [{}] trying to connect into another account character", convertIPToString(ip));
return false;
}

if (AccountErrors_t::Ok != enumFromValue<AccountErrors_t>(account.load())) {
g_logger().error("Failed to load account [{}]", accountDescriptor);
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/io/iologindata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using ItemBlockList = std::list<std::pair<int32_t, std::shared_ptr<Item>>>;

class IOLoginData {
public:
static bool gameWorldAuthentication(const std::string &accountDescriptor, const std::string &sessionOrPassword, std::string &characterName, uint32_t &accountId, bool oldProcotol);
static bool gameWorldAuthentication(const std::string &accountDescriptor, const std::string &sessionOrPassword, std::string &characterName, uint32_t &accountId, bool oldProcotol, const uint32_t ip);
static uint8_t getAccountType(uint32_t accountId);
static void updateOnlineStatus(uint32_t guid, bool login);
static bool loadPlayerById(std::shared_ptr<Player> player, uint32_t id, bool disableIrrelevantInfo = true);
Expand Down
13 changes: 11 additions & 2 deletions src/lua/functions/core/game/config_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,21 @@ int ConfigFunctions::luaConfigManagerGetBoolean(lua_State* L) {
}

int ConfigFunctions::luaConfigManagerGetFloat(lua_State* L) {
auto key = getNumber<ConfigKey_t>(L, -1);
// configManager.getFloat(key, shouldRound = true)

// Ensure the first argument (key) is provided and is a valid enum
auto key = getNumber<ConfigKey_t>(L, 1);
if (!key) {
reportErrorFunc("Wrong enum");
return 1;
}

lua_pushnumber(L, g_configManager().getFloat(key, __FUNCTION__));
// Check if the second argument (shouldRound) is provided and is a boolean; default to true if not provided
bool shouldRound = getBoolean(L, 2, true);
float value = g_configManager().getFloat(key, __FUNCTION__);
double finalValue = shouldRound ? static_cast<double>(std::round(value * 100.0) / 100.0) : value;

g_logger().debug("[{}] key: {}, finalValue: {}, shouldRound: {}", __METHOD_NAME__, magic_enum::enum_name(key), finalValue, shouldRound);
lua_pushnumber(L, finalValue);
return 1;
}
27 changes: 27 additions & 0 deletions src/lua/functions/core/game/config_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@ class ConfigFunctions final : LuaScriptInterface {
static void init(lua_State* L);

private:
/**
* @brief Retrieves a float configuration value from the configuration manager, with an optional rounding.
*
* This function is a Lua binding used to get a float value from the configuration manager. It requires
* a key as the first argument, which should be a valid enumeration. An optional second boolean argument
* specifies whether the retrieved float should be rounded to two decimal places.
*
* @param L Pointer to the Lua state. The first argument must be a valid enum key, and the second argument (optional)
* can be a boolean indicating whether to round the result.
*
* @return Returns 1 after pushing the result onto the Lua stack, indicating the number of return values.
*
* @exception reportErrorFunc Throws an error if the first argument is not a valid enum.
*
* Usage:
* local result = ConfigManager.getFloat(ConfigKey.SomeKey)
* local result_rounded = ConfigManager.getFloat(ConfigKey.SomeKey, false)
*
* Detailed behavior:
* 1. Extracts the key from the first Lua stack argument as an enumeration of type `ConfigKey_t`.
* 2. Checks if the second argument is provided; if not, defaults to true for rounding.
* 3. Retrieves the float value associated with the key from the configuration manager.
* 4. If rounding is requested, rounds the value to two decimal places.
* 5. Logs the method call and the obtained value using the debug logger.
* 6. Pushes the final value (rounded or original) back onto the Lua stack.
* 7. Returns 1 to indicate a single return value.
*/
static int luaConfigManagerGetFloat(lua_State* L);
static int luaConfigManagerGetBoolean(lua_State* L);
static int luaConfigManagerGetNumber(lua_State* L);
Expand Down
2 changes: 1 addition & 1 deletion src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ void ProtocolGame::onRecvFirstMessage(NetworkMessage &msg) {
}

uint32_t accountId;
if (!IOLoginData::gameWorldAuthentication(accountDescriptor, password, characterName, accountId, oldProtocol)) {
if (!IOLoginData::gameWorldAuthentication(accountDescriptor, password, characterName, accountId, oldProtocol, getIP())) {
ss.str(std::string());
if (authType == "session") {
ss << "Your session has expired. Please log in again.";
Expand Down
11 changes: 11 additions & 0 deletions tests/fixture/account/in_memory_account_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ namespace tests {
return true;
}

bool getCharacterByAccountIdAndName(const uint32_t &id, const std::string &name) final {
for (auto it = accounts.begin(); it != accounts.end(); ++it) {
if (it->second.id == id) {
if (it->second.players.find(name) != it->second.players.end()) {
return true;
}
}
}
return false;
}

InMemoryAccountRepository &reset() {
accounts.clear();
coins_.clear();
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/account/account_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,32 @@ suite<"account"> accountTest = [] {
expect(acc.load() == enumToValue(AccountErrors_t::Ok));
expect(acc.authenticate());
};

test("Account::getCharacterByAccountIdAndName using an account with the given character.") = [&injectionFixture] {
auto [accountRepository] = injectionFixture.get<AccountRepository>();

Account acc { 1 };
accountRepository.addAccount(
"session-key",
AccountInfo { 1, 1, 1, AccountType::ACCOUNT_TYPE_GOD, { { "Canary", 1 }, { "Canary2", 2 } }, false, getTimeNow() + 24 * 60 * 60 * 1000 }
);

const auto hasCharacter = accountRepository.getCharacterByAccountIdAndName(1, "Canary");

expect(hasCharacter);
};

test("Account::getCharacterByAccountIdAndName using an account without the given character.") = [&injectionFixture] {
auto [accountRepository] = injectionFixture.get<AccountRepository>();

Account acc { 1 };
accountRepository.addAccount(
"session-key",
AccountInfo { 1, 1, 1, AccountType::ACCOUNT_TYPE_GOD, { { "Canary", 1 }, { "Canary2", 2 } }, false, getTimeNow() + 24 * 60 * 60 * 1000 }
);

const auto hasCharacter = accountRepository.getCharacterByAccountIdAndName(1, "Invalid");

expect(!hasCharacter);
};
};

0 comments on commit d681263

Please sign in to comment.