Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
beats-dh committed Dec 5, 2024
1 parent 42bd15b commit c4f128d
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 78 deletions.
1 change: 1 addition & 0 deletions cmake/modules/BaseConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ find_package(pugixml CONFIG REQUIRED)
find_package(spdlog REQUIRED)
find_package(unofficial-argon2 CONFIG REQUIRED)
find_package(unofficial-libmariadb CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)

find_path(BOOST_DI_INCLUDE_DIRS "boost/di.hpp")

Expand Down
1 change: 1 addition & 0 deletions cmake/modules/CanaryLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ target_link_libraries(${PROJECT_NAME}_lib
unofficial::argon2::libargon2
unofficial::libmariadb
protobuf
nlohmann_json::nlohmann_json
)

if(FEATURE_METRICS)
Expand Down
29 changes: 29 additions & 0 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10882,3 +10882,32 @@ void Game::updatePlayersOnline() const {
g_logger().error("[Game::updatePlayersOnline] Failed to update players online.");
}
}

std::map<uint32_t, std::vector<std::shared_ptr<Player>>> Game::groupPlayersByIP() {
std::map<uint32_t, std::vector<std::shared_ptr<Player>>> groupedPlayers;

for (const auto &player : g_game().getPlayers() | std::views::values) {
uint32_t ip = player->getIP();
if (ip != 0 && player->idleTime <= 900000) {
groupedPlayers[ip].emplace_back(player);
}
}

return groupedPlayers;
}

PlayerStats Game::getPlayerStats() {
auto groupedPlayers = groupPlayersByIP();

uint32_t totalUniqueIPs = 0;
uint32_t totalPlayers = 0;

for (const auto &players : groupedPlayers | std::views::values) {
totalUniqueIPs++;

int activePlayers = std::min(static_cast<int>(players.size()), 4);
totalPlayers += activePlayers;
}

return { totalPlayers, totalUniqueIPs };
}
8 changes: 8 additions & 0 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ struct HighscoreCacheEntry {
std::chrono::time_point<std::chrono::system_clock> timestamp;
};

struct PlayerStats {
uint32_t totalPlayers;
uint32_t totalUniqueIPs;
};

class Game {
public:
Game();
Expand Down Expand Up @@ -713,6 +718,8 @@ class Game {
const std::unordered_map<uint16_t, std::string> &getHirelingSkills();
const std::unordered_map<uint16_t, std::string> &getHirelingOutfits();

PlayerStats getPlayerStats();

private:
std::map<uint16_t, Achievement> m_achievements;
std::map<std::string, uint16_t> m_achievementsNameToId;
Expand Down Expand Up @@ -948,6 +955,7 @@ class Game {
std::string generateHighscoreOrGetCachedQueryForOurRank(const std::string &categoryName, uint8_t entriesPerPage, uint32_t playerGUID, uint32_t vocation);

void updatePlayersOnline() const;
std::map<uint32_t, std::vector<std::shared_ptr<Player>>> groupPlayersByIP();
};

constexpr auto g_game = Game::getInstance;
3 changes: 2 additions & 1 deletion src/lua/functions/core/game/global_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ int GlobalFunctions::luaGetWorldLight(lua_State* L) {

int GlobalFunctions::luaGetWorldUpTime(lua_State* L) {
// getWorldUpTime()
const uint64_t uptime = (OTSYS_TIME(true) - ProtocolStatus::start) / 1000;
const auto now = std::chrono::steady_clock::now();
const auto uptime = std::chrono::duration_cast<std::chrono::seconds>(now - ProtocolStatus::start).count();
lua_pushnumber(L, uptime);
return 1;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lua/functions/core/network/network_message_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

#include "server/network/protocol/protocolgame.hpp"
#include "creatures/players/player.hpp"
#include "server/network/protocol/protocolstatus.hpp"
#include "lua/functions/lua_functions_loader.hpp"

#include <server/network/message/networkmessage.hpp>

void NetworkMessageFunctions::init(lua_State* L) {
Lua::registerSharedClass(L, "NetworkMessage", "", NetworkMessageFunctions::luaNetworkMessageCreate);
Lua::registerMetaMethod(L, "NetworkMessage", "__eq", Lua::luaUserdataCompare);
Expand Down
Loading

0 comments on commit c4f128d

Please sign in to comment.