210 lines
7.8 KiB
C#
210 lines
7.8 KiB
C#
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Text.Json;
|
|
|
|
namespace GameServices.Admin.Pages.DungeonBuilder;
|
|
|
|
public class PaletteEditModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
|
|
public PaletteEditModel(GameDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
[BindProperty]
|
|
public DungeonPalette Palette { get; set; } = new();
|
|
|
|
// Available items
|
|
public List<TileTemplate> FloorTiles { get; set; } = new();
|
|
public List<TileTemplate> WallTiles { get; set; } = new();
|
|
public List<TileTemplate> DoorTiles { get; set; } = new();
|
|
public List<ObjectTemplate> Decorations { get; set; } = new();
|
|
public List<ObjectTemplate> Containers { get; set; } = new();
|
|
public List<ObjectTemplate> Traps { get; set; } = new();
|
|
public List<BuilderRoomTemplate> Rooms { get; set; } = new();
|
|
public List<TileBucket> Buckets { get; set; } = new();
|
|
|
|
// Selected IDs
|
|
public List<int> SelectedFloorIds { get; set; } = new();
|
|
public List<int> SelectedWallIds { get; set; } = new();
|
|
public List<int> SelectedDoorIds { get; set; } = new();
|
|
public List<int> SelectedDecorationIds { get; set; } = new();
|
|
public List<int> SelectedContainerIds { get; set; } = new();
|
|
public List<int> SelectedTrapIds { get; set; } = new();
|
|
public List<int> SelectedRoomIds { get; set; } = new();
|
|
public List<int> SelectedBucketIds { get; set; } = new();
|
|
|
|
public string? Message { get; set; }
|
|
public bool IsError { get; set; }
|
|
|
|
public async Task OnGetAsync(int? id)
|
|
{
|
|
await LoadReferenceData();
|
|
|
|
if (id.HasValue)
|
|
{
|
|
var palette = await _db.DungeonPalettes.FindAsync(id.Value);
|
|
if (palette != null)
|
|
{
|
|
Palette = palette;
|
|
|
|
// Parse selected IDs
|
|
SelectedFloorIds = ParseIds(palette.FloorTemplateIdsJson);
|
|
SelectedWallIds = ParseIds(palette.WallTemplateIdsJson);
|
|
SelectedDoorIds = ParseIds(palette.DoorTemplateIdsJson);
|
|
SelectedDecorationIds = ParseIds(palette.DecorationIdsJson);
|
|
SelectedContainerIds = ParseIds(palette.ContainerIdsJson);
|
|
SelectedTrapIds = ParseIds(palette.TrapIdsJson);
|
|
SelectedRoomIds = ParseIds(palette.RoomTemplateIdsJson);
|
|
SelectedBucketIds = ParseIds(palette.AllowedBucketIdsJson);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Defaults
|
|
Palette.IsEnabled = true;
|
|
Palette.MinRooms = 5;
|
|
Palette.MaxRooms = 15;
|
|
Palette.MinRoomWidth = 6;
|
|
Palette.MaxRoomWidth = 20;
|
|
Palette.MinRoomHeight = 6;
|
|
Palette.MaxRoomHeight = 20;
|
|
Palette.MinCorridorWidth = 3;
|
|
Palette.MaxCorridorWidth = 5;
|
|
Palette.DecorationDensity = 30;
|
|
Palette.ContainerDensity = 20;
|
|
Palette.TrapDensity = 10;
|
|
}
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostAsync()
|
|
{
|
|
await LoadReferenceData();
|
|
|
|
if (string.IsNullOrWhiteSpace(Palette.Name))
|
|
{
|
|
IsError = true;
|
|
Message = "Name is required.";
|
|
return Page();
|
|
}
|
|
|
|
Palette.UpdatedAt = DateTime.UtcNow;
|
|
|
|
if (Palette.Id == 0)
|
|
{
|
|
Palette.CreatedAt = DateTime.UtcNow;
|
|
_db.DungeonPalettes.Add(Palette);
|
|
Message = "Palette created!";
|
|
}
|
|
else
|
|
{
|
|
var existing = await _db.DungeonPalettes.FindAsync(Palette.Id);
|
|
if (existing == null)
|
|
{
|
|
IsError = true;
|
|
Message = "Palette not found.";
|
|
return Page();
|
|
}
|
|
|
|
existing.Name = Palette.Name;
|
|
existing.Description = Palette.Description;
|
|
existing.Biome = Palette.Biome;
|
|
existing.Tags = Palette.Tags;
|
|
existing.AllowedBucketIdsJson = Palette.AllowedBucketIdsJson;
|
|
existing.EnforceBiomeBuckets = Palette.EnforceBiomeBuckets;
|
|
existing.FloorTemplateIdsJson = Palette.FloorTemplateIdsJson;
|
|
existing.WallTemplateIdsJson = Palette.WallTemplateIdsJson;
|
|
existing.DoorTemplateIdsJson = Palette.DoorTemplateIdsJson;
|
|
existing.DecorationIdsJson = Palette.DecorationIdsJson;
|
|
existing.ContainerIdsJson = Palette.ContainerIdsJson;
|
|
existing.TrapIdsJson = Palette.TrapIdsJson;
|
|
existing.RoomTemplateIdsJson = Palette.RoomTemplateIdsJson;
|
|
existing.MinRooms = Palette.MinRooms;
|
|
existing.MaxRooms = Palette.MaxRooms;
|
|
existing.MinRoomWidth = Palette.MinRoomWidth;
|
|
existing.MaxRoomWidth = Palette.MaxRoomWidth;
|
|
existing.MinRoomHeight = Palette.MinRoomHeight;
|
|
existing.MaxRoomHeight = Palette.MaxRoomHeight;
|
|
existing.MinCorridorWidth = Palette.MinCorridorWidth;
|
|
existing.MaxCorridorWidth = Palette.MaxCorridorWidth;
|
|
existing.DecorationDensity = Palette.DecorationDensity;
|
|
existing.ContainerDensity = Palette.ContainerDensity;
|
|
existing.TrapDensity = Palette.TrapDensity;
|
|
existing.IsEnabled = Palette.IsEnabled;
|
|
existing.UpdatedAt = DateTime.UtcNow;
|
|
|
|
Message = "Changes saved!";
|
|
}
|
|
|
|
await _db.SaveChangesAsync();
|
|
|
|
// Re-parse selected IDs for display
|
|
SelectedFloorIds = ParseIds(Palette.FloorTemplateIdsJson);
|
|
SelectedWallIds = ParseIds(Palette.WallTemplateIdsJson);
|
|
SelectedDoorIds = ParseIds(Palette.DoorTemplateIdsJson);
|
|
SelectedDecorationIds = ParseIds(Palette.DecorationIdsJson);
|
|
SelectedContainerIds = ParseIds(Palette.ContainerIdsJson);
|
|
SelectedTrapIds = ParseIds(Palette.TrapIdsJson);
|
|
SelectedRoomIds = ParseIds(Palette.RoomTemplateIdsJson);
|
|
SelectedBucketIds = ParseIds(Palette.AllowedBucketIdsJson);
|
|
|
|
return RedirectToPage(new { id = Palette.Id });
|
|
}
|
|
|
|
private async Task LoadReferenceData()
|
|
{
|
|
// Tile templates by category
|
|
var allTiles = await _db.TileTemplates
|
|
.Where(t => t.IsEnabled)
|
|
.OrderBy(t => t.Name)
|
|
.ToListAsync();
|
|
|
|
FloorTiles = allTiles.Where(t => t.Category == TileTemplateCategory.Floor).ToList();
|
|
WallTiles = allTiles.Where(t => t.Category == TileTemplateCategory.Wall).ToList();
|
|
DoorTiles = allTiles.Where(t => t.Category == TileTemplateCategory.Door).ToList();
|
|
|
|
// Object templates by type
|
|
var allObjects = await _db.ObjectTemplates
|
|
.Where(o => o.IsEnabled)
|
|
.OrderBy(o => o.Name)
|
|
.ToListAsync();
|
|
|
|
Decorations = allObjects.Where(o =>
|
|
o.Type == ObjectTemplateType.Decoration ||
|
|
o.Type == ObjectTemplateType.Furniture ||
|
|
o.Type == ObjectTemplateType.Light).ToList();
|
|
Containers = allObjects.Where(o => o.Type == ObjectTemplateType.Container).ToList();
|
|
Traps = allObjects.Where(o =>
|
|
o.Type == ObjectTemplateType.Trap ||
|
|
o.Type == ObjectTemplateType.Hazard).ToList();
|
|
|
|
// Room templates
|
|
Rooms = await _db.BuilderRoomTemplates
|
|
.Where(r => r.IsEnabled)
|
|
.OrderBy(r => r.Name)
|
|
.ToListAsync();
|
|
|
|
Buckets = await _db.TileBuckets
|
|
.Where(b => b.IsEnabled)
|
|
.OrderBy(b => b.Name)
|
|
.ToListAsync();
|
|
}
|
|
|
|
private List<int> ParseIds(string? json)
|
|
{
|
|
if (string.IsNullOrEmpty(json)) return new List<int>();
|
|
try
|
|
{
|
|
return JsonSerializer.Deserialize<List<int>>(json) ?? new List<int>();
|
|
}
|
|
catch
|
|
{
|
|
return new List<int>();
|
|
}
|
|
}
|
|
}
|