mirror of
https://github.com/OpenRCT2/OpenRCT2.git
synced 2025-01-23 02:41:58 -05:00
Add ipAddress and publicHashKey properties to player API
This commit is contained in:
parent
7b9fa972b6
commit
ecce4da6e8
6 changed files with 101 additions and 3 deletions
2
distribution/openrct2.d.ts
vendored
2
distribution/openrct2.d.ts
vendored
|
@ -590,6 +590,8 @@ declare global {
|
|||
readonly ping: number;
|
||||
readonly commandsRan: number;
|
||||
readonly moneySpent: number;
|
||||
readonly ipAddress: string;
|
||||
readonly publicKeyHash: string;
|
||||
}
|
||||
|
||||
interface PlayerGroup {
|
||||
|
|
|
@ -136,6 +136,7 @@ public:
|
|||
void ProcessDisconnectedClients();
|
||||
std::vector<std::unique_ptr<NetworkPlayer>>::iterator GetPlayerIteratorByID(uint8_t id);
|
||||
NetworkPlayer* GetPlayerByID(uint8_t id);
|
||||
NetworkConnection* GetPlayerConnection(uint8_t id);
|
||||
std::vector<std::unique_ptr<NetworkGroup>>::iterator GetGroupIteratorByID(uint8_t id);
|
||||
NetworkGroup* GetGroupByID(uint8_t id);
|
||||
static const char* FormatChat(NetworkPlayer* fromplayer, const char* text);
|
||||
|
@ -665,6 +666,22 @@ uint8_t Network::GetPlayerID()
|
|||
return player_id;
|
||||
}
|
||||
|
||||
NetworkConnection* Network::GetPlayerConnection(uint8_t id)
|
||||
{
|
||||
auto player = GetPlayerByID(id);
|
||||
if (player != nullptr)
|
||||
{
|
||||
for (auto& connection : client_connection_list)
|
||||
{
|
||||
if (connection->Player == player)
|
||||
{
|
||||
return connection.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Network::Update()
|
||||
{
|
||||
_closeLock = true;
|
||||
|
@ -3385,6 +3402,26 @@ money32 network_get_player_money_spent(uint32_t index)
|
|||
return gNetwork.player_list[index]->MoneySpent;
|
||||
}
|
||||
|
||||
std::string network_get_player_ip_address(uint32_t id)
|
||||
{
|
||||
auto conn = gNetwork.GetPlayerConnection(id);
|
||||
if (conn != nullptr)
|
||||
{
|
||||
return conn->Socket->GetIpAddress();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string network_get_player_public_key_hash(uint32_t id)
|
||||
{
|
||||
auto player = gNetwork.GetPlayerByID(id);
|
||||
if (player != nullptr)
|
||||
{
|
||||
return player->KeyHash;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void network_add_player_money_spent(uint32_t index, money32 cost)
|
||||
{
|
||||
gNetwork.player_list[index]->AddMoneySpent(cost);
|
||||
|
@ -4054,6 +4091,14 @@ money32 network_get_player_money_spent(uint32_t index)
|
|||
{
|
||||
return MONEY(0, 0);
|
||||
}
|
||||
std::string network_get_player_ip_address(uint32_t id)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
std::string network_get_player_public_key_hash(uint32_t id)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
void network_add_player_money_spent(uint32_t index, money32 cost)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -201,6 +201,7 @@ private:
|
|||
uint16_t _listeningPort = 0;
|
||||
SOCKET _socket = INVALID_SOCKET;
|
||||
|
||||
std::string _ipAddress;
|
||||
std::string _hostName;
|
||||
std::future<void> _connectFuture;
|
||||
std::string _error;
|
||||
|
@ -322,18 +323,21 @@ public:
|
|||
}
|
||||
else
|
||||
{
|
||||
auto ipAddress = GetIpAddressFromSocket(reinterpret_cast<sockaddr_in*>(&client_addr));
|
||||
|
||||
char hostName[NI_MAXHOST];
|
||||
int32_t rc = getnameinfo(
|
||||
reinterpret_cast<struct sockaddr*>(&client_addr), client_len, hostName, sizeof(hostName), nullptr, 0,
|
||||
NI_NUMERICHOST | NI_NUMERICSERV);
|
||||
SetOption(socket, IPPROTO_TCP, TCP_NODELAY, true);
|
||||
|
||||
if (rc == 0)
|
||||
{
|
||||
tcpSocket = std::unique_ptr<ITcpSocket>(new TcpSocket(socket, hostName));
|
||||
tcpSocket = std::unique_ptr<ITcpSocket>(new TcpSocket(socket, hostName, ipAddress));
|
||||
}
|
||||
else
|
||||
{
|
||||
tcpSocket = std::unique_ptr<ITcpSocket>(new TcpSocket(socket, ""));
|
||||
tcpSocket = std::unique_ptr<ITcpSocket>(new TcpSocket(socket, "", ipAddress));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -546,11 +550,17 @@ public:
|
|||
return _hostName.empty() ? nullptr : _hostName.c_str();
|
||||
}
|
||||
|
||||
std::string GetIpAddress() const override
|
||||
{
|
||||
return _ipAddress;
|
||||
}
|
||||
|
||||
private:
|
||||
explicit TcpSocket(SOCKET socket, const std::string& hostName)
|
||||
explicit TcpSocket(SOCKET socket, const std::string& hostName, const std::string& ipAddress)
|
||||
{
|
||||
_socket = socket;
|
||||
_hostName = hostName;
|
||||
_ipAddress = ipAddress;
|
||||
_status = SOCKET_STATUS_CONNECTED;
|
||||
}
|
||||
|
||||
|
@ -563,6 +573,32 @@ private:
|
|||
}
|
||||
_status = SOCKET_STATUS_CLOSED;
|
||||
}
|
||||
|
||||
std::string GetIpAddressFromSocket(const sockaddr_in* addr)
|
||||
{
|
||||
std::string result;
|
||||
# if defined(__MINGW32__)
|
||||
if (addr->sin_family == AF_INET)
|
||||
{
|
||||
result = inet_ntoa(addr->sin_addr);
|
||||
}
|
||||
# else
|
||||
if (addr->sin_family == AF_INET)
|
||||
{
|
||||
char str[INET_ADDRSTRLEN]{};
|
||||
inet_ntop(AF_INET, &addr->sin_addr, str, sizeof(str));
|
||||
result = str;
|
||||
}
|
||||
else if (addr->sin_family == AF_INET6)
|
||||
{
|
||||
auto addrv6 = reinterpret_cast<const sockaddr_in6*>(&addr);
|
||||
char str[INET6_ADDRSTRLEN]{};
|
||||
inet_ntop(AF_INET6, &addrv6->sin6_addr, str, sizeof(str));
|
||||
result = str;
|
||||
}
|
||||
# endif
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
class UdpSocket final : public IUdpSocket, protected Socket
|
||||
|
|
|
@ -55,6 +55,7 @@ public:
|
|||
virtual SOCKET_STATUS GetStatus() const abstract;
|
||||
virtual const char* GetError() const abstract;
|
||||
virtual const char* GetHostName() const abstract;
|
||||
virtual std::string GetIpAddress() const abstract;
|
||||
|
||||
virtual void Listen(uint16_t port) abstract;
|
||||
virtual void Listen(const std::string& address, uint16_t port) abstract;
|
||||
|
|
|
@ -61,6 +61,8 @@ uint32_t network_get_player_flags(uint32_t index);
|
|||
int32_t network_get_player_ping(uint32_t index);
|
||||
int32_t network_get_player_id(uint32_t index);
|
||||
money32 network_get_player_money_spent(uint32_t index);
|
||||
std::string network_get_player_ip_address(uint32_t id);
|
||||
std::string network_get_player_public_key_hash(uint32_t id);
|
||||
void network_add_player_money_spent(uint32_t index, money32 cost);
|
||||
int32_t network_get_player_last_action(uint32_t index, int32_t time);
|
||||
void network_set_player_last_action(uint32_t index, int32_t command);
|
||||
|
|
|
@ -233,6 +233,16 @@ namespace OpenRCT2::Scripting
|
|||
# endif
|
||||
}
|
||||
|
||||
std::string ipAddress_get() const
|
||||
{
|
||||
return network_get_player_ip_address(_id);
|
||||
}
|
||||
|
||||
std::string publicKeyHash_get() const
|
||||
{
|
||||
return network_get_player_public_key_hash(_id);
|
||||
}
|
||||
|
||||
static void Register(duk_context* ctx)
|
||||
{
|
||||
dukglue_register_property(ctx, &ScPlayer::id_get, nullptr, "id");
|
||||
|
@ -241,6 +251,8 @@ namespace OpenRCT2::Scripting
|
|||
dukglue_register_property(ctx, &ScPlayer::ping_get, nullptr, "ping");
|
||||
dukglue_register_property(ctx, &ScPlayer::commandsRan_get, nullptr, "commandsRan");
|
||||
dukglue_register_property(ctx, &ScPlayer::moneySpent_get, nullptr, "moneySpent");
|
||||
dukglue_register_property(ctx, &ScPlayer::ipAddress_get, nullptr, "ipAddress");
|
||||
dukglue_register_property(ctx, &ScPlayer::publicKeyHash_get, nullptr, "publicKeyHash");
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue