117 lines
3.7 KiB
GDScript
117 lines
3.7 KiB
GDScript
extends PanelContainer
|
||
## Liderlik tablosu — 👣 adım / ⭐ level / 🪙 gold sekmeleri, top 20 + kendi sıran.
|
||
## Kod ile kurulur. Node adı "LeaderboardPanel".
|
||
|
||
const PANEL_HEIGHT := 560.0
|
||
|
||
var _list: VBoxContainer
|
||
var _tabs := {}
|
||
var _my_rank_lbl: Label
|
||
var _by := "steps"
|
||
|
||
func _ready() -> void:
|
||
name = "LeaderboardPanel"
|
||
visible = false
|
||
_build_ui()
|
||
|
||
func _build_ui() -> void:
|
||
set_anchors_preset(Control.PRESET_BOTTOM_WIDE)
|
||
offset_top = -PANEL_HEIGHT
|
||
offset_left = 8
|
||
offset_right = -8
|
||
offset_bottom = -8
|
||
var sb := StyleBoxFlat.new()
|
||
sb.bg_color = Color(0.12, 0.10, 0.13, 0.97)
|
||
sb.set_corner_radius_all(10)
|
||
add_theme_stylebox_override("panel", sb)
|
||
|
||
var margin := MarginContainer.new()
|
||
margin.add_theme_constant_override("margin_left", 14)
|
||
margin.add_theme_constant_override("margin_right", 14)
|
||
margin.add_theme_constant_override("margin_top", 10)
|
||
margin.add_theme_constant_override("margin_bottom", 10)
|
||
add_child(margin)
|
||
|
||
var vbox := VBoxContainer.new()
|
||
vbox.add_theme_constant_override("separation", 6)
|
||
margin.add_child(vbox)
|
||
|
||
var top := HBoxContainer.new()
|
||
var title := Label.new()
|
||
title.text = "🏆 Liderlik"
|
||
title.add_theme_font_size_override("font_size", 18)
|
||
title.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
top.add_child(title)
|
||
for entry in [["steps", "👣"], ["level", "⭐"], ["gold", "🪙"]]:
|
||
var btn := Button.new()
|
||
btn.text = str(entry[1])
|
||
btn.toggle_mode = true
|
||
var key := str(entry[0])
|
||
btn.pressed.connect(func() -> void: _switch(key))
|
||
top.add_child(btn)
|
||
_tabs[key] = btn
|
||
(_tabs["steps"] as Button).button_pressed = true
|
||
var close_btn := Button.new()
|
||
close_btn.text = "✕"
|
||
close_btn.pressed.connect(func() -> void: visible = false)
|
||
top.add_child(close_btn)
|
||
vbox.add_child(top)
|
||
|
||
_my_rank_lbl = Label.new()
|
||
_my_rank_lbl.add_theme_font_size_override("font_size", 13)
|
||
_my_rank_lbl.add_theme_color_override("font_color", Color(0.75, 0.7, 0.55))
|
||
vbox.add_child(_my_rank_lbl)
|
||
|
||
var scroll := ScrollContainer.new()
|
||
scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL
|
||
scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
|
||
vbox.add_child(scroll)
|
||
|
||
_list = VBoxContainer.new()
|
||
_list.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
_list.add_theme_constant_override("separation", 4)
|
||
scroll.add_child(_list)
|
||
|
||
func open() -> void:
|
||
visible = true
|
||
await _render()
|
||
|
||
func _switch(by: String) -> void:
|
||
_by = by
|
||
for key in _tabs:
|
||
(_tabs[key] as Button).button_pressed = key == by
|
||
_render()
|
||
|
||
func _render() -> void:
|
||
for c in _list.get_children():
|
||
c.queue_free()
|
||
var res: Dictionary = await GameState.leaderboard(_by)
|
||
if not res.ok or not (res.body is Dictionary):
|
||
_my_rank_lbl.text = "Yüklenemedi"
|
||
return
|
||
var d: Dictionary = res.body
|
||
_my_rank_lbl.text = "Senin sıran: #%d" % int(d.get("my_rank", 0))
|
||
var unit := {"steps": "👣", "level": "⭐", "gold": "🪙"}.get(_by, "")
|
||
for r in d.get("rows", []):
|
||
if r is Dictionary:
|
||
_list.add_child(_row(r, str(unit)))
|
||
|
||
func _row(r: Dictionary, unit: String) -> HBoxContainer:
|
||
var row := HBoxContainer.new()
|
||
row.add_theme_constant_override("separation", 10)
|
||
var me: bool = bool(r.get("me", false))
|
||
var rank := int(r.get("rank", 0))
|
||
var rank_lbl := Label.new()
|
||
rank_lbl.text = ["🥇", "🥈", "🥉"][rank - 1] if rank <= 3 else "#%d" % rank
|
||
rank_lbl.custom_minimum_size = Vector2(44, 0)
|
||
row.add_child(rank_lbl)
|
||
var name_lbl := Label.new()
|
||
name_lbl.text = "%s (Lv %d)" % [str(r.get("name", "")), int(r.get("level", 1))]
|
||
name_lbl.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
if me:
|
||
name_lbl.add_theme_color_override("font_color", Color(0.5, 0.9, 0.5))
|
||
row.add_child(name_lbl)
|
||
var val_lbl := Label.new()
|
||
val_lbl.text = "%s %d" % [unit, int(r.get("value", 0))]
|
||
row.add_child(val_lbl)
|
||
return row
|