OOC sent by an admin will have a different color (#3117)

* Admin OOC is sent with a different color than regular OOC

- Also adds the OOC color to the database

* Command to set the color

* Ooc -> OOC

* Change default color to Red (`#ff0000`)

* Outdated namespace
This commit is contained in:
Leo
2021-02-14 11:59:56 -03:00
committed by GitHub
parent 54f48aa6a9
commit a3d0e3f6a7
17 changed files with 1324 additions and 30 deletions

View File

@@ -0,0 +1,53 @@
#nullable enable
using Content.Server.Database;
using Content.Server.Interfaces;
using Content.Shared.Administration;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
namespace Content.Server.Administration.Commands
{
[AdminCommand(AdminFlags.Admin)]
internal class SetAdminOOC : IConsoleCommand
{
public string Command => "setadminooc";
public string Description => Loc.GetString($"Sets the color of your OOC messages. Color must be in hex format, example: {Command} #c43b23");
public string Help => Loc.GetString($"Usage: {Command} <color>");
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (!(shell.Player is IPlayerSession))
{
shell.WriteError(Loc.GetString("Only players can use this command"));
return;
}
if (args.Length < 1)
return;
var colorArg = string.Join(" ", args).Trim();
if (string.IsNullOrEmpty(colorArg))
return;
var color = Color.TryFromHex(colorArg);
if (!color.HasValue)
{
shell.WriteError(Loc.GetString("Invalid color hex!"));
return;
}
var userId = shell.Player.UserId;
// Save the DB
var dbMan = IoCManager.Resolve<IServerDbManager>();
dbMan.SaveAdminOOCColorAsync(userId, color.Value);
// Update the cached preference
var prefManager = IoCManager.Resolve<IServerPreferencesManager>();
var prefs = prefManager.GetPreferences(userId);
prefs.AdminOOCColor = color.Value;
}
}
}

View File

@@ -8,6 +8,7 @@ using Content.Server.GameObjects.Components.Observer;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces;
using Content.Server.Interfaces.Chat;
using Content.Shared.Administration;
using Content.Shared.Chat;
using Content.Shared.GameObjects.Components.Inventory;
using Content.Shared.GameObjects.EntitySystems.ActionBlocker;
@@ -47,6 +48,7 @@ namespace Content.Server.Chat
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IMoMMILink _mommiLink = default!;
[Dependency] private readonly IAdminManager _adminManager = default!;
[Dependency] private readonly IServerPreferencesManager _preferencesManager = default!;
public void Initialize()
{
@@ -187,7 +189,7 @@ namespace Content.Server.Chat
public void SendOOC(IPlayerSession player, string message)
{
// Check if message exceeds the character limi
// Check if message exceeds the character limit
if (message.Length > MaxMessageLength)
{
DispatchServerMessage(player, Loc.GetString(MaxLengthExceededMessage, MaxMessageLength));
@@ -198,6 +200,12 @@ namespace Content.Server.Chat
msg.Channel = ChatChannel.OOC;
msg.Message = message;
msg.MessageWrap = $"OOC: {player.Name}: {{0}}";
if (_adminManager.HasAdminFlag(player, AdminFlags.Admin))
{
var prefs = _preferencesManager.GetPreferences((player.UserId));
msg.MessageColorOverride = prefs.AdminOOCColor;
}
//TODO: player.Name color, this will need to change the structure of the MsgChatMessage
_netManager.ServerSendToAll(msg);
_mommiLink.SendOOCMessage(player.Name, message);

View File

@@ -15,6 +15,8 @@ namespace Content.Server.Database
{
public abstract class ServerDbBase
{
#region Preferences
public async Task<PlayerPreferences?> GetPlayerPreferencesAsync(NetUserId userId)
{
await using var db = await GetDb();
@@ -34,7 +36,7 @@ namespace Content.Server.Database
profiles[profile.Slot] = ConvertProfiles(profile);
}
return new PlayerPreferences(profiles, prefs.SelectedCharacterSlot);
return new PlayerPreferences(profiles, prefs.SelectedCharacterSlot, Color.FromHex(prefs.AdminOOCColor));
}
public async Task SaveSelectedCharacterIndexAsync(NetUserId userId, int index)
@@ -99,7 +101,8 @@ namespace Content.Server.Database
var prefs = new Preference
{
UserId = userId.UserId,
SelectedCharacterSlot = 0
SelectedCharacterSlot = 0,
AdminOOCColor = Color.Red.ToHex()
};
prefs.Profiles.Add(profile);
@@ -108,7 +111,7 @@ namespace Content.Server.Database
await db.DbContext.SaveChangesAsync();
return new PlayerPreferences(new[] {new KeyValuePair<int, ICharacterProfile>(0, defaultProfile)}, 0);
return new PlayerPreferences(new[] {new KeyValuePair<int, ICharacterProfile>(0, defaultProfile)}, 0, Color.FromHex(prefs.AdminOOCColor));
}
public async Task DeleteSlotAndSetSelectedIndex(NetUserId userId, int deleteSlot, int newSlot)
@@ -121,6 +124,19 @@ namespace Content.Server.Database
await db.DbContext.SaveChangesAsync();
}
public async Task SaveAdminOOCColorAsync(NetUserId userId, Color color)
{
await using var db = await GetDb();
var prefs = await db.DbContext
.Preference
.Include(p => p.Profiles)
.SingleAsync(p => p.UserId == userId.UserId);
prefs.AdminOOCColor = color.ToHex();
await db.DbContext.SaveChangesAsync();
}
private static async Task SetSelectedCharacterSlotAsync(NetUserId userId, int newSlot, ServerDbContext db)
{
var prefs = await db.Preference.SingleAsync(p => p.UserId == userId.UserId);
@@ -203,6 +219,7 @@ namespace Content.Server.Database
return entity;
}
#endregion
public async Task<NetUserId?> GetAssignedUserIdAsync(string name)
{

View File

@@ -14,6 +14,7 @@ using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using LogLevel = Robust.Shared.Log.LogLevel;
using MSLogLevel = Microsoft.Extensions.Logging.LogLevel;
@@ -32,6 +33,8 @@ namespace Content.Server.Database
Task SaveCharacterSlotAsync(NetUserId userId, ICharacterProfile? profile, int slot);
Task SaveAdminOOCColorAsync(NetUserId userId, Color color);
// Single method for two operations for transaction.
Task DeleteSlotAndSetSelectedIndex(NetUserId userId, int deleteSlot, int newSlot);
Task<PlayerPreferences?> GetPlayerPreferencesAsync(NetUserId userId);
@@ -151,6 +154,11 @@ namespace Content.Server.Database
return _db.DeleteSlotAndSetSelectedIndex(userId, deleteSlot, newSlot);
}
public Task SaveAdminOOCColorAsync(NetUserId userId, Color color)
{
return _db.SaveAdminOOCColorAsync(userId, color);
}
public Task<PlayerPreferences?> GetPlayerPreferencesAsync(NetUserId userId)
{
return _db.GetPlayerPreferencesAsync(userId);

View File

@@ -12,6 +12,7 @@ using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
@@ -72,7 +73,7 @@ namespace Content.Server.Preferences
return;
}
prefsData.Prefs = new PlayerPreferences(curPrefs.Characters, index);
prefsData.Prefs = new PlayerPreferences(curPrefs.Characters, index, curPrefs.AdminOOCColor);
if (ShouldStorePrefs(message.MsgChannel.AuthType))
{
@@ -111,7 +112,7 @@ namespace Content.Server.Preferences
[slot] = HumanoidCharacterProfile.EnsureValid((HumanoidCharacterProfile) profile, _protos)
};
prefsData.Prefs = new PlayerPreferences(profiles, slot);
prefsData.Prefs = new PlayerPreferences(profiles, slot, curPrefs.AdminOOCColor);
if (ShouldStorePrefs(message.MsgChannel.AuthType))
{
@@ -156,7 +157,7 @@ namespace Content.Server.Preferences
var arr = new Dictionary<int, ICharacterProfile>(curPrefs.Characters);
arr.Remove(slot);
prefsData.Prefs = new PlayerPreferences(arr, nextSlot ?? curPrefs.SelectedCharacterIndex);
prefsData.Prefs = new PlayerPreferences(arr, nextSlot ?? curPrefs.SelectedCharacterIndex, curPrefs.AdminOOCColor);
if (ShouldStorePrefs(message.MsgChannel.AuthType))
{
@@ -181,7 +182,7 @@ namespace Content.Server.Preferences
PrefsLoaded = Task.CompletedTask,
Prefs = new PlayerPreferences(
new[] {new KeyValuePair<int, ICharacterProfile>(0, HumanoidCharacterProfile.Default())},
0)
0, Color.Transparent)
};
_cachedPlayerPrefs[session.UserId] = prefsData;
@@ -279,7 +280,7 @@ namespace Content.Server.Preferences
}
return new KeyValuePair<int, ICharacterProfile>(p.Key, newProf);
}), prefs.SelectedCharacterIndex);
}), prefs.SelectedCharacterIndex, prefs.AdminOOCColor);
}
public IEnumerable<KeyValuePair<NetUserId, ICharacterProfile>> GetSelectedProfilesForPlayers(