Added permission presets (or ranks). Current presets are none (no special permissions), moderator (round management & kicking) and admin (almost everything permitted).

This commit is contained in:
Joonas Rikkonen
2017-12-19 22:27:07 +02:00
parent 0a8c79c1cb
commit 0204bc2c49
7 changed files with 203 additions and 41 deletions

View File

@@ -857,24 +857,54 @@ namespace Barotrauma
playerFrame = new GUIFrame(new Rectangle(0, 0, 0, 0), Color.Black * 0.6f);
var playerFrameInner = new GUIFrame(new Rectangle(0, 0, 300, 370), null, Alignment.Center, "", playerFrame);
var playerFrameInner = new GUIFrame(GameMain.Server != null ? new Rectangle(0, 0, 450, 370) : new Rectangle(0, 0, 450, 150), null, Alignment.Center, "", playerFrame);
playerFrameInner.Padding = new Vector4(20.0f, 20.0f, 20.0f, 20.0f);
new GUITextBlock(new Rectangle(0, 0, 200, 20), component.UserData.ToString(),
new GUITextBlock(new Rectangle(0, 0, 200, 20), obj.ToString(),
"", Alignment.TopLeft, Alignment.TopLeft,
playerFrameInner, false, GUI.LargeFont);
if (GameMain.Server != null)
{
var selectedClient = GameMain.Server.ConnectedClients.Find(c => c.Name == component.UserData.ToString());
var selectedClient = GameMain.Server.ConnectedClients.Find(c => c.Name == obj.ToString());
playerFrame.UserData = selectedClient;
new GUITextBlock(new Rectangle(0, 25, 150, 15), selectedClient.Connection.RemoteEndPoint.Address.ToString(), "", playerFrameInner);
var permissionsBox = new GUIFrame(new Rectangle(0, 40, 0, 110), null, playerFrameInner);
new GUITextBlock(new Rectangle(0, 45, 0, 15), "Rank", "", playerFrameInner);
var rankDropDown = new GUIDropDown(new Rectangle(0, 70, 150, 20), "Rank", "", playerFrameInner);
rankDropDown.UserData = selectedClient;
foreach (PermissionPreset permissionPreset in PermissionPreset.List)
{
rankDropDown.AddItem(permissionPreset.Name, permissionPreset, permissionPreset.Description);
}
rankDropDown.AddItem("Custom", null);
PermissionPreset currentPreset = PermissionPreset.List.Find(p =>
p.Permissions == selectedClient.Permissions &&
p.PermittedCommands.Count == selectedClient.PermittedConsoleCommands.Count && !p.PermittedCommands.Except(selectedClient.PermittedConsoleCommands).Any());
rankDropDown.SelectItem(currentPreset);
rankDropDown.OnSelected += (c, userdata) =>
{
PermissionPreset selectedPreset = (PermissionPreset)userdata;
if (selectedPreset != null)
{
var client = playerFrame.UserData as Client;
client.SetPermissions(selectedPreset.Permissions, selectedPreset.PermittedCommands);
GameMain.Server.UpdateClientPermissions(client);
playerFrame = null;
SelectPlayer(null, client.Name);
}
return true;
};
var permissionsBox = new GUIFrame(new Rectangle(0, 125, (int)(playerFrameInner.Rect.Width * 0.5f), 160), null, playerFrameInner);
permissionsBox.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f);
permissionsBox.UserData = selectedClient;
new GUITextBlock(new Rectangle(0, 0, 0, 15), "Permissions:", "", permissionsBox);
new GUITextBlock(new Rectangle(0, 100, permissionsBox.Rect.Width, 15), "Permissions:", "", playerFrameInner);
int x = 0, y = 0;
foreach (ClientPermissions permission in Enum.GetValues(typeof(ClientPermissions)))
{
@@ -885,13 +915,16 @@ namespace Barotrauma
string permissionStr = attributes.Length > 0 ? attributes[0].Description : permission.ToString();
var permissionTick = new GUITickBox(new Rectangle(x, y + 25, 15, 15), permissionStr, Alignment.TopLeft, GUI.SmallFont, permissionsBox);
var permissionTick = new GUITickBox(new Rectangle(x, y, 15, 15), permissionStr, Alignment.TopLeft, GUI.SmallFont, permissionsBox);
permissionTick.UserData = permission;
permissionTick.Selected = selectedClient.HasPermission(permission);
permissionTick.OnSelected = (tickBox) =>
{
var client = tickBox.Parent.UserData as Client;
//reset rank to custom
rankDropDown.SelectItem(null);
var client = playerFrame.UserData as Client;
if (client == null) return false;
var thisPermission = (ClientPermissions)tickBox.UserData;
@@ -906,28 +939,29 @@ namespace Barotrauma
return true;
};
y += 20;
if (y >= permissionsBox.Rect.Height - 40)
if (y >= permissionsBox.Rect.Height - 15)
{
y = 0;
x += 120;
}
}
new GUITextBlock(new Rectangle(0, 145, 0, 15), "Permitted console commands:", "", playerFrameInner);
var commandList = new GUIListBox(new Rectangle(0,170,0, 80), "", playerFrameInner);
new GUITextBlock(new Rectangle(0, 100, (int)(playerFrameInner.Rect.Width * 0.5f), 15), "Permitted console commands:", "", Alignment.TopRight, Alignment.TopLeft, playerFrameInner, true);
var commandList = new GUIListBox(new Rectangle(0, 125, (int)(playerFrameInner.Rect.Width * 0.5f), 160), "", Alignment.TopRight, playerFrameInner);
commandList.UserData = selectedClient;
foreach (DebugConsole.Command command in DebugConsole.Commands)
{
var commandTickBox = new GUITickBox(new Rectangle(0,0,15,15), command.names[0], Alignment.TopLeft, GUI.SmallFont, commandList);
var commandTickBox = new GUITickBox(new Rectangle(0, 0, 15, 15), command.names[0], Alignment.TopLeft, GUI.SmallFont, commandList);
commandTickBox.Selected = selectedClient.PermittedConsoleCommands.Contains(command);
commandTickBox.ToolTip = command.help;
commandTickBox.UserData = command;
commandTickBox.OnSelected += (GUITickBox tickBox) =>
{
Client client = tickBox.Parent.UserData as Client;
//reset rank to custom
rankDropDown.SelectItem(null);
Client client = playerFrame.UserData as Client;
DebugConsole.Command selectedCommand = tickBox.UserData as DebugConsole.Command;
if (client == null) return false;
@@ -948,7 +982,7 @@ namespace Barotrauma
if (GameMain.Server != null || GameMain.Client.HasPermission(ClientPermissions.Kick))
{
var kickButton = new GUIButton(new Rectangle(0, -50, 100, 20), "Kick", Alignment.BottomLeft, "", playerFrameInner);
var kickButton = new GUIButton(new Rectangle(0, 0, 80, 20), "Kick", Alignment.BottomLeft, "", playerFrameInner);
kickButton.UserData = obj;
kickButton.OnClicked += KickPlayer;
kickButton.OnClicked += ClosePlayerFrame;
@@ -956,12 +990,12 @@ namespace Barotrauma
if (GameMain.Server != null || GameMain.Client.HasPermission(ClientPermissions.Ban))
{
var banButton = new GUIButton(new Rectangle(0, 0, 100, 20), "Ban", Alignment.BottomLeft, "", playerFrameInner);
var banButton = new GUIButton(new Rectangle(90, 0, 80, 20), "Ban", Alignment.BottomLeft, "", playerFrameInner);
banButton.UserData = obj;
banButton.OnClicked += BanPlayer;
banButton.OnClicked += ClosePlayerFrame;
var rangebanButton = new GUIButton(new Rectangle(0, -25, 100, 20), "Ban range", Alignment.BottomLeft, "", playerFrameInner);
var rangebanButton = new GUIButton(new Rectangle(180, 0, 80, 20), "Ban range", Alignment.BottomLeft, "", playerFrameInner);
rangebanButton.UserData = obj;
rangebanButton.OnClicked += BanPlayerRange;
rangebanButton.OnClicked += ClosePlayerFrame;

View File

@@ -739,6 +739,9 @@
<Content Include="$(MSBuildThisFileDirectory)Data\ContentPackages\Vanilla 0.7.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="$(MSBuildThisFileDirectory)Data\permissionpresets.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="$(MSBuildThisFileDirectory)Icon.ico" />
<Content Include="$(MSBuildThisFileDirectory)Mods\info.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@@ -1479,6 +1482,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Source\Networking\BanList.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Networking\ChatMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Networking\Client.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Networking\ClientPermissions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Networking\EntitySpawner.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Networking\FileTransfer\FileSender.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Source\Networking\GameServer.cs" />

View File

@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8" ?>
<PermissionPresets>
<Preset
name="None"
description="No special privileges."
permissions="None"/>
<Preset
name="Moderator"
description="Allowed to manage round settings and kick players."
permissions="EndRound,Kick,SelectSub,SelectMode,ManageCampaign,ConsoleCommands">
<Command name="clientlist"/>
<Command name="autorestart"/>
<Command name="autorestartinterval"/>
<Command name="autorestarttimer"/>
<Command name="kick"/>
<Command name="kickid"/>
<Command name="campaigninfo"/>
<Command name="campaigndestination"/>
</Preset>
<Preset
name="Admin"
description="Allowed to ban and kick players, manage round settings and use nearly all console commands."
permissions="EndRound,Kick,Ban,SelectSub,SelectMode,ManageCampaign,ConsoleCommands">
<Command name="clientlist"/>
<Command name="autorestart"/>
<Command name="autorestartinterval"/>
<Command name="autorestarttimer"/>
<Command name="kick"/>
<Command name="kickid"/>
<Command name="campaigninfo"/>
<Command name="campaigndestination"/>
<Command name="spawn"/>
<Command name="spawnitem"/>
<Command name="disablecrewai"/>
<Command name="enablecrewai"/>
<Command name="ban"/>
<Command name="banid"/>
<Command name="banip"/>
<Command name="teleportcharacter"/>
<Command name="godmode"/>
<Command name="lockx"/>
<Command name="locky"/>
<Command name="heal"/>
<Command name="revive"/>
<Command name="freeze"/>
<Command name="freecam"/>
<Command name="explosion"/>
<Command name="fixitems"/>
<Command name="fixhulls"/>
<Command name="power"/>
<Command name="oxygen"/>
<Command name="setclientcharacter"/>
</Preset>
</PermissionPresets>

View File

@@ -1,31 +1,10 @@
using Lidgren.Network;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace Barotrauma.Networking
{
[Flags]
enum ClientPermissions
{
None = 0,
[Description("End round")]
EndRound = 1,
[Description("Kick")]
Kick = 2,
[Description("Ban")]
Ban = 4,
[Description("Select submarine")]
SelectSub = 8,
[Description("Select game mode")]
SelectMode = 16,
[Description("Manage campaign")]
ManageCampaign = 32,
[Description("Console commands")]
ConsoleCommands = 64
}
class Client
{
public string Name;
@@ -81,7 +60,11 @@ namespace Barotrauma.Networking
public float DeleteDisconnectedTimer;
public ClientPermissions Permissions = ClientPermissions.None;
public List<DebugConsole.Command> PermittedConsoleCommands = new List<DebugConsole.Command>();
public List<DebugConsole.Command> PermittedConsoleCommands
{
get;
private set;
}
public bool SpectateOnly;
@@ -116,6 +99,7 @@ namespace Barotrauma.Networking
this.Name = name;
this.ID = ID;
PermittedConsoleCommands = new List<DebugConsole.Command>();
kickVoters = new List<Client>();
votes = new object[Enum.GetNames(typeof(VoteType)).Length];
@@ -160,7 +144,7 @@ namespace Barotrauma.Networking
public void SetPermissions(ClientPermissions permissions, List<DebugConsole.Command> permittedConsoleCommands)
{
this.Permissions = permissions;
this.PermittedConsoleCommands = permittedConsoleCommands;
this.PermittedConsoleCommands = new List<DebugConsole.Command>(permittedConsoleCommands);
}
public void GivePermission(ClientPermissions permission)

View File

@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Xml.Linq;
namespace Barotrauma.Networking
{
[Flags]
enum ClientPermissions
{
None = 0,
[Description("End round")]
EndRound = 1,
[Description("Kick")]
Kick = 2,
[Description("Ban")]
Ban = 4,
[Description("Select submarine")]
SelectSub = 8,
[Description("Select game mode")]
SelectMode = 16,
[Description("Manage campaign")]
ManageCampaign = 32,
[Description("Console commands")]
ConsoleCommands = 64
}
class PermissionPreset
{
public static List<PermissionPreset> List = new List<PermissionPreset>();
public readonly string Name;
public readonly string Description;
public readonly ClientPermissions Permissions;
public readonly List<DebugConsole.Command> PermittedCommands;
public PermissionPreset(XElement element)
{
Name = element.GetAttributeString("name", "");
Description = element.GetAttributeString("description", "");
string permissionsStr = element.GetAttributeString("permissions", "");
if (!Enum.TryParse(permissionsStr, out Permissions))
{
DebugConsole.ThrowError("Error in permission preset \"" + Name + "\" - " + permissionsStr + " is not a valid permission!");
}
PermittedCommands = new List<DebugConsole.Command>();
if (Permissions.HasFlag(ClientPermissions.ConsoleCommands))
{
foreach (XElement subElement in element.Elements())
{
if (subElement.Name.ToString().ToLowerInvariant() != "command") continue;
string commandName = subElement.GetAttributeString("name", "");
DebugConsole.Command command = DebugConsole.FindCommand(commandName);
if (command == null)
{
DebugConsole.ThrowError("Error in permission preset \"" + Name + "\" - " + commandName + "\" is not a valid console command.");
continue;
}
PermittedCommands.Add(command);
}
}
}
public static void LoadAll(string file)
{
if (!File.Exists(file)) return;
XDocument doc = XMLExtensions.TryLoadXml(file);
if (doc == null || doc.Root == null) return;
foreach (XElement element in doc.Root.Elements())
{
List.Add(new PermissionPreset(element));
}
}
}
}

View File

@@ -125,8 +125,9 @@ namespace Barotrauma.Networking
banList = new BanList();
LoadSettings();
PermissionPreset.LoadAll(PermissionPresetFile);
LoadClientPermissions();
CoroutineManager.StartCoroutine(StartServer(isPublic));
}

View File

@@ -40,6 +40,7 @@ namespace Barotrauma.Networking
}
public const string SettingsFile = "serversettings.xml";
public static readonly string PermissionPresetFile = "Data" + Path.DirectorySeparatorChar + "permissionpresets.xml";
public static readonly string ClientPermissionsFile = "Data" + Path.DirectorySeparatorChar + "clientpermissions.xml";
public Dictionary<string, SerializableProperty> SerializableProperties