Files
step3/scripts/ui/rz_store_screen.gd
T
2026-07-29 18:34:54 -05:00

122 lines
4.2 KiB
GDScript

extends VBoxContainer
const Brand := preload("res://scripts/ui/medmind_style.gd")
var main: Control
var balance_label: Label
var inventory_label: Label
var products_box: VBoxContainer
var status_label: Label
var confirm_dialog: ConfirmationDialog
var pending_sku := ""
func _ready() -> void:
size_flags_horizontal = Control.SIZE_EXPAND_FILL
size_flags_vertical = Control.SIZE_EXPAND_FILL
add_theme_constant_override("separation", 18)
var title := Label.new()
title.text = "Rz Coin Store"
title.add_theme_font_override("font", Brand.font)
title.add_theme_font_size_override("font_size", 30)
add_child(title)
var intro := Label.new()
intro.text = "Spend coins earned from correct, on-time first answers. Purchases use Rz Coins only."
intro.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
add_child(intro)
balance_label = Label.new()
balance_label.add_theme_font_size_override("font_size", 20)
add_child(balance_label)
inventory_label = Label.new()
add_child(inventory_label)
products_box = VBoxContainer.new()
products_box.add_theme_constant_override("separation", 12)
add_child(products_box)
status_label = Label.new()
status_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
add_child(status_label)
confirm_dialog = ConfirmationDialog.new()
confirm_dialog.confirmed.connect(_confirm_purchase)
add_child(confirm_dialog)
CreditsService.credits_changed.connect(_refresh)
CreditsService.credits_failed.connect(_show_error)
CreditsService.store_operation_completed.connect(_show_message)
_refresh()
func on_show(_payload: Variant = null) -> void:
CreditsService.refresh()
_refresh()
func _refresh() -> void:
balance_label.text = "%d Rz Coins" % CreditsService.credits
inventory_label.text = "Inventory: %d exam token(s) · %d QBank reset token(s)" % [
CreditsService.exam_tokens, CreditsService.qbank_reset_tokens]
for child in products_box.get_children():
child.queue_free()
for product_variant in CreditsService.products:
if typeof(product_variant) != TYPE_DICTIONARY:
continue
var product: Dictionary = product_variant
var card := PanelContainer.new()
card.add_theme_stylebox_override("panel", Brand.content_card_box(UIThemeService.is_dark_mode(), true, Brand.ROYAL_BLUE))
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 16)
card.add_child(row)
var copy := VBoxContainer.new()
copy.size_flags_horizontal = Control.SIZE_EXPAND_FILL
var name_label := Label.new()
name_label.text = str(product.get("name", "Store item"))
name_label.add_theme_font_size_override("font_size", 19)
copy.add_child(name_label)
var description := Label.new()
description.text = str(product.get("description", ""))
description.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
copy.add_child(description)
row.add_child(copy)
var buy := Button.new()
var price := int(product.get("coinPrice", 0))
buy.text = "Buy · %d coins" % price
buy.disabled = CreditsService.credits < price
buy.pressed.connect(_ask_purchase.bind(str(product.get("sku", "")), str(product.get("name", "item")), price))
row.add_child(buy)
products_box.add_child(card)
if CreditsService.qbank_reset_tokens > 0:
var reset := Button.new()
reset.text = "Use QBank Reset Token"
reset.tooltip_text = "Question eligibility resets; ELO, history, bookmarks, notes, reviews, and earning limits are preserved."
reset.pressed.connect(_ask_reset)
products_box.add_child(reset)
func _ask_purchase(sku: String, product_name: String, price: int) -> void:
pending_sku = sku
confirm_dialog.title = "Confirm purchase"
confirm_dialog.dialog_text = "Buy %s for %d Rz Coins?" % [product_name, price]
confirm_dialog.popup_centered()
func _confirm_purchase() -> void:
if pending_sku == "__reset__":
CreditsService.reset_qbank()
elif not pending_sku.is_empty():
CreditsService.purchase(pending_sku)
pending_sku = ""
func _ask_reset() -> void:
pending_sku = "__reset__"
confirm_dialog.title = "Reset QBank eligibility"
confirm_dialog.dialog_text = "Make all QBank questions eligible again? ELO, answer history, bookmarks, notes, reviews, and the rolling coin cap will be preserved."
confirm_dialog.popup_centered()
func _show_error(message: String) -> void:
status_label.text = message
func _show_message(message: String) -> void:
status_label.text = message
_refresh()