deck list importing (mtgo) is now working

This commit is contained in:
Nathan Singer 2025-04-24 20:55:09 -04:00
parent b82f5e4c19
commit 90912c062f
3 changed files with 64 additions and 41 deletions

View File

@ -1,15 +1,36 @@
extends Node 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") var _consts = preload("res://data/consts.gd")
func _init() -> void: func _init() -> void:
_req_headers = PackedStringArray(["User-Agent: " + _consts.APP_NAME + "/" + _consts.APP_VERSION, "Accept: */*"]) _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: 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: 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 var selected_entry = null
for entry in bulk_json: for entry in _bulk_data:
if entry[field] == search_query: if entry[field] == search_query:
selected_entry = entry selected_entry = entry
break break
continue continue
file = null
if selected_entry == null: if selected_entry == null:
return {} 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.make_dir_recursive("user://card_cache/" + selected_entry["id"] + "/")
dir = null 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.store_line(JSON.stringify(selected_entry, "\t"))
file = null file = null
print("Card: " + selected_entry["name"] + "(" + selected_entry["id"] + ") found, and cached.")
return selected_entry return selected_entry
func _fetch_card_img(data: Dictionary) -> Error: func _fetch_card_img(data: Dictionary) -> Error:
fetch_start.emit()
if FileAccess.file_exists("user://card_cache/" + data["id"] + "card.png"): if FileAccess.file_exists("user://card_cache/" + data["id"] + "card.png"):
return OK return OK
@ -94,12 +114,14 @@ func _fetch_card_img(data: Dictionary) -> Error:
img.save_png("user://card_cache/" + data["id"] + "/card.png") img.save_png("user://card_cache/" + data["id"] + "/card.png")
img = null img = null
fetch_done.emit()
return OK return OK
func get_bulk_data(force: bool) -> Error: 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: if force:
DirAccess.remove_absolute("user://card_cahce/bulk.json") DirAccess.remove_absolute("user://bulk.json")
else: else:
return OK 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.") push_error(_cache_error("PARSING") + "Failed to parse the Scryfall card results.")
return FAILED return FAILED
var dir = DirAccess.open("user://") var data_cache = FileAccess.open("user://bulk.json", FileAccess.WRITE)
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)
data_cache.store_string(unprocessed_body) data_cache.store_string(unprocessed_body)
data_cache = null data_cache = null
cache_done.emit()
return OK 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!")

View File

@ -66,43 +66,45 @@ var cards = "1 All That Glitters\n1 Ancestral Mask\n1 Angelic Destiny\n1 Arcane
1 Sythis, Harvest's Hand" 1 Sythis, Harvest's Hand"
signal done_fetching
func _init() -> void: func _init() -> void:
_decklist = Dictionary() _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") var lines = decklist.split("\n")
for line in lines: for line in lines:
var words = line.split(" ", false, 1) var words = line.split(" ", false, 1)
if words.size() != 2: if words.size() != 2:
continue continue
words[1] = words[1].replace(" ", "+") _cards[words[1]] = words[0]
links["https://api.scryfall.com/cards/named?exact=" + words[1]] = words[0] return _cards
return links
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() var cache = _caching.new()
add_child(cache) 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: for card in _decklist:
print(card + " : " + _decklist[card]) print(card + " : " + _decklist[card])
func _ready() -> void: func _ready() -> void:
var queries = convert_mtgo_to_http(cards) do_decklist_grab(cards)
_do_decklist_grab(queries) _show_decklist()

View File

@ -16,10 +16,10 @@ func _ready() -> void:
add_child(player) add_child(player)
move_child(player, 0) 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() var deck = deck_input.new()
#add_child(deck) add_child(deck)
pass # Replace with function body. pass # Replace with function body.