Add overworld region-zone-chunk data model and admin live world editor
This commit is contained in:
@@ -0,0 +1,624 @@
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GameServices.Admin.Pages.WorldEditor;
|
||||
|
||||
public class IndexModel : AdminPageModel
|
||||
{
|
||||
private readonly GameDbContext _db;
|
||||
|
||||
public IndexModel(GameDbContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public List<WorldRegion> Regions { get; set; } = new();
|
||||
public List<TileMapSheet> TileSheets { get; set; } = new();
|
||||
public List<DungeonObjectDefinition> ObjectDefinitions { get; set; } = new();
|
||||
public List<DungeonNpcDefinition> NpcDefinitions { get; set; } = new();
|
||||
|
||||
public int SelectedRegionId { get; set; }
|
||||
public int SelectedZoneX { get; set; }
|
||||
public int SelectedZoneY { get; set; }
|
||||
public int SelectedChunkX { get; set; }
|
||||
public int SelectedChunkY { get; set; }
|
||||
|
||||
public async Task OnGetAsync(int? regionId, int zoneX = 0, int zoneY = 0, int chunkX = 0, int chunkY = 0)
|
||||
{
|
||||
await EnsureDefaultRegionAsync();
|
||||
|
||||
Regions = await _db.WorldRegions
|
||||
.Where(r => r.IsEnabled)
|
||||
.OrderBy(r => r.Id)
|
||||
.ToListAsync();
|
||||
|
||||
var selected = Regions.FirstOrDefault(r => r.Id == regionId) ?? Regions.First();
|
||||
SelectedRegionId = selected.Id;
|
||||
SelectedZoneX = Math.Clamp(zoneX, 0, Math.Max(0, selected.ZonesPerRegionX - 1));
|
||||
SelectedZoneY = Math.Clamp(zoneY, 0, Math.Max(0, selected.ZonesPerRegionY - 1));
|
||||
SelectedChunkX = Math.Clamp(chunkX, 0, Math.Max(0, selected.ChunksPerZoneX - 1));
|
||||
SelectedChunkY = Math.Clamp(chunkY, 0, Math.Max(0, selected.ChunksPerZoneY - 1));
|
||||
|
||||
await EnsureZonesAsync(selected);
|
||||
|
||||
TileSheets = await _db.TileMapSheets
|
||||
.Where(s => s.IsEnabled)
|
||||
.OrderBy(s => s.Name)
|
||||
.ToListAsync();
|
||||
|
||||
ObjectDefinitions = await _db.DungeonObjects
|
||||
.Where(o => o.IsEnabled)
|
||||
.OrderBy(o => o.Name)
|
||||
.ToListAsync();
|
||||
|
||||
NpcDefinitions = await _db.DungeonNpcDefinitions
|
||||
.Where(n => n.IsEnabled)
|
||||
.OrderBy(n => n.Name)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnGetRegionAsync(int regionId)
|
||||
{
|
||||
var region = await _db.WorldRegions.FirstOrDefaultAsync(r => r.Id == regionId && r.IsEnabled);
|
||||
if (region == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = "region_not_found" }) { StatusCode = 404 };
|
||||
}
|
||||
|
||||
await EnsureZonesAsync(region);
|
||||
|
||||
var zones = await _db.WorldZones
|
||||
.Where(z => z.RegionId == region.Id && z.IsEnabled)
|
||||
.OrderBy(z => z.ZoneY).ThenBy(z => z.ZoneX)
|
||||
.ToListAsync();
|
||||
|
||||
var zoneIds = zones.Select(z => z.Id).ToList();
|
||||
var chunkCounts = await _db.WorldChunks
|
||||
.Where(c => zoneIds.Contains(c.WorldZoneId))
|
||||
.GroupBy(c => c.WorldZoneId)
|
||||
.Select(g => new { ZoneId = g.Key, Count = g.Count() })
|
||||
.ToDictionaryAsync(x => x.ZoneId, x => x.Count);
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
region = new
|
||||
{
|
||||
id = region.Id,
|
||||
name = region.Name,
|
||||
zonesX = region.ZonesPerRegionX,
|
||||
zonesY = region.ZonesPerRegionY,
|
||||
chunksPerZoneX = region.ChunksPerZoneX,
|
||||
chunksPerZoneY = region.ChunksPerZoneY,
|
||||
chunkWidthTiles = region.ChunkWidthTiles,
|
||||
chunkHeightTiles = region.ChunkHeightTiles
|
||||
},
|
||||
zones = zones.Select(z => new
|
||||
{
|
||||
id = z.Id,
|
||||
zoneX = z.ZoneX,
|
||||
zoneY = z.ZoneY,
|
||||
name = z.Name,
|
||||
editedChunkCount = chunkCounts.GetValueOrDefault(z.Id)
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnGetZoneAsync(int regionId, int zoneX, int zoneY)
|
||||
{
|
||||
var region = await _db.WorldRegions.FirstOrDefaultAsync(r => r.Id == regionId && r.IsEnabled);
|
||||
if (region == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = "region_not_found" }) { StatusCode = 404 };
|
||||
}
|
||||
|
||||
if (!IsInside(region.ZonesPerRegionX, region.ZonesPerRegionY, zoneX, zoneY))
|
||||
{
|
||||
return new JsonResult(new { success = false, error = "zone_out_of_bounds" }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
await EnsureZonesAsync(region);
|
||||
var zone = await _db.WorldZones.FirstAsync(z => z.RegionId == regionId && z.ZoneX == zoneX && z.ZoneY == zoneY);
|
||||
|
||||
var chunks = await _db.WorldChunks
|
||||
.Where(c => c.WorldZoneId == zone.Id)
|
||||
.Select(c => new { c.ChunkX, c.ChunkY, c.Version })
|
||||
.ToListAsync();
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
zone = new
|
||||
{
|
||||
id = zone.Id,
|
||||
regionId,
|
||||
zoneX,
|
||||
zoneY,
|
||||
chunksPerZoneX = region.ChunksPerZoneX,
|
||||
chunksPerZoneY = region.ChunksPerZoneY
|
||||
},
|
||||
chunks
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnGetChunkAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY)
|
||||
{
|
||||
var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
if (!locate.Success || locate.Region == null || locate.Zone == null || locate.Chunk == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
var chunk = locate.Chunk;
|
||||
var region = locate.Region;
|
||||
|
||||
var tiles = await _db.WorldChunkTiles
|
||||
.Where(t => t.WorldChunkId == chunk.Id)
|
||||
.OrderBy(t => t.TileY).ThenBy(t => t.TileX)
|
||||
.Select(t => new
|
||||
{
|
||||
id = t.Id,
|
||||
x = t.TileX,
|
||||
y = t.TileY,
|
||||
sheetId = t.SheetId,
|
||||
frameIndex = t.FrameIndex,
|
||||
isWalkable = t.IsWalkable,
|
||||
isInvisiblePaywall = t.IsInvisiblePaywall
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
var objects = await _db.WorldChunkObjects
|
||||
.Include(o => o.ObjectDefinition)
|
||||
.Where(o => o.WorldChunkId == chunk.Id)
|
||||
.OrderBy(o => o.TileY).ThenBy(o => o.TileX)
|
||||
.Select(o => new
|
||||
{
|
||||
id = o.Id,
|
||||
x = o.TileX,
|
||||
y = o.TileY,
|
||||
objectDefinitionId = o.ObjectDefinitionId,
|
||||
objectName = o.ObjectDefinition != null ? o.ObjectDefinition.Name : $"Object {o.ObjectDefinitionId}",
|
||||
rotation = o.Rotation,
|
||||
metadataJson = o.MetadataJson
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
var npcs = await _db.WorldChunkNpcSpawns
|
||||
.Include(n => n.NpcDefinition)
|
||||
.Where(n => n.WorldChunkId == chunk.Id)
|
||||
.OrderBy(n => n.TileY).ThenBy(n => n.TileX)
|
||||
.Select(n => new
|
||||
{
|
||||
id = n.Id,
|
||||
x = n.TileX,
|
||||
y = n.TileY,
|
||||
npcDefinitionId = n.NpcDefinitionId,
|
||||
npcName = n.NpcDefinition != null ? n.NpcDefinition.Name : $"NPC {n.NpcDefinitionId}",
|
||||
rotation = n.Rotation,
|
||||
metadataJson = n.MetadataJson
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
chunk = new
|
||||
{
|
||||
id = chunk.Id,
|
||||
regionId,
|
||||
zoneX,
|
||||
zoneY,
|
||||
chunkX,
|
||||
chunkY,
|
||||
version = chunk.Version,
|
||||
width = region.ChunkWidthTiles,
|
||||
height = region.ChunkHeightTiles
|
||||
},
|
||||
tiles,
|
||||
objects,
|
||||
npcs
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostUpsertTileAsync(
|
||||
int regionId,
|
||||
int zoneX,
|
||||
int zoneY,
|
||||
int chunkX,
|
||||
int chunkY,
|
||||
int x,
|
||||
int y,
|
||||
int sheetId,
|
||||
int frameIndex,
|
||||
bool isWalkable,
|
||||
bool isInvisiblePaywall)
|
||||
{
|
||||
var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
if (!locate.Success || locate.Region == null || locate.Chunk == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
if (!IsInside(locate.Region.ChunkWidthTiles, locate.Region.ChunkHeightTiles, x, y))
|
||||
{
|
||||
return new JsonResult(new { success = false, error = "tile_out_of_bounds" }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
var sheetExists = await _db.TileMapSheets.AnyAsync(s => s.Id == sheetId && s.IsEnabled);
|
||||
if (!sheetExists)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = "sheet_not_found" }) { StatusCode = 404 };
|
||||
}
|
||||
|
||||
var tile = await _db.WorldChunkTiles
|
||||
.FirstOrDefaultAsync(t => t.WorldChunkId == locate.Chunk.Id && t.TileX == x && t.TileY == y);
|
||||
|
||||
if (tile == null)
|
||||
{
|
||||
tile = new WorldChunkTile
|
||||
{
|
||||
WorldChunkId = locate.Chunk.Id,
|
||||
TileX = x,
|
||||
TileY = y
|
||||
};
|
||||
_db.WorldChunkTiles.Add(tile);
|
||||
}
|
||||
|
||||
tile.SheetId = sheetId;
|
||||
tile.FrameIndex = frameIndex;
|
||||
tile.IsWalkable = isWalkable;
|
||||
tile.IsInvisiblePaywall = isInvisiblePaywall;
|
||||
|
||||
locate.Chunk.Version++;
|
||||
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
chunkVersion = locate.Chunk.Version,
|
||||
op = new
|
||||
{
|
||||
type = "tile_upsert",
|
||||
x,
|
||||
y,
|
||||
sheetId,
|
||||
frameIndex,
|
||||
isWalkable,
|
||||
isInvisiblePaywall
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostRemoveTileAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY, int x, int y)
|
||||
{
|
||||
var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
if (!locate.Success || locate.Chunk == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
var tile = await _db.WorldChunkTiles
|
||||
.FirstOrDefaultAsync(t => t.WorldChunkId == locate.Chunk.Id && t.TileX == x && t.TileY == y);
|
||||
if (tile != null)
|
||||
{
|
||||
_db.WorldChunkTiles.Remove(tile);
|
||||
locate.Chunk.Version++;
|
||||
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
chunkVersion = locate.Chunk.Version,
|
||||
op = new { type = "tile_remove", x, y }
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostUpsertObjectAsync(
|
||||
int regionId,
|
||||
int zoneX,
|
||||
int zoneY,
|
||||
int chunkX,
|
||||
int chunkY,
|
||||
long? id,
|
||||
int objectDefinitionId,
|
||||
int x,
|
||||
int y,
|
||||
int rotation = 0,
|
||||
string? metadataJson = null)
|
||||
{
|
||||
var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
if (!locate.Success || locate.Region == null || locate.Chunk == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
if (!IsInside(locate.Region.ChunkWidthTiles, locate.Region.ChunkHeightTiles, x, y))
|
||||
{
|
||||
return new JsonResult(new { success = false, error = "tile_out_of_bounds" }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
var objectExists = await _db.DungeonObjects.AnyAsync(o => o.Id == objectDefinitionId && o.IsEnabled);
|
||||
if (!objectExists)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = "object_definition_not_found" }) { StatusCode = 404 };
|
||||
}
|
||||
|
||||
WorldChunkObject obj;
|
||||
if (id.HasValue)
|
||||
{
|
||||
obj = await _db.WorldChunkObjects.FirstOrDefaultAsync(o => o.Id == id.Value && o.WorldChunkId == locate.Chunk.Id)
|
||||
?? new WorldChunkObject { WorldChunkId = locate.Chunk.Id };
|
||||
if (obj.Id == 0)
|
||||
{
|
||||
_db.WorldChunkObjects.Add(obj);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
obj = new WorldChunkObject { WorldChunkId = locate.Chunk.Id };
|
||||
_db.WorldChunkObjects.Add(obj);
|
||||
}
|
||||
|
||||
obj.ObjectDefinitionId = objectDefinitionId;
|
||||
obj.TileX = x;
|
||||
obj.TileY = y;
|
||||
obj.Rotation = rotation;
|
||||
obj.MetadataJson = string.IsNullOrWhiteSpace(metadataJson) ? null : metadataJson.Trim();
|
||||
|
||||
locate.Chunk.Version++;
|
||||
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
chunkVersion = locate.Chunk.Version,
|
||||
op = new
|
||||
{
|
||||
type = "object_upsert",
|
||||
id = obj.Id,
|
||||
x = obj.TileX,
|
||||
y = obj.TileY,
|
||||
objectDefinitionId = obj.ObjectDefinitionId,
|
||||
rotation = obj.Rotation,
|
||||
metadataJson = obj.MetadataJson
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostRemoveObjectAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY, long id)
|
||||
{
|
||||
var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
if (!locate.Success || locate.Chunk == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
var obj = await _db.WorldChunkObjects.FirstOrDefaultAsync(o => o.Id == id && o.WorldChunkId == locate.Chunk.Id);
|
||||
if (obj != null)
|
||||
{
|
||||
_db.WorldChunkObjects.Remove(obj);
|
||||
locate.Chunk.Version++;
|
||||
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
chunkVersion = locate.Chunk.Version,
|
||||
op = new { type = "object_remove", id }
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostUpsertNpcAsync(
|
||||
int regionId,
|
||||
int zoneX,
|
||||
int zoneY,
|
||||
int chunkX,
|
||||
int chunkY,
|
||||
long? id,
|
||||
int npcDefinitionId,
|
||||
int x,
|
||||
int y,
|
||||
int rotation = 0,
|
||||
string? metadataJson = null)
|
||||
{
|
||||
var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
if (!locate.Success || locate.Region == null || locate.Chunk == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
if (!IsInside(locate.Region.ChunkWidthTiles, locate.Region.ChunkHeightTiles, x, y))
|
||||
{
|
||||
return new JsonResult(new { success = false, error = "tile_out_of_bounds" }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
var npcExists = await _db.DungeonNpcDefinitions.AnyAsync(n => n.Id == npcDefinitionId && n.IsEnabled);
|
||||
if (!npcExists)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = "npc_definition_not_found" }) { StatusCode = 404 };
|
||||
}
|
||||
|
||||
WorldChunkNpcSpawn npc;
|
||||
if (id.HasValue)
|
||||
{
|
||||
npc = await _db.WorldChunkNpcSpawns.FirstOrDefaultAsync(n => n.Id == id.Value && n.WorldChunkId == locate.Chunk.Id)
|
||||
?? new WorldChunkNpcSpawn { WorldChunkId = locate.Chunk.Id };
|
||||
if (npc.Id == 0)
|
||||
{
|
||||
_db.WorldChunkNpcSpawns.Add(npc);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
npc = new WorldChunkNpcSpawn { WorldChunkId = locate.Chunk.Id };
|
||||
_db.WorldChunkNpcSpawns.Add(npc);
|
||||
}
|
||||
|
||||
npc.NpcDefinitionId = npcDefinitionId;
|
||||
npc.TileX = x;
|
||||
npc.TileY = y;
|
||||
npc.Rotation = rotation;
|
||||
npc.MetadataJson = string.IsNullOrWhiteSpace(metadataJson) ? null : metadataJson.Trim();
|
||||
|
||||
locate.Chunk.Version++;
|
||||
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
chunkVersion = locate.Chunk.Version,
|
||||
op = new
|
||||
{
|
||||
type = "npc_upsert",
|
||||
id = npc.Id,
|
||||
x = npc.TileX,
|
||||
y = npc.TileY,
|
||||
npcDefinitionId = npc.NpcDefinitionId,
|
||||
rotation = npc.Rotation,
|
||||
metadataJson = npc.MetadataJson
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostRemoveNpcAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY, long id)
|
||||
{
|
||||
var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
if (!locate.Success || locate.Chunk == null)
|
||||
{
|
||||
return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 };
|
||||
}
|
||||
|
||||
var npc = await _db.WorldChunkNpcSpawns.FirstOrDefaultAsync(n => n.Id == id && n.WorldChunkId == locate.Chunk.Id);
|
||||
if (npc != null)
|
||||
{
|
||||
_db.WorldChunkNpcSpawns.Remove(npc);
|
||||
locate.Chunk.Version++;
|
||||
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return new JsonResult(new
|
||||
{
|
||||
success = true,
|
||||
chunkVersion = locate.Chunk.Version,
|
||||
op = new { type = "npc_remove", id }
|
||||
});
|
||||
}
|
||||
|
||||
private async Task EnsureDefaultRegionAsync()
|
||||
{
|
||||
if (await _db.WorldRegions.AnyAsync())
|
||||
return;
|
||||
|
||||
_db.WorldRegions.Add(new WorldRegion
|
||||
{
|
||||
Name = "Default Region",
|
||||
ZonesPerRegionX = 4,
|
||||
ZonesPerRegionY = 4,
|
||||
ChunksPerZoneX = 8,
|
||||
ChunksPerZoneY = 8,
|
||||
ChunkWidthTiles = 64,
|
||||
ChunkHeightTiles = 64,
|
||||
IsEnabled = true,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
});
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private async Task EnsureZonesAsync(WorldRegion region)
|
||||
{
|
||||
var existing = await _db.WorldZones
|
||||
.Where(z => z.RegionId == region.Id)
|
||||
.Select(z => new { z.ZoneX, z.ZoneY })
|
||||
.ToListAsync();
|
||||
|
||||
var existingSet = existing.Select(z => $"{z.ZoneX}:{z.ZoneY}").ToHashSet();
|
||||
var now = DateTime.UtcNow;
|
||||
var added = false;
|
||||
for (var y = 0; y < region.ZonesPerRegionY; y++)
|
||||
{
|
||||
for (var x = 0; x < region.ZonesPerRegionX; x++)
|
||||
{
|
||||
if (existingSet.Contains($"{x}:{y}"))
|
||||
continue;
|
||||
|
||||
_db.WorldZones.Add(new WorldZone
|
||||
{
|
||||
RegionId = region.Id,
|
||||
ZoneX = x,
|
||||
ZoneY = y,
|
||||
Name = $"Zone {x},{y}",
|
||||
IsEnabled = true,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now
|
||||
});
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (added)
|
||||
{
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<(bool Success, string? Error, WorldRegion? Region, WorldZone? Zone, WorldChunk? Chunk)> GetOrCreateChunkAsync(
|
||||
int regionId,
|
||||
int zoneX,
|
||||
int zoneY,
|
||||
int chunkX,
|
||||
int chunkY)
|
||||
{
|
||||
var region = await _db.WorldRegions.FirstOrDefaultAsync(r => r.Id == regionId && r.IsEnabled);
|
||||
if (region == null)
|
||||
return (false, "region_not_found", null, null, null);
|
||||
|
||||
if (!IsInside(region.ZonesPerRegionX, region.ZonesPerRegionY, zoneX, zoneY))
|
||||
return (false, "zone_out_of_bounds", region, null, null);
|
||||
|
||||
if (!IsInside(region.ChunksPerZoneX, region.ChunksPerZoneY, chunkX, chunkY))
|
||||
return (false, "chunk_out_of_bounds", region, null, null);
|
||||
|
||||
await EnsureZonesAsync(region);
|
||||
|
||||
var zone = await _db.WorldZones
|
||||
.FirstOrDefaultAsync(z => z.RegionId == regionId && z.ZoneX == zoneX && z.ZoneY == zoneY && z.IsEnabled);
|
||||
if (zone == null)
|
||||
return (false, "zone_not_found", region, null, null);
|
||||
|
||||
var chunk = await _db.WorldChunks
|
||||
.FirstOrDefaultAsync(c => c.WorldZoneId == zone.Id && c.ChunkX == chunkX && c.ChunkY == chunkY);
|
||||
|
||||
if (chunk == null)
|
||||
{
|
||||
chunk = new WorldChunk
|
||||
{
|
||||
WorldZoneId = zone.Id,
|
||||
ChunkX = chunkX,
|
||||
ChunkY = chunkY,
|
||||
Version = 1,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
};
|
||||
_db.WorldChunks.Add(chunk);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return (true, null, region, zone, chunk);
|
||||
}
|
||||
|
||||
private static bool IsInside(int width, int height, int x, int y)
|
||||
{
|
||||
return x >= 0 && y >= 0 && x < width && y < height;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user