converts card cache getting to a coroutine function
This commit is contained in:
parent
a3dfa96efd
commit
37975021cb
113
card.gd
113
card.gd
@ -15,75 +15,98 @@ 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 image_path = "placeholder_image_path"
|
var png_path = "placeholder_image_path"
|
||||||
|
var jpg_path = "placeholder_image_path"
|
||||||
|
|
||||||
|
func _init(id) -> void:
|
||||||
|
card_id = id
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
load_card("d3f10f07-7cfe-4a6f-8de6-373e367a731b")
|
await do_cache_grab()
|
||||||
pass
|
|
||||||
|
|
||||||
## _check_cache
|
func do_cache_grab() -> void:
|
||||||
## id - String containing the card's ID [br]
|
await _do_http_request_card()
|
||||||
## Checks if the card has already been cached
|
await _do_http_request_imgs(png_path, true)
|
||||||
func _check_cache(id) -> bool:
|
await _do_http_request_imgs(jpg_path, false)
|
||||||
if (FileAccess.file_exists("user://card_cache/" + id + "/card.json")):
|
|
||||||
return true
|
|
||||||
return false
|
|
||||||
|
|
||||||
## _do_scryfall_get
|
|
||||||
## id - String containing the card's ID [br]
|
func _check_cache(id: String) -> bool:
|
||||||
## Using the scryfall API, fetches JSON information about the card
|
if (!FileAccess.file_exists("user://card_cache/" + id + "/card.json")):
|
||||||
## caches this data to the disk at user://card_cache/[id].json, and
|
return false
|
||||||
## the image of the card at user://card_cache/[id].png
|
if (!FileAccess.file_exists("user://card_cache/" + id + "/card.png")):
|
||||||
func _scryfall_response(_result, response_code, _headers, body):
|
return false
|
||||||
if (response_code != 200):
|
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("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("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("An error occurred in the Scryfall request.")
|
||||||
|
|
||||||
|
var response = await httpr.request_completed
|
||||||
|
|
||||||
|
|
||||||
|
if response[0] != HTTPRequest.RESULT_SUCCESS:
|
||||||
push_error("Failed to fetch card data from Scryfall")
|
push_error("Failed to fetch card data from Scryfall")
|
||||||
|
return
|
||||||
|
|
||||||
var unprocessed_body = body.get_string_from_utf8()
|
var unprocessed_body = response[3].get_string_from_utf8()
|
||||||
var card_content = JSON.parse_string(unprocessed_body)
|
var card_content = JSON.parse_string(unprocessed_body)
|
||||||
if (card_content == null):
|
if (card_content == null):
|
||||||
push_error("Failed to parse the Scryfall card results.")
|
push_error("Failed to parse the Scryfall card results.")
|
||||||
|
|
||||||
var dir = DirAccess.open("user://")
|
var dir = DirAccess.open("user://")
|
||||||
dir.make_dir_recursive("user://card_cache/" + card_content["id"] + "/") # lets ensure the path is there
|
dir.make_dir_recursive("user://card_cache/" + card_id + "/") # lets ensure the path is there
|
||||||
dir = null
|
dir = null
|
||||||
|
|
||||||
var card_cache = FileAccess.open("user://card_cache/" + card_content["id"] + "/card.json", FileAccess.WRITE)
|
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.store_string(unprocessed_body) # cache the json response
|
||||||
card_cache = null # closes the file
|
card_cache = null # closes the file
|
||||||
|
|
||||||
var image_uris = card_content["image_uris"]
|
var image_uris = card_content["image_uris"]
|
||||||
print(image_uris["png"])
|
png_path = image_uris["png"]
|
||||||
|
jpg_path = image_uris["normal"]
|
||||||
|
|
||||||
load_card(card_content["id"]) # call again, since we have filled the cache
|
func load_card(id) -> bool:
|
||||||
# TODO store image at the same spot ("user://card_cache/id.png")
|
if !_check_cache(id):
|
||||||
|
await do_cache_grab()
|
||||||
|
push_error("Cache wasn't ready, this card will need to be reinitialized")
|
||||||
|
return false
|
||||||
|
|
||||||
func _do_http_request(id) -> void:
|
|
||||||
var httpr = HTTPRequest.new()
|
|
||||||
add_child(httpr)
|
|
||||||
httpr.request_completed.connect(_scryfall_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("An error occurred in the Scryfall request.")
|
|
||||||
|
|
||||||
func load_card(id) -> void:
|
|
||||||
if !(_check_cache(id)):
|
|
||||||
_do_http_request(id)
|
|
||||||
return
|
|
||||||
|
|
||||||
var ondisk_card = FileAccess.open("user://card_cache/" + id + "/card.json", FileAccess.READ)
|
var ondisk_card = FileAccess.open("user://card_cache/" + id + "/card.json", FileAccess.READ)
|
||||||
var card_json = JSON.parse_string(ondisk_card.get_as_text())
|
var card_json = JSON.parse_string(ondisk_card.get_as_text())
|
||||||
# parse json here to get card content
|
|
||||||
card_id = card_json["id"]
|
|
||||||
card_name = card_json["name"]
|
card_name = card_json["name"]
|
||||||
card_type = card_json["type_line"]
|
card_type = card_json["type_line"]
|
||||||
oracle_text = card_json["oracle_text"]
|
oracle_text = card_json["oracle_text"]
|
||||||
var image_uris = card_json["image_uris"]
|
|
||||||
image_path = image_uris["png"] #TODO: change this to file path of the image
|
|
||||||
|
|
||||||
|
|
||||||
ondisk_card = null
|
ondisk_card = null
|
||||||
|
return true
|
||||||
## these variables above will be used for the helper boxes we draw
|
|
||||||
## on hover, to make cards easier to read
|
|
||||||
|
@ -9,14 +9,13 @@ func _on_request_completed(result, response_code, headers, body):
|
|||||||
# Called when the node enters the scene tree for the first time.
|
# Called when the node enters the scene tree for the first time.
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
# TODO: Create 2-4 player instances as children of this tabletop node.
|
# TODO: Create 2-4 player instances as children of this tabletop node.
|
||||||
var card = _card_class.new()
|
var card = _card_class.new("d3f10f07-7cfe-4a6f-8de6-373e367a731b")
|
||||||
add_child(card)
|
add_child(card)
|
||||||
|
|
||||||
print(card.card_id)
|
print(card.card_id)
|
||||||
print(card.card_name)
|
print(card.card_name)
|
||||||
print(card.card_type)
|
print(card.card_type)
|
||||||
print(card.oracle_text)
|
print(card.oracle_text)
|
||||||
print(card.image_path)
|
|
||||||
pass # Replace with function body.
|
pass # Replace with function body.
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user