using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Newtonsoft.Json; using SCHALE.Common.Database.Models; namespace SCHALE.Common.Database { public class SCHALEContext : DbContext { public DbSet GuestAccounts { get; set; } public DbSet Accounts { get; set; } public DbSet MissionProgresses { get; set; } public DbSet Items { get; set; } public DbSet Characters { get; set; } public DbSet Equipment { get; set; } public DbSet Weapons { get; set; } public DbSet Gears { get; set; } public DbSet MemoryLobbies { get; set; } public DbSet Scenarios { get; set; } public DbSet Echelons { get; set; } public DbSet AccountTutorials { get; set; } public static SCHALEContext Create(string connectionString) => new(new DbContextOptionsBuilder() .UseSqlServer(connectionString) .Options); public SCHALEContext() { } public SCHALEContext(DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity().Property(x => x.Uid).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(x => x.ServerId).ValueGeneratedOnAdd(); modelBuilder.Entity() .HasMany(x => x.Items) .WithOne(x => x.Account) .HasForeignKey(x => x.AccountServerId) .IsRequired(); modelBuilder.Entity() .HasMany(x => x.Characters) .WithOne(x => x.Account) .HasForeignKey(x => x.AccountServerId) .IsRequired(); modelBuilder.Entity() .HasMany(x => x.MissionProgresses) .WithOne(x => x.Account) .HasForeignKey(x => x.AccountServerId) .IsRequired(); modelBuilder.Entity() .HasMany(x => x.Equipment) .WithOne(x => x.Account) .HasForeignKey(x => x.AccountServerId) .IsRequired(); modelBuilder.Entity() .HasMany(x => x.Weapons) .WithOne(x => x.Account) .HasForeignKey(x => x.AccountServerId) .IsRequired(); modelBuilder.Entity() .HasMany(x => x.Gears) .WithOne(x => x.Account) .HasForeignKey(x => x.AccountServerId) .IsRequired(); modelBuilder.Entity() .HasMany(x => x.MemoryLobbies) .WithOne(x => x.Account) .HasForeignKey(x => x.AccountServerId) .IsRequired(); modelBuilder.Entity() .HasMany(x => x.Scenarios) .WithOne(x => x.Account) .HasForeignKey(x => x.AccountServerId) .IsRequired(); modelBuilder.Entity(x => x.Property(b => b.RaidInfo).HasJsonConversion()); modelBuilder.Entity().Property(x => x.ServerId).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(x => x.ServerId).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(x => x.ServerId).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(x => x.ServerId).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(x => x.ServerId).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(x => x.ServerId).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(x => x.EquipmentSlotAndDBIds).HasJsonConversion(); modelBuilder.Entity().Property(x => x.PotentialStats).HasJsonConversion(); modelBuilder.Entity().Property(x => x.AccountServerId).ValueGeneratedNever(); modelBuilder.Entity().Property(x => x.ServerId).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(x => x.ProgressParameters).HasJsonConversion(); } } public class SCHALESqliteContext : SCHALEContext { public SCHALESqliteContext() { } public SCHALESqliteContext(DbContextOptions options) : base(options) { } public static SCHALESqliteContext Create(string connectionString) => new(new DbContextOptionsBuilder() .UseSqlite(connectionString).Options); protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("Data Source=schale.sqlite3"); } } public static class PropertyBuilderExtensions { public static PropertyBuilder HasJsonConversion(this PropertyBuilder propertyBuilder) where T : class, new() { ValueConverter converter = new ( v => JsonConvert.SerializeObject(v), v => JsonConvert.DeserializeObject(v) ?? new T() ); propertyBuilder.HasConversion(converter); propertyBuilder.Metadata.SetValueConverter(converter); return propertyBuilder; } } }