Files
systemrevivalserver/data/entities/WorldEntities.cs
T

199 lines
5.5 KiB
C#

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace DataServer.Data.Entities;
[Table("world_regions")]
public class WorldRegion
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(64)]
public string Name { get; set; } = "";
public int ZonesPerRegionX { get; set; } = 4;
public int ZonesPerRegionY { get; set; } = 4;
public int ChunksPerZoneX { get; set; } = 8;
public int ChunksPerZoneY { get; set; } = 8;
public int ChunkWidthTiles { get; set; } = 64;
public int ChunkHeightTiles { get; set; } = 64;
public bool IsEnabled { get; set; } = true;
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
}
[Table("world_zones")]
public class WorldZone
{
[Key]
public long Id { get; set; }
public int RegionId { get; set; }
[ForeignKey(nameof(RegionId))]
public WorldRegion? Region { get; set; }
public int ZoneX { get; set; }
public int ZoneY { get; set; }
[MaxLength(64)]
public string Name { get; set; } = "";
public bool IsEnabled { get; set; } = true;
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
}
[Table("world_chunks")]
public class WorldChunk
{
[Key]
public long Id { get; set; }
public long WorldZoneId { get; set; }
[ForeignKey(nameof(WorldZoneId))]
public WorldZone? Zone { get; set; }
public int ChunkX { get; set; }
public int ChunkY { get; set; }
public long Version { get; set; } = 1;
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
}
[Table("world_chunk_tiles")]
public class WorldChunkTile
{
[Key]
public long Id { get; set; }
public long WorldChunkId { get; set; }
[ForeignKey(nameof(WorldChunkId))]
public WorldChunk? Chunk { get; set; }
public int TileX { get; set; }
public int TileY { get; set; }
public int SheetId { get; set; }
public int FrameIndex { get; set; }
public bool IsWalkable { get; set; } = true;
public bool IsInvisiblePaywall { get; set; } = false;
}
[Table("world_chunk_objects")]
public class WorldChunkObject
{
[Key]
public long Id { get; set; }
public long WorldChunkId { get; set; }
[ForeignKey(nameof(WorldChunkId))]
public WorldChunk? Chunk { get; set; }
public int ObjectDefinitionId { get; set; }
[ForeignKey(nameof(ObjectDefinitionId))]
public DungeonObjectDefinition? ObjectDefinition { get; set; }
public int TileX { get; set; }
public int TileY { get; set; }
public int Rotation { get; set; }
[MaxLength(1024)]
public string? MetadataJson { get; set; }
}
[Table("world_chunk_npc_spawns")]
public class WorldChunkNpcSpawn
{
[Key]
public long Id { get; set; }
public long WorldChunkId { get; set; }
[ForeignKey(nameof(WorldChunkId))]
public WorldChunk? Chunk { get; set; }
public int NpcDefinitionId { get; set; }
[ForeignKey(nameof(NpcDefinitionId))]
public DungeonNpcDefinition? NpcDefinition { get; set; }
public int TileX { get; set; }
public int TileY { get; set; }
public int Rotation { get; set; }
[MaxLength(1024)]
public string? MetadataJson { get; set; }
}
public sealed class WorldRegionConfig : IEntityTypeConfiguration<WorldRegion>
{
public void Configure(EntityTypeBuilder<WorldRegion> e)
{
e.HasIndex(x => x.Name).IsUnique();
}
}
public sealed class WorldZoneConfig : IEntityTypeConfiguration<WorldZone>
{
public void Configure(EntityTypeBuilder<WorldZone> e)
{
e.HasIndex(x => new { x.RegionId, x.ZoneX, x.ZoneY }).IsUnique();
e.HasOne(x => x.Region)
.WithMany()
.HasForeignKey(x => x.RegionId)
.OnDelete(DeleteBehavior.Cascade);
}
}
public sealed class WorldChunkConfig : IEntityTypeConfiguration<WorldChunk>
{
public void Configure(EntityTypeBuilder<WorldChunk> e)
{
e.HasIndex(x => new { x.WorldZoneId, x.ChunkX, x.ChunkY }).IsUnique();
e.HasOne(x => x.Zone)
.WithMany()
.HasForeignKey(x => x.WorldZoneId)
.OnDelete(DeleteBehavior.Cascade);
}
}
public sealed class WorldChunkTileConfig : IEntityTypeConfiguration<WorldChunkTile>
{
public void Configure(EntityTypeBuilder<WorldChunkTile> e)
{
e.HasIndex(x => new { x.WorldChunkId, x.TileX, x.TileY }).IsUnique();
e.HasOne(x => x.Chunk)
.WithMany()
.HasForeignKey(x => x.WorldChunkId)
.OnDelete(DeleteBehavior.Cascade);
}
}
public sealed class WorldChunkObjectConfig : IEntityTypeConfiguration<WorldChunkObject>
{
public void Configure(EntityTypeBuilder<WorldChunkObject> e)
{
e.HasIndex(x => new { x.WorldChunkId, x.TileX, x.TileY, x.ObjectDefinitionId });
e.HasOne(x => x.Chunk)
.WithMany()
.HasForeignKey(x => x.WorldChunkId)
.OnDelete(DeleteBehavior.Cascade);
}
}
public sealed class WorldChunkNpcSpawnConfig : IEntityTypeConfiguration<WorldChunkNpcSpawn>
{
public void Configure(EntityTypeBuilder<WorldChunkNpcSpawn> e)
{
e.HasIndex(x => new { x.WorldChunkId, x.TileX, x.TileY, x.NpcDefinitionId });
e.HasOne(x => x.Chunk)
.WithMany()
.HasForeignKey(x => x.WorldChunkId)
.OnDelete(DeleteBehavior.Cascade);
}
}