2021-08-09 20:55:58 +03:00
|
|
|
/*****************************************************************************
|
2024-01-01 05:52:28 -06:00
|
|
|
* Copyright (c) 2014-2024 OpenRCT2 developers
|
2021-08-09 20:55:58 +03:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_SCRIPTING
|
|
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "ScPlayerGroup.hpp"
|
2021-08-09 20:55:58 +03:00
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "../../../Context.h"
|
|
|
|
|
#include "../../../actions/NetworkModifyGroupAction.h"
|
|
|
|
|
#include "../../../actions/PlayerSetGroupAction.h"
|
|
|
|
|
#include "../../../core/String.hpp"
|
|
|
|
|
#include "../../../network/NetworkAction.h"
|
|
|
|
|
#include "../../../network/network.h"
|
|
|
|
|
#include "../../Duktape.hpp"
|
2021-08-09 20:55:58 +03:00
|
|
|
|
|
|
|
|
namespace OpenRCT2::Scripting
|
|
|
|
|
{
|
|
|
|
|
ScPlayerGroup::ScPlayerGroup(int32_t id)
|
|
|
|
|
: _id(id)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t ScPlayerGroup::id_get()
|
|
|
|
|
{
|
|
|
|
|
return _id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string ScPlayerGroup::name_get() const
|
|
|
|
|
{
|
2024-11-29 12:04:56 +09:00
|
|
|
#ifndef DISABLE_NETWORK
|
2023-01-18 07:05:53 +01:00
|
|
|
auto index = NetworkGetGroupIndex(_id);
|
2021-08-09 20:55:58 +03:00
|
|
|
if (index == -1)
|
|
|
|
|
return {};
|
2023-01-18 07:05:53 +01:00
|
|
|
return NetworkGetGroupName(index);
|
2024-11-29 12:04:56 +09:00
|
|
|
#else
|
2021-08-09 20:55:58 +03:00
|
|
|
return {};
|
2024-11-29 12:04:56 +09:00
|
|
|
#endif
|
2021-08-09 20:55:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScPlayerGroup::name_set(std::string value)
|
|
|
|
|
{
|
2024-11-29 12:04:56 +09:00
|
|
|
#ifndef DISABLE_NETWORK
|
2021-08-09 20:55:58 +03:00
|
|
|
auto action = NetworkModifyGroupAction(ModifyGroupType::SetName, _id, value);
|
|
|
|
|
GameActions::Execute(&action);
|
2024-11-29 12:04:56 +09:00
|
|
|
#endif
|
2021-08-09 20:55:58 +03:00
|
|
|
}
|
|
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#ifndef DISABLE_NETWORK
|
2021-08-09 20:55:58 +03:00
|
|
|
static std::string TransformPermissionKeyToJS(const std::string& s)
|
|
|
|
|
{
|
|
|
|
|
auto result = s.substr(sizeof("PERMISSION_") - 1);
|
|
|
|
|
for (auto& c : result)
|
|
|
|
|
{
|
2021-10-28 10:54:09 +02:00
|
|
|
c = std::tolower(static_cast<unsigned char>(c));
|
2021-08-09 20:55:58 +03:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 22:22:01 +03:00
|
|
|
static std::string TransformPermissionKeyToInternal(const std::string& s)
|
|
|
|
|
{
|
2024-12-11 22:28:31 +01:00
|
|
|
return "PERMISSION_" + String::toUpper(s);
|
2021-08-09 22:22:01 +03:00
|
|
|
}
|
2024-11-29 12:04:56 +09:00
|
|
|
#endif
|
2021-08-09 22:22:01 +03:00
|
|
|
|
2021-08-09 20:55:58 +03:00
|
|
|
std::vector<std::string> ScPlayerGroup::permissions_get() const
|
|
|
|
|
{
|
2024-11-29 12:04:56 +09:00
|
|
|
#ifndef DISABLE_NETWORK
|
2023-01-18 07:05:53 +01:00
|
|
|
auto index = NetworkGetGroupIndex(_id);
|
2021-08-09 20:55:58 +03:00
|
|
|
if (index == -1)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
// Create array of permissions
|
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
auto permissionIndex = 0;
|
|
|
|
|
for (const auto& action : NetworkActions::Actions)
|
|
|
|
|
{
|
2023-01-18 07:05:53 +01:00
|
|
|
if (NetworkCanPerformAction(index, static_cast<NetworkPermission>(permissionIndex)))
|
2021-08-09 20:55:58 +03:00
|
|
|
{
|
|
|
|
|
result.push_back(TransformPermissionKeyToJS(action.PermissionName));
|
|
|
|
|
}
|
|
|
|
|
permissionIndex++;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2024-11-29 12:04:56 +09:00
|
|
|
#else
|
2021-08-09 20:55:58 +03:00
|
|
|
return {};
|
2024-11-29 12:04:56 +09:00
|
|
|
#endif
|
2021-08-09 20:55:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScPlayerGroup::permissions_set(std::vector<std::string> value)
|
|
|
|
|
{
|
2024-11-29 12:04:56 +09:00
|
|
|
#ifndef DISABLE_NETWORK
|
2023-01-18 07:05:53 +01:00
|
|
|
auto groupIndex = NetworkGetGroupIndex(_id);
|
2021-08-09 20:55:58 +03:00
|
|
|
if (groupIndex == -1)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// First clear all permissions
|
|
|
|
|
auto networkAction = NetworkModifyGroupAction(ModifyGroupType::SetPermissions, _id, "", 0, PermissionState::ClearAll);
|
|
|
|
|
GameActions::Execute(&networkAction);
|
|
|
|
|
|
|
|
|
|
std::vector<bool> enabledPermissions;
|
|
|
|
|
enabledPermissions.resize(NetworkActions::Actions.size());
|
|
|
|
|
for (const auto& p : value)
|
|
|
|
|
{
|
|
|
|
|
auto permissionName = TransformPermissionKeyToInternal(p);
|
|
|
|
|
|
|
|
|
|
auto permissionIndex = 0;
|
|
|
|
|
for (const auto& action : NetworkActions::Actions)
|
|
|
|
|
{
|
|
|
|
|
if (action.PermissionName == permissionName)
|
|
|
|
|
{
|
|
|
|
|
enabledPermissions[permissionIndex] = true;
|
|
|
|
|
}
|
|
|
|
|
permissionIndex++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < enabledPermissions.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
auto toggle
|
2023-01-18 07:05:53 +01:00
|
|
|
= (enabledPermissions[i] != (NetworkCanPerformAction(groupIndex, static_cast<NetworkPermission>(i)) != 0));
|
2021-08-09 20:55:58 +03:00
|
|
|
if (toggle)
|
|
|
|
|
{
|
|
|
|
|
auto networkAction2 = NetworkModifyGroupAction(
|
|
|
|
|
ModifyGroupType::SetPermissions, _id, "", static_cast<uint32_t>(i), PermissionState::Toggle);
|
|
|
|
|
GameActions::Execute(&networkAction2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-29 12:04:56 +09:00
|
|
|
#endif
|
2021-08-09 20:55:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScPlayerGroup::Register(duk_context* ctx)
|
|
|
|
|
{
|
|
|
|
|
dukglue_register_property(ctx, &ScPlayerGroup::id_get, nullptr, "id");
|
|
|
|
|
dukglue_register_property(ctx, &ScPlayerGroup::name_get, &ScPlayerGroup::name_set, "name");
|
|
|
|
|
dukglue_register_property(ctx, &ScPlayerGroup::permissions_get, &ScPlayerGroup::permissions_set, "permissions");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace OpenRCT2::Scripting
|
|
|
|
|
|
|
|
|
|
#endif
|