|
| 1 | +using System.Threading.Tasks; |
| 2 | + |
| 3 | +namespace SCommandLogger; |
| 4 | + |
| 5 | +public abstract class CommandLogger : ICommandLogger |
| 6 | +{ |
| 7 | + protected List<PlayerLogs> List { get; } = new(); |
| 8 | + |
| 9 | + public virtual void Clear() => List.Clear(); |
| 10 | + public virtual ICommandLoggerEntry Insert(ICommandLoggerEntry entry) |
| 11 | + { |
| 12 | + if (entry is not PlayerLogEntry e) |
| 13 | + return null; |
| 14 | + GetOrAdd(e.Executor).Entries.Add(e); |
| 15 | + return entry; |
| 16 | + } |
| 17 | + public PlayerLogs GetOrAdd(CSteamID id) |
| 18 | + { |
| 19 | + var logs = List.FirstOrDefault(x => x.Id.m_SteamID == id.m_SteamID); |
| 20 | + if(logs is null) |
| 21 | + List.Add(logs = new() { Id = id }); |
| 22 | + return logs; |
| 23 | + } |
| 24 | + public virtual void Remove(ICommandLoggerEntry entry) |
| 25 | + { |
| 26 | + if (entry is not PlayerLogEntry e) |
| 27 | + return; |
| 28 | + GetOrAdd(entry.Executor).Entries.Remove(e); |
| 29 | + } |
| 30 | + public ICommandLoggerEntry Produce(IRocketPlayer caller, IRocketCommand command, string format, string[] args) |
| 31 | + { |
| 32 | + var fullArgs = string.Join(" ", args); |
| 33 | + var id = CSteamID.Nil; |
| 34 | + if (caller is not ConsolePlayer && ulong.TryParse(caller.Id, out var uid)) |
| 35 | + id = new CSteamID(uid); |
| 36 | + var formattedMessage = format; |
| 37 | + args = args.Prepend(fullArgs).ToArray(); |
| 38 | + for (int i = 0; i < args.Length; i++) |
| 39 | + formattedMessage = formattedMessage.Replace($"{{{i}}}", args[i]); |
| 40 | + formattedMessage = formattedMessage.FormatWith(new { Name = command.Name, Id = id, Caller = caller.DisplayName, Time = DateTime.Now }); |
| 41 | + args = args.Skip(1).ToArray(); |
| 42 | + return new PlayerLogEntry(id, new CommandInfo(command.Name, args), formattedMessage); |
| 43 | + } |
| 44 | + protected virtual void OnDispose() { } |
| 45 | + public void Dispose() |
| 46 | + { |
| 47 | + OnDispose(); |
| 48 | + Clear(); |
| 49 | + } |
| 50 | +} |
0 commit comments