fixes dual cards not properly loading!

This commit is contained in:
Nathan Singer 2025-04-27 23:36:33 -04:00
parent d1ef1d6f4f
commit 86cc3bf8a8
4 changed files with 151 additions and 36 deletions

View File

@ -68,7 +68,7 @@ func _get_dict_from_file(filepath: String) -> Dictionary:
## _name: String [br]
## A wrapper for searching for a card by name. Use **get_card_data_from_id** where possible, as it avoids an expensive search for the new card, if the card has been cached already.
func get_card_data_from_name(_name: String) -> Dictionary:
return _get_card_data_from_bulk("name", _name)
return _get_card_data_from_bulk(_search_results_name(_name))
## get_card_data_from_id
@ -78,34 +78,50 @@ func get_card_data_from_name(_name: String) -> Dictionary:
func get_card_data_from_id(id: String) -> Dictionary:
if FileAccess.file_exists("user://card_cache/" + id + "/card.json"):
return _get_dict_from_file("user://card_cache/" + id + "/card.json")
return _get_card_data_from_bulk("id", id)
return _get_card_data_from_bulk(_search_results_generic("id", id))
func _get_card_data_from_bulk(field: String, search_query: String) -> Dictionary:
func _search_results_name(search_query: String) -> Dictionary:
var selected_entry = null
for entry in _bulk_data:
if entry["layout"] == "art_series":
continue
var entry_name = entry["name"]
if entry_name.contains("//"):
entry_name = entry_name.left(entry_name.find("//") - 1)
if entry_name == search_query:
return entry
push_error("Could not find desired card {" + search_query + "}")
return {}
func _search_results_generic(field: String, search_query: String) -> Dictionary:
var selected_entry = null
for entry in _bulk_data:
if entry["layout"] == "art_series":
continue
if entry[field] == search_query:
selected_entry = entry
break
continue
return entry[field]
if selected_entry == null:
return {}
push_error("Could not find desired card {" + search_query + "}")
return {}
if selected_entry["image_status"] != "missing":
_fetch_card_img(selected_entry)
func _get_card_data_from_bulk(dict_entry: Dictionary) -> Dictionary:
if dict_entry["image_status"] != "missing":
_fetch_card_img(dict_entry)
var dir = DirAccess.open("user://")
dir.make_dir_recursive("user://card_cache/" + selected_entry["id"] + "/")
dir.make_dir_recursive("user://card_cache/" + dict_entry["id"] + "/")
dir = null
var file = FileAccess.open("user://card_cache/" + selected_entry["id"] + "/card.json", FileAccess.WRITE)
file.store_line(JSON.stringify(selected_entry, "\t"))
var file = FileAccess.open("user://card_cache/" + dict_entry["id"] + "/card.json", FileAccess.WRITE)
file.store_line(JSON.stringify(dict_entry, "\t"))
file.close()
print("Card: " + selected_entry["name"] + "(" + selected_entry["id"] + ") found, and cached.")
print("Card: " + dict_entry["name"] + " (" + dict_entry["id"] + ") found, and cached.")
return selected_entry
return dict_entry
func _fetch_card_img(data: Dictionary) -> Error:
fetch_start.emit()
@ -144,6 +160,7 @@ func get_bulk_data(force: bool) -> Error:
DirAccess.remove_absolute("user://bulk.json")
else:
return OK
print("downloading ")
var httpr = HTTPRequest.new()
add_child(httpr)

View File

@ -4,12 +4,25 @@ var _caching = preload("res://caching.gd")
var _decklist
var cards: String
func _init(_cards: String) -> void:
cards = _cards
func _init() -> void:
_decklist = Dictionary()
func _write_to_decks(_decks: Array) -> void:
if FileAccess.file_exists("user://decks.json"):
DirAccess.remove_absolute("user://decks.json")
var file = FileAccess.open("user://decks.json", FileAccess.WRITE)
file.store_line(JSON.stringify(_decks))
file.close()
func load_decks() -> Array:
if !FileAccess.file_exists("user://decks.json"):
return []
var file = FileAccess.open("user://decks.json", FileAccess.READ)
var file_text = file.get_as_text()
file.close()
return JSON.parse_string(file_text)
func _convert_mtgo_to_cache_lookup(decklist: String) -> Dictionary:
var _cards = {}
@ -29,14 +42,26 @@ func _do_free(cache) -> void:
cache.queue_free()
func do_decklist_grab(decklist: String) -> void:
func add_new_deck(cards: String, name: String, about = "") -> void:
var _queries = _convert_mtgo_to_cache_lookup(cards)
_do_decklist_cache(_queries)
var _decks = load_decks()
_decks.push_back({"name": name, "about": about, "decklist": _queries})
_write_to_decks(_decks)
func _do_decklist_cache(_queries: Dictionary) -> void:
var cache = _caching.new()
add_child(cache)
cache.setup()
var queries = _convert_mtgo_to_cache_lookup(decklist)
for query in queries:
for query in _queries:
var entry = cache.get_card_data_from_name(query)
_decklist[entry["id"]] = queries[query]
if entry.size() == 0:
push_error("Failed to find card: " + query)
continue
_decklist[entry["id"]] = _queries[query]
cache.fetch_done.connect(_do_free.bind(cache))
@ -44,8 +69,3 @@ func do_decklist_grab(decklist: String) -> void:
func _show_decklist() -> void:
for card in _decklist:
print(card + " : " + _decklist[card])
func _ready() -> void:
do_decklist_grab(cards)
_show_decklist()

View File

@ -7,12 +7,6 @@ var fields: Array[Node] = []
var decks: Array[Dictionary]
func _load_decks():
if !FileAccess.file_exists("user://decks.json"):
return # no loaded decks
var file = FileAccess.open("user://decks.json", FileAccess.READ)
decks = JSON.parse_string(file.get_as_text())
file.close()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:

View File

@ -5,7 +5,90 @@ var deck_input = preload("res://deck_input.gd")
var _caching = preload("res://caching.gd")
var cards = "1 All That Glitters\n1 Ancestral Mask\n1 Angelic Destiny\n1 Arcane Signet\n1 Archon of Sun's Grace\n1 Austere Command\n1 Banishing Light\n1 Bear Umbra\n1 Blossoming Sands\n1 Canopy Vista\n1 Celestial Mantle\n1 Collective Resistance\n1 Command Tower\n1 Danitha Capashen, Paragon\n1 Danitha, New Benalia's Light\n1 Darksteel Mutation\n1 Daybreak Coronet\n1 Destiny Spinner\n1 Eidolon of Blossoms\n1 Eidolon of Countless Battles\n1 Ellivere of the Wild Court\n1 Enchantress's Presence\n1 Envoy of the Ancestors\n1 Ethereal Armor\n1 Fertile Ground\n13 Forest\n1 Frantic Strength\n1 Generous Gift\n1 Gilded Lotus\n1 Glittering Frost\n1 Grasp of Fate\n1 Gylwain, Casting Director\n1 Hall of Heliod's Generosity\n1 Heliod's Pilgrim\n1 Hidden Grotto\n1 Horrid Vigor\n1 Idyllic Tutor\n1 Jukai Naturalist\n1 Kenrith's Transformation\n1 Kor Spiritdancer\n1 Krosan Verge\n1 Light-Paws, Emperor's Voice\n1 Luminous Broodmoth\n1 Mantle of the Ancients\n1 Overgrowth\n1 Overprotect\n1 Pacifism\n14 Plains\n1 Rancor\n1 Retether\n1 Rogue's Passage\n1 Sage's Reverie\n1 Sanctum Weaver\n1 Selesnya Guildgate\n1 Setessan Champion\n1 Shalai, Voice of Plenty\n1 Snake Umbra\n1 Sol Ring\n1 Solemnity\n1 Songbirds' Blessing\n1 Starfield Mystic\n1 Swords to Plowshares\n1 Tanglespan Lookout\n1 Timber Paladin\n1 Timely Ward\n1 Tithe Takern1 Transcendent Envoy\n1 Twinblade Blessing\n1 Umbra Mystic\n1 Unfinished Business\n1 Utopia Sprawl\n1 Wild Growth\n1 Winds of Rath\n1 Yenna, Redtooth Regent\n1 Sythis, Harvest's Hand"
var cards = "1 Arcane Signet
1 Austere Command
1 Bartolomé del Presidio
1 Blade of the Bloodchief
1 Blood Artist
1 Bloodghast
1 Bloodline Necromancer
1 Bloodtracker
1 Bojuka Bog
1 Butcher of Malakir
1 Carmen, Cruel Skymarcher
1 Champion of Dusk
1 Charismatic Conqueror
1 Command Tower
1 Commander's Sphere
1 Cordial Vampire
1 Crossway Troublemakers
1 Cruel Celebrant
1 Damn
1 Drana, Liberator of Malakir
1 Dusk Legion Sergeant
1 Dusk Legion Zealot
1 Elenda, the Dusk Rose
1 Elenda's Hierophant
1 Etchings of the Chosen
1 Exquisite Blood
1 Falkenrath Noble
1 Glass-Cast Heart
1 Heirloom Blade
1 Indulgent Aristocrat
1 Isolated Chapel
1 Kindred Boon
1 Legion Lieutenant
1 March of the Canonized
1 Martyr of Dusk
1 Master of Dark Rites
1 Mavren Fein, Dusk Apostle
1 Mind Stone
1 Myriad Landscape
1 New Blood
1 Nighthawk Scavenger
1 Oathsworn Vampire
1 Olivia's Wrath
1 Order of Sacred Dusk
1 Orzhov Basilica
1 Orzhov Signet
1 Pact of the Serpent
1 Path of Ancestry
1 Patron of the Vein
4 Plains
4 Plains
1 Promise of Aclazotz
1 Radiant Destiny
1 Redemption Choir
1 Return to Dust
1 Rogue's Passage
1 Sanctum Seeker
1 Secluded Courtyard
1 Shineshadow Snarl
1 Sol Ring
1 Sorin, Lord of Innistrad
7 Swamp
6 Swamp
1 Swiftfoot Boots
1 Swords to Plowshares
1 Tainted Field
1 Talisman of Hierarchy
1 Temple of Silence
1 Temple of the False God
1 Timothar, Baron of Bats
1 Twilight Prophet
1 Unclaimed Territory
1 Utter End
1 Vault of the Archangel
1 Village Rites
1 Viscera Seer
1 Voldaren Estate
1 Vona, Butcher of Magan
1 Wayfarer's Bauble
1 Welcoming Vampire
1 Windbrisk Heights
1 Yahenni, Undying Partisan
1 Clavileño, First of the Blessed"
func _bulk_callback(cache) -> void:
cache.setup()
@ -25,8 +108,9 @@ func _ready() -> void:
cache.get_card_data_from_name("1996 World Champion")
var deck = deck_input.new(cards)
var deck = deck_input.new()
add_child(deck)
deck.add_new_deck(cards, "Blood rites")
pass # Replace with function body.