2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-07-02 00:43:42 -04:00

Fix issues with Journal handling

Implement basic database
Handle startup events
only send events after the most recent LoadGame
This commit is contained in:
2024-05-25 16:17:36 +10:00
parent efd0b3e0c0
commit 579b2b115d
24 changed files with 571 additions and 102 deletions

View File

@ -0,0 +1,17 @@
namespace Pulsar.Context.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Observatory.Framework.Files.Journal.StationServices;
public class EngineerProgressConfiguration : IEntityTypeConfiguration<EngineerProgress>
{
public void Configure(EntityTypeBuilder<EngineerProgress> builder)
{
builder.HasKey(x => x.Timestamp);
builder.OwnsMany(x => x.Engineers, b =>
{
b.ToJson();
});
}
}

View File

@ -0,0 +1,13 @@
namespace Pulsar.Context.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Observatory.Framework.Files.Journal.Startup;
public class LoadGameConfiguration : IEntityTypeConfiguration<LoadGame>
{
public void Configure(EntityTypeBuilder<LoadGame> builder)
{
builder.HasKey(j => j.Timestamp);
}
}

View File

@ -0,0 +1,25 @@
namespace Pulsar.Context.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Observatory.Framework.Files.Journal.Startup;
public class MaterialsConfiguration : IEntityTypeConfiguration<Materials>
{
public void Configure(EntityTypeBuilder<Materials> builder)
{
builder.HasKey(x => x.Timestamp);
builder.OwnsMany(x => x.Raw, b =>
{
b.ToJson();
});
builder.OwnsMany(x => x.Encoded, b =>
{
b.ToJson();
});
builder.OwnsMany(x => x.Manufactured, b =>
{
b.ToJson();
});
}
}

View File

@ -0,0 +1,13 @@
namespace Pulsar.Context.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Observatory.Framework.Files.Journal.Startup;
public class ProgressConfiguration : IEntityTypeConfiguration<Progress>
{
public void Configure(EntityTypeBuilder<Progress> builder)
{
builder.Ignore(x => x.AdditionalProperties);
}
}

View File

@ -0,0 +1,14 @@
namespace Pulsar.Context.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Observatory.Framework.Files.Journal.Startup;
public class RanksConfiguration : IEntityTypeConfiguration<Rank>
{
public void Configure(EntityTypeBuilder<Rank> builder)
{
builder.Ignore(x => x.AdditionalProperties);
builder.HasKey(x => x.Timestamp);
}
}

View File

@ -0,0 +1,13 @@
namespace Pulsar.Context.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Observatory.Framework.Files.Journal.Startup;
public class ReputationConfiguration : IEntityTypeConfiguration<Reputation>
{
public void Configure(EntityTypeBuilder<Reputation> builder)
{
builder.HasKey(x => x.Timestamp);
}
}

View File

@ -0,0 +1,83 @@
using Observatory.Framework.Files.ParameterTypes;
namespace Pulsar.Context.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Observatory.Framework.Files.Journal.Startup;
public class StatisticsConfiguration : IEntityTypeConfiguration<Statistics>
{
public void Configure(EntityTypeBuilder<Statistics> builder)
{
builder.HasKey(x => x.Timestamp);
builder.OwnsOne(x => x.BankAccount, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Combat, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Crime, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Smuggling, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Trading, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Mining, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Exploration, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Passengers, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.SearchAndRescue, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Crafting, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Crew, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Multicrew, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Thargoid, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.MaterialTrader, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.CQC, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.FleetCarrier, b =>
{
b.ToJson();
});
builder.OwnsOne(x => x.Exobiology, b =>
{
b.ToJson();
});
}
}

View File

@ -0,0 +1,72 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Observatory.Framework.Files.Journal;
using Observatory.Framework.Files.Journal.Startup;
using Observatory.Framework.Files.Journal.StationServices;
/// <summary>
/// An in-memory database context for Pulsar.
/// </summary>
public class PulsarContext : DbContext
{
public SqliteConnection Connection { get; private set; }
// Start of game events:
/**
* Materials
Rank
Progress
Reputation
EngineerProgress
LoadGame
--Some time later--
Statistics
*/
public DbSet<Materials> Materials { get; set; }
public DbSet<Rank> Ranks { get; set; }
public DbSet<Progress> Progress { get; set; }
public DbSet<Reputation> Reputations { get; set; }
public DbSet<EngineerProgress> EngineerProgress { get; set; }
public DbSet<LoadGame> LoadGames { get; set; }
public DbSet<Statistics> Statistics { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Connection = new SqliteConnection("Data Source=Journals.sqlite");
optionsBuilder.UseSqlite(Connection);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(PulsarContext).Assembly);
base.OnModelCreating(modelBuilder);
if (Database.ProviderName != "Microsoft.EntityFrameworkCore.Sqlite") return;
// SQLite does not have proper support for DateTimeOffset via Entity Framework Core, see the limitations
// here: https://docs.microsoft.com/en-us/ef/core/providers/sqlite/limitations#query-limitations
// To work around this, when the Sqlite database provider is used, all model properties of type DateTimeOffset
// use the DateTimeOffsetToBinaryConverter
// Based on: https://github.com/aspnet/EntityFrameworkCore/issues/10784#issuecomment-415769754
// This only supports millisecond precision, but should be sufficient for most use cases.
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var properties = entityType.ClrType.GetProperties().Where(p => p.PropertyType == typeof(DateTimeOffset)
|| p.PropertyType == typeof(DateTimeOffset?));
foreach (var property in properties)
{
modelBuilder
.Entity(entityType.Name)
.Property(property.Name)
.HasConversion(new DateTimeOffsetToBinaryConverter());
}
}
}
public override void Dispose()
{
Connection.Dispose();
base.Dispose();
}
}