Role ban UI updates (#16736)
This commit is contained in:
@@ -9,6 +9,10 @@ namespace Content.Server.Administration.Commands;
|
||||
[AdminCommand(AdminFlags.Ban)]
|
||||
public sealed class DepartmentBanCommand : IConsoleCommand
|
||||
{
|
||||
[Dependency] private readonly IPlayerLocator _locater = default!;
|
||||
[Dependency] private readonly IPrototypeManager _protoManager = default!;
|
||||
[Dependency] private readonly RoleBanManager _bans = default!;
|
||||
|
||||
public string Command => "departmentban";
|
||||
public string Description => Loc.GetString("cmd-departmentban-desc");
|
||||
public string Help => Loc.GetString("cmd-departmentban-help");
|
||||
@@ -46,19 +50,25 @@ public sealed class DepartmentBanCommand : IConsoleCommand
|
||||
return;
|
||||
}
|
||||
|
||||
var protoManager = IoCManager.Resolve<IPrototypeManager>();
|
||||
|
||||
if (!protoManager.TryIndex<DepartmentPrototype>(department, out var departmentProto))
|
||||
if (!_protoManager.TryIndex<DepartmentPrototype>(department, out var departmentProto))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var banManager = IoCManager.Resolve<RoleBanManager>();
|
||||
var located = await _locater.LookupIdByNameOrIdAsync(target);
|
||||
|
||||
if (located == null)
|
||||
{
|
||||
shell.WriteError(Loc.GetString("cmd-roleban-name-parse"));
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var job in departmentProto.Roles)
|
||||
{
|
||||
banManager.CreateJobBan(shell, target, job, reason, minutes);
|
||||
_bans.CreateJobBan(shell, located, job, reason, minutes);
|
||||
}
|
||||
|
||||
_bans.SendRoleBans(located);
|
||||
}
|
||||
|
||||
public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
|
||||
|
||||
@@ -12,6 +12,9 @@ namespace Content.Server.Administration.Commands;
|
||||
[AdminCommand(AdminFlags.Ban)]
|
||||
public sealed class RoleBanCommand : IConsoleCommand
|
||||
{
|
||||
[Dependency] private readonly IPlayerLocator _locator = default!;
|
||||
[Dependency] private readonly RoleBanManager _bans = default!;
|
||||
|
||||
public string Command => "roleban";
|
||||
public string Description => Loc.GetString("cmd-roleban-desc");
|
||||
public string Help => Loc.GetString("cmd-roleban-help");
|
||||
@@ -49,7 +52,16 @@ public sealed class RoleBanCommand : IConsoleCommand
|
||||
return;
|
||||
}
|
||||
|
||||
IoCManager.Resolve<RoleBanManager>().CreateJobBan(shell, target, job, reason, minutes);
|
||||
var located = await _locator.LookupIdByNameOrIdAsync(target);
|
||||
|
||||
if (located == null)
|
||||
{
|
||||
shell.WriteError(Loc.GetString("cmd-roleban-name-parse"));
|
||||
return;
|
||||
}
|
||||
|
||||
_bans.CreateJobBan(shell, located, job, reason, minutes);
|
||||
_bans.SendRoleBans(located);
|
||||
}
|
||||
|
||||
public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Database;
|
||||
using Content.Shared.Players;
|
||||
using Content.Shared.Roles;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Console;
|
||||
@@ -16,17 +17,21 @@ namespace Content.Server.Administration.Managers;
|
||||
|
||||
public sealed class RoleBanManager
|
||||
{
|
||||
[Dependency] private readonly INetManager _netManager = default!;
|
||||
[Dependency] private readonly IServerDbManager _db = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IPlayerLocator _playerLocator = default!;
|
||||
|
||||
private const string JobPrefix = "Job:";
|
||||
|
||||
private ISawmill _sawmill = default!;
|
||||
|
||||
private readonly Dictionary<NetUserId, HashSet<ServerRoleBanDef>> _cachedRoleBans = new();
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_sawmill = Logger.GetSawmill("rolebans");
|
||||
_netManager.RegisterNetMessage<MsgRoleBans>();
|
||||
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
|
||||
}
|
||||
|
||||
@@ -34,10 +39,13 @@ public sealed class RoleBanManager
|
||||
{
|
||||
if (e.NewStatus != SessionStatus.Connected
|
||||
|| _cachedRoleBans.ContainsKey(e.Session.UserId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var netChannel = e.Session.ConnectedClient;
|
||||
await CacheDbRoleBans(e.Session.UserId, netChannel.RemoteEndPoint.Address, netChannel.UserData.HWId.Length == 0 ? null : netChannel.UserData.HWId);
|
||||
SendRoleBans(e.Session);
|
||||
}
|
||||
|
||||
private async Task<bool> AddRoleBan(ServerRoleBanDef banDef)
|
||||
@@ -49,14 +57,41 @@ public sealed class RoleBanManager
|
||||
roleBans = new HashSet<ServerRoleBanDef>();
|
||||
_cachedRoleBans.Add(banDef.UserId.Value, roleBans);
|
||||
}
|
||||
if (!roleBans.Contains(banDef))
|
||||
roleBans.Add(banDef);
|
||||
|
||||
roleBans.Add(banDef);
|
||||
}
|
||||
|
||||
await _db.AddServerRoleBanAsync(banDef);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SendRoleBans(LocatedPlayerData located)
|
||||
{
|
||||
if (!_playerManager.TryGetSessionById(located.UserId, out var player))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SendRoleBans(player);
|
||||
}
|
||||
|
||||
public void SendRoleBans(IPlayerSession pSession)
|
||||
{
|
||||
if (!_cachedRoleBans.TryGetValue(pSession.UserId, out var roleBans))
|
||||
{
|
||||
_sawmill.Error($"Tried to send rolebans for {pSession.Name} but none cached?");
|
||||
return;
|
||||
}
|
||||
|
||||
var bans = new MsgRoleBans()
|
||||
{
|
||||
Bans = roleBans.Select(o => o.Role).ToList()
|
||||
};
|
||||
|
||||
_sawmill.Debug($"Sent rolebans to {pSession.Name}");
|
||||
_netManager.ServerSendMessage(bans, pSession.ConnectedClient);
|
||||
}
|
||||
|
||||
public HashSet<string>? GetRoleBans(NetUserId playerUserId)
|
||||
{
|
||||
return _cachedRoleBans.TryGetValue(playerUserId, out var roleBans) ? roleBans.Select(banDef => banDef.Role).ToHashSet() : null;
|
||||
@@ -98,7 +133,7 @@ public sealed class RoleBanManager
|
||||
}
|
||||
|
||||
#region Job Bans
|
||||
public async void CreateJobBan(IConsoleShell shell, string target, string job, string reason, uint minutes)
|
||||
public async void CreateJobBan(IConsoleShell shell, LocatedPlayerData located, string job, string reason, uint minutes)
|
||||
{
|
||||
if (!_prototypeManager.TryIndex(job, out JobPrototype? _))
|
||||
{
|
||||
@@ -107,7 +142,7 @@ public sealed class RoleBanManager
|
||||
}
|
||||
|
||||
job = string.Concat(JobPrefix, job);
|
||||
CreateRoleBan(shell, target, job, reason, minutes);
|
||||
CreateRoleBan(shell, located, job, reason, minutes);
|
||||
}
|
||||
|
||||
public HashSet<string>? GetJobBans(NetUserId playerUserId)
|
||||
@@ -122,15 +157,8 @@ public sealed class RoleBanManager
|
||||
#endregion
|
||||
|
||||
#region Commands
|
||||
private async void CreateRoleBan(IConsoleShell shell, string target, string role, string reason, uint minutes)
|
||||
private async void CreateRoleBan(IConsoleShell shell, LocatedPlayerData located, string role, string reason, uint minutes)
|
||||
{
|
||||
var located = await _playerLocator.LookupIdByNameOrIdAsync(target);
|
||||
if (located == null)
|
||||
{
|
||||
shell.WriteError(Loc.GetString("cmd-roleban-name-parse"));
|
||||
return;
|
||||
}
|
||||
|
||||
var targetUid = located.UserId;
|
||||
var targetHWid = located.LastHWId;
|
||||
var targetAddress = located.LastAddress;
|
||||
@@ -167,12 +195,12 @@ public sealed class RoleBanManager
|
||||
|
||||
if (!await AddRoleBan(banDef))
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("cmd-roleban-existing", ("target", target), ("role", role)));
|
||||
shell.WriteLine(Loc.GetString("cmd-roleban-existing", ("target", located.Username), ("role", role)));
|
||||
return;
|
||||
}
|
||||
|
||||
var length = expires == null ? Loc.GetString("cmd-roleban-inf") : Loc.GetString("cmd-roleban-until", ("expires", expires));
|
||||
shell.WriteLine(Loc.GetString("cmd-roleban-success", ("target", target), ("role", role), ("reason", reason), ("length", length)));
|
||||
shell.WriteLine(Loc.GetString("cmd-roleban-success", ("target", located.Username), ("role", role), ("reason", reason), ("length", length)));
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user