2024-04-18 07:12:10 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.ValueGeneration;
|
|
|
|
|
using MongoDB.EntityFrameworkCore.Extensions;
|
|
|
|
|
using SCHALE.Common.Database.Models;
|
|
|
|
|
|
|
|
|
|
namespace SCHALE.Common.Database
|
|
|
|
|
{
|
|
|
|
|
public class SCHALEContext : DbContext
|
|
|
|
|
{
|
2024-04-25 03:50:09 +00:00
|
|
|
|
public DbSet<GuestAccount> GuestAccounts { get; set; }
|
2024-04-28 01:41:38 +00:00
|
|
|
|
public DbSet<AccountDB> Accounts { get; set; }
|
|
|
|
|
public DbSet<MissionProgressDB> MissionProgresses { get; set; }
|
2024-04-18 07:12:10 +00:00
|
|
|
|
public DbSet<Counter> Counters { get; set; }
|
|
|
|
|
|
|
|
|
|
public SCHALEContext(DbContextOptions<SCHALEContext> options) : base(options)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
base.OnModelCreating(modelBuilder);
|
2024-04-25 03:50:09 +00:00
|
|
|
|
|
|
|
|
|
modelBuilder.Entity<GuestAccount>().Property(x => x.Uid).HasValueGenerator<GuestAccountAutoIncrementValueGenerator>();
|
|
|
|
|
modelBuilder.Entity<GuestAccount>().ToCollection("guest_accounts");
|
|
|
|
|
|
2024-04-28 01:41:38 +00:00
|
|
|
|
modelBuilder.Entity<AccountDB>().Property(x => x.ServerId).HasValueGenerator<AccountAutoIncrementValueGenerator>();
|
|
|
|
|
modelBuilder.Entity<AccountDB>().ToCollection("accounts");
|
2024-04-26 15:50:30 +00:00
|
|
|
|
|
2024-04-28 01:41:38 +00:00
|
|
|
|
modelBuilder.Entity<MissionProgressDB>().Property(x => x.ServerId).HasValueGenerator<MissionProgressAutoIncrementValueGenerator>();
|
|
|
|
|
modelBuilder.Entity<MissionProgressDB>().ToCollection("mission_progresses");
|
2024-04-25 03:50:09 +00:00
|
|
|
|
|
2024-04-18 07:12:10 +00:00
|
|
|
|
modelBuilder.Entity<Counter>().ToCollection("counters");
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-28 01:41:38 +00:00
|
|
|
|
private class AccountAutoIncrementValueGenerator : AutoIncrementValueGenerator
|
|
|
|
|
{
|
|
|
|
|
protected override string Collection => "account";
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 03:50:09 +00:00
|
|
|
|
private class GuestAccountAutoIncrementValueGenerator : AutoIncrementValueGenerator
|
|
|
|
|
{
|
|
|
|
|
protected override string Collection => "guest_account";
|
|
|
|
|
}
|
2024-04-18 07:12:10 +00:00
|
|
|
|
|
2024-04-28 01:41:38 +00:00
|
|
|
|
private class MissionProgressAutoIncrementValueGenerator : AutoIncrementValueGenerator
|
|
|
|
|
{
|
|
|
|
|
protected override string Collection => "mission_progress";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private abstract class AutoIncrementValueGenerator : ValueGenerator<long>
|
2024-04-18 07:12:10 +00:00
|
|
|
|
{
|
2024-04-25 03:50:09 +00:00
|
|
|
|
protected abstract string Collection { get; }
|
|
|
|
|
public override bool GeneratesTemporaryValues => false;
|
|
|
|
|
|
2024-04-28 01:41:38 +00:00
|
|
|
|
public override long Next(EntityEntry entry)
|
2024-04-18 07:12:10 +00:00
|
|
|
|
{
|
2024-04-25 03:50:09 +00:00
|
|
|
|
if (entry.Context is not SCHALEContext)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException($"{nameof(AutoIncrementValueGenerator)} is only implemented for {nameof(SCHALEContext)}");
|
|
|
|
|
}
|
2024-04-18 07:12:10 +00:00
|
|
|
|
|
2024-04-25 03:50:09 +00:00
|
|
|
|
var context = ((SCHALEContext)entry.Context);
|
|
|
|
|
var counter = context.Counters.SingleOrDefault(x => x.Id == Collection);
|
2024-04-18 07:12:10 +00:00
|
|
|
|
|
2024-04-25 03:50:09 +00:00
|
|
|
|
if (counter is null)
|
|
|
|
|
{
|
|
|
|
|
counter = new Counter() { Id = Collection, Seq = 0 };
|
|
|
|
|
context.Add(counter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
counter.Seq++;
|
2024-04-20 05:24:36 +00:00
|
|
|
|
context.SaveChanges();
|
2024-04-25 03:50:09 +00:00
|
|
|
|
|
|
|
|
|
return counter.Seq;
|
2024-04-18 07:12:10 +00:00
|
|
|
|
}
|
2024-04-25 03:50:09 +00:00
|
|
|
|
}
|
2024-04-18 07:12:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|