scryfall requests

This commit is contained in:
Nathan Singer 2025-04-22 17:16:38 -04:00
parent 651b267dd2
commit a3dfa96efd
5 changed files with 47 additions and 27 deletions

41
card.gd
View File

@ -17,11 +17,15 @@ 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 image_path = "placeholder_image_path"
func _ready() -> void:
load_card("d3f10f07-7cfe-4a6f-8de6-373e367a731b")
pass
## _check_cache ## _check_cache
## id - String containing the card's ID [br] ## id - String containing the card's ID [br]
## Checks if the card has already been cached ## Checks if the card has already been cached
func _check_cache(id) -> bool: func _check_cache(id) -> bool:
if (FileAccess.file_exists("user://card_cache/" + id + ".json")): if (FileAccess.file_exists("user://card_cache/" + id + "/card.json")):
return true return true
return false return false
@ -34,40 +38,49 @@ func _scryfall_response(_result, response_code, _headers, body):
if (response_code != 200): if (response_code != 200):
push_error("Failed to fetch card data from Scryfall") push_error("Failed to fetch card data from Scryfall")
var card_content = JSON.parse_string(body.get_string_from_utf8()) var unprocessed_body = body.get_string_from_utf8()
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 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 var dir = DirAccess.open("user://")
print(body.get_string_from_utf8()) dir.make_dir_recursive("user://card_cache/" + card_content["id"] + "/") # lets ensure the path is there
dir = null
var card_cache = FileAccess.open("user://card_cache/" + card_content["id"] + "/card.json", FileAccess.WRITE)
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"]
print(image_uris["png"])
load_card(card_content["id"]) # call again, since we have filled the cache
# TODO store image at the same spot ("user://card_cache/id.png") # TODO store image at the same spot ("user://card_cache/id.png")
func _do_http_request(id) -> void: func _do_http_request(id) -> void:
$HTTPRequest.request_completed.connect(self._scryfall_response) var httpr = HTTPRequest.new()
add_child(httpr)
httpr.request_completed.connect(_scryfall_response)
var headers = PackedStringArray(["User-Agent: MTGUntapClone/0.1", "Accept: */*"]) var headers = PackedStringArray(["User-Agent: MTGUntapClone/0.1", "Accept: */*"])
var error = $HTTPRequest.request("https://api.scryfall.com/cards/" + id, headers) var error = httpr.request("https://api.scryfall.com/cards/" + id, headers)
if error != OK: if error != OK:
push_error("An error occurred in the Scryfall request.") push_error("An error occurred in the Scryfall request.")
func load_card(id) -> void: func load_card(id) -> void:
if !(_check_cache(id)):
_do_http_request(id) _do_http_request(id)
return return
var ondisk_card = FileAccess.open("user://card_cache/" + id + "/card.json", FileAccess.READ)
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()) var card_json = JSON.parse_string(ondisk_card.get_as_text())
# parse json here to get card content # parse json here to get card content
card_id = card_json["id"] 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"]
image_path = card_json["images_uris"]["png"] #TODO: change this to file path of the image 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

View File

@ -2,21 +2,21 @@ extends Node2D
var _card_class = preload("res://card.gd") var _card_class = preload("res://card.gd")
func _on_request_completed(result, response_code, headers, body):
var json = JSON.parse_string(body.get_string_from_utf8())
print(json["name"])
# 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 new_card = _card_class.new() var card = _card_class.new()
add_child(new_card) add_child(card)
new_card.load_card("d3f10f07-7cfe-4a6f-8de6-373e367a731b")
print("new player!") print(card.card_id)
print(card.card_name)
print(new_card.card_id) print(card.card_type)
print(new_card.card_name) print(card.oracle_text)
print(new_card.card_type) print(card.image_path)
print(new_card.oracle_text)
print(new_card.image_path)
pass # Replace with function body. pass # Replace with function body.

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://cx0vga81xwckh"] [gd_scene load_steps=3 format=3 uid="uid://cx0vga81xwckh"]
[ext_resource type="Script" uid="uid://w2rqm1u7p7im" path="res://player.gd" id="1_4flbx"] [ext_resource type="Script" uid="uid://w2rqm1u7p7im" path="res://player.gd" id="1_4flbx"]
[ext_resource type="Script" uid="uid://b3yqd1qu7dyq" path="res://card.gd" id="2_onrkg"]
[node name="Player" type="Node2D"] [node name="Player" type="Node2D"]
script = ExtResource("1_4flbx") script = ExtResource("1_4flbx")
@ -8,6 +9,7 @@ script = ExtResource("1_4flbx")
[node name="Field" type="Node2D" parent="."] [node name="Field" type="Node2D" parent="."]
[node name="Hand" type="Node2D" parent="."] [node name="Hand" type="Node2D" parent="."]
script = ExtResource("2_onrkg")
[node name="Library" type="Node2D" parent="."] [node name="Library" type="Node2D" parent="."]

View File

@ -2,11 +2,14 @@ extends Node2D
var player_class = preload("res://player.gd") var player_class = preload("res://player.gd")
# 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 player = player_class.new() var player = player_class.new()
add_child(player) add_child(player)

View File

@ -4,3 +4,5 @@
[node name="Tabletop" type="Node2D"] [node name="Tabletop" type="Node2D"]
script = ExtResource("1_3we3x") script = ExtResource("1_3we3x")
[node name="HTTPRequest" type="HTTPRequest" parent="."]