Files

175 lines
5.8 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using DataServer.Data;
using DataServer.Data.Entities;
namespace GameServices.Admin.Pages.LootTables;
public class EditModel : AdminPageModel
{
private readonly GameDbContext _db;
public EditModel(GameDbContext db)
{
_db = db;
}
[BindProperty]
public DungeonLootTable LootTable { get; set; } = new();
public List<DungeonLootTableEntry> Entries { get; set; } = new();
public List<DungeonItemDefinition> AllItems { get; set; } = new();
public bool IsNew => LootTable.Id == 0;
public string? Message { get; set; }
public bool IsError { get; set; }
public string CdnBaseUrl { get; set; } = "https://assets.aeternalisgames.com";
public async Task OnGetAsync(int? id)
{
var cdnUrl = Environment.GetEnvironmentVariable("CDN_BASE_URL");
if (!string.IsNullOrEmpty(cdnUrl)) CdnBaseUrl = cdnUrl;
AllItems = await _db.DungeonItemDefinitions
.Where(i => i.IsEnabled)
.OrderBy(i => i.Name)
.ToListAsync();
if (id.HasValue)
{
var table = await _db.DungeonLootTables
.FirstOrDefaultAsync(t => t.Id == id.Value);
if (table != null)
{
LootTable = table;
Entries = await _db.DungeonLootTableEntries
.Include(e => e.ItemDefinition)
.Where(e => e.LootTableId == id.Value)
.OrderByDescending(e => e.Guaranteed)
.ThenByDescending(e => e.DropChance)
.ToListAsync();
}
}
else
{
// Defaults
LootTable.IsEnabled = true;
LootTable.MinTier = 1;
LootTable.MaxTier = 99;
LootTable.MinDrops = 1;
LootTable.MaxDrops = 3;
LootTable.DropChanceMultiplier = 1.0f;
LootTable.GoldDropChance = 0f;
LootTable.MinGold = 0;
LootTable.MaxGold = 0;
}
}
public async Task<IActionResult> OnPostAsync()
{
var cdnUrl = Environment.GetEnvironmentVariable("CDN_BASE_URL");
if (!string.IsNullOrEmpty(cdnUrl)) CdnBaseUrl = cdnUrl;
AllItems = await _db.DungeonItemDefinitions.Where(i => i.IsEnabled).OrderBy(i => i.Name).ToListAsync();
// Handle checkbox binding
LootTable.IsEnabled = Request.Form.ContainsKey("LootTable.IsEnabled");
LootTable.GuaranteedDrop = Request.Form.ContainsKey("LootTable.GuaranteedDrop");
// Ensure Description is never null
LootTable.Description ??= "";
if (string.IsNullOrWhiteSpace(LootTable.Name))
{
Message = "Name is required.";
IsError = true;
return Page();
}
try
{
if (LootTable.Id == 0)
{
_db.DungeonLootTables.Add(LootTable);
await _db.SaveChangesAsync();
Message = $"Loot table '{LootTable.Name}' created. Now add entries below.";
return RedirectToPage("/LootTables/Edit", new { id = LootTable.Id });
}
else
{
var existing = await _db.DungeonLootTables.FindAsync(LootTable.Id);
if (existing == null)
{
Message = "Loot table not found.";
IsError = true;
return Page();
}
existing.Name = LootTable.Name;
existing.Description = LootTable.Description;
existing.MinTier = LootTable.MinTier;
existing.MaxTier = LootTable.MaxTier;
existing.MinDrops = LootTable.MinDrops;
existing.MaxDrops = LootTable.MaxDrops;
existing.DropChanceMultiplier = LootTable.DropChanceMultiplier;
existing.GuaranteedDrop = LootTable.GuaranteedDrop;
existing.GoldDropChance = LootTable.GoldDropChance;
existing.MinGold = LootTable.MinGold;
existing.MaxGold = LootTable.MaxGold;
existing.IsEnabled = LootTable.IsEnabled;
await _db.SaveChangesAsync();
LootTable = existing;
Entries = await _db.DungeonLootTableEntries
.Include(e => e.ItemDefinition)
.Where(e => e.LootTableId == LootTable.Id)
.OrderByDescending(e => e.Guaranteed)
.ThenByDescending(e => e.DropChance)
.ToListAsync();
Message = "Changes saved successfully.";
}
}
catch (Exception ex)
{
Message = $"Error: {ex.Message}";
IsError = true;
}
return Page();
}
public async Task<IActionResult> OnPostAddEntryAsync(int tableId, int itemId, float dropChance, int weight, int minQty, int maxQty, bool guaranteed)
{
var entry = new DungeonLootTableEntry
{
LootTableId = tableId,
ItemDefinitionId = itemId,
DropChance = dropChance,
Weight = weight,
MinQuantity = minQty,
MaxQuantity = maxQty,
Guaranteed = guaranteed
};
_db.DungeonLootTableEntries.Add(entry);
await _db.SaveChangesAsync();
return RedirectToPage("/LootTables/Edit", new { id = tableId });
}
public async Task<IActionResult> OnPostDeleteEntryAsync(int entryId, int tableId)
{
var entry = await _db.DungeonLootTableEntries.FindAsync(entryId);
if (entry != null)
{
_db.DungeonLootTableEntries.Remove(entry);
await _db.SaveChangesAsync();
}
return RedirectToPage("/LootTables/Edit", new { id = tableId });
}
}