77 lines
2.6 KiB
GDScript
77 lines
2.6 KiB
GDScript
extends Node
|
|
## The card class [br][br]
|
|
##
|
|
##
|
|
## Contains helper text for the text, the cards ID, and the image path.
|
|
## The goal of this class is to make card management easier.
|
|
|
|
# we want to use this to convert the mana cost into text
|
|
# in the helper text box, but thats for the future
|
|
const ManaCosts = preload("res://data/mana.gd")
|
|
|
|
var cached = false
|
|
|
|
var card_id = "placedholder_id"
|
|
var card_name = "placeholder_name"
|
|
var card_type = "placeholder_card_type"
|
|
var oracle_text = "placeholder_oracle_text"
|
|
var image_path = "placeholder_image_path"
|
|
|
|
## _check_cache
|
|
## id - String containing the card's ID [br]
|
|
## Checks if the card has already been cached
|
|
func _check_cache(id) -> bool:
|
|
if (FileAccess.file_exists("user://card_cache/" + id + ".json")):
|
|
return true
|
|
return false
|
|
|
|
## _do_scryfall_get
|
|
## id - String containing the card's ID [br]
|
|
## Using the scryfall API, fetches JSON information about the card
|
|
## caches this data to the disk at user://card_cache/[id].json, and
|
|
## the image of the card at user://card_cache/[id].png
|
|
func _scryfall_response(_result, response_code, _headers, body):
|
|
if (response_code != 200):
|
|
push_error("Failed to fetch card data from Scryfall")
|
|
|
|
var card_content = JSON.parse_string(body.get_string_from_utf8())
|
|
if (card_content == null):
|
|
push_error("Failed to parse the Scryfall card results.")
|
|
var card_cache = FileAccess.open("user://card_cache/" + card_content["id"] + ".json", FileAccess.WRITE)
|
|
card_cache.store_string(body.get_string_from_utf8()) # cache the json response
|
|
print(body.get_string_from_utf8())
|
|
card_cache = null # closes the file
|
|
# TODO store image at the same spot ("user://card_cache/id.png")
|
|
|
|
func _do_http_request(id) -> void:
|
|
$HTTPRequest.request_completed.connect(self._scryfall_response)
|
|
|
|
var headers = PackedStringArray(["User-Agent: MTGUntapClone/0.1", "Accept: */*"])
|
|
|
|
var error = $HTTPRequest.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:
|
|
_do_http_request(id)
|
|
return
|
|
|
|
|
|
if !(_check_cache(id)):
|
|
_do_http_request(id)
|
|
|
|
var ondisk_card = FileAccess.open("user://card_cache/" + id + ".json", FileAccess.READ)
|
|
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_type = card_json["type_line"]
|
|
oracle_text = card_json["oracle_text"]
|
|
image_path = card_json["images_uris"]["png"] #TODO: change this to file path of the image
|
|
|
|
|
|
ondisk_card = null
|
|
|
|
## these variables above will be used for the helper boxes we draw
|
|
## on hover, to make cards easier to read
|