130 lines
3.8 KiB
GDScript
130 lines
3.8 KiB
GDScript
extends Node
|
|
|
|
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"
|
|
|
|
|
|
func _check_cache(id: String) -> bool:
|
|
if !FileAccess.file_exists("user://card_cache/" + id + "/card.json"):
|
|
return false
|
|
if !FileAccess.file_exists("user://card_cache/" + id + "/card.png"):
|
|
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_id_request_card(id)
|
|
OS.delay_msec(100)
|
|
await _do_http_request_imgs(id)
|
|
OS.delay_msec(100)
|
|
cache_done.emit()
|
|
|
|
|
|
func _do_http_request_imgs(id: String) -> void:
|
|
var httpr = HTTPRequest.new()
|
|
add_child(httpr)
|
|
|
|
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()
|
|
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")
|
|
img = null
|
|
|
|
|
|
func _do_http_id_request_card(id: String) -> void:
|
|
var httpr = HTTPRequest.new()
|
|
add_child(httpr)
|
|
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.")
|
|
|
|
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
|
|
|
|
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.")
|
|
|
|
var dir = DirAccess.open("user://")
|
|
dir.make_dir_recursive("user://card_cache/" + id + "/") # lets ensure the path is there
|
|
dir = null
|
|
|
|
var card_cache = FileAccess.open("user://card_cache/" + 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"]
|