Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(skymp5-server): add onlinePlayersInfo property #2345

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "OnlinePlayersInfoBinding.h"

#include "Config.h"
#include <napi.h>

namespace {
struct PlayerInfo
{
uint32_t actorId; // may be zero if not assigned to any actor
std::string guid; // may be an empty string if
Networking::UserId userId;
};
}

Napi::Value OnlinePlayersInfoBinding::Get(Napi::Env env,
ScampServer& scampServer, uint32_t)
{
auto& partOne = scampServer.GetPartOne();
// const auto& serverState = partOne->serverState;

thread_local std::vector<PlayerInfo> g_onlineActors(kMaxPlayers);
size_t numOnlineActors = 0;

auto maxConnectedId = partOne->serverState.maxConnectedId;
for (size_t i = 0; i <= maxConnectedId; ++i) {
if (auto actor = partOne->serverState.ActorByUser(i)) {
g_onlineActors[numOnlineActors] = {
actor->GetFormId(),
partOne->serverState.UserGuid(i),
static_cast<Networking::UserId>(i),
};
++numOnlineActors;
}
}

auto arr = Napi::Array::New(env, numOnlineActors);
for (size_t k = 0; k < numOnlineActors; ++k) {
const auto& info = g_onlineActors[k];
auto obj = Napi::Object::New(env);
obj.Set("actorId", info.actorId);
obj.Set("guid", info.guid); // hide inside another 'networking' obj?
obj.Set("userId", info.userId);
arr.Set(k, obj);
}
return arr;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include "PropertyBinding.h"

class OnlinePlayersInfoBinding : public PropertyBinding
{
public:
std::string GetPropertyName() const override { return "onlinePlayersInfo"; }
Napi::Value Get(Napi::Env env, ScampServer& scampServer,
uint32_t formId) override;
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "LocationalDataBinding.h"
#include "NeighborsBinding.h"
#include "OnlinePlayersBinding.h"
#include "OnlinePlayersInfoBinding.h"
#include "PercentagesBinding.h"
#include "PosBinding.h"
#include "ProfileIdBinding.h"
Expand All @@ -42,6 +43,7 @@ PropertyBindingFactory::CreateStandardPropertyBindings()
result["locationalData"] = std::make_shared<LocationalDataBinding>();
result["neighbors"] = std::make_shared<NeighborsBinding>();
result["onlinePlayers"] = std::make_shared<OnlinePlayersBinding>();
result["onlinePlayersInfo"] = std::make_shared<OnlinePlayersInfoBinding>();
result["percentages"] = std::make_shared<PercentagesBinding>();
result["pos"] = std::make_shared<PosBinding>();
result["profileId"] = std::make_shared<ProfileIdBinding>();
Expand Down
2 changes: 1 addition & 1 deletion skymp5-server/cpp/server_guest_lib/ServerState.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct DeferredMessage

struct UserInfo
{
bool isDisconnecting = false;
// bool isDisconnecting = false;

bool isPacketHistoryRecording = false;
PacketHistory packetHistory;
Expand Down
Loading