269 lines
9.4 KiB
GDScript
269 lines
9.4 KiB
GDScript
extends VBoxContainer
|
|
## Cards tab — paginated, filterable 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.
|
|
|
|
const CARD_WIDGET := preload("res://scenes/collection/components/CardWidget.tscn")
|
|
const PAGE_SIZE := 20
|
|
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
|
|
|
|
# ── 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
|
|
|
|
# Emitted when the user taps a card — parent uses this to show the detail popup.
|
|
signal card_selected(dto: Dictionary, def: Dictionary)
|
|
|
|
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
|
|
|
func _ready() -> void:
|
|
_populate_type_options()
|
|
_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)
|
|
get_viewport().size_changed.connect(_update_columns)
|
|
_reload()
|
|
|
|
|
|
func _exit_tree() -> void:
|
|
if get_viewport() and get_viewport().size_changed.is_connected(_update_columns):
|
|
get_viewport().size_changed.disconnect(_update_columns)
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
if _search_timer > 0.0:
|
|
_search_timer -= delta
|
|
if _search_timer <= 0.0:
|
|
_search_timer = 0.0
|
|
_reload()
|
|
|
|
# ── Option population ─────────────────────────────────────────────────────────
|
|
|
|
func _populate_type_options() -> void:
|
|
type_option.clear()
|
|
type_option.add_item("All Types")
|
|
for t in ["Spirit", "Repentance", "Terramorpha", "ArchSpirit", "Ruin", "Instinct"]:
|
|
type_option.add_item(t)
|
|
|
|
|
|
func _populate_affinity_options() -> void:
|
|
affinity_option.clear()
|
|
affinity_option.add_item("All Affinities")
|
|
for a in [
|
|
# Basic
|
|
"Void", "Aether", "Lake", "Hill", "Forest", "Field",
|
|
# Non-basic
|
|
"Ocean", "Island", "Marsh", "Swamp",
|
|
"Mountain", "Steppe", "Fort",
|
|
"Plains", "Grove", "Jungle",
|
|
"PerfectAether", "PureVoid",
|
|
]:
|
|
affinity_option.add_item(a)
|
|
|
|
# ── Responsive columns ────────────────────────────────────────────────────────
|
|
|
|
func _update_columns() -> void:
|
|
var available_width := card_grid.size.x
|
|
if available_width <= 0.0:
|
|
available_width = get_viewport_rect().size.x - 16.0
|
|
card_grid.columns = max(1, int(available_width / CARD_WIDTH_ESTIMATE))
|
|
|
|
# ── Data fetching ─────────────────────────────────────────────────────────────
|
|
|
|
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
|
|
_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)
|
|
|
|
Network.request(
|
|
"collection.get_cards",
|
|
payload,
|
|
func(_t: String, p: Variant): _on_cards_response(p, page),
|
|
func(_code: String, msg: String): _on_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_error(msg: String) -> void:
|
|
_loading = false
|
|
_set_status("empty")
|
|
push_warning("CardsView: %s" % msg)
|
|
|
|
# ── Button handlers ───────────────────────────────────────────────────────────
|
|
|
|
func _on_search_changed(_text: String) -> void:
|
|
_search_timer = _SEARCH_DEBOUNCE
|
|
|
|
|
|
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:
|
|
_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
|
|
return
|
|
|
|
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))
|
|
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)
|
|
|
|
card_grid.add_child(wrapper)
|
|
_card_entries[def_id] = {
|
|
"count": 1,
|
|
"label": count_label,
|
|
}
|
|
|
|
|
|
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 _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)
|