Files
OpenRCT2-Web/src/openrct2/network/NetworkGroup.cpp
T

131 lines
3.4 KiB
C++
Raw Normal View History

2016-05-28 18:29:56 +01:00
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
2016-05-28 18:29:56 +01:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2016-05-28 18:29:56 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-05-28 18:29:56 +01:00
*****************************************************************************/
2016-05-28 22:27:08 +01:00
#ifndef DISABLE_NETWORK
2018-07-21 16:17:06 +02:00
# include "NetworkGroup.h"
2016-05-28 18:29:56 +01:00
# include "../openrct2/core/Json.hpp"
2018-07-21 16:17:06 +02:00
# include "NetworkAction.h"
# include "NetworkTypes.h"
2018-06-22 23:02:47 +02:00
2022-02-10 05:57:25 +09:00
NetworkGroup NetworkGroup::FromJson(const json_t& jsonData)
2016-05-28 18:29:56 +01:00
{
Guard::Assert(jsonData.is_object(), "NetworkGroup::FromJson expects parameter jsonData to be object");
2017-12-06 00:12:36 +01:00
NetworkGroup group;
json_t jsonId = jsonData["id"];
json_t jsonName = jsonData["name"];
json_t jsonPermissions = jsonData["permissions"];
if (jsonId.is_null() || jsonName.is_null() || jsonPermissions.is_null())
2016-05-28 18:29:56 +01:00
{
2018-01-02 19:23:22 +00:00
throw std::runtime_error("Missing group data");
2016-05-28 18:29:56 +01:00
}
2017-12-06 00:12:36 +01:00
group.Id = Json::GetNumber<uint8_t>(jsonId);
group._name = Json::GetString(jsonName);
2017-12-06 14:38:33 +01:00
std::fill(group.ActionsAllowed.begin(), group.ActionsAllowed.end(), 0);
2017-12-06 00:12:36 +01:00
for (const auto& jsonValue : jsonPermissions)
2017-12-06 00:12:36 +01:00
{
const std::string permission = Json::GetString(jsonValue);
NetworkPermission action_id = NetworkActions::FindCommandByPermissionName(permission);
if (action_id != NetworkPermission::Count)
2018-06-22 23:02:47 +02:00
{
2016-05-28 18:29:56 +01:00
group.ToggleActionPermission(action_id);
}
}
return group;
}
json_t NetworkGroup::ToJson() const
2016-05-28 18:29:56 +01:00
{
json_t jsonGroup = {
{ "id", Id },
{ "name", GetName() },
};
json_t actionsArray = json_t::array();
2016-05-28 18:29:56 +01:00
for (size_t i = 0; i < NetworkActions::Actions.size(); i++)
{
if (CanPerformAction(static_cast<NetworkPermission>(i)))
2016-05-28 18:29:56 +01:00
{
actionsArray.emplace_back(NetworkActions::Actions[i].PermissionName);
2016-05-28 18:29:56 +01:00
}
}
jsonGroup["permissions"] = actionsArray;
2016-05-28 18:29:56 +01:00
return jsonGroup;
}
2022-02-10 05:57:25 +09:00
const std::string& NetworkGroup::GetName() const noexcept
2016-05-28 18:29:56 +01:00
{
2016-05-28 19:03:57 +01:00
return _name;
2016-05-28 18:29:56 +01:00
}
void NetworkGroup::SetName(std::string_view name)
2016-05-28 18:29:56 +01:00
{
2016-05-28 19:03:57 +01:00
_name = name;
2016-05-28 18:29:56 +01:00
}
2018-06-22 23:02:47 +02:00
void NetworkGroup::Read(NetworkPacket& packet)
2016-05-28 18:29:56 +01:00
{
2016-05-28 19:03:57 +01:00
packet >> Id;
2016-05-28 18:29:56 +01:00
SetName(packet.ReadString());
2018-06-22 23:02:47 +02:00
for (auto& action : ActionsAllowed)
2016-05-28 18:29:56 +01:00
{
2017-12-06 00:12:36 +01:00
packet >> action;
2016-05-28 18:29:56 +01:00
}
}
2022-02-10 05:57:25 +09:00
void NetworkGroup::Write(NetworkPacket& packet) const
2016-05-28 18:29:56 +01:00
{
2016-05-28 19:03:57 +01:00
packet << Id;
2016-05-28 18:29:56 +01:00
packet.WriteString(GetName().c_str());
2018-06-22 23:02:47 +02:00
for (const auto& action : ActionsAllowed)
2016-05-28 18:29:56 +01:00
{
packet << action;
2016-05-28 18:29:56 +01:00
}
}
void NetworkGroup::ToggleActionPermission(NetworkPermission index)
2016-05-28 18:29:56 +01:00
{
size_t index_st = static_cast<size_t>(index);
size_t byte = index_st / 8;
size_t bit = index_st % 8;
2016-05-28 19:03:57 +01:00
if (byte >= ActionsAllowed.size())
2016-05-28 18:29:56 +01:00
{
return;
}
2016-05-28 19:03:57 +01:00
ActionsAllowed[byte] ^= (1 << bit);
2016-05-28 18:29:56 +01:00
}
2022-02-10 05:57:25 +09:00
bool NetworkGroup::CanPerformAction(NetworkPermission index) const noexcept
2016-05-28 18:29:56 +01:00
{
size_t index_st = static_cast<size_t>(index);
size_t byte = index_st / 8;
size_t bit = index_st % 8;
2016-05-28 19:03:57 +01:00
if (byte >= ActionsAllowed.size())
2016-05-28 18:29:56 +01:00
{
return false;
}
2017-10-03 00:00:32 +02:00
return (ActionsAllowed[byte] & (1 << bit)) != 0;
2016-05-28 18:29:56 +01:00
}
bool NetworkGroup::CanPerformCommand(GameCommand command) const
2016-05-28 18:29:56 +01:00
{
NetworkPermission action = NetworkActions::FindCommand(command);
if (action != NetworkPermission::Count)
2016-05-28 18:29:56 +01:00
{
return CanPerformAction(action);
}
return false;
}
2016-05-28 22:27:08 +01:00
#endif