deck input changes

This commit is contained in:
Nathan Singer 2025-04-24 13:20:57 -04:00
parent f5edba7402
commit a40ccab3c8
9 changed files with 166 additions and 31 deletions

View File

@ -1,10 +1,16 @@
extends Node
var _jpg_path
var _png_path
var _card_id
var _img_path
var _req_headers
signal cache_done
var _consts = preload("res://data/consts.gd")
func _init() -> void:
_req_headers = PackedStringArray(["User-Agent: " + _consts.APP_NAME + "/" + _consts.APP_VERSION, "Accept: */*"])
func _cache_error(err: String) -> String:
return "CACHE::ERROR::" + err + "\n"
@ -15,52 +21,88 @@ func _check_cache(id: String) -> bool:
return false
if !FileAccess.file_exists("user://card_cache/" + id + "/card.png"):
return false
if !FileAccess.file_exists("user://card_cache/" + id + "/card.jpg"):
return false
return true
func custom_query_fetch(query: String) -> String:
var error = await _do_custom_http_request_card(query)
if error != OK:
return "NONE"
OS.delay_msec(100)
await _do_http_request_imgs(_card_id)
OS.delay_msec(100)
cache_done.emit()
return _card_id
func _do_custom_http_request_card(query: String) -> Error:
var httpr = HTTPRequest.new()
add_child(httpr)
var error = httpr.request(query, _req_headers)
if error != OK:
push_error(_cache_error("GET_REQUEST") + "An error occurred in the Scryfall request.")
var response = await httpr.request_completed
if response[0] != HTTPRequest.RESULT_SUCCESS:
push_error(_cache_error("GET_REQUEST") + "Failed to fetch card data from Scryfall")
return FAILED
var unprocessed_body = response[3].get_string_from_utf8()
var card_content = JSON.parse_string(unprocessed_body)
if card_content == null:
push_error(_cache_error("PARSING") + "Failed to parse the Scryfall card results.")
_card_id = card_content["id"]
if _check_cache(_card_id):
return FAILED
var dir = DirAccess.open("user://")
dir.make_dir_recursive("user://card_cache/" + _card_id + "/") # lets ensure the path is there
dir = null
var card_cache = FileAccess.open("user://card_cache/" + _card_id + "/card.json", FileAccess.WRITE)
card_cache.store_string(unprocessed_body) # cache the json response
card_cache = null # closes the file
var image_uris = card_content["image_uris"]
_img_path = image_uris["png"]
return OK
func fetch_card(id: String) -> void:
if _check_cache(id):
return
await _do_http_request_card(id)
await _do_http_id_request_card(id)
OS.delay_msec(100)
await _do_http_request_imgs(id, _png_path, true)
await _do_http_request_imgs(id)
OS.delay_msec(100)
await _do_http_request_imgs(id, _jpg_path, false)
cache_done.emit()
func _do_http_request_imgs(id: String, image_path: String, png: bool) -> void:
func _do_http_request_imgs(id: String) -> void:
var httpr = HTTPRequest.new()
add_child(httpr)
var headers = PackedStringArray(["User-Agent: MTGUntapClone/0.1", "Accept: */*"])
var error = httpr.request(image_path, headers)
var error = httpr.request(_img_path, _req_headers)
if error != OK:
push_error(_cache_error("GET_REQUEST") + "An error occurred in the Scryfall request.")
var response = await httpr.request_completed
var img = Image.new()
var imgerr
if png:
imgerr = img.load_png_from_buffer(response[3])
else:
imgerr = img.load_jpg_from_buffer(response[3])
if imgerr != OK:
error = img.load_png_from_buffer(response[3])
if error != OK:
push_error(_cache_error("IMG_LOADING") + "Couldn't load the image.")
img.save_png("user://card_cache/" + id + ("/card.png" if png else "/card.jpg"))
img.save_png("user://card_cache/" + id + "/card.png")
img = null
func _do_http_request_card(id: String) -> void:
func _do_http_id_request_card(id: String) -> void:
var httpr = HTTPRequest.new()
add_child(httpr)
#httpr.request_completed.connect(_scryfall_card_response)
var headers = PackedStringArray(["User-Agent: MTGUntapClone/0.1", "Accept: */*"])
var error = httpr.request("https://api.scryfall.com/cards/" + id, headers)
var error = httpr.request("https://api.scryfall.com/cards/" + id, _req_headers)
if error != OK:
push_error(_cache_error("GET_REQUEST") + "An error occurred in the Scryfall request.")
@ -84,5 +126,4 @@ func _do_http_request_card(id: String) -> void:
card_cache = null # closes the file
var image_uris = card_content["image_uris"]
_png_path = image_uris["png"]
_jpg_path = image_uris["normal"]
_img_path = image_uris["png"]

View File

@ -17,7 +17,7 @@ func _card_error(error_type: String) -> String:
return "ERROR::CARD::%s::%s::%s::\n" % [card_id, card_name, error_type]
func _init(id) -> void:
func init(id: String) -> void:
card_id = id

2
data/consts.gd Normal file
View File

@ -0,0 +1,2 @@
const APP_NAME = "MTG_UNTAP_CLONE"
const APP_VERSION = "0.1"

1
data/consts.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://bo0256822a2ss

View File

@ -1,5 +1,75 @@
extends Node
var _caching = preload("res://caching.gd")
var _decklist
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
1 Daybreak Coronet
1 Destiny Spinner
1 Eidolon of Blossoms
1 Eidolon of Countless Battles
1 Ellivere of the Wild Court
1 Enchantress's Presence
1 Envoy of the Ancestors
1 Ethereal Armor
1 Fertile Ground
13 Forest
1 Frantic Strength
1 Generous Gift
1 Gilded Lotus
1 Glittering Frost
1 Grasp of Fate
1 Gylwain, Casting Director
1 Hall of Heliod's Generosity
1 Heliod's Pilgrim
1 Hidden Grotto
1 Horrid Vigor
1 Idyllic Tutor
1 Jukai Naturalist
1 Kenrith's Transformation
1 Kor Spiritdancer
1 Krosan Verge
1 Light-Paws, Emperor's Voice
1 Luminous Broodmoth
1 Mantle of the Ancients
1 Overgrowth
1 Overprotect
1 Pacifism
14 Plains
1 Rancor
1 Retether
1 Rogue's Passage
1 Sage's Reverie
1 Sanctum Weaver
1 Selesnya Guildgate
1 Setessan Champion
1 Shalai, Voice of Plenty
1 Snake Umbra
1 Sol Ring
1 Solemnity
1 Songbirds' Blessing
1 Starfield Mystic
1 Swords to Plowshares
1 Tanglespan Lookout
1 Timber Paladin
1 Timely Ward
1 Tithe Taker
1 Transcendent Envoy
1 Twinblade Blessing
1 Umbra Mystic
1 Unfinished Business
1 Utopia Sprawl
1 Wild Growth
1 Winds of Rath
1 Yenna, Redtooth Regent
1 Sythis, Harvest's Hand"
signal done_fetching
func _init() -> void:
_decklist = Dictionary()
func convert_mtgo_to_http(decklist: String) -> Dictionary:
var links = {}
@ -13,5 +83,26 @@ func convert_mtgo_to_http(decklist: String) -> Dictionary:
links["https://api.scryfall.com/cards/named?exact=" + words[1]] = words[0]
return links
func _do_decklist_grab(queries: Dictionary) -> 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():
for card in _decklist:
print(card + " : " + _decklist[card])
func _ready() -> void:
pass
var queries = convert_mtgo_to_http(cards)
_do_decklist_grab(queries)

View File

@ -40,5 +40,5 @@ func set_colors(colors: Array[Color]) -> void:
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
func _process(_delta: float) -> void:
pass

View File

@ -15,7 +15,7 @@ func _init(card_ids: Array) -> void:
lib_cards = Array()
var temp_card
for id in card_ids:
temp_card = _card_class.new(id)
temp_card = _card_class.new()
temp_card.cache_done.connect(_load_card_callback.bind(temp_card))
lib_cards.push_back(temp_card)
num_cards += 1

View File

@ -17,5 +17,5 @@ func _ready() -> void:
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
func _process(_delta: float) -> void:
pass

View File

@ -20,5 +20,5 @@ func _ready() -> void:
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
func _process(_delta: float) -> void:
pass