feat(collection): replace paginated collection browser with full owned map
Adds collection.get_owned endpoint that returns { definitionId → count }
for the player's entire collection in a single DB round-trip. The client
caches this map and filters/sorts entirely client-side using definitions
already in ContentCache, eliminating all pagination and "load more" UI.
- Search, type, and affinity filters now work across the full collection
instantly with no network round-trips
- deck_editor_panel: arch spirit picker also uses the owned map —
removes a separate collection.get_cards request
- cards_view: same pattern, grid rebuilds client-side on filter change
- CollectionCache: persists owned map to disk (owned.json per player)
Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -1,36 +1,28 @@
|
||||
extends VBoxContainer
|
||||
## Cards tab — paginated, filterable grid of the player's owned cards.
|
||||
## Cards tab — filterable, sortable grid of the player's owned cards.
|
||||
##
|
||||
## Fetches from collection.get_cards (page, pageSize, nameFilter?,
|
||||
## cardTypeFilter?, affinityFilter?). Displays results as CardWidget
|
||||
## instances in a responsive GridContainer.
|
||||
##
|
||||
## Typing in the search box starts a 0.4 s debounce before reloading.
|
||||
## "Load More" appends the next page without clearing the existing cards.
|
||||
## Fetches the full owned map once (collection.get_owned), then all filtering,
|
||||
## search, and sorting happen client-side using ContentCache definitions.
|
||||
## No "load more" or pagination.
|
||||
|
||||
const CARD_WIDGET := preload("res://scenes/collection/components/CardWidget.tscn")
|
||||
const PAGE_SIZE := 20
|
||||
const CARD_WIDGET := preload("res://scenes/collection/components/CardWidget.tscn")
|
||||
const CARD_WIDTH_ESTIMATE := 168
|
||||
|
||||
# ── Node references ───────────────────────────────────────────────────────────
|
||||
|
||||
@onready var search_input : LineEdit = %SearchInput
|
||||
@onready var type_option : OptionButton = %TypeOption
|
||||
@onready var affinity_option : OptionButton = %AffinityOption
|
||||
@onready var card_grid : GridContainer = %CardGrid
|
||||
@onready var loading_label : Label = %LoadingLabel
|
||||
@onready var empty_label : Label = %EmptyLabel
|
||||
@onready var load_more_btn : Button = %LoadMoreBtn
|
||||
@onready var search_input : LineEdit = %SearchInput
|
||||
@onready var type_option : OptionButton = %TypeOption
|
||||
@onready var affinity_option : OptionButton = %AffinityOption
|
||||
@onready var card_grid : GridContainer = %CardGrid
|
||||
@onready var loading_label : Label = %LoadingLabel
|
||||
@onready var empty_label : Label = %EmptyLabel
|
||||
|
||||
# ── State ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
var _current_page : int = 1
|
||||
var _total_count : int = 0
|
||||
var _has_more : bool = false
|
||||
var _loading : bool = false
|
||||
var _search_timer : float = 0.0
|
||||
var _card_entries : Dictionary = {} ## definitionId(String) -> { count, label }
|
||||
const _SEARCH_DEBOUNCE := 0.4
|
||||
## Full owned map: { definitionId(String) → ownedCount(int) }
|
||||
var _owned : Dictionary = {}
|
||||
var _search_timer : float = 0.0
|
||||
const _SEARCH_DEBOUNCE := 0.15
|
||||
|
||||
# Emitted when the user taps a card — parent uses this to show the detail popup.
|
||||
signal card_selected(dto: Dictionary, def: Dictionary)
|
||||
@@ -42,11 +34,11 @@ func _ready() -> void:
|
||||
_populate_affinity_options()
|
||||
_update_columns()
|
||||
search_input.text_changed.connect(_on_search_changed)
|
||||
type_option.item_selected.connect(func(_i): _reload())
|
||||
affinity_option.item_selected.connect(func(_i): _reload())
|
||||
load_more_btn.pressed.connect(_on_load_more)
|
||||
type_option.item_selected.connect(func(_i): _rebuild_grid())
|
||||
affinity_option.item_selected.connect(func(_i): _rebuild_grid())
|
||||
%LoadMoreBtn.hide()
|
||||
get_viewport().size_changed.connect(_update_columns)
|
||||
_reload()
|
||||
_fetch_owned()
|
||||
|
||||
|
||||
func _exit_tree() -> void:
|
||||
@@ -59,7 +51,7 @@ func _process(delta: float) -> void:
|
||||
_search_timer -= delta
|
||||
if _search_timer <= 0.0:
|
||||
_search_timer = 0.0
|
||||
_reload()
|
||||
_rebuild_grid()
|
||||
|
||||
# ── Option population ─────────────────────────────────────────────────────────
|
||||
|
||||
@@ -92,177 +84,119 @@ func _update_columns() -> void:
|
||||
available_width = get_viewport_rect().size.x - 16.0
|
||||
card_grid.columns = max(1, int(available_width / CARD_WIDTH_ESTIMATE))
|
||||
|
||||
# ── Data fetching ─────────────────────────────────────────────────────────────
|
||||
# ── Data fetching (one-time) ──────────────────────────────────────────────────
|
||||
|
||||
func _reload() -> void:
|
||||
_current_page = 1
|
||||
_total_count = 0
|
||||
_has_more = false
|
||||
_card_entries.clear()
|
||||
for child in card_grid.get_children():
|
||||
child.queue_free()
|
||||
var cached := CollectionCache.get_cards_response(_query_key_for_page(1))
|
||||
if not cached.is_empty():
|
||||
_render_cards_response(cached, 1)
|
||||
_fetch_page(1)
|
||||
|
||||
|
||||
func _fetch_page(page: int) -> void:
|
||||
if _loading:
|
||||
return
|
||||
_loading = true
|
||||
func _fetch_owned() -> void:
|
||||
_set_status("loading")
|
||||
|
||||
var payload := {
|
||||
"page": page,
|
||||
"pageSize": PAGE_SIZE,
|
||||
}
|
||||
var search := search_input.text.strip_edges()
|
||||
if not search.is_empty():
|
||||
payload["nameFilter"] = search
|
||||
|
||||
# Type filter — index 0 = "All Types" (skip)
|
||||
var type_idx := type_option.selected
|
||||
if type_idx > 0:
|
||||
payload["cardTypeFilter"] = type_option.get_item_text(type_idx)
|
||||
|
||||
# Affinity filter — index 0 = "All Affinities" (skip)
|
||||
var aff_idx := affinity_option.selected
|
||||
if aff_idx > 0:
|
||||
payload["affinityFilter"] = affinity_option.get_item_text(aff_idx)
|
||||
|
||||
var cached := CollectionCache.get_owned_map()
|
||||
if not cached.is_empty():
|
||||
_owned = cached
|
||||
_rebuild_grid()
|
||||
Network.request(
|
||||
"collection.get_cards",
|
||||
payload,
|
||||
func(_t: String, p: Variant): _on_cards_response(p, page),
|
||||
func(_code: String, msg: String): _on_error(msg)
|
||||
"collection.get_owned",
|
||||
{},
|
||||
func(_t: String, p: Variant): _on_owned_response(p),
|
||||
func(_code: String, msg: String): _on_owned_error(msg)
|
||||
)
|
||||
|
||||
|
||||
func _on_cards_response(payload: Variant, page: int) -> void:
|
||||
_loading = false
|
||||
var p := payload as Dictionary
|
||||
CollectionCache.store_cards_response(_query_key_for_page(page), p)
|
||||
_render_cards_response(p, page)
|
||||
func _on_owned_response(payload: Variant) -> void:
|
||||
var p := payload as Dictionary
|
||||
var raw : Variant = p.get("owned", {})
|
||||
_owned = raw if typeof(raw) == TYPE_DICTIONARY else {}
|
||||
CollectionCache.store_owned_map(_owned)
|
||||
_rebuild_grid()
|
||||
|
||||
|
||||
func _on_error(msg: String) -> void:
|
||||
_loading = false
|
||||
func _on_owned_error(msg: String) -> void:
|
||||
_set_status("empty")
|
||||
push_warning("CardsView: %s" % msg)
|
||||
|
||||
# ── Button handlers ───────────────────────────────────────────────────────────
|
||||
# ── Grid rendering (client-side filtered) ────────────────────────────────────
|
||||
|
||||
func _on_search_changed(_text: String) -> void:
|
||||
_search_timer = _SEARCH_DEBOUNCE
|
||||
func _rebuild_grid() -> void:
|
||||
for child in card_grid.get_children():
|
||||
child.queue_free()
|
||||
|
||||
|
||||
func _on_load_more() -> void:
|
||||
_fetch_page(_current_page + 1)
|
||||
|
||||
# ── Status helper ─────────────────────────────────────────────────────────────
|
||||
|
||||
func _set_status(state: String) -> void:
|
||||
loading_label.visible = (state == "loading")
|
||||
empty_label.visible = (state == "empty")
|
||||
if state != "loading":
|
||||
load_more_btn.visible = _has_more
|
||||
|
||||
|
||||
func _render_cards_response(payload: Dictionary, page: int) -> void:
|
||||
var cards : Array = payload.get("cards", [])
|
||||
_total_count = int(payload.get("totalCount", 0))
|
||||
var page_size : int = int(payload.get("pageSize", PAGE_SIZE))
|
||||
_current_page = page
|
||||
_has_more = (page * page_size) < _total_count
|
||||
|
||||
if page == 1:
|
||||
_card_entries.clear()
|
||||
for child in card_grid.get_children():
|
||||
child.queue_free()
|
||||
|
||||
for raw_dto in cards:
|
||||
var dto := raw_dto as Dictionary
|
||||
var def := ContentCache.get_definition(str(dto.get("cardDefinitionId", "")))
|
||||
var hydrated := _hydrate_card_dto(dto, def)
|
||||
_add_or_increment_card(hydrated, def)
|
||||
|
||||
if cards.is_empty() and page == 1:
|
||||
if _owned.is_empty():
|
||||
_set_status("empty")
|
||||
else:
|
||||
_set_status("done")
|
||||
|
||||
load_more_btn.visible = _has_more
|
||||
|
||||
|
||||
func _add_or_increment_card(dto: Dictionary, def: Dictionary) -> void:
|
||||
var def_id := str(dto.get("cardDefinitionId", ""))
|
||||
if def_id.is_empty():
|
||||
return
|
||||
|
||||
if _card_entries.has(def_id):
|
||||
var entry := _card_entries.get(def_id, {}) as Dictionary
|
||||
var next_count := int(entry.get("count", 1)) + 1
|
||||
entry["count"] = next_count
|
||||
var count_label := entry.get("label", null) as Label
|
||||
if count_label != null:
|
||||
count_label.text = "x%d" % next_count
|
||||
_card_entries[def_id] = entry
|
||||
var search := search_input.text.strip_edges().to_lower()
|
||||
var type_idx := type_option.selected
|
||||
var aff_idx := affinity_option.selected
|
||||
var type_filter := type_option.get_item_text(type_idx) if type_idx > 0 else ""
|
||||
var aff_filter := affinity_option.get_item_text(aff_idx) if aff_idx > 0 else ""
|
||||
|
||||
var entries : Array = []
|
||||
for def_id_var in _owned:
|
||||
var def_id := str(def_id_var)
|
||||
var count := int(_owned[def_id_var])
|
||||
var def := ContentCache.get_definition(def_id)
|
||||
if def.is_empty():
|
||||
continue
|
||||
var name := str(def.get("name", ""))
|
||||
var c_type := str(def.get("cardType", ""))
|
||||
var affinity := str(def.get("affinity", ""))
|
||||
if not search.is_empty() and not name.to_lower().contains(search):
|
||||
continue
|
||||
if not type_filter.is_empty() and c_type != type_filter:
|
||||
continue
|
||||
if not aff_filter.is_empty() and affinity != aff_filter:
|
||||
continue
|
||||
entries.append({
|
||||
"cardDefinitionId": def_id,
|
||||
"name": name,
|
||||
"cardType": c_type,
|
||||
"affinity": affinity,
|
||||
"rarity": str(def.get("rarity", "")),
|
||||
"tier": def.get("tier"),
|
||||
"copyCount": count,
|
||||
})
|
||||
|
||||
entries.sort_custom(func(a, b): return str(a["name"]) < str(b["name"]))
|
||||
|
||||
if entries.is_empty():
|
||||
_set_status("empty")
|
||||
return
|
||||
|
||||
for dto in entries:
|
||||
var def := ContentCache.get_definition(str(dto["cardDefinitionId"]))
|
||||
_add_card_to_grid(dto, def)
|
||||
|
||||
_set_status("done")
|
||||
|
||||
|
||||
func _add_card_to_grid(dto: Dictionary, def: Dictionary) -> void:
|
||||
var count := int(dto.get("copyCount", 1))
|
||||
|
||||
var wrapper := VBoxContainer.new()
|
||||
wrapper.add_theme_constant_override("separation", 4)
|
||||
wrapper.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
|
||||
|
||||
var widget := CARD_WIDGET.instantiate() as PanelContainer
|
||||
widget.card_pressed.connect(func(_pressed_id: String):
|
||||
var selected_dto := dto.duplicate(true)
|
||||
var entry := _card_entries.get(def_id, {}) as Dictionary
|
||||
selected_dto["copyCount"] = int(entry.get("count", 1))
|
||||
card_selected.emit(selected_dto, def))
|
||||
card_selected.emit(dto.duplicate(true), def))
|
||||
wrapper.add_child(widget)
|
||||
widget.setup(dto, def)
|
||||
|
||||
var count_label := Label.new()
|
||||
count_label.text = "x1"
|
||||
count_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
count_label.add_theme_font_size_override("font_size", 11)
|
||||
count_label.modulate = Color(0.72, 0.72, 0.80)
|
||||
wrapper.add_child(count_label)
|
||||
if count > 1:
|
||||
var count_label := Label.new()
|
||||
count_label.text = "x%d" % count
|
||||
count_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
count_label.add_theme_font_size_override("font_size", 11)
|
||||
count_label.modulate = Color(0.72, 0.72, 0.80)
|
||||
wrapper.add_child(count_label)
|
||||
|
||||
card_grid.add_child(wrapper)
|
||||
_card_entries[def_id] = {
|
||||
"count": 1,
|
||||
"label": count_label,
|
||||
}
|
||||
|
||||
# ── Event handlers ────────────────────────────────────────────────────────────
|
||||
|
||||
func _hydrate_card_dto(dto: Dictionary, def: Dictionary) -> Dictionary:
|
||||
var hydrated := dto.duplicate(true)
|
||||
if def.is_empty():
|
||||
return hydrated
|
||||
hydrated["name"] = str(def.get("name", hydrated.get("name", "")))
|
||||
hydrated["cardType"] = str(def.get("cardType", hydrated.get("cardType", "")))
|
||||
hydrated["affinity"] = str(def.get("affinity", hydrated.get("affinity", "")))
|
||||
hydrated["rarity"] = str(def.get("rarity", hydrated.get("rarity", "")))
|
||||
var tier_value: Variant = def.get("tier", hydrated.get("tier", null))
|
||||
hydrated["tier"] = tier_value if tier_value != null else null
|
||||
return hydrated
|
||||
func _on_search_changed(_text: String) -> void:
|
||||
_search_timer = _SEARCH_DEBOUNCE
|
||||
|
||||
# ── Status helper ─────────────────────────────────────────────────────────────
|
||||
|
||||
func _query_key_for_page(page: int) -> String:
|
||||
var search := search_input.text.strip_edges()
|
||||
var type_filter := ""
|
||||
var affinity_filter := ""
|
||||
var type_idx := type_option.selected
|
||||
if type_idx > 0:
|
||||
type_filter = type_option.get_item_text(type_idx)
|
||||
var aff_idx := affinity_option.selected
|
||||
if aff_idx > 0:
|
||||
affinity_filter = affinity_option.get_item_text(aff_idx)
|
||||
return CollectionCache.cards_query_key(
|
||||
page,
|
||||
PAGE_SIZE,
|
||||
search,
|
||||
type_filter,
|
||||
affinity_filter)
|
||||
func _set_status(state: String) -> void:
|
||||
loading_label.visible = (state == "loading")
|
||||
empty_label.visible = (state == "empty")
|
||||
|
||||
Reference in New Issue
Block a user