Command Logs

This commit is contained in:
hydrant
2021-04-19 11:09:44 +02:00
parent a60f92f992
commit 3d5cf17761
6 changed files with 2117 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace ReallifeGamemode.Database.Entities.Logs
{
public class CommandLogEntry
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
public User User { get; set; }
public string Command { get; set; }
public DateTime Time { get; set; }
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,44 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace ReallifeGamemode.Database.Migrations
{
public partial class CommandLogs : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "CommandLogs",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
UserId = table.Column<int>(nullable: true),
Command = table.Column<string>(nullable: true),
Time = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_CommandLogs", x => x.Id);
table.ForeignKey(
name: "FK_CommandLogs_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_CommandLogs_UserId",
table: "CommandLogs",
column: "UserId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CommandLogs");
}
}
}

View File

@@ -797,6 +797,28 @@ namespace ReallifeGamemode.Database.Migrations
b.ToTable("BankAccountTransactionLogs");
});
modelBuilder.Entity("ReallifeGamemode.Database.Entities.Logs.CommandLogEntry", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
b.Property<string>("Command")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<DateTime>("Time")
.HasColumnType("datetime(6)");
b.Property<int?>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("CommandLogs");
});
modelBuilder.Entity("ReallifeGamemode.Database.Entities.Logs.Death", b =>
{
b.Property<int>("Id")
@@ -1856,6 +1878,13 @@ namespace ReallifeGamemode.Database.Migrations
.HasForeignKey("UserId");
});
modelBuilder.Entity("ReallifeGamemode.Database.Entities.Logs.CommandLogEntry", b =>
{
b.HasOne("ReallifeGamemode.Database.Entities.User", "User")
.WithMany()
.HasForeignKey("UserId");
});
modelBuilder.Entity("ReallifeGamemode.Database.Entities.Logs.Death", b =>
{
b.HasOne("ReallifeGamemode.Database.Entities.User", "Killer")

View File

@@ -97,6 +97,7 @@ namespace ReallifeGamemode.Database.Models
//public DbSet<Logs.Ban> BanLogs { get; set; }
public DbSet<Entities.Logs.BankAccountTransactionHistory> BankAccountTransactionLogs { get; set; }
public DbSet<Entities.Logs.Death> DeathLogs { get; set; }
public DbSet<Entities.Logs.CommandLogEntry> CommandLogs { get; set; }
//Saves
public DbSet<Entities.ATM> ATMs { get; set; }

View File

@@ -8,6 +8,8 @@ using ReallifeGamemode.Server.Types;
using ReallifeGamemode.Server.Common;
using ReallifeGamemode.Server.Log;
using Microsoft.Extensions.Logging;
using ReallifeGamemode.Database.Entities.Logs;
using ReallifeGamemode.Server.Core.Extensions;
namespace ReallifeGamemode.Server.Core.Commands
{
@@ -33,6 +35,18 @@ namespace ReallifeGamemode.Server.Core.Commands
logger.LogInformation("Player '{Name}' executed command '{command}'", player.Name, command);
using var dbContext = Main.GetDbContext();
var commandLogEntry = new CommandLogEntry()
{
Command = "/" + string.Join(' ', args),
Time = DateTime.Now,
User = player.GetUser(dbContext)
};
dbContext.CommandLogs.Add(commandLogEntry);
dbContext.SaveChanges();
if (legacyCommands.Contains(command))
{
return;