From 90912c062f26383ea1b5a9dd64cf44117d6442ce Mon Sep 17 00:00:00 2001 From: Nathan Singer Date: Thu, 24 Apr 2025 20:55:09 -0400 Subject: [PATCH] deck list importing (mtgo) is now working --- caching.gd | 55 +++++++++++++++++++++++++++++++++++---------------- deck_input.gd | 44 +++++++++++++++++++++-------------------- tabletop.gd | 6 +++--- 3 files changed, 64 insertions(+), 41 deletions(-) diff --git a/caching.gd b/caching.gd index cf9e3fb..2b7c7c2 100644 --- a/caching.gd +++ b/caching.gd @@ -1,15 +1,36 @@ extends Node +var _req_headers: PackedStringArray -signal cache_done +var _bulk_data: Array +signal fetch_done +var _emitted_done = 0 +signal fetch_start +var _emitted_start = 0 -var _req_headers var _consts = preload("res://data/consts.gd") func _init() -> void: _req_headers = PackedStringArray(["User-Agent: " + _consts.APP_NAME + "/" + _consts.APP_VERSION, "Accept: */*"]) + + var file = FileAccess.open("user://bulk.json", FileAccess.READ) + _bulk_data = JSON.parse_string(file.get_as_text()) + file = null + + fetch_done.connect(_on_end_emit) + fetch_start.connect(_on_start_emit) + +func _on_start_emit() -> void: + _emitted_start += 1 + + +func _on_end_emit() -> void: + _emitted_done += 1 + +func has_emitted_all() -> bool: + return _emitted_start == _emitted_done func _cache_error(err: String) -> String: @@ -41,16 +62,12 @@ func get_card_data_from_id(id: String) -> Dictionary: func _get_card_data_from_bulk(field: String, search_query: String) -> Dictionary: - var file = FileAccess.open("user://card_cache/bulk.json", FileAccess.READ) - var bulk_json = JSON.parse_string(file.get_as_text()) - var selected_entry = null - for entry in bulk_json: + for entry in _bulk_data: if entry[field] == search_query: selected_entry = entry break continue - file = null if selected_entry == null: return {} @@ -62,13 +79,16 @@ func _get_card_data_from_bulk(field: String, search_query: String) -> Dictionary dir.make_dir_recursive("user://card_cache/" + selected_entry["id"] + "/") dir = null - file = FileAccess.open("user://card_cache/" + selected_entry["id"] + "/card.json", FileAccess.WRITE) + var file = FileAccess.open("user://card_cache/" + selected_entry["id"] + "/card.json", FileAccess.WRITE) file.store_line(JSON.stringify(selected_entry, "\t")) file = null + print("Card: " + selected_entry["name"] + "(" + selected_entry["id"] + ") found, and cached.") + return selected_entry func _fetch_card_img(data: Dictionary) -> Error: + fetch_start.emit() if FileAccess.file_exists("user://card_cache/" + data["id"] + "card.png"): return OK @@ -94,12 +114,14 @@ func _fetch_card_img(data: Dictionary) -> Error: img.save_png("user://card_cache/" + data["id"] + "/card.png") img = null + fetch_done.emit() + return OK func get_bulk_data(force: bool) -> Error: - if FileAccess.file_exists("user://card_cache/bulk.json"): + if FileAccess.file_exists("user://bulk.json"): if force: - DirAccess.remove_absolute("user://card_cahce/bulk.json") + DirAccess.remove_absolute("user://bulk.json") else: return OK @@ -139,14 +161,13 @@ func get_bulk_data(force: bool) -> Error: push_error(_cache_error("PARSING") + "Failed to parse the Scryfall card results.") return FAILED - var dir = DirAccess.open("user://") - dir.make_dir_recursive("user://card_cache/") # lets ensure the path is there - dir = null - - var data_cache = FileAccess.open("user://card_cache/bulk.json", FileAccess.WRITE) + var data_cache = FileAccess.open("user://bulk.json", FileAccess.WRITE) data_cache.store_string(unprocessed_body) data_cache = null - cache_done.emit() - return OK + +func _notification(what): + if what == NOTIFICATION_PREDELETE: + if _emitted_done != _emitted_start: + push_error("ERR::MEM::CACHE\nCache being deleted before all threads have finished processing!") diff --git a/deck_input.gd b/deck_input.gd index 291d244..2bac8ff 100644 --- a/deck_input.gd +++ b/deck_input.gd @@ -66,43 +66,45 @@ var cards = "1 All That Glitters\n1 Ancestral Mask\n1 Angelic Destiny\n1 Arcane 1 Sythis, Harvest's Hand" -signal done_fetching - func _init() -> void: _decklist = Dictionary() -func convert_mtgo_to_http(decklist: String) -> Dictionary: - var links = {} + +func _convert_mtgo_to_cache_lookup(decklist: String) -> Dictionary: + var _cards = {} var lines = decklist.split("\n") for line in lines: var words = line.split(" ", false, 1) if words.size() != 2: continue - words[1] = words[1].replace(" ", "+") - links["https://api.scryfall.com/cards/named?exact=" + words[1]] = words[0] - return links + _cards[words[1]] = words[0] + return _cards -func _do_decklist_grab(queries: Dictionary) -> void: +func _do_free(cache) -> void: + if !cache.has_emitted_all(): + return + cache.queue_free() + + +func do_decklist_grab(decklist: String) -> void: var cache = _caching.new() add_child(cache) - print("test") - for query in queries: - print("Test2") - var id = await cache.custom_query_fetch(query) - if id == "NONE": - continue - _decklist[id] = queries[query] - print("test3") - done_fetching.emit() - _show_decklist() -func _show_decklist(): + var queries = _convert_mtgo_to_cache_lookup(decklist) + for query in queries: + var entry = cache.get_card_data_from_name(query) + _decklist[entry["id"]] = queries[query] + + cache.fetch_done.connect(_do_free.bind(cache)) + +func _show_decklist() -> void: for card in _decklist: print(card + " : " + _decklist[card]) func _ready() -> void: - var queries = convert_mtgo_to_http(cards) - _do_decklist_grab(queries) + do_decklist_grab(cards) + _show_decklist() + diff --git a/tabletop.gd b/tabletop.gd index 2a2f05b..4ee33ed 100644 --- a/tabletop.gd +++ b/tabletop.gd @@ -16,10 +16,10 @@ func _ready() -> void: add_child(player) move_child(player, 0) - cache.get_card_data_from_name("1996 World Champion") + # cache.get_card_data_from_name("1996 World Champion") - #var deck = deck_input.new() - #add_child(deck) + var deck = deck_input.new() + add_child(deck) pass # Replace with function body.