Persist player overworld location and add world mob spawn editing/runtime payloads
This commit is contained in:
@@ -26,6 +26,7 @@ public class IndexModel : AdminPageModel
|
||||
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; }
|
||||
@@ -82,6 +83,11 @@ public class IndexModel : AdminPageModel
|
||||
.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)
|
||||
@@ -282,6 +288,24 @@ public class IndexModel : AdminPageModel
|
||||
})
|
||||
.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,
|
||||
@@ -299,7 +323,8 @@ public class IndexModel : AdminPageModel
|
||||
},
|
||||
tiles,
|
||||
objects,
|
||||
npcs
|
||||
npcs,
|
||||
mobs
|
||||
});
|
||||
}
|
||||
|
||||
@@ -443,10 +468,12 @@ public class IndexModel : AdminPageModel
|
||||
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;
|
||||
@@ -734,6 +761,121 @@ public class IndexModel : AdminPageModel
|
||||
});
|
||||
}
|
||||
|
||||
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())
|
||||
|
||||
Reference in New Issue
Block a user