66 lines
2.0 KiB
GDScript
66 lines
2.0 KiB
GDScript
extends Node
|
|
## Global Admin Manager -- listens for F10 and handles Admin Panel instantiation.
|
|
|
|
var _admin_panel_instance: Control = null
|
|
var _is_checking: bool = false
|
|
|
|
func _ready() -> void:
|
|
# Persistent across scene changes
|
|
process_mode = PROCESS_MODE_ALWAYS
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if event is InputEventKey and event.pressed and event.keycode == KEY_F10:
|
|
toggle_admin_panel()
|
|
|
|
func toggle_admin_panel() -> void:
|
|
if _admin_panel_instance and _admin_panel_instance.visible:
|
|
_admin_panel_instance.hide()
|
|
return
|
|
|
|
if _is_checking: return
|
|
_is_checking = true
|
|
|
|
# Verify admin status before showing
|
|
var is_admin = await _check_admin_status()
|
|
_is_checking = false
|
|
|
|
if not is_admin:
|
|
print("[AdminManager] Access denied: Not an admin.")
|
|
return
|
|
|
|
_show_panel()
|
|
|
|
func _show_panel() -> void:
|
|
if not is_instance_valid(_admin_panel_instance):
|
|
print("[AdminManager] Instantiating new Admin Panel...")
|
|
var scene = load("res://scenes/ui/admin_panel.tscn")
|
|
if scene:
|
|
_admin_panel_instance = scene.instantiate()
|
|
get_tree().root.add_child(_admin_panel_instance)
|
|
else:
|
|
print("[AdminManager] ERROR: Could not load admin_panel.tscn")
|
|
return
|
|
|
|
if is_instance_valid(_admin_panel_instance):
|
|
print("[AdminManager] Showing Admin Panel.")
|
|
# Move to bottom of root's children to ensure it's on top of new scenes
|
|
get_tree().root.move_child(_admin_panel_instance, -1)
|
|
_admin_panel_instance.show_panel()
|
|
else:
|
|
print("[AdminManager] ERROR: Admin Panel instance is invalid after instantiation attempt.")
|
|
|
|
func _check_admin_status() -> bool:
|
|
if not NakamaManager.client or not NakamaManager.session:
|
|
return false
|
|
|
|
var account = await NakamaManager.client.get_account_async(NakamaManager.session)
|
|
if account.is_exception():
|
|
return false
|
|
|
|
var metadata_str: String = account.user.metadata if account.user.metadata else "{}"
|
|
var metadata = JSON.parse_string(metadata_str)
|
|
if metadata and metadata is Dictionary:
|
|
var role = metadata.get("role", "")
|
|
return role in ["admin", "moderator", "owner"]
|
|
return false
|