2021-03-22 12:09:25 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Data.Extensions;
|
|
|
|
|
using Data.Models;
|
|
|
|
|
using Data.Models.Client;
|
|
|
|
|
using Data.Models.Client.Stats;
|
|
|
|
|
using Data.Models.Client.Stats.Reference;
|
2021-07-08 22:12:09 -04:00
|
|
|
|
using Data.Models.Misc;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Models.Server;
|
|
|
|
|
|
|
|
|
|
namespace Data.Context
|
|
|
|
|
{
|
|
|
|
|
public abstract class DatabaseContext : DbContext
|
|
|
|
|
{
|
|
|
|
|
public DbSet<EFClient> Clients { get; set; }
|
|
|
|
|
public DbSet<EFAlias> Aliases { get; set; }
|
|
|
|
|
public DbSet<EFAliasLink> AliasLinks { get; set; }
|
|
|
|
|
public DbSet<EFPenalty> Penalties { get; set; }
|
2022-02-22 18:09:50 -05:00
|
|
|
|
public DbSet<EFPenaltyIdentifier> PenaltyIdentifiers { get; set; }
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public DbSet<EFMeta> EFMeta { get; set; }
|
|
|
|
|
public DbSet<EFChangeHistory> EFChangeHistory { get; set; }
|
|
|
|
|
|
|
|
|
|
#region STATS
|
|
|
|
|
|
|
|
|
|
public DbSet<Models.Vector3> Vector3s { get; set; }
|
|
|
|
|
public DbSet<EFACSnapshotVector3> SnapshotVector3s { get; set; }
|
|
|
|
|
public DbSet<EFACSnapshot> ACSnapshots { get; set; }
|
|
|
|
|
public DbSet<EFServer> Servers { get; set; }
|
|
|
|
|
public DbSet<EFClientKill> ClientKills { get; set; }
|
|
|
|
|
public DbSet<EFClientMessage> ClientMessages { get; set; }
|
|
|
|
|
|
|
|
|
|
public DbSet<EFServerStatistics> ServerStatistics { get; set; }
|
2022-10-23 14:29:01 -04:00
|
|
|
|
public DbSet<EFClientStatistics> ClientStatistics { get; set; }
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public DbSet<EFHitLocation> HitLocations { get; set; }
|
|
|
|
|
public DbSet<EFClientHitStatistic> HitStatistics { get; set; }
|
|
|
|
|
public DbSet<EFWeapon> Weapons { get; set; }
|
|
|
|
|
public DbSet<EFWeaponAttachment> WeaponAttachments { get; set; }
|
|
|
|
|
public DbSet<EFMap> Maps { get; set; }
|
2021-08-26 18:35:05 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
2021-07-08 22:12:09 -04:00
|
|
|
|
#region MISC
|
|
|
|
|
|
|
|
|
|
public DbSet<EFInboxMessage> InboxMessages { get; set; }
|
2021-08-26 18:35:05 -04:00
|
|
|
|
public DbSet<EFServerSnapshot> ServerSnapshots { get;set; }
|
2021-08-31 19:21:40 -04:00
|
|
|
|
public DbSet<EFClientConnectionHistory> ConnectionHistory { get; set; }
|
2021-07-08 22:12:09 -04:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
private void SetAuditColumns()
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DatabaseContext()
|
|
|
|
|
{
|
|
|
|
|
if (!MigrationExtensions.IsMigration)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected DatabaseContext(DbContextOptions options) : base(options)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
SetAuditColumns();
|
|
|
|
|
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int SaveChanges()
|
|
|
|
|
{
|
|
|
|
|
SetAuditColumns();
|
|
|
|
|
return base.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
// make network id unique
|
2022-06-15 20:37:34 -04:00
|
|
|
|
modelBuilder.Entity<EFClient>(entity =>
|
|
|
|
|
{
|
2023-01-23 22:10:33 -05:00
|
|
|
|
entity.HasIndex(client => client.NetworkId);
|
|
|
|
|
entity.HasIndex(client => client.LastConnection);
|
2022-06-15 20:37:34 -04:00
|
|
|
|
entity.HasAlternateKey(client => new
|
|
|
|
|
{
|
|
|
|
|
client.NetworkId,
|
|
|
|
|
client.GameName
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<EFPenalty>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasOne(p => p.Offender)
|
|
|
|
|
.WithMany(c => c.ReceivedPenalties)
|
|
|
|
|
.HasForeignKey(c => c.OffenderId)
|
|
|
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
|
|
|
|
|
|
|
|
entity.HasOne(p => p.Punisher)
|
|
|
|
|
.WithMany(p => p.AdministeredPenalties)
|
|
|
|
|
.HasForeignKey(c => c.PunisherId)
|
|
|
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
|
|
|
|
|
|
|
|
entity.Property(p => p.Expires)
|
|
|
|
|
.IsRequired(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<EFAliasLink>(entity =>
|
|
|
|
|
{
|
|
|
|
|
entity.HasMany(e => e.Children)
|
|
|
|
|
.WithOne(a => a.Link)
|
|
|
|
|
.HasForeignKey(k => k.LinkId)
|
|
|
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<EFAlias>(ent =>
|
|
|
|
|
{
|
|
|
|
|
ent.Property(a => a.IPAddress).IsRequired(false);
|
|
|
|
|
ent.HasIndex(a => a.IPAddress);
|
|
|
|
|
ent.Property(a => a.Name).HasMaxLength(24);
|
|
|
|
|
ent.HasIndex(a => a.Name);
|
|
|
|
|
ent.Property(_alias => _alias.SearchableName).HasMaxLength(24);
|
|
|
|
|
ent.HasIndex(_alias => _alias.SearchableName);
|
2022-01-02 17:19:48 -05:00
|
|
|
|
ent.HasIndex(_alias => new {_alias.Name, _alias.IPAddress});
|
2022-04-04 15:27:22 -04:00
|
|
|
|
ent.Property(alias => alias.SearchableIPAddress)
|
2023-02-13 09:24:45 -05:00
|
|
|
|
.HasMaxLength(255)
|
2022-04-04 15:27:22 -04:00
|
|
|
|
.HasComputedColumnSql(@"((IPAddress & 255) || '.' || ((IPAddress >> 8) & 255)) || '.' || ((IPAddress >> 16) & 255) || '.' || ((IPAddress >> 24) & 255)", stored: true);
|
|
|
|
|
ent.HasIndex(alias => alias.SearchableIPAddress);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<EFMeta>(ent =>
|
|
|
|
|
{
|
|
|
|
|
ent.HasIndex(_meta => _meta.Key);
|
|
|
|
|
ent.HasIndex(_meta => _meta.LinkedMetaId);
|
|
|
|
|
ent.HasOne(_meta => _meta.LinkedMeta)
|
|
|
|
|
.WithMany()
|
|
|
|
|
.OnDelete(DeleteBehavior.SetNull);
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-22 18:09:50 -05:00
|
|
|
|
modelBuilder.Entity<EFPenaltyIdentifier>(ent =>
|
|
|
|
|
{
|
|
|
|
|
ent.HasIndex(identifiers => identifiers.NetworkId);
|
|
|
|
|
ent.HasIndex(identifiers => identifiers.IPv4Address);
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-31 19:21:40 -04:00
|
|
|
|
modelBuilder.Entity<EFClientConnectionHistory>(ent => ent.HasIndex(history => history.CreatedDateTime));
|
|
|
|
|
|
2023-07-05 09:33:20 -04:00
|
|
|
|
modelBuilder.Entity<EFServerSnapshot>(ent => ent.HasIndex(snapshot => snapshot.CapturedAt));
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
// force full name for database conversion
|
|
|
|
|
modelBuilder.Entity<EFClient>().ToTable("EFClients");
|
|
|
|
|
modelBuilder.Entity<EFAlias>().ToTable("EFAlias");
|
|
|
|
|
modelBuilder.Entity<EFAliasLink>().ToTable("EFAliasLinks");
|
|
|
|
|
modelBuilder.Entity<EFPenalty>().ToTable("EFPenalties");
|
2022-02-22 18:09:50 -05:00
|
|
|
|
modelBuilder.Entity<EFPenaltyIdentifier>().ToTable("EFPenaltyIdentifiers");
|
2021-08-26 18:35:05 -04:00
|
|
|
|
modelBuilder.Entity<EFServerSnapshot>().ToTable(nameof(EFServerSnapshot));
|
2021-08-31 19:21:40 -04:00
|
|
|
|
modelBuilder.Entity<EFClientConnectionHistory>().ToTable(nameof(EFClientConnectionHistory));
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
|
|
|
|
Models.Configuration.StatsModelConfiguration.Configure(modelBuilder);
|
|
|
|
|
|
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-02 17:19:48 -05:00
|
|
|
|
}
|