mirror of https://github.com/OpenRCT2/OpenRCT2.git
Move ScNetwork and ScPlayer into their own files
This commit is contained in:
parent
64b57734e1
commit
99eee53c60
|
@ -783,6 +783,9 @@
|
|||
359328A3A65D49578912CA40 /* ScLitter.hpp in Headers */ = {isa = PBXBuildFile; fileRef = C30388F4F6D34DF59EF7072C /* ScLitter.hpp */; };
|
||||
149D1ACFD5FA42938364C843 /* ScPlayerGroup.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DA49170D35604E6A986D09D9 /* ScPlayerGroup.cpp */; };
|
||||
97C222A2A4F543E9AEC3912B /* ScPlayerGroup.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 887EBCE57E4A48C88191346E /* ScPlayerGroup.hpp */; };
|
||||
E436DE7807A74621B7BF2276 /* ScPlayer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E2A4181A243F4B77BA36ACE2 /* ScPlayer.cpp */; };
|
||||
2D0A43F28D5747C9B617F342 /* ScPlayer.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 7A08C451885D4D419FB4820D /* ScPlayer.hpp */; };
|
||||
F08623EA69E7456DB79F3E06 /* ScNetwork.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 200F0E6CAD3B41499A3CFC36 /* ScNetwork.cpp */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
|
@ -1868,6 +1871,9 @@
|
|||
C30388F4F6D34DF59EF7072C /* ScLitter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ScLitter.hpp; path = src/openrct2/scripting/bindings/entity/ScLitter.hpp; sourceTree = SOURCE_ROOT; };
|
||||
DA49170D35604E6A986D09D9 /* ScPlayerGroup.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ScPlayerGroup.cpp; path = src/openrct2/scripting/bindings/network/ScPlayerGroup.cpp; sourceTree = SOURCE_ROOT; };
|
||||
887EBCE57E4A48C88191346E /* ScPlayerGroup.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ScPlayerGroup.hpp; path = src/openrct2/scripting/bindings/network/ScPlayerGroup.hpp; sourceTree = SOURCE_ROOT; };
|
||||
E2A4181A243F4B77BA36ACE2 /* ScPlayer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ScPlayer.cpp; path = src/openrct2/scripting/bindings/network/ScPlayer.cpp; sourceTree = SOURCE_ROOT; };
|
||||
7A08C451885D4D419FB4820D /* ScPlayer.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ScPlayer.hpp; path = src/openrct2/scripting/bindings/network/ScPlayer.hpp; sourceTree = SOURCE_ROOT; };
|
||||
200F0E6CAD3B41499A3CFC36 /* ScNetwork.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ScNetwork.cpp; path = src/openrct2/scripting/bindings/network/ScNetwork.cpp; sourceTree = SOURCE_ROOT; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
|
@ -3413,6 +3419,9 @@
|
|||
D4DD54E62DAE4E4889E26DF6 /* ScSocket.hpp */,
|
||||
DA49170D35604E6A986D09D9 /* ScPlayerGroup.cpp */,
|
||||
887EBCE57E4A48C88191346E /* ScPlayerGroup.hpp */,
|
||||
E2A4181A243F4B77BA36ACE2 /* ScPlayer.cpp */,
|
||||
7A08C451885D4D419FB4820D /* ScPlayer.hpp */,
|
||||
200F0E6CAD3B41499A3CFC36 /* ScNetwork.cpp */,
|
||||
);
|
||||
name = network;
|
||||
sourceTree = "<group>";
|
||||
|
@ -3595,6 +3604,7 @@
|
|||
9DF65BAE250C4307AE5794DB /* ScStaff.hpp in Headers */,
|
||||
359328A3A65D49578912CA40 /* ScLitter.hpp in Headers */,
|
||||
97C222A2A4F543E9AEC3912B /* ScPlayerGroup.hpp in Headers */,
|
||||
2D0A43F28D5747C9B617F342 /* ScPlayer.hpp in Headers */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
@ -4405,6 +4415,8 @@
|
|||
7A06F880718F49CA9FB71F52 /* ScStaff.cpp in Sources */,
|
||||
8E6484F6C43644F4B644D361 /* ScLitter.cpp in Sources */,
|
||||
149D1ACFD5FA42938364C843 /* ScPlayerGroup.cpp in Sources */,
|
||||
E436DE7807A74621B7BF2276 /* ScPlayer.cpp in Sources */,
|
||||
F08623EA69E7456DB79F3E06 /* ScNetwork.cpp in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
|
@ -410,6 +410,7 @@
|
|||
<ClInclude Include="scripting\bindings\entity\ScPeep.hpp" />
|
||||
<ClInclude Include="scripting\bindings\entity\ScStaff.hpp" />
|
||||
<ClInclude Include="scripting\bindings\entity\ScVehicle.hpp" />
|
||||
<ClInclude Include="scripting\bindings\network\ScPlayer.hpp" />
|
||||
<ClInclude Include="scripting\bindings\network\ScPlayerGroup.hpp" />
|
||||
<ClInclude Include="scripting\Duktape.hpp" />
|
||||
<ClInclude Include="scripting\HookEngine.h" />
|
||||
|
@ -859,6 +860,8 @@
|
|||
<ClCompile Include="scripting\bindings\entity\ScLitter.cpp" />
|
||||
<ClCompile Include="scripting\bindings\entity\ScStaff.cpp" />
|
||||
<ClCompile Include="scripting\bindings\entity\ScVehicle.cpp" />
|
||||
<ClCompile Include="scripting\bindings\network\ScNetwork.cpp" />
|
||||
<ClCompile Include="scripting\bindings\network\ScPlayer.cpp" />
|
||||
<ClCompile Include="scripting\bindings\network\ScPlayerGroup.cpp" />
|
||||
<ClCompile Include="scripting\HookEngine.cpp" />
|
||||
<ClCompile Include="scripting\Plugin.cpp" />
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
# include "bindings/game/ScContext.hpp"
|
||||
# include "bindings/game/ScDisposable.hpp"
|
||||
# include "bindings/network/ScNetwork.hpp"
|
||||
# include "bindings/network/ScPlayer.hpp"
|
||||
# include "bindings/network/ScPlayerGroup.hpp"
|
||||
# include "bindings/network/ScSocket.hpp"
|
||||
# include "bindings/object/ScObject.hpp"
|
||||
|
|
|
@ -0,0 +1,297 @@
|
|||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2020 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
|
||||
# include "ScNetwork.hpp"
|
||||
|
||||
# include "../../../Context.h"
|
||||
# include "../../../actions/NetworkModifyGroupAction.h"
|
||||
# include "../../../actions/PlayerKickAction.h"
|
||||
# include "../../../network/NetworkAction.h"
|
||||
# include "../../../network/network.h"
|
||||
|
||||
namespace OpenRCT2::Scripting
|
||||
{
|
||||
ScNetwork::ScNetwork(duk_context* ctx)
|
||||
: _context(ctx)
|
||||
{
|
||||
}
|
||||
|
||||
std::string ScNetwork::mode_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
switch (network_get_mode())
|
||||
{
|
||||
case NETWORK_MODE_SERVER:
|
||||
return "server";
|
||||
case NETWORK_MODE_CLIENT:
|
||||
return "client";
|
||||
}
|
||||
# endif
|
||||
return "none";
|
||||
}
|
||||
|
||||
int32_t ScNetwork::numPlayers_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
return network_get_num_players();
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
|
||||
int32_t ScNetwork::numGroups_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
return network_get_num_groups();
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
|
||||
int32_t ScNetwork::defaultGroup_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
return network_get_default_group();
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
|
||||
void ScNetwork::defaultGroup_set(int32_t value)
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto action = NetworkModifyGroupAction(ModifyGroupType::SetDefault, value);
|
||||
GameActions::Execute(&action);
|
||||
# endif
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<ScPlayerGroup>> ScNetwork::groups_get() const
|
||||
{
|
||||
std::vector<std::shared_ptr<ScPlayerGroup>> groups;
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numGroups = network_get_num_groups();
|
||||
for (int32_t i = 0; i < numGroups; i++)
|
||||
{
|
||||
auto groupId = network_get_group_id(i);
|
||||
groups.push_back(std::make_shared<ScPlayerGroup>(groupId));
|
||||
}
|
||||
# endif
|
||||
return groups;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<ScPlayer>> ScNetwork::players_get() const
|
||||
{
|
||||
std::vector<std::shared_ptr<ScPlayer>> players;
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numPlayers = network_get_num_players();
|
||||
for (int32_t i = 0; i < numPlayers; i++)
|
||||
{
|
||||
auto playerId = network_get_player_id(i);
|
||||
players.push_back(std::make_shared<ScPlayer>(playerId));
|
||||
}
|
||||
# endif
|
||||
return players;
|
||||
}
|
||||
|
||||
std::shared_ptr<ScPlayer> ScNetwork::currentPlayer_get() const
|
||||
{
|
||||
std::shared_ptr<ScPlayer> player;
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto playerId = network_get_current_player_id();
|
||||
player = std::make_shared<ScPlayer>(playerId);
|
||||
# endif
|
||||
return player;
|
||||
}
|
||||
|
||||
std::shared_ptr<ScPlayer> ScNetwork::getPlayer(int32_t index) const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numPlayers = network_get_num_players();
|
||||
if (index < numPlayers)
|
||||
{
|
||||
auto playerId = network_get_player_id(index);
|
||||
return std::make_shared<ScPlayer>(playerId);
|
||||
}
|
||||
# endif
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
DukValue ScNetwork::stats_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto obj = OpenRCT2::Scripting::DukObject(_context);
|
||||
auto networkStats = network_get_stats();
|
||||
{
|
||||
duk_push_array(_context);
|
||||
duk_uarridx_t index = 0;
|
||||
for (auto v : networkStats.bytesReceived)
|
||||
{
|
||||
duk_push_number(_context, v);
|
||||
duk_put_prop_index(_context, -2, index);
|
||||
index++;
|
||||
}
|
||||
obj.Set("bytesReceived", DukValue::take_from_stack(_context));
|
||||
}
|
||||
{
|
||||
duk_push_array(_context);
|
||||
duk_uarridx_t index = 0;
|
||||
for (auto v : networkStats.bytesSent)
|
||||
{
|
||||
duk_push_number(_context, v);
|
||||
duk_put_prop_index(_context, -2, index);
|
||||
index++;
|
||||
}
|
||||
obj.Set("bytesSent", DukValue::take_from_stack(_context));
|
||||
}
|
||||
return obj.Take();
|
||||
# else
|
||||
return ToDuk(_context, nullptr);
|
||||
# endif
|
||||
}
|
||||
|
||||
std::shared_ptr<ScPlayerGroup> ScNetwork::getGroup(int32_t index) const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numGroups = network_get_num_groups();
|
||||
if (index < numGroups)
|
||||
{
|
||||
auto groupId = network_get_group_id(index);
|
||||
return std::make_shared<ScPlayerGroup>(groupId);
|
||||
}
|
||||
# endif
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ScNetwork::addGroup()
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto networkModifyGroup = NetworkModifyGroupAction(ModifyGroupType::AddGroup);
|
||||
GameActions::Execute(&networkModifyGroup);
|
||||
# endif
|
||||
}
|
||||
|
||||
void ScNetwork::removeGroup(int32_t index)
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numGroups = network_get_num_groups();
|
||||
if (index < numGroups)
|
||||
{
|
||||
auto groupId = network_get_group_id(index);
|
||||
auto networkAction = NetworkModifyGroupAction(ModifyGroupType::RemoveGroup, groupId);
|
||||
GameActions::Execute(&networkAction);
|
||||
}
|
||||
# endif
|
||||
}
|
||||
|
||||
void ScNetwork::kickPlayer(int32_t index)
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numPlayers = network_get_num_players();
|
||||
if (index < numPlayers)
|
||||
{
|
||||
auto playerId = network_get_player_id(index);
|
||||
auto kickPlayerAction = PlayerKickAction(playerId);
|
||||
GameActions::Execute(&kickPlayerAction);
|
||||
}
|
||||
# endif
|
||||
}
|
||||
|
||||
void ScNetwork::sendMessage(std::string message, DukValue players)
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
if (players.is_array())
|
||||
{
|
||||
if (network_get_mode() == NETWORK_MODE_SERVER)
|
||||
{
|
||||
std::vector<uint8_t> playerIds;
|
||||
auto playerArray = players.as_array();
|
||||
for (const auto& item : playerArray)
|
||||
{
|
||||
if (item.type() == DukValue::Type::NUMBER)
|
||||
{
|
||||
playerIds.push_back(static_cast<uint8_t>(item.as_int()));
|
||||
}
|
||||
}
|
||||
if (!playerArray.empty())
|
||||
{
|
||||
network_send_chat(message.c_str(), playerIds);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
duk_error(players.context(), DUK_ERR_ERROR, "Only servers can send private messages.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
network_send_chat(message.c_str());
|
||||
}
|
||||
# endif
|
||||
}
|
||||
|
||||
# ifndef DISABLE_NETWORK
|
||||
std::shared_ptr<ScListener> ScNetwork::createListener()
|
||||
{
|
||||
auto& scriptEngine = GetContext()->GetScriptEngine();
|
||||
auto plugin = scriptEngine.GetExecInfo().GetCurrentPlugin();
|
||||
auto socket = std::make_shared<ScListener>(plugin);
|
||||
scriptEngine.AddSocket(socket);
|
||||
return socket;
|
||||
}
|
||||
# else
|
||||
void ScNetwork::createListener()
|
||||
{
|
||||
duk_error(_context, DUK_ERR_ERROR, "Networking has been disabled.");
|
||||
}
|
||||
# endif
|
||||
|
||||
# ifndef DISABLE_NETWORK
|
||||
std::shared_ptr<ScSocket> ScNetwork::createSocket()
|
||||
{
|
||||
auto& scriptEngine = GetContext()->GetScriptEngine();
|
||||
auto plugin = scriptEngine.GetExecInfo().GetCurrentPlugin();
|
||||
auto socket = std::make_shared<ScSocket>(plugin);
|
||||
scriptEngine.AddSocket(socket);
|
||||
return socket;
|
||||
}
|
||||
# else
|
||||
void ScNetwork::createSocket()
|
||||
{
|
||||
duk_error(_context, DUK_ERR_ERROR, "Networking has been disabled.");
|
||||
}
|
||||
# endif
|
||||
|
||||
void ScNetwork::Register(duk_context* ctx)
|
||||
{
|
||||
dukglue_register_property(ctx, &ScNetwork::mode_get, nullptr, "mode");
|
||||
dukglue_register_property(ctx, &ScNetwork::numGroups_get, nullptr, "numGroups");
|
||||
dukglue_register_property(ctx, &ScNetwork::numPlayers_get, nullptr, "numPlayers");
|
||||
dukglue_register_property(ctx, &ScNetwork::groups_get, nullptr, "groups");
|
||||
dukglue_register_property(ctx, &ScNetwork::players_get, nullptr, "players");
|
||||
dukglue_register_property(ctx, &ScNetwork::currentPlayer_get, nullptr, "currentPlayer");
|
||||
dukglue_register_property(ctx, &ScNetwork::defaultGroup_get, &ScNetwork::defaultGroup_set, "defaultGroup");
|
||||
dukglue_register_property(ctx, &ScNetwork::stats_get, nullptr, "stats");
|
||||
dukglue_register_method(ctx, &ScNetwork::addGroup, "addGroup");
|
||||
dukglue_register_method(ctx, &ScNetwork::getGroup, "getGroup");
|
||||
dukglue_register_method(ctx, &ScNetwork::removeGroup, "removeGroup");
|
||||
dukglue_register_method(ctx, &ScNetwork::getPlayer, "getPlayer");
|
||||
dukglue_register_method(ctx, &ScNetwork::kickPlayer, "kickPlayer");
|
||||
dukglue_register_method(ctx, &ScNetwork::sendMessage, "sendMessage");
|
||||
|
||||
dukglue_register_method(ctx, &ScNetwork::createListener, "createListener");
|
||||
dukglue_register_method(ctx, &ScNetwork::createSocket, "createSocket");
|
||||
}
|
||||
|
||||
} // namespace OpenRCT2::Scripting
|
||||
|
||||
#endif
|
|
@ -11,124 +11,13 @@
|
|||
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
|
||||
# include "../../../Context.h"
|
||||
# include "../../../actions/NetworkModifyGroupAction.h"
|
||||
# include "../../../actions/PlayerKickAction.h"
|
||||
# include "../../../actions/PlayerSetGroupAction.h"
|
||||
# include "../../../network/NetworkAction.h"
|
||||
# include "../../../network/network.h"
|
||||
# include "../../Duktape.hpp"
|
||||
# include "../network/ScSocket.hpp"
|
||||
# include "ScPlayer.hpp"
|
||||
# include "ScPlayerGroup.hpp"
|
||||
# include "ScSocket.hpp"
|
||||
|
||||
namespace OpenRCT2::Scripting
|
||||
{
|
||||
class ScPlayer
|
||||
{
|
||||
private:
|
||||
int32_t _id;
|
||||
|
||||
public:
|
||||
ScPlayer(int32_t id)
|
||||
: _id(id)
|
||||
{
|
||||
}
|
||||
|
||||
int32_t id_get() const
|
||||
{
|
||||
return _id;
|
||||
}
|
||||
|
||||
std::string name_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto index = network_get_player_index(_id);
|
||||
if (index == -1)
|
||||
return {};
|
||||
return network_get_player_name(index);
|
||||
# else
|
||||
return {};
|
||||
# endif
|
||||
}
|
||||
|
||||
int32_t group_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto index = network_get_player_index(_id);
|
||||
if (index == -1)
|
||||
return {};
|
||||
return network_get_player_group(index);
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
void group_set(int32_t value)
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto playerSetGroupAction = PlayerSetGroupAction(_id, value);
|
||||
GameActions::Execute(&playerSetGroupAction);
|
||||
# endif
|
||||
}
|
||||
|
||||
int32_t ping_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto index = network_get_player_index(_id);
|
||||
if (index == -1)
|
||||
return {};
|
||||
return network_get_player_ping(index);
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
|
||||
int32_t commandsRan_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto index = network_get_player_index(_id);
|
||||
if (index == -1)
|
||||
return {};
|
||||
return network_get_player_commands_ran(index);
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
|
||||
int32_t moneySpent_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto index = network_get_player_index(_id);
|
||||
if (index == -1)
|
||||
return {};
|
||||
return network_get_player_money_spent(index);
|
||||
# else
|
||||
return 0;
|
||||
# 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");
|
||||
dukglue_register_property(ctx, &ScPlayer::name_get, nullptr, "name");
|
||||
dukglue_register_property(ctx, &ScPlayer::group_get, &ScPlayer::group_set, "group");
|
||||
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");
|
||||
}
|
||||
};
|
||||
|
||||
class ScNetwork
|
||||
{
|
||||
private:
|
||||
|
@ -138,272 +27,47 @@ namespace OpenRCT2::Scripting
|
|||
duk_context* _context;
|
||||
|
||||
public:
|
||||
ScNetwork(duk_context* ctx)
|
||||
: _context(ctx)
|
||||
{
|
||||
}
|
||||
ScNetwork(duk_context* ctx);
|
||||
|
||||
std::string mode_get() const;
|
||||
int32_t numPlayers_get() const;
|
||||
int32_t numGroups_get() const;
|
||||
int32_t defaultGroup_get() const;
|
||||
void defaultGroup_set(int32_t value);
|
||||
|
||||
std::vector<std::shared_ptr<ScPlayerGroup>> groups_get() const;
|
||||
|
||||
std::vector<std::shared_ptr<ScPlayer>> players_get() const;
|
||||
|
||||
std::shared_ptr<ScPlayer> currentPlayer_get() const;
|
||||
|
||||
std::shared_ptr<ScPlayer> getPlayer(int32_t index) const;
|
||||
|
||||
DukValue stats_get() const;
|
||||
|
||||
std::shared_ptr<ScPlayerGroup> getGroup(int32_t index) const;
|
||||
|
||||
void addGroup();
|
||||
|
||||
void removeGroup(int32_t index);
|
||||
|
||||
void kickPlayer(int32_t index);
|
||||
|
||||
void sendMessage(std::string message, DukValue players);
|
||||
|
||||
std::string mode_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
switch (network_get_mode())
|
||||
{
|
||||
case NETWORK_MODE_SERVER:
|
||||
return "server";
|
||||
case NETWORK_MODE_CLIENT:
|
||||
return "client";
|
||||
}
|
||||
# endif
|
||||
return "none";
|
||||
}
|
||||
int32_t numPlayers_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
return network_get_num_players();
|
||||
std::shared_ptr<ScListener> createListener();
|
||||
# else
|
||||
return 0;
|
||||
void createListener();
|
||||
# endif
|
||||
}
|
||||
int32_t numGroups_get() const
|
||||
{
|
||||
|
||||
# ifndef DISABLE_NETWORK
|
||||
return network_get_num_groups();
|
||||
std::shared_ptr<ScSocket> createSocket();
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
int32_t defaultGroup_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
return network_get_default_group();
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
void defaultGroup_set(int32_t value)
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto action = NetworkModifyGroupAction(ModifyGroupType::SetDefault, value);
|
||||
GameActions::Execute(&action);
|
||||
# endif
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<ScPlayerGroup>> groups_get() const
|
||||
{
|
||||
std::vector<std::shared_ptr<ScPlayerGroup>> groups;
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numGroups = network_get_num_groups();
|
||||
for (int32_t i = 0; i < numGroups; i++)
|
||||
{
|
||||
auto groupId = network_get_group_id(i);
|
||||
groups.push_back(std::make_shared<ScPlayerGroup>(groupId));
|
||||
}
|
||||
# endif
|
||||
return groups;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<ScPlayer>> players_get() const
|
||||
{
|
||||
std::vector<std::shared_ptr<ScPlayer>> players;
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numPlayers = network_get_num_players();
|
||||
for (int32_t i = 0; i < numPlayers; i++)
|
||||
{
|
||||
auto playerId = network_get_player_id(i);
|
||||
players.push_back(std::make_shared<ScPlayer>(playerId));
|
||||
}
|
||||
# endif
|
||||
return players;
|
||||
}
|
||||
|
||||
std::shared_ptr<ScPlayer> currentPlayer_get() const
|
||||
{
|
||||
std::shared_ptr<ScPlayer> player;
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto playerId = network_get_current_player_id();
|
||||
player = std::make_shared<ScPlayer>(playerId);
|
||||
# endif
|
||||
return player;
|
||||
}
|
||||
|
||||
std::shared_ptr<ScPlayer> getPlayer(int32_t index) const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numPlayers = network_get_num_players();
|
||||
if (index < numPlayers)
|
||||
{
|
||||
auto playerId = network_get_player_id(index);
|
||||
return std::make_shared<ScPlayer>(playerId);
|
||||
}
|
||||
# endif
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
DukValue stats_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto obj = OpenRCT2::Scripting::DukObject(_context);
|
||||
auto networkStats = network_get_stats();
|
||||
{
|
||||
duk_push_array(_context);
|
||||
duk_uarridx_t index = 0;
|
||||
for (auto v : networkStats.bytesReceived)
|
||||
{
|
||||
duk_push_number(_context, v);
|
||||
duk_put_prop_index(_context, -2, index);
|
||||
index++;
|
||||
}
|
||||
obj.Set("bytesReceived", DukValue::take_from_stack(_context));
|
||||
}
|
||||
{
|
||||
duk_push_array(_context);
|
||||
duk_uarridx_t index = 0;
|
||||
for (auto v : networkStats.bytesSent)
|
||||
{
|
||||
duk_push_number(_context, v);
|
||||
duk_put_prop_index(_context, -2, index);
|
||||
index++;
|
||||
}
|
||||
obj.Set("bytesSent", DukValue::take_from_stack(_context));
|
||||
}
|
||||
return obj.Take();
|
||||
# else
|
||||
return ToDuk(_context, nullptr);
|
||||
# endif
|
||||
}
|
||||
|
||||
std::shared_ptr<ScPlayerGroup> getGroup(int32_t index) const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numGroups = network_get_num_groups();
|
||||
if (index < numGroups)
|
||||
{
|
||||
auto groupId = network_get_group_id(index);
|
||||
return std::make_shared<ScPlayerGroup>(groupId);
|
||||
}
|
||||
# endif
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void addGroup()
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto networkModifyGroup = NetworkModifyGroupAction(ModifyGroupType::AddGroup);
|
||||
GameActions::Execute(&networkModifyGroup);
|
||||
# endif
|
||||
}
|
||||
|
||||
void removeGroup(int32_t index)
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numGroups = network_get_num_groups();
|
||||
if (index < numGroups)
|
||||
{
|
||||
auto groupId = network_get_group_id(index);
|
||||
auto networkAction = NetworkModifyGroupAction(ModifyGroupType::RemoveGroup, groupId);
|
||||
GameActions::Execute(&networkAction);
|
||||
}
|
||||
# endif
|
||||
}
|
||||
|
||||
void kickPlayer(int32_t index)
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto numPlayers = network_get_num_players();
|
||||
if (index < numPlayers)
|
||||
{
|
||||
auto playerId = network_get_player_id(index);
|
||||
auto kickPlayerAction = PlayerKickAction(playerId);
|
||||
GameActions::Execute(&kickPlayerAction);
|
||||
}
|
||||
# endif
|
||||
}
|
||||
|
||||
void sendMessage(std::string message, DukValue players)
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
if (players.is_array())
|
||||
{
|
||||
if (network_get_mode() == NETWORK_MODE_SERVER)
|
||||
{
|
||||
std::vector<uint8_t> playerIds;
|
||||
auto playerArray = players.as_array();
|
||||
for (const auto& item : playerArray)
|
||||
{
|
||||
if (item.type() == DukValue::Type::NUMBER)
|
||||
{
|
||||
playerIds.push_back(static_cast<uint8_t>(item.as_int()));
|
||||
}
|
||||
}
|
||||
if (!playerArray.empty())
|
||||
{
|
||||
network_send_chat(message.c_str(), playerIds);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
duk_error(players.context(), DUK_ERR_ERROR, "Only servers can send private messages.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
network_send_chat(message.c_str());
|
||||
}
|
||||
# endif
|
||||
}
|
||||
|
||||
# ifndef DISABLE_NETWORK
|
||||
std::shared_ptr<ScListener> createListener()
|
||||
{
|
||||
auto& scriptEngine = GetContext()->GetScriptEngine();
|
||||
auto plugin = scriptEngine.GetExecInfo().GetCurrentPlugin();
|
||||
auto socket = std::make_shared<ScListener>(plugin);
|
||||
scriptEngine.AddSocket(socket);
|
||||
return socket;
|
||||
}
|
||||
# else
|
||||
void createListener()
|
||||
{
|
||||
duk_error(_context, DUK_ERR_ERROR, "Networking has been disabled.");
|
||||
}
|
||||
void createSocket();
|
||||
# endif
|
||||
|
||||
# ifndef DISABLE_NETWORK
|
||||
std::shared_ptr<ScSocket> createSocket()
|
||||
{
|
||||
auto& scriptEngine = GetContext()->GetScriptEngine();
|
||||
auto plugin = scriptEngine.GetExecInfo().GetCurrentPlugin();
|
||||
auto socket = std::make_shared<ScSocket>(plugin);
|
||||
scriptEngine.AddSocket(socket);
|
||||
return socket;
|
||||
}
|
||||
# else
|
||||
void createSocket()
|
||||
{
|
||||
duk_error(_context, DUK_ERR_ERROR, "Networking has been disabled.");
|
||||
}
|
||||
# endif
|
||||
|
||||
static void Register(duk_context* ctx)
|
||||
{
|
||||
dukglue_register_property(ctx, &ScNetwork::mode_get, nullptr, "mode");
|
||||
dukglue_register_property(ctx, &ScNetwork::numGroups_get, nullptr, "numGroups");
|
||||
dukglue_register_property(ctx, &ScNetwork::numPlayers_get, nullptr, "numPlayers");
|
||||
dukglue_register_property(ctx, &ScNetwork::groups_get, nullptr, "groups");
|
||||
dukglue_register_property(ctx, &ScNetwork::players_get, nullptr, "players");
|
||||
dukglue_register_property(ctx, &ScNetwork::currentPlayer_get, nullptr, "currentPlayer");
|
||||
dukglue_register_property(ctx, &ScNetwork::defaultGroup_get, &ScNetwork::defaultGroup_set, "defaultGroup");
|
||||
dukglue_register_property(ctx, &ScNetwork::stats_get, nullptr, "stats");
|
||||
dukglue_register_method(ctx, &ScNetwork::addGroup, "addGroup");
|
||||
dukglue_register_method(ctx, &ScNetwork::getGroup, "getGroup");
|
||||
dukglue_register_method(ctx, &ScNetwork::removeGroup, "removeGroup");
|
||||
dukglue_register_method(ctx, &ScNetwork::getPlayer, "getPlayer");
|
||||
dukglue_register_method(ctx, &ScNetwork::kickPlayer, "kickPlayer");
|
||||
dukglue_register_method(ctx, &ScNetwork::sendMessage, "sendMessage");
|
||||
|
||||
dukglue_register_method(ctx, &ScNetwork::createListener, "createListener");
|
||||
dukglue_register_method(ctx, &ScNetwork::createSocket, "createSocket");
|
||||
}
|
||||
static void Register(duk_context* ctx);
|
||||
};
|
||||
} // namespace OpenRCT2::Scripting
|
||||
|
||||
|
|
|
@ -0,0 +1,124 @@
|
|||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2021 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
|
||||
# include "ScPlayer.hpp"
|
||||
|
||||
# include "../../../Context.h"
|
||||
# include "../../../actions/PlayerSetGroupAction.h"
|
||||
# include "../../../network/NetworkAction.h"
|
||||
# include "../../../network/network.h"
|
||||
|
||||
namespace OpenRCT2::Scripting
|
||||
{
|
||||
ScPlayer::ScPlayer(int32_t id)
|
||||
: _id(id)
|
||||
{
|
||||
}
|
||||
|
||||
int32_t ScPlayer::id_get() const
|
||||
{
|
||||
return _id;
|
||||
}
|
||||
|
||||
std::string ScPlayer::name_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto index = network_get_player_index(_id);
|
||||
if (index == -1)
|
||||
return {};
|
||||
return network_get_player_name(index);
|
||||
# else
|
||||
return {};
|
||||
# endif
|
||||
}
|
||||
|
||||
int32_t ScPlayer::group_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto index = network_get_player_index(_id);
|
||||
if (index == -1)
|
||||
return {};
|
||||
return network_get_player_group(index);
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
void ScPlayer::group_set(int32_t value)
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto playerSetGroupAction = PlayerSetGroupAction(_id, value);
|
||||
GameActions::Execute(&playerSetGroupAction);
|
||||
# endif
|
||||
}
|
||||
|
||||
int32_t ScPlayer::ping_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto index = network_get_player_index(_id);
|
||||
if (index == -1)
|
||||
return {};
|
||||
return network_get_player_ping(index);
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
|
||||
int32_t ScPlayer::commandsRan_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto index = network_get_player_index(_id);
|
||||
if (index == -1)
|
||||
return {};
|
||||
return network_get_player_commands_ran(index);
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
|
||||
int32_t ScPlayer::moneySpent_get() const
|
||||
{
|
||||
# ifndef DISABLE_NETWORK
|
||||
auto index = network_get_player_index(_id);
|
||||
if (index == -1)
|
||||
return {};
|
||||
return network_get_player_money_spent(index);
|
||||
# else
|
||||
return 0;
|
||||
# endif
|
||||
}
|
||||
|
||||
std::string ScPlayer::ipAddress_get() const
|
||||
{
|
||||
return network_get_player_ip_address(_id);
|
||||
}
|
||||
|
||||
std::string ScPlayer::publicKeyHash_get() const
|
||||
{
|
||||
return network_get_player_public_key_hash(_id);
|
||||
}
|
||||
|
||||
void ScPlayer::Register(duk_context* ctx)
|
||||
{
|
||||
dukglue_register_property(ctx, &ScPlayer::id_get, nullptr, "id");
|
||||
dukglue_register_property(ctx, &ScPlayer::name_get, nullptr, "name");
|
||||
dukglue_register_property(ctx, &ScPlayer::group_get, &ScPlayer::group_set, "group");
|
||||
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");
|
||||
}
|
||||
|
||||
} // namespace OpenRCT2::Scripting
|
||||
|
||||
#endif
|
|
@ -0,0 +1,50 @@
|
|||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2021 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
|
||||
# include "../../Duktape.hpp"
|
||||
|
||||
# include <string.h>
|
||||
|
||||
namespace OpenRCT2::Scripting
|
||||
{
|
||||
class ScPlayer
|
||||
{
|
||||
private:
|
||||
int32_t _id;
|
||||
|
||||
public:
|
||||
ScPlayer(int32_t id);
|
||||
|
||||
int32_t id_get() const;
|
||||
|
||||
std::string name_get() const;
|
||||
|
||||
int32_t group_get() const;
|
||||
void group_set(int32_t value);
|
||||
|
||||
int32_t ping_get() const;
|
||||
|
||||
int32_t commandsRan_get() const;
|
||||
|
||||
int32_t moneySpent_get() const;
|
||||
|
||||
std::string ipAddress_get() const;
|
||||
|
||||
std::string publicKeyHash_get() const;
|
||||
|
||||
static void Register(duk_context* ctx);
|
||||
};
|
||||
|
||||
} // namespace OpenRCT2::Scripting
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue