196 lines
5.1 KiB
GDScript
196 lines
5.1 KiB
GDScript
extends Node
|
|
## Local player-specific cache for collection payloads.
|
|
##
|
|
## Stored under:
|
|
## user://collection/<player_id>/decks.json
|
|
## user://collection/<player_id>/deck_<deck_id>.json
|
|
## user://collection/<player_id>/cards_<query_hash>.json
|
|
|
|
const CACHE_ROOT := "user://collection"
|
|
|
|
var _loaded_player_id : String = ""
|
|
var _cards_pages : Dictionary = {}
|
|
var _decks_payload : Dictionary = {}
|
|
var _deck_payloads : Dictionary = {}
|
|
|
|
|
|
func _ready() -> void:
|
|
_ensure_loaded()
|
|
|
|
|
|
func cards_query_key(
|
|
page: int,
|
|
page_size: int,
|
|
name_filter: String = "",
|
|
card_type_filter: String = "",
|
|
affinity_filter: String = "",
|
|
tier_filter: Variant = null) -> String:
|
|
var tier_text := ""
|
|
if tier_filter != null:
|
|
tier_text = str(tier_filter)
|
|
return "p=%d|ps=%d|n=%s|t=%s|a=%s|tier=%s" % [
|
|
page,
|
|
page_size,
|
|
name_filter,
|
|
card_type_filter,
|
|
affinity_filter,
|
|
tier_text,
|
|
]
|
|
|
|
|
|
func get_cards_response(query_key: String) -> Dictionary:
|
|
_ensure_loaded()
|
|
if _loaded_player_id.is_empty():
|
|
return {}
|
|
if _cards_pages.has(query_key):
|
|
var cached: Variant = _cards_pages.get(query_key, {})
|
|
if typeof(cached) == TYPE_DICTIONARY:
|
|
return (cached as Dictionary).duplicate(true)
|
|
return {}
|
|
|
|
var path := _cards_file_path(query_key)
|
|
var payload := _read_json_file(path)
|
|
if not payload.is_empty():
|
|
_cards_pages[query_key] = payload
|
|
return payload.duplicate(true)
|
|
return {}
|
|
|
|
|
|
func store_cards_response(query_key: String, payload: Dictionary) -> void:
|
|
_ensure_loaded()
|
|
if _loaded_player_id.is_empty():
|
|
return
|
|
var copy := payload.duplicate(true)
|
|
_cards_pages[query_key] = copy
|
|
_write_json_file(_cards_file_path(query_key), copy)
|
|
|
|
|
|
func get_decks_response() -> Dictionary:
|
|
_ensure_loaded()
|
|
if _loaded_player_id.is_empty():
|
|
return {}
|
|
if not _decks_payload.is_empty():
|
|
return _decks_payload.duplicate(true)
|
|
_decks_payload = _read_json_file(_decks_file_path())
|
|
return _decks_payload.duplicate(true)
|
|
|
|
|
|
func store_decks_response(payload: Dictionary) -> void:
|
|
_ensure_loaded()
|
|
if _loaded_player_id.is_empty():
|
|
return
|
|
_decks_payload = payload.duplicate(true)
|
|
_write_json_file(_decks_file_path(), _decks_payload)
|
|
|
|
|
|
func get_deck_response(deck_id: String) -> Dictionary:
|
|
_ensure_loaded()
|
|
if _loaded_player_id.is_empty() or deck_id.is_empty():
|
|
return {}
|
|
if _deck_payloads.has(deck_id):
|
|
var cached: Variant = _deck_payloads.get(deck_id, {})
|
|
if typeof(cached) == TYPE_DICTIONARY:
|
|
return (cached as Dictionary).duplicate(true)
|
|
return {}
|
|
|
|
var payload := _read_json_file(_deck_file_path(deck_id))
|
|
if not payload.is_empty():
|
|
_deck_payloads[deck_id] = payload
|
|
return payload.duplicate(true)
|
|
return {}
|
|
|
|
|
|
func store_deck_response(deck_id: String, payload: Dictionary) -> void:
|
|
_ensure_loaded()
|
|
if _loaded_player_id.is_empty() or deck_id.is_empty():
|
|
return
|
|
var copy := payload.duplicate(true)
|
|
_deck_payloads[deck_id] = copy
|
|
_write_json_file(_deck_file_path(deck_id), copy)
|
|
|
|
|
|
func remove_deck(deck_id: String) -> void:
|
|
_ensure_loaded()
|
|
if _loaded_player_id.is_empty() or deck_id.is_empty():
|
|
return
|
|
_deck_payloads.erase(deck_id)
|
|
var path := _deck_file_path(deck_id)
|
|
if FileAccess.file_exists(path):
|
|
DirAccess.remove_absolute(path)
|
|
|
|
var decks_payload := get_decks_response()
|
|
if decks_payload.is_empty():
|
|
return
|
|
|
|
var decks_raw : Variant = decks_payload.get("decks", [])
|
|
if typeof(decks_raw) != TYPE_ARRAY:
|
|
return
|
|
|
|
var next_decks : Array = []
|
|
for deck in decks_raw as Array:
|
|
if typeof(deck) != TYPE_DICTIONARY:
|
|
continue
|
|
var dto := deck as Dictionary
|
|
if str(dto.get("id", "")) != deck_id:
|
|
next_decks.append(dto)
|
|
|
|
_decks_payload = { "decks": next_decks }
|
|
_write_json_file(_decks_file_path(), _decks_payload)
|
|
|
|
|
|
func _ensure_loaded() -> void:
|
|
var current_player_id := Auth.user_id
|
|
if current_player_id == _loaded_player_id:
|
|
return
|
|
_loaded_player_id = current_player_id
|
|
_cards_pages.clear()
|
|
_decks_payload.clear()
|
|
_deck_payloads.clear()
|
|
|
|
|
|
func _player_dir() -> String:
|
|
return "%s/%s" % [CACHE_ROOT, _sanitize_path_component(_loaded_player_id)]
|
|
|
|
|
|
func _cards_file_path(query_key: String) -> String:
|
|
return "%s/cards_%s.json" % [_player_dir(), query_key.md5_text()]
|
|
|
|
|
|
func _decks_file_path() -> String:
|
|
return "%s/decks.json" % _player_dir()
|
|
|
|
|
|
func _deck_file_path(deck_id: String) -> String:
|
|
return "%s/deck_%s.json" % [_player_dir(), _sanitize_path_component(deck_id)]
|
|
|
|
|
|
func _sanitize_path_component(value: String) -> String:
|
|
var safe := value.strip_edges()
|
|
if safe.is_empty():
|
|
return "anonymous"
|
|
return safe.replace("/", "_").replace("\\", "_").replace(":", "_")
|
|
|
|
|
|
func _read_json_file(path: String) -> Dictionary:
|
|
if not FileAccess.file_exists(path):
|
|
return {}
|
|
var file := FileAccess.open(path, FileAccess.READ)
|
|
if not file:
|
|
return {}
|
|
var text := file.get_as_text()
|
|
file.close()
|
|
var parsed: Variant = JSON.parse_string(text)
|
|
if typeof(parsed) == TYPE_DICTIONARY:
|
|
return parsed as Dictionary
|
|
return {}
|
|
|
|
|
|
func _write_json_file(path: String, payload: Dictionary) -> void:
|
|
DirAccess.make_dir_recursive_absolute(_player_dir())
|
|
var file := FileAccess.open(path, FileAccess.WRITE)
|
|
if not file:
|
|
push_warning("CollectionCache: could not write %s" % path)
|
|
return
|
|
file.store_string(JSON.stringify(payload))
|
|
file.close()
|