diff --git a/Client/origin-client/autoloads/BoardTemplateCache.gd b/Client/origin-client/autoloads/BoardTemplateCache.gd new file mode 100644 index 0000000..6ab1b15 --- /dev/null +++ b/Client/origin-client/autoloads/BoardTemplateCache.gd @@ -0,0 +1,70 @@ +extends Node +## Board template cache. +## +## Fetches all enabled board templates from the server (content.board_templates) +## and keeps them in memory. No disk persistence — templates are lightweight and +## server-managed; a fresh fetch on login is sufficient. +## +## Call fetch() after login (e.g. from main_screen or init_screen) so the list is +## ready before the player opens a custom lobby or the matchmaking screen. +## +## Usage: +## BoardTemplateCache.fetch() +## BoardTemplateCache.get_templates_for_custom() → Array of Dictionary +## BoardTemplateCache.get_templates_for_ranked() → Array of Dictionary +## BoardTemplateCache.get_template_by_id("guid") → Dictionary or {} +## BoardTemplateCache.templates_ready signal + +signal templates_ready(templates: Array) + +var _templates: Array = [] + + +# ── Public API ──────────────────────────────────────────────────────────────── + +## Fetch (or refresh) the full enabled template list from the server. +## Emits templates_ready when done. Safe to call multiple times. +func fetch() -> void: + Network.request( + "content.board_templates", + {}, + func(_type: String, p: Variant): _on_response(p), + func(code: String, msg: String): + push_warning("BoardTemplateCache: fetch failed — %s %s" % [code, msg]) + ) + + +## All templates eligible for custom lobbies (IsAvailableForCustom = true). +func get_templates_for_custom() -> Array: + return _templates.filter(func(t: Dictionary) -> bool: + return bool(t.get("isAvailableForCustom", false))) + + +## All templates eligible for ranked games (IsAvailableForRanked = true). +func get_templates_for_ranked() -> Array: + return _templates.filter(func(t: Dictionary) -> bool: + return bool(t.get("isAvailableForRanked", false))) + + +## Return the template Dictionary for a given GUID, or {} if not cached. +func get_template_by_id(id: String) -> Dictionary: + for t: Dictionary in _templates: + if str(t.get("id", "")) == id: + return t + return {} + + +## True if the cache has been populated at least once. +func is_loaded() -> bool: + return not _templates.is_empty() + + +# ── Private ─────────────────────────────────────────────────────────────────── + +func _on_response(payload: Variant) -> void: + var p := payload as Dictionary + _templates.clear() + for item in p.get("templates", []): + if typeof(item) == TYPE_DICTIONARY: + _templates.append(item as Dictionary) + templates_ready.emit(_templates) diff --git a/Client/origin-client/project.godot b/Client/origin-client/project.godot index 7729d31..7aa7a24 100644 --- a/Client/origin-client/project.godot +++ b/Client/origin-client/project.godot @@ -19,6 +19,7 @@ config/icon="res://icon.svg" Network="*res://autoloads/Network.gd" ContentCache="*res://autoloads/ContentCache.gd" +BoardTemplateCache="*res://autoloads/BoardTemplateCache.gd" Auth="*res://autoloads/Auth.gd" CollectionCache="*res://autoloads/CollectionCache.gd" Chat="*res://autoloads/Chat.gd" diff --git a/Client/origin-client/scenes/free_play/components/rules_editor_dialog.gd b/Client/origin-client/scenes/free_play/components/rules_editor_dialog.gd index 40804f3..9e189e6 100644 --- a/Client/origin-client/scenes/free_play/components/rules_editor_dialog.gd +++ b/Client/origin-client/scenes/free_play/components/rules_editor_dialog.gd @@ -16,7 +16,10 @@ signal cancelled @onready var cancel_btn : Button = %CancelBtn @onready var confirm_btn : Button = %ConfirmBtn -var _controls : Dictionary = {} +var _controls : Dictionary = {} +## For OptionButtons that carry GUID values rather than display text: +## key → Array[String] of IDs, parallel to the button's item list. +var _option_ids : Dictionary = {} func _ready() -> void: @@ -33,6 +36,7 @@ func setup(current_rules: Dictionary, is_host: bool) -> void: func _build_fields(r: Dictionary) -> void: _controls.clear() + _option_ids.clear() for child in fields_box.get_children(): child.queue_free() @@ -114,6 +118,37 @@ func _build_fields(r: Dictionary) -> void: add_int.call("Starting Tile Affinity %","startingRandomTilePct",0, 100, 0, "% of tiles with a random basic affinity") add_option.call("Board Shape", "boardShape", ["Default", "Rectangle", "Cross", "Triangular", "RadialStar", "HexRing"], "Default") + # Board template picker — populate from BoardTemplateCache (GUID values, not labels). + var tmpl_key := "boardTemplateIdOverride" + var cur_tmpl_id := get_s.call(tmpl_key, "") + var custom_tmpls : Array = BoardTemplateCache.get_templates_for_custom() + var tmpl_hbox := HBoxContainer.new() + var tmpl_lbl := Label.new() + tmpl_lbl.text = "Board Template" + tmpl_lbl.tooltip_text = "Overrides the board layout with an admin-defined template. Ignores Board Shape when set." + tmpl_lbl.size_flags_horizontal = Control.SIZE_EXPAND_FILL + tmpl_hbox.add_child(tmpl_lbl) + var tmpl_opt := OptionButton.new() + var tmpl_ids : Array[String] = [] + tmpl_opt.add_item("— None —") + tmpl_ids.append("") + var tmpl_sel := 0 + for i: int in range(custom_tmpls.size()): + var t := custom_tmpls[i] as Dictionary + var tid := str(t.get("id", "")) + var tname := str(t.get("name", tid.substr(0, 8))) + var tw := int(t.get("width", 0)) + var th := int(t.get("height", 0)) + tmpl_opt.add_item("%s (%d×%d)" % [tname, tw, th]) + tmpl_ids.append(tid) + if tid == cur_tmpl_id: + tmpl_sel = i + 1 + tmpl_opt.selected = tmpl_sel + tmpl_hbox.add_child(tmpl_opt) + fields_box.add_child(tmpl_hbox) + _controls[tmpl_key] = tmpl_opt + _option_ids[tmpl_key] = tmpl_ids + # ── Deck ─────────────────────────────────────────────────────────────────── _section("DECK") add_int.call("Min Deck Size", "minDeckSize", 0, 60, 0, "0 = no limit") @@ -144,6 +179,12 @@ func _on_confirm() -> void: elif ctrl is CheckBox: overrides[key] = ctrl.button_pressed elif ctrl is OptionButton: - overrides[key] = ctrl.get_item_text(ctrl.selected) + if _option_ids.has(key): + # Emit the underlying GUID, not the display label. + var ids : Array = _option_ids[key] + var idx : int = ctrl.selected + overrides[key] = ids[idx] if idx < ids.size() else "" + else: + overrides[key] = ctrl.get_item_text(ctrl.selected) confirmed.emit(overrides) queue_free() diff --git a/Server/src/SOR.Content/ContentMessages.cs b/Server/src/SOR.Content/ContentMessages.cs index 028adef..83358a2 100644 --- a/Server/src/SOR.Content/ContentMessages.cs +++ b/Server/src/SOR.Content/ContentMessages.cs @@ -186,6 +186,30 @@ public sealed record HighlightTypeConfigDto( float Fps, string FallbackHex); +// ── Board templates ─────────────────────────────────────────────────────────── + +/// Fetch all enabled board templates. +public sealed record GetBoardTemplatesRequest; + +public sealed record GetBoardTemplatesResponse(IEnumerable Templates); + +/// +/// Lightweight descriptor for an admin-defined board template. +/// TilesJson and StartingZonesJson are forwarded verbatim for the client to cache; +/// the client only needs to decode them when loading a game. +/// +public sealed record BoardTemplateDto( + Guid Id, + string Name, + int Width, + int Height, + int MinPlayers, + int MaxPlayers, + bool IsAvailableForRanked, + bool IsAvailableForCustom, + string TilesJson, + string StartingZonesJson); + // ── BGM config ─────────────────────────────────────────────────────────────── /// Fetch all active background music tracks (keyed by game context). diff --git a/Server/src/SOR.Content/Handlers/ContentBoardTemplatesHandler.cs b/Server/src/SOR.Content/Handlers/ContentBoardTemplatesHandler.cs new file mode 100644 index 0000000..bd97778 --- /dev/null +++ b/Server/src/SOR.Content/Handlers/ContentBoardTemplatesHandler.cs @@ -0,0 +1,51 @@ +namespace SOR.Content.Handlers; + +using Microsoft.EntityFrameworkCore; +using SOR.Database.Contexts; +using SOR.Networking.Abstractions.Messaging; +using SOR.Networking.Abstractions.Transport; +using SOR.Shared.Networking; + +/// +/// content.board_templates — returns all enabled board templates. +/// +/// The client caches the full list (including TilesJson and StartingZonesJson) so +/// it can populate the board-template dropdown in the custom-lobby rules editor and, +/// later, render the board preview before a game starts. +/// +internal sealed class ContentBoardTemplatesHandler : IMessageHandler +{ + private readonly GameDbContext _gameDb; + + public ContentBoardTemplatesHandler(GameDbContext gameDb) => _gameDb = gameDb; + + public string MessageType => "content.board_templates"; + + public async Task HandleAsync( + IClientMessage message, + AuthenticatedConnection connection, + CancellationToken ct) + { + var templates = await _gameDb.BoardTemplates + .AsNoTracking() + .Where(t => t.IsEnabled) + .OrderBy(t => t.Name) + .Select(t => new BoardTemplateDto( + t.Id, + t.Name, + t.Width, + t.Height, + t.MinPlayers, + t.MaxPlayers, + t.IsAvailableForRanked, + t.IsAvailableForCustom, + t.TilesJson, + t.StartingZonesJson)) + .ToListAsync(ct); + + return ServerMessage.Reply( + "content.board_templates.ack", + message.Seq, + new GetBoardTemplatesResponse(templates)); + } +} diff --git a/Server/src/SOR.Content/ServiceRegistration.cs b/Server/src/SOR.Content/ServiceRegistration.cs index f96054e..0d2ed45 100644 --- a/Server/src/SOR.Content/ServiceRegistration.cs +++ b/Server/src/SOR.Content/ServiceRegistration.cs @@ -17,6 +17,7 @@ public static class ServiceRegistration IConfiguration configuration) { services.AddCatalogDatabase(configuration); + services.AddGameDatabase(configuration); services.AddTransient(); services.AddTransient(); @@ -26,6 +27,7 @@ public static class ServiceRegistration services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); return services; }