DungeonBuilder UI Attempt
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Server.Services.Admin.Pages.DungeonBuilder;
|
||||
|
||||
public class ObjectEditModel : AdminPageModel
|
||||
{
|
||||
private readonly GameDbContext _db;
|
||||
private readonly IConfiguration _config;
|
||||
|
||||
public ObjectEditModel(GameDbContext db, IConfiguration config)
|
||||
{
|
||||
_db = db;
|
||||
_config = config;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public ObjectTemplate Object { get; set; } = new();
|
||||
|
||||
public List<TileMapSheet> SpriteSheets { get; set; } = new();
|
||||
public List<DungeonLootTable> LootTables { get; set; } = new();
|
||||
public List<DamageResistanceType> DamageTypes { get; set; } = new();
|
||||
public List<string> CdnImages { get; set; } = new();
|
||||
|
||||
public string CdnBaseUrl => _config["Cdn:BaseUrl"]?.TrimEnd('/') ?? "";
|
||||
|
||||
public string? Message { get; set; }
|
||||
public bool IsError { get; set; }
|
||||
|
||||
public async Task OnGetAsync(int? id)
|
||||
{
|
||||
await LoadReferenceData();
|
||||
|
||||
if (id.HasValue)
|
||||
{
|
||||
var obj = await _db.ObjectTemplates
|
||||
.Include(o => o.SpriteSheet)
|
||||
.FirstOrDefaultAsync(o => o.Id == id.Value);
|
||||
|
||||
if (obj != null)
|
||||
{
|
||||
Object = obj;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Defaults for new object
|
||||
Object.IsEnabled = true;
|
||||
Object.Weight = 100;
|
||||
Object.Width = 1;
|
||||
Object.Height = 1;
|
||||
Object.AnimationFps = 8.0f;
|
||||
Object.AnimationFrameCount = 1;
|
||||
Object.AnimationLoops = true;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAsync()
|
||||
{
|
||||
await LoadReferenceData();
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
IsError = true;
|
||||
Message = "Please fix validation errors.";
|
||||
return Page();
|
||||
}
|
||||
|
||||
Object.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
if (Object.Id == 0)
|
||||
{
|
||||
Object.CreatedAt = DateTime.UtcNow;
|
||||
_db.ObjectTemplates.Add(Object);
|
||||
Message = "Object template created!";
|
||||
}
|
||||
else
|
||||
{
|
||||
var existing = await _db.ObjectTemplates.FindAsync(Object.Id);
|
||||
if (existing == null)
|
||||
{
|
||||
IsError = true;
|
||||
Message = "Object not found.";
|
||||
return Page();
|
||||
}
|
||||
|
||||
// Update properties
|
||||
existing.Name = Object.Name;
|
||||
existing.Description = Object.Description;
|
||||
existing.Type = Object.Type;
|
||||
existing.Tags = Object.Tags;
|
||||
existing.Biome = Object.Biome;
|
||||
existing.SpriteSheetId = Object.SpriteSheetId;
|
||||
existing.FrameIndex = Object.FrameIndex;
|
||||
existing.SpritePath = Object.SpritePath;
|
||||
existing.Width = Object.Width;
|
||||
existing.Height = Object.Height;
|
||||
existing.OffsetX = Object.OffsetX;
|
||||
existing.OffsetY = Object.OffsetY;
|
||||
existing.ZIndex = Object.ZIndex;
|
||||
existing.BlocksMovement = Object.BlocksMovement;
|
||||
existing.BlocksProjectiles = Object.BlocksProjectiles;
|
||||
existing.BlocksLineOfSight = Object.BlocksLineOfSight;
|
||||
existing.BlockedCellsJson = Object.BlockedCellsJson;
|
||||
existing.IsAnimated = Object.IsAnimated;
|
||||
existing.AnimationFrameCount = Object.AnimationFrameCount;
|
||||
existing.AnimationFps = Object.AnimationFps;
|
||||
existing.AnimationLoops = Object.AnimationLoops;
|
||||
existing.AnimationStatesJson = Object.AnimationStatesJson;
|
||||
existing.DefaultState = Object.DefaultState;
|
||||
existing.IsInteractable = Object.IsInteractable;
|
||||
existing.TriggeredByWalk = Object.TriggeredByWalk;
|
||||
existing.InteractionType = Object.InteractionType;
|
||||
existing.InteractionState = Object.InteractionState;
|
||||
existing.InteractionCooldown = Object.InteractionCooldown;
|
||||
existing.RequiredItemId = Object.RequiredItemId;
|
||||
existing.LootTableId = Object.LootTableId;
|
||||
existing.GoldAmount = Object.GoldAmount;
|
||||
existing.XpReward = Object.XpReward;
|
||||
existing.IsDestructible = Object.IsDestructible;
|
||||
existing.Health = Object.Health;
|
||||
existing.DestroyedState = Object.DestroyedState;
|
||||
existing.DestroyedLootTableId = Object.DestroyedLootTableId;
|
||||
existing.Damage = Object.Damage;
|
||||
existing.DamageTypeKey = Object.DamageTypeKey;
|
||||
existing.TriggerDelay = Object.TriggerDelay;
|
||||
existing.ResetTime = Object.ResetTime;
|
||||
existing.Weight = Object.Weight;
|
||||
existing.MinTier = Object.MinTier;
|
||||
existing.MaxTier = Object.MaxTier;
|
||||
existing.MaxPerRoom = Object.MaxPerRoom;
|
||||
existing.Placement = Object.Placement;
|
||||
existing.IsEnabled = Object.IsEnabled;
|
||||
existing.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
Message = "Changes saved!";
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return RedirectToPage(new { id = Object.Id });
|
||||
}
|
||||
|
||||
private async Task LoadReferenceData()
|
||||
{
|
||||
SpriteSheets = await _db.TileMapSheets
|
||||
.Where(s => s.IsEnabled)
|
||||
.OrderBy(s => s.Name)
|
||||
.ToListAsync();
|
||||
|
||||
LootTables = await _db.DungeonLootTables
|
||||
.Where(t => t.IsEnabled)
|
||||
.OrderBy(t => t.Name)
|
||||
.ToListAsync();
|
||||
|
||||
DamageTypes = await _db.DamageResistanceTypes
|
||||
.OrderBy(d => d.Name)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public string GetCdnUrl(string? path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path)) return "";
|
||||
var baseUrl = CdnBaseUrl;
|
||||
if (!baseUrl.EndsWith("/downloads")) baseUrl += "/downloads";
|
||||
return $"{baseUrl}/{path.TrimStart('/')}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user