Files
systemrevivalserver/Services/Admin/Pages/Mobs/Edit.cshtml.cs
T
2025-12-25 17:39:00 -05:00

226 lines
7.9 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Server.Data;
using Server.Data.Entities;
namespace VoidScavengers.Services.Admin.Pages.Mobs;
public class EditModel : AdminPageModel
{
private readonly GameDbContext _db;
public EditModel(GameDbContext db)
{
_db = db;
}
[BindProperty]
public DungeonMobDefinition Mob { get; set; } = new();
public List<DungeonMobLootTable> MobLootTables { get; set; } = new();
public List<DungeonLootTable> AllLootTables { get; set; } = new();
public bool IsNew => Mob.Id == 0;
public string? Message { get; set; }
public bool IsError { get; set; }
public async Task OnGetAsync(int? id)
{
AllLootTables = await _db.DungeonLootTables
.Where(t => t.IsEnabled)
.OrderBy(t => t.Name)
.ToListAsync();
if (id.HasValue)
{
var mob = await _db.DungeonMobDefinitions.FindAsync(id.Value);
if (mob != null)
{
Mob = mob;
MobLootTables = await _db.DungeonMobLootTables
.Include(m => m.LootTable)
.ThenInclude(t => t!.Entries)
.Where(m => m.MobDefinitionId == id.Value)
.OrderBy(m => m.Priority)
.ToListAsync();
}
}
else
{
// Defaults
Mob.IsEnabled = true;
Mob.MinTier = 1;
Mob.MaxTier = 99;
Mob.BaseHealth = 50;
Mob.BasePhysicalDamage = 5;
Mob.MoveSpeed = 60f;
Mob.AttackRange = 40f;
Mob.AttackSpeed = 1.0f;
Mob.AggroRange = 150f;
Mob.LeashRange = 400f;
Mob.HealthPerTier = 1.2f;
Mob.DamagePerTier = 1.15f;
Mob.ArmorPerTier = 1.1f;
Mob.BaseXp = 10;
Mob.XpPerTier = 1.25f;
Mob.TintColor = "#ffffff";
Mob.Scale = 1.0f;
Mob.AnimationSpeed = 8f;
}
}
public async Task<IActionResult> OnPostAsync(string? action)
{
AllLootTables = await _db.DungeonLootTables.Where(t => t.IsEnabled).OrderBy(t => t.Name).ToListAsync();
// Handle checkbox binding
Mob.IsEnabled = Request.Form.ContainsKey("Mob.IsEnabled");
// Ensure nullable strings are never null
Mob.Description ??= "";
Mob.TintColor ??= "#ffffff";
if (string.IsNullOrWhiteSpace(Mob.Name))
{
Message = "Name is required.";
IsError = true;
return Page();
}
try
{
if (action == "duplicate")
{
var newMob = new DungeonMobDefinition
{
Name = Mob.Name + " (Copy)",
Description = Mob.Description ?? "",
Type = Mob.Type,
Category = Mob.Category,
MinTier = Mob.MinTier,
MaxTier = Mob.MaxTier,
BaseHealth = Mob.BaseHealth,
BaseMana = Mob.BaseMana,
BasePhysicalDamage = Mob.BasePhysicalDamage,
BaseMagicDamage = Mob.BaseMagicDamage,
BaseArmor = Mob.BaseArmor,
BaseMagicResist = Mob.BaseMagicResist,
MoveSpeed = Mob.MoveSpeed,
AttackRange = Mob.AttackRange,
AttackSpeed = Mob.AttackSpeed,
AggroRange = Mob.AggroRange,
LeashRange = Mob.LeashRange,
HealthPerTier = Mob.HealthPerTier,
DamagePerTier = Mob.DamagePerTier,
ArmorPerTier = Mob.ArmorPerTier,
BaseXp = Mob.BaseXp,
XpPerTier = Mob.XpPerTier,
TintColor = Mob.TintColor,
Scale = Mob.Scale,
AnimationSpeed = Mob.AnimationSpeed,
SpriteConfigJson = Mob.SpriteConfigJson,
ResistancesJson = Mob.ResistancesJson,
AbilitiesJson = Mob.AbilitiesJson,
IsEnabled = Mob.IsEnabled
};
_db.DungeonMobDefinitions.Add(newMob);
await _db.SaveChangesAsync();
return RedirectToPage("/Mobs/Edit", new { id = newMob.Id });
}
if (Mob.Id == 0)
{
_db.DungeonMobDefinitions.Add(Mob);
await _db.SaveChangesAsync();
Message = $"Mob '{Mob.Name}' created. Now assign loot tables below.";
return RedirectToPage("/Mobs/Edit", new { id = Mob.Id });
}
else
{
var existing = await _db.DungeonMobDefinitions.FindAsync(Mob.Id);
if (existing == null)
{
Message = "Mob not found.";
IsError = true;
return Page();
}
existing.Name = Mob.Name;
existing.Description = Mob.Description;
existing.Type = Mob.Type;
existing.Category = Mob.Category;
existing.MinTier = Mob.MinTier;
existing.MaxTier = Mob.MaxTier;
existing.BaseHealth = Mob.BaseHealth;
existing.BaseMana = Mob.BaseMana;
existing.BasePhysicalDamage = Mob.BasePhysicalDamage;
existing.BaseMagicDamage = Mob.BaseMagicDamage;
existing.BaseArmor = Mob.BaseArmor;
existing.BaseMagicResist = Mob.BaseMagicResist;
existing.MoveSpeed = Mob.MoveSpeed;
existing.AttackRange = Mob.AttackRange;
existing.AttackSpeed = Mob.AttackSpeed;
existing.AggroRange = Mob.AggroRange;
existing.LeashRange = Mob.LeashRange;
existing.HealthPerTier = Mob.HealthPerTier;
existing.DamagePerTier = Mob.DamagePerTier;
existing.ArmorPerTier = Mob.ArmorPerTier;
existing.BaseXp = Mob.BaseXp;
existing.XpPerTier = Mob.XpPerTier;
existing.TintColor = Mob.TintColor;
existing.Scale = Mob.Scale;
existing.AnimationSpeed = Mob.AnimationSpeed;
existing.SpriteConfigJson = Mob.SpriteConfigJson;
existing.IsEnabled = Mob.IsEnabled;
await _db.SaveChangesAsync();
Mob = existing;
MobLootTables = await _db.DungeonMobLootTables
.Include(m => m.LootTable)
.ThenInclude(t => t!.Entries)
.Where(m => m.MobDefinitionId == Mob.Id)
.OrderBy(m => m.Priority)
.ToListAsync();
Message = "Changes saved successfully.";
}
}
catch (Exception ex)
{
Message = $"Error: {ex.Message}";
IsError = true;
}
return Page();
}
public async Task<IActionResult> OnPostAddLootAsync(int mobId, int lootTableId, int priority, float rollChance)
{
var mapping = new DungeonMobLootTable
{
MobDefinitionId = mobId,
LootTableId = lootTableId,
Priority = priority,
RollChance = rollChance
};
_db.DungeonMobLootTables.Add(mapping);
await _db.SaveChangesAsync();
return RedirectToPage("/Mobs/Edit", new { id = mobId });
}
public async Task<IActionResult> OnPostRemoveLootAsync(int mappingId, int mobId)
{
var mapping = await _db.DungeonMobLootTables.FindAsync(mappingId);
if (mapping != null)
{
_db.DungeonMobLootTables.Remove(mapping);
await _db.SaveChangesAsync();
}
return RedirectToPage("/Mobs/Edit", new { id = mobId });
}
}