From a3dfa96efd748b8d492fe110ad95a6ebd49243f7 Mon Sep 17 00:00:00 2001 From: Nathan Singer Date: Tue, 22 Apr 2025 17:16:38 -0400 Subject: [PATCH] scryfall requests --- card.gd | 45 +++++++++++++++++++++++++++++---------------- player.gd | 20 ++++++++++---------- player.tscn | 4 +++- tabletop.gd | 3 +++ tabletop.tscn | 2 ++ 5 files changed, 47 insertions(+), 27 deletions(-) diff --git a/card.gd b/card.gd index 28c99fd..1fa36d5 100644 --- a/card.gd +++ b/card.gd @@ -17,11 +17,15 @@ var card_type = "placeholder_card_type" var oracle_text = "placeholder_oracle_text" var image_path = "placeholder_image_path" +func _ready() -> void: + load_card("d3f10f07-7cfe-4a6f-8de6-373e367a731b") + pass + ## _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")): + if (FileAccess.file_exists("user://card_cache/" + id + "/card.json")): return true return false @@ -33,41 +37,50 @@ func _check_cache(id) -> bool: 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()) + + var unprocessed_body = body.get_string_from_utf8() + var card_content = JSON.parse_string(unprocessed_body) 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()) + + var dir = DirAccess.open("user://") + 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 + + 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") 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 error = $HTTPRequest.request("https://api.scryfall.com/cards/" + id, headers) + 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: - _do_http_request(id) - return - - if !(_check_cache(id)): _do_http_request(id) + return - var ondisk_card = FileAccess.open("user://card_cache/" + id + ".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()) # 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 + var image_uris = card_json["image_uris"] + image_path = image_uris["png"] #TODO: change this to file path of the image ondisk_card = null diff --git a/player.gd b/player.gd index bb1bca3..aa2b666 100644 --- a/player.gd +++ b/player.gd @@ -2,21 +2,21 @@ extends Node2D 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. func _ready() -> void: # TODO: Create 2-4 player instances as children of this tabletop node. - var new_card = _card_class.new() - add_child(new_card) - new_card.load_card("d3f10f07-7cfe-4a6f-8de6-373e367a731b") + var card = _card_class.new() + add_child(card) - print("new player!") - - print(new_card.card_id) - print(new_card.card_name) - print(new_card.card_type) - print(new_card.oracle_text) - print(new_card.image_path) + print(card.card_id) + print(card.card_name) + print(card.card_type) + print(card.oracle_text) + print(card.image_path) pass # Replace with function body. diff --git a/player.tscn b/player.tscn index dc9a8c5..8d34023 100644 --- a/player.tscn +++ b/player.tscn @@ -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://b3yqd1qu7dyq" path="res://card.gd" id="2_onrkg"] [node name="Player" type="Node2D"] script = ExtResource("1_4flbx") @@ -8,6 +9,7 @@ script = ExtResource("1_4flbx") [node name="Field" type="Node2D" parent="."] [node name="Hand" type="Node2D" parent="."] +script = ExtResource("2_onrkg") [node name="Library" type="Node2D" parent="."] diff --git a/tabletop.gd b/tabletop.gd index b70da20..725cb7b 100644 --- a/tabletop.gd +++ b/tabletop.gd @@ -2,10 +2,13 @@ extends Node2D var player_class = preload("res://player.gd") + + # Called when the node enters the scene tree for the first time. func _ready() -> void: # TODO: Create 2-4 player instances as children of this tabletop node. + var player = player_class.new() add_child(player) diff --git a/tabletop.tscn b/tabletop.tscn index 9bd0c02..ae63f6c 100644 --- a/tabletop.tscn +++ b/tabletop.tscn @@ -4,3 +4,5 @@ [node name="Tabletop" type="Node2D"] script = ExtResource("1_3we3x") + +[node name="HTTPRequest" type="HTTPRequest" parent="."]