1022 lines
36 KiB
C#
1022 lines
36 KiB
C#
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using GameServices;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Text;
|
|
|
|
namespace GameServices.Admin.Pages.WorldEditor;
|
|
|
|
public class IndexModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
private readonly WorldEditDeltaHub _deltaHub;
|
|
private readonly IConfiguration _config;
|
|
|
|
public IndexModel(GameDbContext db, WorldEditDeltaHub deltaHub, IConfiguration config)
|
|
{
|
|
_db = db;
|
|
_deltaHub = deltaHub;
|
|
_config = config;
|
|
}
|
|
|
|
public List<WorldRegion> Regions { get; set; } = new();
|
|
public List<TileMapSheet> TileSheets { get; set; } = new();
|
|
public List<TileDefinition> TileDefinitions { get; set; } = new();
|
|
public List<DungeonObjectDefinition> ObjectDefinitions { get; set; } = new();
|
|
public List<DungeonNpcDefinition> NpcDefinitions { get; set; } = new();
|
|
public List<DungeonMobDefinition> MobDefinitions { 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 string CdnBaseUrl { get; set; } = "https://assets.aeternalisgames.com/downloads";
|
|
|
|
public async Task OnGetAsync(int? regionId, int zoneX = 0, int zoneY = 0, int chunkX = 0, int chunkY = 0)
|
|
{
|
|
CdnBaseUrl = NormalizeCdnBaseUrl(_config["Cdn:BaseUrl"]);
|
|
|
|
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();
|
|
|
|
var enabledSheetIds = TileSheets.Select(s => s.Id).ToList();
|
|
TileDefinitions = await _db.TileDefinitions
|
|
.Where(t => t.IsEnabled && enabledSheetIds.Contains(t.TileMapSheetId))
|
|
.Select(t => new TileDefinition
|
|
{
|
|
Id = t.Id,
|
|
TileMapSheetId = t.TileMapSheetId,
|
|
FrameIndex = t.FrameIndex,
|
|
Name = t.Name,
|
|
Type = t.Type,
|
|
IsWalkable = t.IsWalkable
|
|
})
|
|
.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();
|
|
|
|
MobDefinitions = await _db.DungeonMobDefinitions
|
|
.Where(m => m.IsEnabled)
|
|
.OrderBy(m => m.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> OnPostCreateRegionAsync(
|
|
string name,
|
|
int zonesPerRegionX = 4,
|
|
int zonesPerRegionY = 4,
|
|
int chunksPerZoneX = 8,
|
|
int chunksPerZoneY = 8,
|
|
int chunkWidthTiles = 64,
|
|
int chunkHeightTiles = 64)
|
|
{
|
|
name = (name ?? string.Empty).Trim();
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
{
|
|
return new JsonResult(new { success = false, error = "name_required" }) { StatusCode = 400 };
|
|
}
|
|
|
|
var exists = await _db.WorldRegions.AnyAsync(r => r.IsEnabled && r.Name == name);
|
|
if (exists)
|
|
{
|
|
return new JsonResult(new { success = false, error = "region_name_exists" }) { StatusCode = 409 };
|
|
}
|
|
|
|
var region = new WorldRegion
|
|
{
|
|
Name = name,
|
|
ZonesPerRegionX = Math.Clamp(zonesPerRegionX, 1, 32),
|
|
ZonesPerRegionY = Math.Clamp(zonesPerRegionY, 1, 32),
|
|
ChunksPerZoneX = Math.Clamp(chunksPerZoneX, 1, 32),
|
|
ChunksPerZoneY = Math.Clamp(chunksPerZoneY, 1, 32),
|
|
ChunkWidthTiles = Math.Clamp(chunkWidthTiles, 8, 512),
|
|
ChunkHeightTiles = Math.Clamp(chunkHeightTiles, 8, 512),
|
|
IsEnabled = true,
|
|
CreatedAt = DateTime.UtcNow,
|
|
UpdatedAt = DateTime.UtcNow
|
|
};
|
|
|
|
_db.WorldRegions.Add(region);
|
|
await _db.SaveChangesAsync();
|
|
await EnsureZonesAsync(region);
|
|
|
|
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
|
|
}
|
|
});
|
|
}
|
|
|
|
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 && z.ZoneZ == 0);
|
|
|
|
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}",
|
|
interactionType = o.ObjectDefinition != null ? o.ObjectDefinition.InteractionType.ToString() : null,
|
|
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();
|
|
|
|
var mobs = await _db.WorldChunkMobSpawns
|
|
.Include(m => m.MobDefinition)
|
|
.Where(m => m.WorldChunkId == chunk.Id)
|
|
.OrderBy(m => m.TileY).ThenBy(m => m.TileX)
|
|
.Select(m => new
|
|
{
|
|
id = m.Id,
|
|
x = m.TileX,
|
|
y = m.TileY,
|
|
mobDefinitionId = m.MobDefinitionId,
|
|
mobName = m.MobDefinition != null ? m.MobDefinition.Name : $"Mob {m.MobDefinitionId}",
|
|
rotation = m.Rotation,
|
|
spawnCount = m.SpawnCount,
|
|
respawnSeconds = m.RespawnSeconds,
|
|
metadataJson = m.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,
|
|
mobs
|
|
});
|
|
}
|
|
|
|
public async Task<IActionResult> OnGetStreamAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY, CancellationToken ct)
|
|
{
|
|
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 key = ChunkKey(regionId, zoneX, zoneY, chunkX, chunkY);
|
|
using var subscription = _deltaHub.Subscribe(key);
|
|
|
|
Response.Headers["Content-Type"] = "text/event-stream";
|
|
Response.Headers["Cache-Control"] = "no-cache";
|
|
Response.Headers["Connection"] = "keep-alive";
|
|
Response.Headers["X-Accel-Buffering"] = "no";
|
|
|
|
while (!ct.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
var readTask = subscription.Reader.ReadAsync(ct).AsTask();
|
|
var heartbeatTask = Task.Delay(TimeSpan.FromSeconds(15), ct);
|
|
var completed = await Task.WhenAny(readTask, heartbeatTask);
|
|
|
|
if (completed == readTask)
|
|
{
|
|
var json = await readTask;
|
|
var data = Encoding.UTF8.GetBytes($"data: {json}\n\n");
|
|
await Response.Body.WriteAsync(data, ct);
|
|
await Response.Body.FlushAsync(ct);
|
|
}
|
|
else
|
|
{
|
|
var heartbeat = Encoding.UTF8.GetBytes(": ping\n\n");
|
|
await Response.Body.WriteAsync(heartbeat, ct);
|
|
await Response.Body.FlushAsync(ct);
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
return new EmptyResult();
|
|
}
|
|
|
|
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;
|
|
var tileDef = await _db.TileDefinitions
|
|
.AsNoTracking()
|
|
.FirstOrDefaultAsync(t => t.IsEnabled && t.TileMapSheetId == sheetId && t.FrameIndex == frameIndex);
|
|
var effectiveWalkable = tileDef?.IsWalkable ?? isWalkable;
|
|
tile.IsWalkable = effectiveWalkable;
|
|
tile.IsInvisiblePaywall = isInvisiblePaywall;
|
|
|
|
var fromVersion = locate.Chunk.Version;
|
|
locate.Chunk.Version++;
|
|
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
|
await _db.SaveChangesAsync();
|
|
|
|
var op = new
|
|
{
|
|
type = "tile_upsert",
|
|
x,
|
|
y,
|
|
sheetId,
|
|
frameIndex,
|
|
isWalkable = effectiveWalkable,
|
|
isInvisiblePaywall
|
|
};
|
|
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
|
|
|
return new JsonResult(new
|
|
{
|
|
success = true,
|
|
chunkVersion = locate.Chunk.Version,
|
|
fromVersion,
|
|
toVersion = locate.Chunk.Version,
|
|
op
|
|
});
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostClearChunkAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY)
|
|
{
|
|
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 fromVersion = locate.Chunk.Version;
|
|
|
|
var tiles = _db.WorldChunkTiles.Where(t => t.WorldChunkId == locate.Chunk.Id);
|
|
var objects = _db.WorldChunkObjects.Where(o => o.WorldChunkId == locate.Chunk.Id);
|
|
var npcs = _db.WorldChunkNpcSpawns.Where(n => n.WorldChunkId == locate.Chunk.Id);
|
|
var mobs = _db.WorldChunkMobSpawns.Where(m => m.WorldChunkId == locate.Chunk.Id);
|
|
|
|
_db.WorldChunkTiles.RemoveRange(tiles);
|
|
_db.WorldChunkObjects.RemoveRange(objects);
|
|
_db.WorldChunkNpcSpawns.RemoveRange(npcs);
|
|
_db.WorldChunkMobSpawns.RemoveRange(mobs);
|
|
|
|
locate.Chunk.Version++;
|
|
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
|
await _db.SaveChangesAsync();
|
|
|
|
var op = new { type = "chunk_clear" };
|
|
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
|
|
|
return new JsonResult(new
|
|
{
|
|
success = true,
|
|
chunkVersion = locate.Chunk.Version,
|
|
fromVersion,
|
|
toVersion = locate.Chunk.Version,
|
|
op
|
|
});
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostDeleteChunkAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY)
|
|
{
|
|
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 fromVersion = locate.Chunk.Version;
|
|
_db.WorldChunks.Remove(locate.Chunk);
|
|
await _db.SaveChangesAsync();
|
|
|
|
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, fromVersion + 1, new { type = "chunk_deleted" });
|
|
|
|
return new JsonResult(new
|
|
{
|
|
success = true,
|
|
op = new { type = "chunk_deleted" }
|
|
});
|
|
}
|
|
|
|
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)
|
|
{
|
|
var fromVersion = locate.Chunk.Version;
|
|
_db.WorldChunkTiles.Remove(tile);
|
|
locate.Chunk.Version++;
|
|
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
|
await _db.SaveChangesAsync();
|
|
|
|
var op = new { type = "tile_remove", x, y };
|
|
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
|
}
|
|
|
|
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 objectDef = await _db.DungeonObjects.FirstOrDefaultAsync(o => o.Id == objectDefinitionId && o.IsEnabled);
|
|
if (objectDef == null)
|
|
{
|
|
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();
|
|
|
|
var fromVersion = locate.Chunk.Version;
|
|
locate.Chunk.Version++;
|
|
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
|
await _db.SaveChangesAsync();
|
|
|
|
var op = new
|
|
{
|
|
type = "object_upsert",
|
|
id = obj.Id,
|
|
x = obj.TileX,
|
|
y = obj.TileY,
|
|
objectDefinitionId = obj.ObjectDefinitionId,
|
|
objectName = objectDef.Name,
|
|
interactionType = objectDef.InteractionType.ToString(),
|
|
rotation = obj.Rotation,
|
|
metadataJson = obj.MetadataJson
|
|
};
|
|
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
|
|
|
return new JsonResult(new
|
|
{
|
|
success = true,
|
|
chunkVersion = locate.Chunk.Version,
|
|
fromVersion,
|
|
toVersion = locate.Chunk.Version,
|
|
op
|
|
});
|
|
}
|
|
|
|
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)
|
|
{
|
|
var fromVersion = locate.Chunk.Version;
|
|
_db.WorldChunkObjects.Remove(obj);
|
|
locate.Chunk.Version++;
|
|
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
|
await _db.SaveChangesAsync();
|
|
|
|
var op = new { type = "object_remove", id };
|
|
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
|
}
|
|
|
|
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 npcDef = await _db.DungeonNpcDefinitions.FirstOrDefaultAsync(n => n.Id == npcDefinitionId && n.IsEnabled);
|
|
if (npcDef == null)
|
|
{
|
|
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();
|
|
|
|
var fromVersion = locate.Chunk.Version;
|
|
locate.Chunk.Version++;
|
|
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
|
await _db.SaveChangesAsync();
|
|
|
|
var op = new
|
|
{
|
|
type = "npc_upsert",
|
|
id = npc.Id,
|
|
x = npc.TileX,
|
|
y = npc.TileY,
|
|
npcDefinitionId = npc.NpcDefinitionId,
|
|
npcName = npcDef.Name,
|
|
rotation = npc.Rotation,
|
|
metadataJson = npc.MetadataJson
|
|
};
|
|
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
|
|
|
return new JsonResult(new
|
|
{
|
|
success = true,
|
|
chunkVersion = locate.Chunk.Version,
|
|
fromVersion,
|
|
toVersion = locate.Chunk.Version,
|
|
op
|
|
});
|
|
}
|
|
|
|
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)
|
|
{
|
|
var fromVersion = locate.Chunk.Version;
|
|
_db.WorldChunkNpcSpawns.Remove(npc);
|
|
locate.Chunk.Version++;
|
|
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
|
await _db.SaveChangesAsync();
|
|
|
|
var op = new { type = "npc_remove", id };
|
|
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
|
}
|
|
|
|
return new JsonResult(new
|
|
{
|
|
success = true,
|
|
chunkVersion = locate.Chunk.Version,
|
|
op = new { type = "npc_remove", id }
|
|
});
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostUpsertMobAsync(
|
|
int regionId,
|
|
int zoneX,
|
|
int zoneY,
|
|
int chunkX,
|
|
int chunkY,
|
|
long? id,
|
|
int mobDefinitionId,
|
|
int x,
|
|
int y,
|
|
int rotation = 0,
|
|
int spawnCount = 1,
|
|
float respawnSeconds = 30,
|
|
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 mobDef = await _db.DungeonMobDefinitions.FirstOrDefaultAsync(m => m.Id == mobDefinitionId && m.IsEnabled);
|
|
if (mobDef == null)
|
|
{
|
|
return new JsonResult(new { success = false, error = "mob_definition_not_found" }) { StatusCode = 404 };
|
|
}
|
|
|
|
WorldChunkMobSpawn mob;
|
|
if (id.HasValue)
|
|
{
|
|
mob = await _db.WorldChunkMobSpawns.FirstOrDefaultAsync(m => m.Id == id.Value && m.WorldChunkId == locate.Chunk.Id)
|
|
?? new WorldChunkMobSpawn { WorldChunkId = locate.Chunk.Id };
|
|
if (mob.Id == 0)
|
|
{
|
|
_db.WorldChunkMobSpawns.Add(mob);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
mob = new WorldChunkMobSpawn { WorldChunkId = locate.Chunk.Id };
|
|
_db.WorldChunkMobSpawns.Add(mob);
|
|
}
|
|
|
|
mob.MobDefinitionId = mobDefinitionId;
|
|
mob.TileX = x;
|
|
mob.TileY = y;
|
|
mob.Rotation = rotation;
|
|
mob.SpawnCount = Math.Clamp(spawnCount, 1, 100);
|
|
mob.RespawnSeconds = Math.Clamp(respawnSeconds, 0f, 86400f);
|
|
mob.MetadataJson = string.IsNullOrWhiteSpace(metadataJson) ? null : metadataJson.Trim();
|
|
|
|
var fromVersion = locate.Chunk.Version;
|
|
locate.Chunk.Version++;
|
|
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
|
await _db.SaveChangesAsync();
|
|
|
|
var op = new
|
|
{
|
|
type = "mob_upsert",
|
|
id = mob.Id,
|
|
x = mob.TileX,
|
|
y = mob.TileY,
|
|
mobDefinitionId = mob.MobDefinitionId,
|
|
mobName = mobDef.Name,
|
|
rotation = mob.Rotation,
|
|
spawnCount = mob.SpawnCount,
|
|
respawnSeconds = mob.RespawnSeconds,
|
|
metadataJson = mob.MetadataJson
|
|
};
|
|
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
|
|
|
return new JsonResult(new
|
|
{
|
|
success = true,
|
|
chunkVersion = locate.Chunk.Version,
|
|
fromVersion,
|
|
toVersion = locate.Chunk.Version,
|
|
op
|
|
});
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostRemoveMobAsync(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 mob = await _db.WorldChunkMobSpawns.FirstOrDefaultAsync(m => m.Id == id && m.WorldChunkId == locate.Chunk.Id);
|
|
if (mob != null)
|
|
{
|
|
var fromVersion = locate.Chunk.Version;
|
|
_db.WorldChunkMobSpawns.Remove(mob);
|
|
locate.Chunk.Version++;
|
|
locate.Chunk.UpdatedAt = DateTime.UtcNow;
|
|
await _db.SaveChangesAsync();
|
|
|
|
var op = new { type = "mob_remove", id };
|
|
PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op);
|
|
}
|
|
|
|
return new JsonResult(new
|
|
{
|
|
success = true,
|
|
chunkVersion = locate.Chunk.Version,
|
|
op = new { type = "mob_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)
|
|
{
|
|
// Only auto-create Z=0 (ground level) zones
|
|
var existing = await _db.WorldZones
|
|
.Where(z => z.RegionId == region.Id && z.ZoneZ == 0)
|
|
.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.ZoneZ == 0 && 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;
|
|
}
|
|
|
|
private void PublishDelta(int regionId, int zoneX, int zoneY, int chunkX, int chunkY, long fromVersion, long toVersion, object op, int zoneZ = 0)
|
|
{
|
|
_deltaHub.Publish(
|
|
ChunkKey(regionId, zoneX, zoneY, chunkX, chunkY, zoneZ),
|
|
new
|
|
{
|
|
kind = "chunk_delta",
|
|
regionId,
|
|
zoneX,
|
|
zoneY,
|
|
zoneZ,
|
|
chunkX,
|
|
chunkY,
|
|
fromVersion,
|
|
toVersion,
|
|
op
|
|
});
|
|
}
|
|
|
|
private static string ChunkKey(int regionId, int zoneX, int zoneY, int chunkX, int chunkY, int zoneZ = 0)
|
|
=> $"{regionId}:{zoneX}:{zoneY}:{zoneZ}:{chunkX}:{chunkY}";
|
|
|
|
private static string NormalizeCdnBaseUrl(string? baseUrl)
|
|
{
|
|
baseUrl = (baseUrl ?? "https://assets.aeternalisgames.com").Trim().TrimEnd('/');
|
|
if (!baseUrl.EndsWith("/downloads", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
baseUrl += "/downloads";
|
|
}
|
|
return baseUrl;
|
|
}
|
|
}
|