feat: overhaul UI main and profile
This commit is contained in:
@@ -9,6 +9,9 @@ signal avatar_changed(url: String)
|
||||
# Profile data
|
||||
var profile: Dictionary = {}
|
||||
var stats: Dictionary = {}
|
||||
var wallet: Dictionary = {"gold": 0, "star": 0}
|
||||
var inventory: Array = []
|
||||
var loadout: Dictionary = {"head": "", "costume": "", "glove": "", "accessory": ""}
|
||||
var is_profile_loaded: bool = false
|
||||
|
||||
# Nakama storage collection names
|
||||
@@ -47,6 +50,8 @@ func load_profile() -> Dictionary:
|
||||
# Reset state first to ensure no old account data carries over
|
||||
profile = {}
|
||||
stats = {}
|
||||
wallet = {"gold": 0, "star": 0}
|
||||
inventory = []
|
||||
is_profile_loaded = false
|
||||
|
||||
if not NakamaManager.session:
|
||||
@@ -84,6 +89,18 @@ func load_profile() -> Dictionary:
|
||||
var stored_data = JSON.parse_string(storage_result.objects[0].value)
|
||||
if stored_data:
|
||||
profile.merge(stored_data, true)
|
||||
if stored_data.has("loadout"):
|
||||
loadout = stored_data["loadout"]
|
||||
|
||||
# Parse Wallet
|
||||
if account.wallet:
|
||||
var w_data = JSON.parse_string(account.wallet)
|
||||
if w_data:
|
||||
wallet["gold"] = w_data.get("gold", 0)
|
||||
wallet["star"] = w_data.get("star", 0)
|
||||
|
||||
# Load Inventory
|
||||
await load_inventory()
|
||||
|
||||
# Load stats
|
||||
await load_stats()
|
||||
@@ -98,6 +115,21 @@ func load_profile() -> Dictionary:
|
||||
|
||||
return profile
|
||||
|
||||
func load_inventory() -> void:
|
||||
if not NakamaManager.session: return
|
||||
|
||||
inventory.clear()
|
||||
var result = await NakamaManager.client.list_storage_objects_async(
|
||||
NakamaManager.session,
|
||||
"inventory",
|
||||
NakamaManager.session.user_id,
|
||||
100
|
||||
)
|
||||
|
||||
if not result.is_exception() and result.objects:
|
||||
for obj in result.objects:
|
||||
inventory.append(obj.key)
|
||||
|
||||
func load_stats() -> Dictionary:
|
||||
# Reset stats first to ensure fresh data for new logins
|
||||
stats = {}
|
||||
@@ -223,7 +255,8 @@ func _save_profile_data() -> bool:
|
||||
"avatar_index": profile.get("avatar_index", 0),
|
||||
"bio": profile.get("bio", ""),
|
||||
"country": profile.get("country", ""),
|
||||
"language": profile.get("language", "en")
|
||||
"language": profile.get("language", "en"),
|
||||
"loadout": loadout
|
||||
}
|
||||
|
||||
var write_obj := NakamaWriteStorageObject.new(
|
||||
@@ -247,6 +280,41 @@ func _save_profile_data() -> bool:
|
||||
emit_signal("profile_updated")
|
||||
return true
|
||||
|
||||
func update_loadout(category: String, item_id: String) -> bool:
|
||||
if not loadout.has(category):
|
||||
return false
|
||||
loadout[category] = item_id
|
||||
return await _save_profile_data()
|
||||
|
||||
func purchase_item(item_id: String, price_gold: int, price_star: int, category: String) -> bool:
|
||||
if not NakamaManager.session: return false
|
||||
|
||||
var payload = JSON.stringify({
|
||||
"item_id": item_id,
|
||||
"price_gold": price_gold,
|
||||
"price_star": price_star,
|
||||
"category": category
|
||||
})
|
||||
|
||||
var result = await NakamaManager.client.rpc_async(
|
||||
NakamaManager.session,
|
||||
"purchase_item",
|
||||
payload
|
||||
)
|
||||
|
||||
if result.is_exception():
|
||||
push_error("[UserProfileManager] Purchase failed: ", result.get_exception().message)
|
||||
return false
|
||||
|
||||
# Update local cache
|
||||
if price_gold > 0: wallet["gold"] -= price_gold
|
||||
if price_star > 0: wallet["star"] -= price_star
|
||||
if not inventory.has(item_id):
|
||||
inventory.append(item_id)
|
||||
|
||||
emit_signal("profile_updated")
|
||||
return true
|
||||
|
||||
# =============================================================================
|
||||
# Stats Management
|
||||
# =============================================================================
|
||||
|
||||
Reference in New Issue
Block a user