Skip to content

Commit

Permalink
refactor: worlds instantiated by game
Browse files Browse the repository at this point in the history
  • Loading branch information
phacUFPE committed Aug 18, 2024
1 parent 578004e commit 36d0a42
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 165 deletions.
4 changes: 2 additions & 2 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ maintainModeMessage = ""
-- NOTE: valid values for worldType are: "pvp", "no-pvp" and "pvp-enforced"
-- NOTE: removeBeginningWeaponAmmunition: spears, arrows, bolt have endless ammo (allows training for paladins)
-- NOTE: refundManaOnBeginningWeapons: wand of vortex and snakebite refund mana used (allows training for mages)
-- NOTE: World id is 0 as default, new worlds must have different ids.
worldId = 0
-- NOTE: World id is 1 as default, new worlds must have different ids.
worldId = 1
worldType = "pvp"
hotkeyAimbotEnabled = true
protectionLevel = 7
Expand Down
3 changes: 1 addition & 2 deletions src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "game/zones/zone.hpp"
#include "game/scheduling/dispatcher.hpp"
#include "game/scheduling/events_scheduler.hpp"
#include "game/world/gameworldconfig.hpp"
#include "io/iomarket.hpp"
#include "lib/thread/thread_pool.hpp"
#include "lua/creature/events.hpp"
Expand Down Expand Up @@ -75,7 +74,7 @@ int CanaryServer::run() {
g_metrics().init(metricsOptions);
#endif
rsa.start();
g_gameworld().setWorldId(g_configManager().getNumber(WORLD_ID, __FUNCTION__));
g_game().worlds()->setId(g_configManager().getNumber(WORLD_ID, __FUNCTION__));
initializeDatabase();
loadModules();
setWorldType();
Expand Down
8 changes: 4 additions & 4 deletions src/database/databasemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "database/databasemanager.hpp"
#include "lua/functions/core/libs/core_libs_functions.hpp"
#include "lua/scripts/luascript.hpp"
#include "game/world/gameworldconfig.hpp"
#include "game/game.hpp"

bool DatabaseManager::optimizeTables() {
Database &db = Database::getInstance();
Expand Down Expand Up @@ -121,7 +121,7 @@ void DatabaseManager::updateDatabase() {

bool DatabaseManager::getDatabaseConfig(const std::string &config, int32_t &value) {
Database &db = Database::getInstance();
std::string query = fmt::format("SELECT `value` FROM `server_config` WHERE `worldId` = {} AND `config` = {}", g_gameworld().getWorldId(), db.escapeString(config));
std::string query = fmt::format("SELECT `value` FROM `server_config` WHERE `worldId` = {} AND `config` = {}", g_game().worlds()->getId(), db.escapeString(config));
if (config == "db_version") {
query = fmt::format("SELECT `value` FROM `server_config` WHERE `config` = {}", db.escapeString(config));
}
Expand All @@ -142,9 +142,9 @@ void DatabaseManager::registerDatabaseConfig(const std::string &config, int32_t
int32_t tmp;

if (!getDatabaseConfig(config, tmp)) {
query = fmt::format("INSERT INTO `server_config` (`config`, `value`, `worldId`) VALUES ({}, {}, {})", db.escapeString(config), value, g_gameworld().getWorldId());
query = fmt::format("INSERT INTO `server_config` (`config`, `value`, `worldId`) VALUES ({}, {}, {})", db.escapeString(config), value, g_game().worlds()->getId());
} else {
query = fmt::format("UPDATE `server_config` SET `value` = {} WHERE `worldId` = {}, `config` = {}", value, g_gameworld().getWorldId(), db.escapeString(config));
query = fmt::format("UPDATE `server_config` SET `value` = {} WHERE `worldId` = {}, `config` = {}", value, g_game().worlds()->getId(), db.escapeString(config));
if (strcasecmp(config.c_str(), "db_version")) {
query = fmt::format("UPDATE `server_config` SET `value` = {} WHERE `config` = {}", value, db.escapeString(config));
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ target_sources(${PROJECT_NAME}_lib PRIVATE
scheduling/dispatcher.cpp
scheduling/task.cpp
scheduling/save_manager.cpp
world/gameworldconfig.cpp
worlds/gameworlds.cpp
zones/zone.cpp
)
30 changes: 19 additions & 11 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "lua/callbacks/event_callback.hpp"
#include "lua/callbacks/events_callbacks.hpp"
#include "creatures/players/highscore_category.hpp"
#include "game/world/gameworldconfig.hpp"
#include "game/worlds/gameworlds.hpp"
#include "game/zones/zone.hpp"
#include "lua/global/globalevent.hpp"
#include "io/iologindata.hpp"
Expand Down Expand Up @@ -196,6 +196,9 @@ Game::Game() {
offlineTrainingWindow.defaultEnterButton = 0;
offlineTrainingWindow.priority = true;

// Create instance of worlds
m_worlds = std::make_unique<Worlds>();

// Create instance of IOWheel to Game class
m_IOWheel = std::make_unique<IOWheel>();

Expand Down Expand Up @@ -397,6 +400,15 @@ Game::Game() {

Game::~Game() = default;

// Worlds interface
std::unique_ptr<Worlds> &Game::worlds() {
return m_worlds;
}

const std::unique_ptr<Worlds> &Game::worlds() const {
return m_worlds;
}

void Game::resetMonsters() const {
for (const auto &[monsterId, monster] : getMonsters()) {
monster->clearTargetList();
Expand All @@ -414,7 +426,7 @@ void Game::resetNpcs() const {

void Game::loadBoostedCreature() {
auto &db = Database::getInstance();
const auto worldId = g_gameworld().getWorldId();
const auto worldId = worlds()->getId();

const std::string selectQuery = "SELECT * FROM `boosted_creature`";

Expand Down Expand Up @@ -536,10 +548,6 @@ GameState_t Game::getGameState() const {
return gameState;
}

void Game::setWorldType(WorldType_t type) {
worldType = type;
}

void Game::setGameState(GameState_t newState) {
if (gameState == GAME_STATE_SHUTDOWN) {
return; // this cannot be stopped
Expand Down Expand Up @@ -8030,7 +8038,7 @@ void Game::updateCreatureWalkthrough(std::shared_ptr<Creature> creature) {
}

void Game::updateCreatureSkull(std::shared_ptr<Creature> creature) {
if (getWorldType() != WORLD_TYPE_PVP) {
if (worlds()->getType() != WORLD_TYPE_PVP) {
return;
}

Expand Down Expand Up @@ -8080,7 +8088,7 @@ void Game::updateCreatureType(std::shared_ptr<Creature> creature) {

void Game::loadMotdNum() {
Database &db = Database::getInstance();
const auto worldId = g_gameworld().getWorldId();
const auto worldId = worlds()->getId();

auto result = db.storeQuery(fmt::format("SELECT `value` FROM `server_config` WHERE `config` = 'motd_num' AND `worldId` = {}", worldId));
if (result) {
Expand All @@ -8102,7 +8110,7 @@ void Game::loadMotdNum() {

void Game::saveMotdNum() const {
Database &db = Database::getInstance();
const auto worldId = g_gameworld().getWorldId();
const auto worldId = worlds()->getId();

std::string query = fmt::format("UPDATE `server_config` SET `value` = {} WHERE `config` = 'motd_num' AND `worldId` = {}", motdNum, worldId);
db.executeQuery(query);
Expand All @@ -8127,13 +8135,13 @@ void Game::checkPlayersRecord() {
void Game::updatePlayersRecord() const {
Database &db = Database::getInstance();

std::string query = fmt::format("UPDATE `server_config` SET `value` = {} WHERE `config` = 'players_record' AND `worldId` = {}", playersRecord, g_gameworld().getWorldId());
std::string query = fmt::format("UPDATE `server_config` SET `value` = {} WHERE `config` = 'players_record' AND `worldId` = {}", playersRecord, worlds()->getId());
db.executeQuery(query);
}

void Game::loadPlayersRecord() {
Database &db = Database::getInstance();
const auto worldId = g_gameworld().getWorldId();
const auto worldId = worlds()->getId();

const auto result = db.storeQuery(fmt::format("SELECT `value` FROM `server_config` WHERE `config` = 'players_record' AND `worldId` = {}", worldId));
if (result) {
Expand Down
13 changes: 6 additions & 7 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "items/items_classification.hpp"
#include "modal_window/modal_window.hpp"
#include "enums/object_category.hpp"
#include "game/worlds/gameworlds.hpp"

// Forward declaration for protobuf class
namespace Canary {
Expand Down Expand Up @@ -91,6 +92,10 @@ class Game {
return inject<Game>();
}

// Game worlds interface
std::unique_ptr<Worlds> &worlds();
const std::unique_ptr<Worlds> &worlds() const;

void resetMonsters() const;
void resetNpcs() const;

Expand Down Expand Up @@ -120,11 +125,6 @@ class Game {
height = map.height;
}

void setWorldType(WorldType_t type);
WorldType_t getWorldType() const {
return worldType;
}

const std::map<uint32_t, std::unique_ptr<TeamFinder>> &getTeamFinderList() const {
return teamFinderMap;
}
Expand Down Expand Up @@ -898,8 +898,6 @@ class Game {
bool browseField = false;

GameState_t gameState = GAME_STATE_NORMAL;
uint16_t worldId = 0;
WorldType_t worldType = WORLD_TYPE_PVP;

LightState_t lightState = LIGHT_STATE_DAY;
LightState_t currentLightState = lightState;
Expand Down Expand Up @@ -964,6 +962,7 @@ class Game {

// Variable members (m_)
std::unique_ptr<IOWheel> m_IOWheel;
std::unique_ptr<Worlds> m_worlds;

void cacheQueryHighscore(const std::string &key, const std::string &query, uint32_t page, uint8_t entriesPerPage);
void processHighscoreResults(DBResult_ptr result, uint32_t playerID, uint8_t category, uint32_t vocation, uint8_t entriesPerPage);
Expand Down
45 changes: 0 additions & 45 deletions src/game/world/gameworldconfig.cpp

This file was deleted.

48 changes: 0 additions & 48 deletions src/game/world/gameworldconfig.hpp

This file was deleted.

41 changes: 41 additions & 0 deletions src/game/worlds/gameworlds.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* 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 "game/worlds/gameworlds.hpp"
#include "game/game_definitions.hpp"

[[nodiscard]] const char* Worlds::getIp(uint16_t id) {
return ip[id];
}

[[nodiscard]] uint16_t Worlds::getPort(uint16_t id) {
return port[id];
}

[[nodiscard]] std::string Worlds::getName(uint16_t id) {
return name[id];
}

[[nodiscard]] uint16_t Worlds::getId() const noexcept {
return id;
}

void Worlds::setId(uint16_t newId) noexcept {
id = newId;
}

void Worlds::setType(WorldType_t newType) noexcept {
type = newType;
}

[[nodiscard]] WorldType_t Worlds::getType() const noexcept {
return type;
}
45 changes: 45 additions & 0 deletions src/game/worlds/gameworlds.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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

struct World {
World(uint16_t id, std::string name, std::string ip, uint16_t port) :
id(id), name(std::move(name)), ip(std::move(ip)), port(port) { }

std::string name = "";
std::string ip = "";

uint16_t id = 0;
uint16_t port = 7171;
};

class Worlds {
public:
void setId(uint16_t id) noexcept;
[[nodiscard]] const char* getIp(uint16_t id);
[[nodiscard]] uint16_t getPort(uint16_t id);
[[nodiscard]] std::string getName(uint16_t id);
[[nodiscard]] uint16_t getId() const noexcept;
void setType(WorldType_t type) noexcept;
[[nodiscard]] WorldType_t getType() const noexcept;

[[nodiscard]] const std::vector<World> &getWorlds() const noexcept {
return worlds;
}

private:
std::vector<GameWorld> worlds;

uint16_t id = 0;
std::map<uint16_t, const char*> ip;
std::map<uint16_t, std::string> name;
WorldType_t type = WORLD_TYPE_PVP;
std::map<uint16_t, uint16_t> port;
};
Loading

0 comments on commit 36d0a42

Please sign in to comment.