Files
sor/Client/origin-client/scenes/account/account_screen.gd
T

298 lines
9.7 KiB
GDScript

extends Control
## Account Management screen.
const NOTIF_ROW = preload("res://scenes/account/components/NotificationRow.tscn")
@onready var back_btn : Button = %BackBtn
@onready var display_label : Label = %DisplayLabel
@onready var op_label : Label = %OpLabel
@onready var rank_label : Label = %RankLabel
@onready var mailbox_list : VBoxContainer = %MailboxList
@onready var loading_label : Label = %LoadingLabel
@onready var empty_label : Label = %EmptyLabel
# ── Lifecycle ─────────────────────────────────────────────────────────────────
func _ready() -> void:
back_btn.pressed.connect(_on_back)
_load_profile()
_load_notifications()
# ── Profile ───────────────────────────────────────────────────────────────────
func _load_profile() -> void:
display_label.text = Auth.display_name
op_label.text = "◆ — OP"
rank_label.text = "Rank: —"
Network.request(
"account.get_profile",
{},
func(_type: String, p: Variant): _on_profile_ok(p),
func(_c: String, _m: String): pass
)
func _on_profile_ok(p: Variant) -> void:
var d := p as Dictionary
op_label.text = "◆ %d OP" % int(d.get("originPoints", 0))
rank_label.text = "Rank Score: %d" % int(d.get("rankScore", 1000))
# ── Notifications (mailbox) ──────────────────────────────────────────────────
func _load_notifications() -> void:
loading_label.visible = true
empty_label.visible = false
loading_label.text = "Loading…"
for c in mailbox_list.get_children():
c.queue_free()
Network.request(
"chat.notifications.get",
{ "unreadOnly": false },
func(_type: String, p: Variant): _on_notifications_ok(p),
func(_c: String, _m: String):
loading_label.visible = true
loading_label.text = "Failed to load mailbox."
)
func _on_notifications_ok(p: Variant) -> void:
loading_label.visible = false
var notifs : Array = (p as Dictionary).get("notifications", []) as Array
if notifs.is_empty():
empty_label.visible = true
return
for n in notifs:
var nd := n as Dictionary
var row := NOTIF_ROW.instantiate()
mailbox_list.add_child(row)
row.setup(nd)
row.open_requested.connect(_open_notification)
row.reply_requested.connect(_reply_to_notification)
row.delete_requested.connect(_delete_notification)
row.mark_read_requested.connect(_mark_notification_read)
func _mark_notification_read(notification_id: String) -> void:
if notification_id.is_empty():
return
Network.request(
"chat.notifications.read",
{ "notificationIds": [notification_id] },
func(_t: String, _p: Variant): _load_notifications(),
func(_c: String, _m: String): _load_notifications()
)
func _delete_notification(notification_id: String) -> void:
if notification_id.is_empty():
return
Network.request(
"chat.notifications.delete",
{ "notificationIds": [notification_id] },
func(_t: String, _p: Variant): _load_notifications(),
func(_c: String, _m: String): _load_notifications()
)
func _open_notification(notification: Dictionary) -> void:
var id := str(notification.get("id", ""))
if not id.is_empty() and notification.get("readAt", null) == null:
_mark_notification_read(id)
_show_notification_dialog(notification, false)
func _reply_to_notification(notification: Dictionary) -> void:
_show_notification_dialog(notification, true)
func _show_notification_dialog(notification: Dictionary, focus_reply: bool) -> void:
var dialog := AcceptDialog.new()
dialog.title = str(notification.get("title", "Mailbox Message"))
dialog.ok_button_text = "Close"
dialog.min_size = Vector2(700, 500)
var root := VBoxContainer.new()
root.custom_minimum_size = Vector2(660, 440)
root.add_theme_constant_override("separation", 8)
dialog.add_child(root)
var body := RichTextLabel.new()
body.fit_content = false
body.scroll_active = true
body.custom_minimum_size = Vector2(0, 220)
body.size_flags_vertical = Control.SIZE_EXPAND_FILL
body.bbcode_enabled = true
body.text = _build_notification_text(notification)
root.add_child(body)
var thread_status := Label.new()
thread_status.modulate = Color(0.7, 0.7, 0.75)
thread_status.text = ""
root.add_child(thread_status)
var reply_box := TextEdit.new()
reply_box.custom_minimum_size = Vector2(0, 100)
reply_box.placeholder_text = "Write a reply to support..."
reply_box.visible = false
root.add_child(reply_box)
var actions := HBoxContainer.new()
actions.add_theme_constant_override("separation", 8)
root.add_child(actions)
var send_btn := Button.new()
send_btn.text = "Send Reply"
send_btn.visible = false
actions.add_child(send_btn)
var delete_btn := Button.new()
delete_btn.text = "Delete"
delete_btn.modulate = Color(1.0, 0.5, 0.5)
delete_btn.pressed.connect(func() -> void:
var nid := str(notification.get("id", ""))
if not nid.is_empty():
_delete_notification(nid)
dialog.queue_free())
actions.add_child(delete_btn)
add_child(dialog)
dialog.popup_centered()
var parsed := _parse_payload(notification)
var bug_report_id := str(parsed.get("bugReportId", ""))
var can_reply := str(notification.get("notificationType", "")) == "admin_response" and not bug_report_id.is_empty()
if can_reply:
reply_box.visible = true
send_btn.visible = true
thread_status.text = "Loading conversation…"
_load_bug_report_thread_into(body, thread_status, bug_report_id)
send_btn.pressed.connect(func() -> void:
var content := reply_box.text.strip_edges()
if content.is_empty():
thread_status.text = "Reply cannot be empty."
thread_status.modulate = Color(1.0, 0.5, 0.5)
return
send_btn.disabled = true
thread_status.text = "Sending reply…"
thread_status.modulate = Color(0.7, 0.7, 0.75)
Network.request(
"support.reply_bug_report",
{ "bugReportId": bug_report_id, "content": content },
func(_t: String, _p: Variant):
reply_box.text = ""
send_btn.disabled = false
thread_status.text = "Reply sent."
thread_status.modulate = Color(0.5, 0.9, 0.5)
_load_bug_report_thread_into(body, thread_status, bug_report_id),
func(code: String, msg: String):
send_btn.disabled = false
thread_status.text = "Reply failed (%s): %s" % [code, msg]
thread_status.modulate = Color(1.0, 0.5, 0.5)
)
)
if focus_reply:
reply_box.grab_focus()
func _load_bug_report_thread_into(body: RichTextLabel, status_label: Label, bug_report_id: String) -> void:
Network.request(
"support.get_bug_report_thread",
{ "bugReportId": bug_report_id },
func(_t: String, p: Variant):
var thread := (p as Dictionary).get("thread", {}) as Dictionary
if thread.is_empty():
status_label.text = "No conversation details available."
status_label.modulate = Color(1.0, 0.5, 0.5)
return
body.text = _build_thread_text(thread)
status_label.text = ""
,
func(code: String, msg: String):
status_label.text = "Failed to load thread (%s): %s" % [code, msg]
status_label.modulate = Color(1.0, 0.5, 0.5)
)
func _build_notification_text(notification: Dictionary) -> String:
var lines: Array[String] = []
lines.append("[b]%s[/b]" % str(notification.get("title", "Mailbox Message")))
lines.append(str(notification.get("body", "")))
lines.append("")
lines.append("Type: %s" % str(notification.get("notificationType", "unknown")))
lines.append("Created: %s" % _format_timestamp(str(notification.get("createdAt", ""))))
return "\n".join(lines)
func _build_thread_text(thread: Dictionary) -> String:
var lines: Array[String] = []
lines.append("[b]Support Thread[/b]")
lines.append("Subject: %s" % str(thread.get("title", "")))
lines.append("Status: %s" % str(thread.get("status", "Open")))
lines.append("Category: %s" % str(thread.get("category", "other")))
lines.append("Created: %s" % _format_timestamp(str(thread.get("createdAt", ""))))
lines.append("")
lines.append("[b]Original Report[/b]")
lines.append(str(thread.get("description", "")))
var steps := str(thread.get("stepsToReproduce", ""))
if not steps.is_empty():
lines.append("")
lines.append("[b]Steps to Reproduce[/b]")
lines.append(steps)
var responses : Array = thread.get("responses", []) as Array
if not responses.is_empty():
lines.append("")
lines.append("[b]Conversation[/b]")
for r in responses:
var rd := r as Dictionary
lines.append("\n• [b]%s[/b] (%s)" % [
str(rd.get("authorDisplayName", "Support")),
_format_timestamp(str(rd.get("createdAt", "")))
])
lines.append(str(rd.get("content", "")))
return "\n".join(lines)
func _parse_payload(notification: Dictionary) -> Dictionary:
var payload_json := str(notification.get("payloadJson", ""))
if payload_json.is_empty():
return {}
var parsed := JSON.parse_string(payload_json)
if typeof(parsed) == TYPE_DICTIONARY:
return parsed as Dictionary
return {}
func _format_timestamp(iso: String) -> String:
if iso.is_empty():
return "—"
var t_idx := iso.find("T")
if t_idx < 0:
return iso
var day := iso.substr(0, t_idx)
var time_part := iso.substr(t_idx + 1)
var z_idx := time_part.find("Z")
if z_idx >= 0:
time_part = time_part.substr(0, z_idx)
if time_part.length() >= 5:
time_part = time_part.substr(0, 5)
return "%s %s" % [day, time_part]
# ── Navigation ────────────────────────────────────────────────────────────────
func _on_back() -> void:
get_tree().change_scene_to_file("res://scenes/main/MainScreen.tscn")