Revert "Admin logs (#5419)"

This reverts commit 319aec109d.
This commit is contained in:
DrSmugleaf
2021-11-22 18:55:17 +01:00
parent edeccabb36
commit c18d07538a
65 changed files with 236 additions and 7021 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.Linq;
using Content.Server.Administration.Managers;
using Content.Server.Players;

View File

@@ -1,64 +0,0 @@
using System.Threading.Tasks;
using Content.Server.Administration.Logs;
using Content.Shared.Administration;
using Content.Shared.Administration.Logs;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.Timing;
namespace Content.Server.Administration.Commands;
#if DEBUG
[AdminCommand(AdminFlags.Host)]
public class AdminLogBulk : IConsoleCommand
{
public string Command => "adminlogbulk";
public string Description => "Adds debug logs to the database.";
public string Help => $"Usage: {Command} <amount> <parallel>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (shell.Player?.AttachedEntity is not { } entity)
{
shell.WriteError("This command can only be ran by a player with an attached entity.");
return;
}
int amount;
var parallel = false;
switch (args)
{
case {Length: 1} when int.TryParse(args[0], out amount):
case {Length: 2} when int.TryParse(args[0], out amount) &&
bool.TryParse(args[1], out parallel):
break;
default:
shell.WriteError(Help);
return;
}
var logs = EntitySystem.Get<AdminLogSystem>();
var stopwatch = new Stopwatch();
stopwatch.Start();
if (parallel)
{
Parallel.For(0, amount, _ =>
{
logs.Add(LogType.Unknown, $"Debug log added by {entity:Player}");
});
}
else
{
for (var i = 0; i < amount; i++)
{
logs.Add(LogType.Unknown, $"Debug log added by {entity:Player}");
}
}
shell.WriteLine($"Added {amount} logs in {stopwatch.Elapsed.TotalMilliseconds} ms");
}
}
#endif

View File

@@ -1,30 +0,0 @@
using Content.Server.Administration.Logs;
using Content.Server.Administration.UI;
using Content.Server.EUI;
using Content.Shared.Administration;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.IoC;
namespace Content.Server.Administration.Commands;
[AdminCommand(AdminFlags.Logs)]
public class OpenAdminLogsCommand : IConsoleCommand
{
public string Command => "adminlogs";
public string Description => "Opens the admin logs panel.";
public string Help => $"Usage: {Command}";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (shell.Player is not IPlayerSession player)
{
shell.WriteLine("This does not work from the server console.");
return;
}
var eui = IoCManager.Resolve<EuiManager>();
var ui = new AdminLogsEui();
eui.OpenEui(ui, player);
}
}

View File

@@ -1,302 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Content.Server.Administration.Logs.Converters;
using Content.Server.Database;
using Content.Server.GameTicking;
using Content.Server.GameTicking.Events;
using Content.Shared.Administration.Logs;
using Content.Shared.CCVar;
using Prometheus;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Reflection;
namespace Content.Server.Administration.Logs;
public class AdminLogSystem : SharedAdminLogSystem
{
[Dependency] private readonly IConfigurationManager _configuration = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IServerDbManager _db = default!;
[Dependency] private readonly IDynamicTypeFactory _typeFactory = default!;
[Dependency] private readonly IReflectionManager _reflection = default!;
[Dependency] private readonly GameTicker _gameTicker = default!;
public const string SawmillId = "admin.logs";
private static readonly Histogram DatabaseUpdateTime = Metrics.CreateHistogram(
"admin_logs_database_time",
"Time used to send logs to the database in ms",
new HistogramConfiguration
{
Buckets = Histogram.LinearBuckets(0, 0.5, 20)
});
private static readonly Gauge QueueCapReached = Metrics.CreateGauge(
"admin_logs_queue_cap_reached",
"Number of times the log queue cap has been reached in a round.");
private static readonly Gauge LogsSent = Metrics.CreateGauge(
"admin_logs_sent",
"Amount of logs sent to the database in a round.");
private static readonly JsonNamingPolicy NamingPolicy = JsonNamingPolicy.CamelCase;
// Init only
private ISawmill _sawmill = default!;
private JsonSerializerOptions _jsonOptions = default!;
// CVars
private bool _metricsEnabled;
private TimeSpan _queueSendDelay;
private int _queueMax;
// Per update
private float _accumulatedFrameTime;
private readonly ConcurrentQueue<QueuedLog> _logsToAdd = new();
private int CurrentRoundId => _gameTicker.RoundId;
public override void Initialize()
{
base.Initialize();
_sawmill = _logManager.GetSawmill(SawmillId);
_jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = NamingPolicy
};
foreach (var converter in _reflection.FindTypesWithAttribute<AdminLogConverterAttribute>())
{
var instance = _typeFactory.CreateInstance<JsonConverter>(converter);
_jsonOptions.Converters.Add(instance);
}
var converterNames = _jsonOptions.Converters.Select(converter => converter.GetType().Name);
_sawmill.Info($"Admin log converters found: {string.Join(" ", converterNames)}");
_configuration.OnValueChanged(CVars.MetricsEnabled,
value => _metricsEnabled = value, true);
_configuration.OnValueChanged(CCVars.AdminLogsQueueSendDelay,
value => _queueSendDelay = TimeSpan.FromSeconds(value), true);
_configuration.OnValueChanged(CCVars.AdminLogsQueueMax,
value => _queueMax = value, true);
if (_metricsEnabled)
{
QueueCapReached.Set(0);
LogsSent.Set(0);
}
SubscribeLocalEvent<RoundStartingEvent>(RoundStarting);
}
public override async void Shutdown()
{
base.Shutdown();
if (!_logsToAdd.IsEmpty)
{
await SendLogs();
}
}
public override async void Update(float frameTime)
{
var count = _logsToAdd.Count;
if (count == 0)
{
return;
}
if (count < _queueMax && _accumulatedFrameTime < _queueSendDelay.TotalSeconds)
{
_accumulatedFrameTime += frameTime;
return;
}
await SendLogs();
}
private async Task SendLogs()
{
var copy = new List<QueuedLog>(_logsToAdd);
_logsToAdd.Clear();
_accumulatedFrameTime = 0;
// ship the logs to Azkaban
var task = Task.Run(() =>
{
_db.AddAdminLogs(copy);
});
if (_metricsEnabled)
{
if (copy.Count >= _queueMax)
{
QueueCapReached.Inc();
}
LogsSent.Inc(copy.Count);
using (DatabaseUpdateTime.NewTimer())
{
await task;
return;
}
}
await task;
}
private void RoundStarting(RoundStartingEvent ev)
{
if (_metricsEnabled)
{
QueueCapReached.Set(0);
LogsSent.Set(0);
}
}
public (JsonDocument json, List<Guid> players, List<(int id, string? name)> entities) ToJson(
Dictionary<string, object?> properties)
{
var entities = new List<(int id, string? name)>();
var players = new List<Guid>();
var parsed = new Dictionary<string, object?>();
foreach (var key in properties.Keys)
{
var value = properties[key];
var parsedKey = NamingPolicy.ConvertName(key);
parsed.Add(parsedKey, value);
EntityUid? entityId = properties[key] switch
{
EntityUid id => id,
IEntity entity => entity.Uid,
IPlayerSession {AttachedEntityUid: { }} session => session.AttachedEntityUid.Value,
IComponent component => component.OwnerUid,
_ => null
};
if (entityId is not { } uid)
{
continue;
}
var entityName = _entityManager.TryGetEntity(uid, out var resolvedEntity)
? resolvedEntity.Name
: null;
entities.Add(((int) uid, entityName));
if (_entityManager.TryGetComponent(uid, out ActorComponent? actor))
{
players.Add(actor.PlayerSession.UserId.UserId);
}
}
return (JsonSerializer.SerializeToDocument(parsed, _jsonOptions), players, entities);
}
private async void Add(LogType type, LogImpact impact, string message, JsonDocument json, List<Guid> players, List<(int id, string? name)> entities)
{
var log = new AdminLog
{
RoundId = CurrentRoundId,
Type = type,
Impact = impact,
Date = DateTime.UtcNow,
Message = message,
Json = json,
Players = new List<AdminLogPlayer>(players.Count)
};
var queued = new QueuedLog(log, entities);
_logsToAdd.Enqueue(queued);
foreach (var id in players)
{
var player = new AdminLogPlayer
{
PlayerUserId = id,
RoundId = CurrentRoundId
};
log.Players.Add(player);
}
}
public override void Add(LogType type, LogImpact impact, ref LogStringHandler handler)
{
var (json, players, entities) = ToJson(handler.Values);
var message = handler.ToStringAndClear();
Add(type, impact, message, json, players, entities);
}
public override void Add(LogType type, ref LogStringHandler handler)
{
Add(type, LogImpact.Medium, ref handler);
}
public IAsyncEnumerable<LogRecord> All(LogFilter? filter = null)
{
return _db.GetAdminLogs(filter);
}
public IAsyncEnumerable<string> AllMessages(LogFilter? filter = null)
{
return _db.GetAdminLogMessages(filter);
}
public IAsyncEnumerable<JsonDocument> AllJson(LogFilter? filter = null)
{
return _db.GetAdminLogsJson(filter);
}
public Task<Round> Round(int roundId)
{
return _db.GetRound(roundId);
}
public IAsyncEnumerable<LogRecord> CurrentRoundLogs(LogFilter? filter = null)
{
filter ??= new LogFilter();
filter.Round = CurrentRoundId;
return All(filter);
}
public IAsyncEnumerable<string> CurrentRoundMessages(LogFilter? filter = null)
{
filter ??= new LogFilter();
filter.Round = CurrentRoundId;
return AllMessages(filter);
}
public IAsyncEnumerable<JsonDocument> CurrentRoundJson(LogFilter? filter = null)
{
filter ??= new LogFilter();
filter.Round = CurrentRoundId;
return AllJson(filter);
}
public Task<Round> CurrentRound()
{
return Round(CurrentRoundId);
}
}

View File

@@ -1,204 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Content.Server.Administration.Managers;
using Content.Server.EUI;
using Content.Server.GameTicking;
using Content.Shared.Administration;
using Content.Shared.Administration.Logs;
using Content.Shared.CCVar;
using Content.Shared.Eui;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using static Content.Shared.Administration.AdminLogsEuiMsg;
namespace Content.Server.Administration.Logs;
public sealed class AdminLogsEui : BaseEui
{
[Dependency] private readonly IAdminManager _adminManager = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IConfigurationManager _configuration = default!;
private readonly ISawmill _sawmill;
private readonly AdminLogSystem _logSystem;
private int _clientBatchSize;
private bool _isLoading = true;
private readonly Dictionary<Guid, string> _players = new();
private CancellationTokenSource _logSendCancellation = new();
private LogFilter _filter;
public AdminLogsEui()
{
IoCManager.InjectDependencies(this);
_sawmill = _logManager.GetSawmill(AdminLogSystem.SawmillId);
_configuration.OnValueChanged(CCVars.AdminLogsClientBatchSize, ClientBatchSizeChanged, true);
_logSystem = EntitySystem.Get<AdminLogSystem>();
_filter = new LogFilter
{
CancellationToken = _logSendCancellation.Token,
Limit = _clientBatchSize
};
}
public int CurrentRoundId => EntitySystem.Get<GameTicker>().RoundId;
public override async void Opened()
{
base.Opened();
_adminManager.OnPermsChanged += OnPermsChanged;
var roundId = _filter.Round ?? EntitySystem.Get<GameTicker>().RoundId;
LoadFromDb(roundId);
}
private void ClientBatchSizeChanged(int value)
{
_clientBatchSize = value;
}
private void OnPermsChanged(AdminPermsChangedEventArgs args)
{
if (args.Player == Player && !_adminManager.HasAdminFlag(Player, AdminFlags.Logs))
{
Close();
}
}
public override EuiStateBase GetNewState()
{
if (_isLoading)
{
return new AdminLogsEuiState(CurrentRoundId, new Dictionary<Guid, string>())
{
IsLoading = true
};
}
var state = new AdminLogsEuiState(CurrentRoundId, _players);
return state;
}
public override async void HandleMessage(EuiMessageBase msg)
{
if (!_adminManager.HasAdminFlag(Player, AdminFlags.Logs))
{
return;
}
switch (msg)
{
case Close _:
{
Close();
break;
}
case LogsRequest request:
{
_sawmill.Info($"Admin log request from admin with id {Player.UserId.UserId} and name {Player.Name}");
_logSendCancellation.Cancel();
_logSendCancellation = new CancellationTokenSource();
_filter = new LogFilter
{
CancellationToken = _logSendCancellation.Token,
Round = request.RoundId,
Types = request.Types,
Impacts = request.Impacts,
Before = request.Before,
After = request.After,
AnyPlayers = request.AnyPlayers,
AllPlayers = request.AllPlayers,
LastLogId = 0,
Limit = _clientBatchSize
};
var roundId = _filter.Round ??= EntitySystem.Get<GameTicker>().RoundId;
LoadFromDb(roundId);
SendLogs(true);
break;
}
case NextLogsRequest:
{
_sawmill.Info($"Admin log next batch request from admin with id {Player.UserId.UserId} and name {Player.Name}");
SendLogs(false);
break;
}
}
}
private async void SendLogs(bool replace)
{
var logs = new List<SharedAdminLog>(_clientBatchSize);
await Task.Run(async () =>
{
var results = await Task.Run(() => _logSystem.All(_filter));
await foreach (var record in results.WithCancellation(_logSendCancellation.Token))
{
var log = new SharedAdminLog(record.Id, record.Type, record.Impact, record.Date, record.Message, record.Players);
logs.Add(log);
}
}, _filter.CancellationToken);
if (logs.Count > 0)
{
var largestId = _filter.DateOrder switch
{
DateOrder.Ascending => ^1,
DateOrder.Descending => 0,
_ => throw new ArgumentOutOfRangeException(nameof(_filter.DateOrder), _filter.DateOrder, null)
};
_filter.LastLogId = logs[largestId].Id;
}
var message = new NewLogs(logs.ToArray(), replace);
SendMessage(message);
}
public override void Closed()
{
base.Closed();
_configuration.UnsubValueChanged(CCVars.AdminLogsClientBatchSize, ClientBatchSizeChanged);
_adminManager.OnPermsChanged -= OnPermsChanged;
_logSendCancellation.Cancel();
_logSendCancellation.Dispose();
}
private async void LoadFromDb(int roundId)
{
_isLoading = true;
StateDirty();
var round = await Task.Run(() => _logSystem.Round(roundId));
var players = round.Players
.ToDictionary(player => player.UserId, player => player.LastSeenUserName);
_players.Clear();
foreach (var (id, name) in players)
{
_players.Add(id, name);
}
_isLoading = false;
StateDirty();
}
}

View File

@@ -1,15 +0,0 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Content.Server.Administration.Logs.Converters;
public abstract class AdminLogConverter<T> : JsonConverter<T>
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotSupportedException();
}
public abstract override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options);
}

View File

@@ -1,10 +0,0 @@
using System;
using JetBrains.Annotations;
namespace Content.Server.Administration.Logs.Converters;
[AttributeUsage(AttributeTargets.Class)]
[BaseTypeRequired(typeof(AdminLogConverter<>))]
public class AdminLogConverterAttribute : Attribute
{
}

View File

@@ -1,27 +0,0 @@
using System.Text.Json;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Administration.Logs.Converters;
[AdminLogConverter]
public class EntityJsonConverter : AdminLogConverter<Entity>
{
[Dependency] private readonly IEntityManager _entities = default!;
public override void Write(Utf8JsonWriter writer, Entity value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteNumber("id", (int) value.Uid);
writer.WriteString("name", value.Name);
if (_entities.TryGetComponent(value.Uid, out ActorComponent? actor))
{
writer.WriteString("player", actor.PlayerSession.UserId.UserId);
}
writer.WriteEndObject();
}
}

View File

@@ -1,13 +0,0 @@
using System.Text.Json;
using Content.Shared.FixedPoint;
namespace Content.Server.Administration.Logs.Converters;
[AdminLogConverter]
public class FixedPoint2Converter : AdminLogConverter<FixedPoint2>
{
public override void Write(Utf8JsonWriter writer, FixedPoint2 value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Int());
}
}

View File

@@ -1,33 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Content.Shared.Administration.Logs;
namespace Content.Server.Administration.Logs;
public sealed class LogFilter
{
public CancellationToken CancellationToken { get; set; }
public int? Round { get; set; }
public string? Search { get; set; }
public List<LogType>? Types { get; set; }
public List<LogImpact>? Impacts { get; set; }
public DateTime? Before { get; set; }
public DateTime? After { get; set; }
public Guid[]? AnyPlayers { get; set; }
public Guid[]? AllPlayers { get; set; }
public int? LastLogId { get; set; }
public int? Limit { get; set; }
public DateOrder DateOrder { get; set; } = DateOrder.Descending;
}

View File

@@ -1,35 +0,0 @@
using System;
using System.Collections.Generic;
using Content.Server.Database;
using Content.Shared.Administration.Logs;
namespace Content.Server.Administration.Logs;
public class LogRecord
{
public LogRecord(
int id,
int roundId,
LogType type,
LogImpact impact,
DateTime date,
string message,
Guid[] players)
{
Id = id;
RoundId = roundId;
Type = type;
Impact = impact;
Date = date;
Message = message;
Players = players;
}
public int Id { get; }
public int RoundId { get; }
public LogType Type { get; }
public LogImpact Impact { get; }
public DateTime Date { get; }
public string Message { get; }
public Guid[] Players { get; }
}

View File

@@ -1,24 +0,0 @@
using System.Collections.Generic;
using Content.Server.Database;
using JetBrains.Annotations;
namespace Content.Server.Administration.Logs;
public readonly struct QueuedLog
{
public QueuedLog(AdminLog log, List<(int id, string? name)> entities)
{
Log = log;
Entities = entities;
}
public AdminLog Log { get; }
public List<(int id, string? name)> Entities { get; }
public void Deconstruct(out AdminLog log, out List<(int id, string? name)> entities)
{
log = Log;
entities = Entities;
}
}

View File

@@ -1,3 +1,4 @@
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;

View File

@@ -3,18 +3,14 @@ using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Net;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Content.Server.Administration.Logs;
using Content.Shared.Administration.Logs;
using Content.Shared.CharacterAppearance;
using Content.Shared.Preferences;
using Microsoft.EntityFrameworkCore;
using Robust.Shared.Enums;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Utility;
namespace Content.Server.Database
{
@@ -444,54 +440,6 @@ namespace Content.Server.Database
await db.DbContext.SaveChangesAsync(cancel);
}
public virtual async Task<int> AddNewRound(params Guid[] playerIds)
{
await using var db = await GetDb();
var players = await db.DbContext.Player
.Where(player => playerIds.Contains(player.UserId))
.ToListAsync();
var round = new Round
{
Players = players
};
db.DbContext.Round.Add(round);
await db.DbContext.SaveChangesAsync();
return round.Id;
}
public async Task<Round> GetRound(int id)
{
await using var db = await GetDb();
var round = await db.DbContext.Round
.Include(round => round.Players)
.SingleAsync(round => round.Id == id);
return round;
}
public async Task AddRoundPlayers(int id, Guid[] playerIds)
{
await using var db = await GetDb();
var round = await db.DbContext.Round
.Include(round => round.Players)
.SingleAsync(round => round.Id == id);
var players = await db.DbContext.Player
.Where(player => playerIds.Contains(player.UserId))
.ToListAsync();
round.Players.AddRange(players);
await db.DbContext.SaveChangesAsync();
}
public async Task UpdateAdminRankAsync(AdminRank rank, CancellationToken cancel)
{
await using var db = await GetDb();
@@ -507,160 +455,6 @@ namespace Content.Server.Database
}
#endregion
#region Admin Logs
public virtual async Task AddAdminLogs(List<QueuedLog> logs)
{
await using var db = await GetDb();
var entities = new Dictionary<int, AdminLogEntity>();
foreach (var (log, entityData) in logs)
{
var logEntities = new List<AdminLogEntity>(entityData.Count);
foreach (var (id, name) in entityData)
{
var entity = entities.GetOrNew(id);
entity.Name = name;
logEntities.Add(entity);
}
log.Entities = logEntities;
db.DbContext.AdminLog.Add(log);
}
await db.DbContext.SaveChangesAsync();
}
private async Task<IQueryable<AdminLog>> GetAdminLogsQuery(ServerDbContext db, LogFilter? filter = null)
{
IQueryable<AdminLog> query = db.AdminLog;
if (filter == null)
{
return query.OrderBy(log => log.Date);
}
if (filter.Round != null)
{
query = query.Where(log => log.RoundId == filter.Round);
}
if (filter.Search != null)
{
query = query.Where(log => log.Message.Contains(filter.Search));
}
if (filter.Types != null)
{
query = query.Where(log => filter.Types.Contains(log.Type));
}
if (filter.Impacts != null)
{
query = query.Where(log => filter.Impacts.Contains(log.Impact));
}
if (filter.Before != null)
{
query = query.Where(log => log.Date < filter.Before);
}
if (filter.After != null)
{
query = query.Where(log => log.Date > filter.After);
}
if (filter.AnyPlayers != null)
{
var players = await db.AdminLogPlayer
.Where(player => filter.AnyPlayers.Contains(player.PlayerUserId))
.ToListAsync();
if (players.Count > 0)
{
query = from log in query
join player in db.AdminLogPlayer on log.Id equals player.LogId
where filter.AnyPlayers.Contains(player.Player.UserId)
select log;
}
}
if (filter.AllPlayers != null)
{
// TODO ADMIN LOGGING
}
query = query.Distinct();
if (filter.LastLogId != null)
{
query = filter.DateOrder switch
{
DateOrder.Ascending => query.Where(log => log.Id < filter.LastLogId),
DateOrder.Descending => query.Where(log => log.Id > filter.LastLogId),
_ => throw new ArgumentOutOfRangeException(nameof(filter),
$"Unknown {nameof(DateOrder)} value {filter.DateOrder}")
};
}
query = filter.DateOrder switch
{
DateOrder.Ascending => query.OrderBy(log => log.Date),
DateOrder.Descending => query.OrderByDescending(log => log.Date),
_ => throw new ArgumentOutOfRangeException(nameof(filter),
$"Unknown {nameof(DateOrder)} value {filter.DateOrder}")
};
if (filter.Limit != null)
{
query = query.Take(filter.Limit.Value);
}
return query;
}
public async IAsyncEnumerable<string> GetAdminLogMessages(LogFilter? filter = null)
{
await using var db = await GetDb();
var query = await GetAdminLogsQuery(db.DbContext, filter);
await foreach (var log in query.Select(log => log.Message).AsAsyncEnumerable())
{
yield return log;
}
}
public async IAsyncEnumerable<LogRecord> GetAdminLogs(LogFilter? filter = null)
{
await using var db = await GetDb();
var query = await GetAdminLogsQuery(db.DbContext, filter);
await foreach (var log in query.AsAsyncEnumerable())
{
var players = new Guid[log.Players.Count];
for (var i = 0; i < log.Players.Count; i++)
{
players[i] = log.Players[i].PlayerUserId;
}
yield return new LogRecord(log.Id, log.RoundId, log.Type, log.Impact, log.Date, log.Message, players);
}
}
public async IAsyncEnumerable<JsonDocument> GetAdminLogsJson(LogFilter? filter = null)
{
await using var db = await GetDb();
var query = await GetAdminLogsQuery(db.DbContext, filter);
await foreach (var json in query.Select(log => log.Json).AsAsyncEnumerable())
{
yield return json;
}
}
#endregion
protected abstract Task<DbGuard> GetDb();
protected abstract class DbGuard : IAsyncDisposable

View File

@@ -3,10 +3,8 @@ using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Content.Server.Administration.Logs;
using Content.Shared.CCVar;
using Content.Shared.Preferences;
using Microsoft.Data.Sqlite;
@@ -19,17 +17,17 @@ using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Logger = Robust.Shared.Log.Logger;
using LogLevel = Robust.Shared.Log.LogLevel;
using MSLogLevel = Microsoft.Extensions.Logging.LogLevel;
namespace Content.Server.Database
{
public interface IServerDbManager
{
void Init();
#region Preferences
// Preferences
Task<PlayerPreferences> InitPrefsAsync(NetUserId userId, ICharacterProfile defaultProfile);
Task SaveSelectedCharacterIndexAsync(NetUserId userId, int index);
@@ -40,15 +38,12 @@ namespace Content.Server.Database
// Single method for two operations for transaction.
Task DeleteSlotAndSetSelectedIndex(NetUserId userId, int deleteSlot, int newSlot);
Task<PlayerPreferences?> GetPlayerPreferencesAsync(NetUserId userId);
#endregion
#region User Ids
// Username assignment (for guest accounts, so they persist GUID)
Task AssignUserIdAsync(string name, NetUserId userId);
Task<NetUserId?> GetAssignedUserIdAsync(string name);
#endregion
#region Bans
// Ban stuff
/// <summary>
/// Looks up a ban by id.
/// This will return a pardoned ban as well.
@@ -87,9 +82,8 @@ namespace Content.Server.Database
Task AddServerBanAsync(ServerBanDef serverBan);
Task AddServerUnbanAsync(ServerUnbanDef serverBan);
#endregion
#region Player Records
// Player records
Task UpdatePlayerRecordAsync(
NetUserId userId,
string userName,
@@ -97,17 +91,15 @@ namespace Content.Server.Database
ImmutableArray<byte> hwId);
Task<PlayerRecord?> GetPlayerRecordByUserName(string userName, CancellationToken cancel = default);
Task<PlayerRecord?> GetPlayerRecordByUserId(NetUserId userId, CancellationToken cancel = default);
#endregion
#region Connection Logs
// Connection log
Task AddConnectionLogAsync(
NetUserId userId,
string userName,
IPAddress address,
ImmutableArray<byte> hwId);
#endregion
#region Admin Ranks
// Admins
Task<Admin?> GetAdminDataForAsync(NetUserId userId, CancellationToken cancel = default);
Task<AdminRank?> GetAdminRankAsync(int id, CancellationToken cancel = default);
@@ -121,24 +113,6 @@ namespace Content.Server.Database
Task RemoveAdminRankAsync(int rankId, CancellationToken cancel = default);
Task AddAdminRankAsync(AdminRank rank, CancellationToken cancel = default);
Task UpdateAdminRankAsync(AdminRank rank, CancellationToken cancel = default);
#endregion
#region Rounds
Task<int> AddNewRound(params Guid[] playerIds);
Task<Round> GetRound(int id);
Task AddRoundPlayers(int id, params Guid[] playerIds);
#endregion
#region Admin Logs
Task AddAdminLogs(List<QueuedLog> logs);
IAsyncEnumerable<string> GetAdminLogMessages(LogFilter? filter = null);
IAsyncEnumerable<LogRecord> GetAdminLogs(LogFilter? filter = null);
IAsyncEnumerable<JsonDocument> GetAdminLogsJson(LogFilter? filter = null);
#endregion
}
public sealed class ServerDbManager : IServerDbManager
@@ -316,46 +290,11 @@ namespace Content.Server.Database
return _db.AddAdminRankAsync(rank, cancel);
}
public Task<int> AddNewRound(params Guid[] playerIds)
{
return _db.AddNewRound(playerIds);
}
public Task<Round> GetRound(int id)
{
return _db.GetRound(id);
}
public Task AddRoundPlayers(int id, params Guid[] playerIds)
{
return _db.AddRoundPlayers(id, playerIds);
}
public Task UpdateAdminRankAsync(AdminRank rank, CancellationToken cancel = default)
{
return _db.UpdateAdminRankAsync(rank, cancel);
}
public Task AddAdminLogs(List<QueuedLog> logs)
{
return _db.AddAdminLogs(logs);
}
public IAsyncEnumerable<string> GetAdminLogMessages(LogFilter? filter = null)
{
return _db.GetAdminLogMessages(filter);
}
public IAsyncEnumerable<LogRecord> GetAdminLogs(LogFilter? filter = null)
{
return _db.GetAdminLogs(filter);
}
public IAsyncEnumerable<JsonDocument> GetAdminLogsJson(LogFilter? filter = null)
{
return _db.GetAdminLogsJson(filter);
}
private DbContextOptions<ServerDbContext> CreatePostgresOptions()
{
var host = _cfg.GetCVar(CCVars.DatabasePgHost);

View File

@@ -3,19 +3,15 @@ using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Net;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Content.Server.Administration.Logs;
using Content.Server.IP;
using Content.Server.Preferences.Managers;
using Content.Shared.Administration.Logs;
using Content.Shared.CCVar;
using Microsoft.EntityFrameworkCore;
using Robust.Shared.Configuration;
using Robust.Shared.IoC;
using Robust.Shared.Network;
using Robust.Shared.Utility;
namespace Content.Server.Database
{
@@ -250,65 +246,6 @@ namespace Content.Server.Database
return (admins.Select(p => (p.a, p.LastSeenUserName)).ToArray(), adminRanks)!;
}
private async Task<int> NextId<TModel>(DbSet<TModel> set, Func<TModel, int> selector) where TModel : class
{
var id = 1;
if (await set.AnyAsync())
{
id = set.Max(selector) + 1;
}
return id;
}
public override async Task<int> AddNewRound(params Guid[] playerIds)
{
await using var db = await GetDb();
var players = await db.DbContext.Player
.Where(player => playerIds.Contains(player.UserId))
.ToListAsync();
var round = new Round
{
Id = await NextId(db.DbContext.Round, round => round.Id),
Players = players
};
db.DbContext.Round.Add(round);
await db.DbContext.SaveChangesAsync();
return round.Id;
}
public override async Task AddAdminLogs(List<QueuedLog> logs)
{
await using var db = await GetDb();
var nextId = await NextId(db.DbContext.AdminLog, log => log.Id);
var entities = new Dictionary<int, AdminLogEntity>();
foreach (var (log, entityData) in logs)
{
log.Id = nextId++;
var logEntities = new List<AdminLogEntity>(entityData.Count);
foreach (var (id, name) in entityData)
{
var entity = entities.GetOrNew(id);
entity.Name = name;
logEntities.Add(entity);
}
log.Entities = logEntities;
db.DbContext.AdminLog.Add(log);
}
await db.DbContext.SaveChangesAsync();
}
private async Task<DbGuardImpl> GetDbImpl()
{
await _dbReadyTask;

View File

@@ -1,10 +0,0 @@
using Robust.Shared.GameObjects;
namespace Content.Server.GameTicking.Events;
/// <summary>
/// Raised at the start of <see cref="GameTicker.StartRound"/>
/// </summary>
public class RoundStartingEvent : EntityEventArgs
{
}

View File

@@ -1,4 +1,3 @@
using System;
using Content.Server.Players;
using Content.Shared.GameTicking;
using Content.Shared.GameWindow;
@@ -36,8 +35,6 @@ namespace Content.Server.GameTicking
case SessionStatus.Connected:
{
AddPlayerToDb(args.Session.UserId.UserId);
// Always make sure the client has player data. Mind gets assigned on spawn.
if (session.Data.ContentDataUncast == null)
session.Data.ContentDataUncast = new PlayerData(session.UserId, args.Session.Name);
@@ -109,14 +106,6 @@ namespace Content.Server.GameTicking
await _prefsManager.WaitPreferencesLoaded(session);
SpawnPlayer(session);
}
async void AddPlayerToDb(Guid id)
{
if (RoundId != 0 && _runLevel != GameRunLevel.PreRoundLobby)
{
await _db.AddRoundPlayers(RoundId, id);
}
}
}
private HumanoidCharacterProfile GetPlayerProfile(IPlayerSession p)

View File

@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Content.Server.Database;
using Content.Server.GameTicking.Events;
using Content.Server.Players;
using Content.Server.Mind;
using Content.Server.Ghost;
@@ -36,8 +33,6 @@ namespace Content.Server.GameTicking
"ss14_round_length",
"Round length in seconds.");
[Dependency] private readonly IServerDbManager _db = default!;
[ViewVariables]
private TimeSpan _roundStartTimeSpan;
@@ -59,9 +54,6 @@ namespace Content.Server.GameTicking
}
}
[ViewVariables]
public int RoundId { get; private set; }
private void PreRoundSetup()
{
DefaultMap = _mapManager.CreateMap();
@@ -96,7 +88,7 @@ namespace Content.Server.GameTicking
Logger.InfoS("ticker", $"Loaded map in {timeSpan.TotalMilliseconds:N2}ms.");
}
public async void StartRound(bool force = false)
public void StartRound(bool force = false)
{
// If this game ticker is a dummy, do nothing!
if (DummyTicker)
@@ -105,12 +97,6 @@ namespace Content.Server.GameTicking
DebugTools.Assert(RunLevel == GameRunLevel.PreRoundLobby);
Logger.InfoS("ticker", "Starting round!");
var playerIds = _playersInLobby.Keys.Select(player => player.UserId.UserId).ToArray();
RoundId = await _db.AddNewRound(playerIds);
var startingEvent = new RoundStartingEvent();
RaiseLocalEvent(startingEvent);
SendServerMessage(Loc.GetString("game-ticker-start-round"));
List<IPlayerSession> readyPlayers;

View File

@@ -2,11 +2,14 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Content.Server.Atmos.Components;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Audio;
using Content.Server.Damage.Systems;
using Content.Server.Power.Components;
using Content.Server.Shuttles.Components;
using Content.Shared.Damage;
using Content.Shared.Damage.Prototypes;
using Content.Shared.FixedPoint;
using Content.Shared.Interaction;
using Content.Shared.Maps;
using Content.Shared.Physics;
@@ -18,6 +21,7 @@ using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Server.Shuttles.EntitySystems