separates all of the cacheing functions to its own class, cleans up card
This commit is contained in:
parent
c085a93c49
commit
a3fb627310
90
caching.gd
Normal file
90
caching.gd
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
extends Node
|
||||||
|
|
||||||
|
var _jpg_path
|
||||||
|
var _png_path
|
||||||
|
|
||||||
|
signal cache_done
|
||||||
|
|
||||||
|
enum cache_action {
|
||||||
|
ALREADY_CACHED,
|
||||||
|
CACHING,
|
||||||
|
FAILED_TO_CACHE
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
if !FileAccess.file_exists("user://card_cache/" + id + "/card.jpg"):
|
||||||
|
return false
|
||||||
|
return true
|
||||||
|
|
||||||
|
func fetch_card(id: String) -> void:
|
||||||
|
if _check_cache(id):
|
||||||
|
return
|
||||||
|
|
||||||
|
await _do_http_request_card(id)
|
||||||
|
await _do_http_request_imgs(id, _png_path, true)
|
||||||
|
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:
|
||||||
|
var httpr = HTTPRequest.new()
|
||||||
|
add_child(httpr)
|
||||||
|
|
||||||
|
var headers = PackedStringArray(["User-Agent: MTGUntapClone/0.1", "Accept: */*"])
|
||||||
|
var error = httpr.request(image_path, 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:
|
||||||
|
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 = null
|
||||||
|
|
||||||
|
|
||||||
|
func _do_http_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)
|
||||||
|
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"]
|
||||||
|
_png_path = image_uris["png"]
|
||||||
|
_jpg_path = image_uris["normal"]
|
1
caching.gd.uid
Normal file
1
caching.gd.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://cml6c3fdbyy75
|
108
card.gd
108
card.gd
@ -9,17 +9,10 @@ extends TextureRect
|
|||||||
# in the helper text box, but thats for the future
|
# in the helper text box, but thats for the future
|
||||||
const ManaCosts = preload("res://data/mana.gd")
|
const ManaCosts = preload("res://data/mana.gd")
|
||||||
|
|
||||||
signal cache_done
|
|
||||||
|
|
||||||
var loaded = false
|
|
||||||
|
|
||||||
var card_id = "placedholder_id"
|
var card_id = "placedholder_id"
|
||||||
var card_name = "placeholder_name"
|
var card_name = "placeholder_name"
|
||||||
var card_type = "placeholder_card_type"
|
var card_type = "placeholder_card_type"
|
||||||
var oracle_text = "placeholder_oracle_text"
|
var oracle_text = "placeholder_oracle_text"
|
||||||
var _png_path = "placeholder_image_path"
|
|
||||||
var _jpg_path = "placeholder_image_path"
|
|
||||||
|
|
||||||
|
|
||||||
func _card_error(error_type: String) -> String:
|
func _card_error(error_type: String) -> String:
|
||||||
return "CARD::" + card_id + "::" + error_type + "\n"
|
return "CARD::" + card_id + "::" + error_type + "\n"
|
||||||
@ -28,109 +21,12 @@ func _card_error(error_type: String) -> String:
|
|||||||
func _init(id) -> void:
|
func _init(id) -> void:
|
||||||
card_id = id
|
card_id = id
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
|
||||||
if _check_cache(card_id):
|
|
||||||
loaded = true
|
|
||||||
return
|
|
||||||
await _do_cache_grab()
|
|
||||||
|
|
||||||
|
|
||||||
func _do_cache_grab() -> void:
|
|
||||||
await _do_http_request_card()
|
|
||||||
await _do_http_request_imgs(_png_path, true)
|
|
||||||
await _do_http_request_imgs(_jpg_path, false)
|
|
||||||
cache_done.emit()
|
|
||||||
loaded = true
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
if !FileAccess.file_exists("user://card_cache/" + id + "/card.jpg"):
|
|
||||||
return false
|
|
||||||
return true
|
|
||||||
|
|
||||||
|
|
||||||
func _do_http_request_imgs(image_path: String, png: bool) -> void:
|
|
||||||
var httpr = HTTPRequest.new()
|
|
||||||
add_child(httpr)
|
|
||||||
|
|
||||||
var headers = PackedStringArray(["User-Agent: MTGUntapClone/0.1", "Accept: */*"])
|
|
||||||
var error = httpr.request(image_path, headers)
|
|
||||||
if error != OK:
|
|
||||||
push_error(_card_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:
|
|
||||||
push_error(_card_error("IMG_LOADING") + "Couldn't load the image.")
|
|
||||||
|
|
||||||
img.save_png("user://card_cache/" + card_id + ("/card.png" if png else "/card.jpg"))
|
|
||||||
img = null
|
|
||||||
|
|
||||||
|
|
||||||
func _do_http_request_card() -> 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/" + card_id, headers)
|
|
||||||
if error != OK:
|
|
||||||
push_error(_card_error("GET_REQUEST") + "An error occurred in the Scryfall request.")
|
|
||||||
|
|
||||||
var response = await httpr.request_completed
|
|
||||||
|
|
||||||
if response[0] != HTTPRequest.RESULT_SUCCESS:
|
|
||||||
push_error(_card_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(_card_error("PARSING") + "Failed to parse the Scryfall card results.")
|
|
||||||
|
|
||||||
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"]
|
|
||||||
_png_path = image_uris["png"]
|
|
||||||
_jpg_path = image_uris["normal"]
|
|
||||||
|
|
||||||
|
|
||||||
## load_card
|
|
||||||
##
|
|
||||||
## Loads the card, returns false, and triggers
|
|
||||||
## a cache fetch if the card is not in the cache,
|
|
||||||
## otherwise sets the cards variables if the cache is present.
|
|
||||||
func load_card() -> bool:
|
func load_card() -> bool:
|
||||||
if !_check_cache(card_id):
|
|
||||||
await _do_cache_grab()
|
|
||||||
push_error(
|
|
||||||
(
|
|
||||||
_card_error("CACHE_FAIL")
|
|
||||||
+ "Cache wasn't ready, this card will need to be reinitialized"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return false
|
|
||||||
|
|
||||||
var ondisk_card = FileAccess.open(
|
var ondisk_card = FileAccess.open(
|
||||||
"user://card_cache/" + card_id + "/card.json", FileAccess.READ
|
"user://card_cache/" + card_id + "/card.json", FileAccess.READ
|
||||||
)
|
)
|
||||||
|
if ondisk_card == null:
|
||||||
|
push_error("ERROR::CRITICAL::CACHE\nCard being accessed wasn't cached, yell at the coders plz")
|
||||||
var card_json = JSON.parse_string(ondisk_card.get_as_text())
|
var card_json = JSON.parse_string(ondisk_card.get_as_text())
|
||||||
|
|
||||||
card_name = card_json["name"]
|
card_name = card_json["name"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user