diff --git a/card.gd b/card.gd index f7dfbc6..28c99fd 100644 --- a/card.gd +++ b/card.gd @@ -30,35 +30,47 @@ func _check_cache(id) -> bool: ## 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 _do_scryfall_get(id) -> void: - # TODO: fetch the card here... +func _scryfall_response(_result, response_code, _headers, body): + if (response_code != 200): + push_error("Failed to fetch card data from Scryfall") - var fetched = false - - if !fetched: - print("Failed to fetch card data from scryfall") - - var card_content = "fetched_card_json" #TODO - var card_cache = FileAccess.open("user://card_cache/" + id + ".json", FileAccess.WRITE) - card_cache.store_string(card_content) + 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 - # 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: + $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_scryfall_get(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 = id - card_name = "loaded_cardname" - card_type = "loaded_type" - oracle_text = "loaded_oracletext" - image_path = "loaded_image_path" + 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 - pass ## these variables above will be used for the helper boxes we draw ## on hover, to make cards easier to read diff --git a/player.gd b/player.gd new file mode 100644 index 0000000..bb1bca3 --- /dev/null +++ b/player.gd @@ -0,0 +1,25 @@ +extends Node2D + +var _card_class = preload("res://card.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 new_card = _card_class.new() + add_child(new_card) + new_card.load_card("d3f10f07-7cfe-4a6f-8de6-373e367a731b") + + 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) + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + pass diff --git a/player.gd.uid b/player.gd.uid new file mode 100644 index 0000000..ca72914 --- /dev/null +++ b/player.gd.uid @@ -0,0 +1 @@ +uid://w2rqm1u7p7im diff --git a/player.tscn b/player.tscn index 9521b1a..dc9a8c5 100644 --- a/player.tscn +++ b/player.tscn @@ -1,9 +1,14 @@ -[gd_scene format=3 uid="uid://cx0vga81xwckh"] +[gd_scene load_steps=2 format=3 uid="uid://cx0vga81xwckh"] + +[ext_resource type="Script" uid="uid://w2rqm1u7p7im" path="res://player.gd" id="1_4flbx"] [node name="Player" type="Node2D"] +script = ExtResource("1_4flbx") [node name="Field" type="Node2D" parent="."] [node name="Hand" type="Node2D" parent="."] [node name="Library" type="Node2D" parent="."] + +[node name="HTTPRequest" type="HTTPRequest" parent="."] diff --git a/tabletop.gd b/tabletop.gd index 57a6ed2..b70da20 100644 --- a/tabletop.gd +++ b/tabletop.gd @@ -1,9 +1,15 @@ 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) + pass # Replace with function body.