182 lines
6.3 KiB
C#
182 lines
6.3 KiB
C#
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace GameServices.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 => NormalizeCdnBaseUrl(_config["Cdn:BaseUrl"]);
|
|
|
|
private string NormalizeCdnBaseUrl(string? baseUrl)
|
|
{
|
|
baseUrl = string.IsNullOrWhiteSpace(baseUrl)
|
|
? "https://assets.aeternalisgames.com"
|
|
: baseUrl;
|
|
baseUrl = baseUrl.TrimEnd('/');
|
|
if (!baseUrl.EndsWith("/downloads", StringComparison.OrdinalIgnoreCase))
|
|
baseUrl += "/downloads";
|
|
return baseUrl;
|
|
}
|
|
|
|
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.DisplayName)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public string GetCdnUrl(string? path)
|
|
{
|
|
if (string.IsNullOrEmpty(path)) return "";
|
|
var baseUrl = CdnBaseUrl;
|
|
if (!baseUrl.EndsWith("/downloads")) baseUrl += "/downloads";
|
|
return $"{baseUrl}/{path.TrimStart('/')}";
|
|
}
|
|
}
|