Compare commits
9 Commits
godot
...
c75aec06ac
Author | SHA1 | Date | |
---|---|---|---|
c75aec06ac | |||
d726290cf2 | |||
fafaf404ab | |||
77da7cf6b2 | |||
21f7d9de04 | |||
6100a50754 | |||
bf9244c2b7 | |||
a3fb627310 | |||
c085a93c49 |
88
caching.gd
Normal file
88
caching.gd
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
extends Node
|
||||||
|
|
||||||
|
var _jpg_path
|
||||||
|
var _png_path
|
||||||
|
|
||||||
|
signal cache_done
|
||||||
|
|
||||||
|
|
||||||
|
func _cache_error(err: String) -> String:
|
||||||
|
return "CACHE::ERROR::" + err + "\n"
|
||||||
|
|
||||||
|
|
||||||
|
func _check_cache(id: String) -> bool:
|
||||||
|
if !FileAccess.file_exists("user://card_cache/" + id + "/card.json"):
|
||||||
|
return false
|
||||||
|
if !FileAccess.file_exists("user://card_cache/" + id + "/card.png"):
|
||||||
|
return false
|
||||||
|
if !FileAccess.file_exists("user://card_cache/" + id + "/card.jpg"):
|
||||||
|
return false
|
||||||
|
return true
|
||||||
|
|
||||||
|
|
||||||
|
func fetch_card(id: String) -> void:
|
||||||
|
if _check_cache(id):
|
||||||
|
return
|
||||||
|
|
||||||
|
await _do_http_request_card(id)
|
||||||
|
OS.delay_msec(100)
|
||||||
|
await _do_http_request_imgs(id, _png_path, true)
|
||||||
|
OS.delay_msec(100)
|
||||||
|
await _do_http_request_imgs(id, _jpg_path, false)
|
||||||
|
cache_done.emit()
|
||||||
|
|
||||||
|
|
||||||
|
func _do_http_request_imgs(id: String, 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(_cache_error("GET_REQUEST") + "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(_cache_error("IMG_LOADING") + "Couldn't load the image.")
|
||||||
|
|
||||||
|
img.save_png("user://card_cache/" + id + ("/card.png" if png else "/card.jpg"))
|
||||||
|
img = null
|
||||||
|
|
||||||
|
|
||||||
|
func _do_http_request_card(id: String) -> 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/" + id, headers)
|
||||||
|
if error != OK:
|
||||||
|
push_error(_cache_error("GET_REQUEST") + "An error occurred in the Scryfall request.")
|
||||||
|
|
||||||
|
var response = await httpr.request_completed
|
||||||
|
|
||||||
|
if response[0] != HTTPRequest.RESULT_SUCCESS:
|
||||||
|
push_error(_cache_error("GET_REQUEST") + "Failed to fetch card data from Scryfall")
|
||||||
|
return
|
||||||
|
|
||||||
|
var unprocessed_body = response[3].get_string_from_utf8()
|
||||||
|
var card_content = JSON.parse_string(unprocessed_body)
|
||||||
|
if card_content == null:
|
||||||
|
push_error(_cache_error("PARSING") + "Failed to parse the Scryfall card results.")
|
||||||
|
|
||||||
|
var dir = DirAccess.open("user://")
|
||||||
|
dir.make_dir_recursive("user://card_cache/" + id + "/") # lets ensure the path is there
|
||||||
|
dir = null
|
||||||
|
|
||||||
|
var card_cache = FileAccess.open("user://card_cache/" + 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"]
|
||||||
|
_png_path = image_uris["png"]
|
||||||
|
_jpg_path = image_uris["normal"]
|
1
caching.gd.uid
Normal file
1
caching.gd.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://cml6c3fdbyy75
|
168
card.gd
168
card.gd
@ -1,141 +1,83 @@
|
|||||||
extends TextureRect
|
extends TextureRect
|
||||||
## The card class [br][br]
|
## The card class
|
||||||
##
|
|
||||||
##
|
##
|
||||||
|
## Represents an instance of a card to be displayed on the tabletop.
|
||||||
## Contains helper text for the text, the cards ID, and the image path.
|
## 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
|
# TODO: Implement card utilities such as mana cost, land value, etc using api data.
|
||||||
# in the helper text box, but thats for the future
|
|
||||||
const ManaCosts = preload("res://data/mana.gd")
|
const ManaCosts = preload("res://data/mana.gd")
|
||||||
|
|
||||||
signal cache_done
|
var card_id: String
|
||||||
|
var card_name: String
|
||||||
var card_id = "placedholder_id"
|
var card_type: String
|
||||||
var card_name = "placeholder_name"
|
var oracle_text: String
|
||||||
var card_type = "placeholder_card_type"
|
|
||||||
var oracle_text = "placeholder_oracle_text"
|
|
||||||
var _png_path = "placeholder_image_path"
|
|
||||||
var _jpg_path = "placeholder_image_path"
|
|
||||||
|
|
||||||
|
|
||||||
func _card_error(error_type: String) -> String:
|
func _card_error(error_type: String) -> String:
|
||||||
return "CARD::" + card_id + "::" + error_type + "\n"
|
return "ERROR::CARD::%s::%s::%s::\n" % [card_id, card_name, error_type]
|
||||||
|
|
||||||
|
|
||||||
func _init(id) -> void:
|
func init(id) -> void:
|
||||||
card_id = id
|
card_id = id
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
if _check_cache(card_id):
|
var load_status = _load_card()
|
||||||
return
|
if load_status != OK:
|
||||||
await _do_cache_grab()
|
# TODO: No need to push another error as the failure state of loading does that already,
|
||||||
|
# if the card is not cached, perhaps a placeholder blank card can be used instead?
|
||||||
|
# Setting that up can be put here later...
|
||||||
|
push_error("Failed to load card.")
|
||||||
|
|
||||||
|
|
||||||
func _do_cache_grab() -> void:
|
func _load_card() -> Error:
|
||||||
await _do_http_request_card()
|
if _load_data() != OK:
|
||||||
await _do_http_request_imgs(_png_path, true)
|
return FAILED
|
||||||
await _do_http_request_imgs(_jpg_path, false)
|
|
||||||
cache_done.emit()
|
if _load_image() != OK:
|
||||||
|
return FAILED
|
||||||
|
|
||||||
|
return OK
|
||||||
|
|
||||||
|
|
||||||
func _check_cache(id: String) -> bool:
|
func _load_data() -> Error:
|
||||||
if !FileAccess.file_exists("user://card_cache/" + id + "/card.json"):
|
var cached_json = FileAccess.get_file_as_string("user://card_cache/" + card_id + "/card.json")
|
||||||
return false
|
|
||||||
if !FileAccess.file_exists("user://card_cache/" + id + "/card.png"):
|
|
||||||
return false
|
|
||||||
if !FileAccess.file_exists("user://card_cache/" + id + "/card.jpg"):
|
|
||||||
return false
|
|
||||||
return true
|
|
||||||
|
|
||||||
|
if cached_json.is_empty():
|
||||||
|
push_error("%s\nCard json data was not found in cache" % _card_error("CACHE"))
|
||||||
|
return FAILED
|
||||||
|
|
||||||
func _do_http_request_imgs(image_path: String, png: bool) -> void:
|
var card_json = JSON.parse_string(cached_json)
|
||||||
var httpr = HTTPRequest.new()
|
|
||||||
add_child(httpr)
|
|
||||||
|
|
||||||
var headers = PackedStringArray(["User-Agent: MTGUntapClone/0.1", "Accept: */*"])
|
if card_json == null:
|
||||||
var error = httpr.request(image_path, headers)
|
push_error("%s\nCard json data is could not be parsed as valid json" % _card_error("DATA"))
|
||||||
if error != OK:
|
return FAILED
|
||||||
push_error(_card_error("GET_REQUEST") + "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(_card_error("IMG_LOADING") + "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(_card_error("GET_REQUEST") + "An error occurred in the Scryfall request.")
|
|
||||||
|
|
||||||
var response = await httpr.request_completed
|
|
||||||
|
|
||||||
if response[0] != HTTPRequest.RESULT_SUCCESS:
|
|
||||||
push_error(_card_error("GET_REQUEST") + "Failed to fetch card data from Scryfall")
|
|
||||||
return
|
|
||||||
|
|
||||||
var unprocessed_body = response[3].get_string_from_utf8()
|
|
||||||
var card_content = JSON.parse_string(unprocessed_body)
|
|
||||||
if card_content == null:
|
|
||||||
push_error(_card_error("PARSING") + "Failed to parse the Scryfall card results.")
|
|
||||||
|
|
||||||
var dir = DirAccess.open("user://")
|
|
||||||
dir.make_dir_recursive("user://card_cache/" + card_id + "/") # lets ensure the path is there
|
|
||||||
dir = null
|
|
||||||
|
|
||||||
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 = null # closes the file
|
|
||||||
|
|
||||||
var image_uris = card_content["image_uris"]
|
|
||||||
_png_path = image_uris["png"]
|
|
||||||
_jpg_path = image_uris["normal"]
|
|
||||||
|
|
||||||
|
|
||||||
## load_card
|
|
||||||
##
|
|
||||||
## Loads the card, returns false, and triggers
|
|
||||||
## a cache fetch if the card is not in the cache,
|
|
||||||
## otherwise sets the cards variables if the cache is present.
|
|
||||||
func load_card() -> bool:
|
|
||||||
if !_check_cache(card_id):
|
|
||||||
await _do_cache_grab()
|
|
||||||
push_error(
|
|
||||||
(
|
|
||||||
_card_error("CACHE_FAIL")
|
|
||||||
+ "Cache wasn't ready, this card will need to be reinitialized"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return false
|
|
||||||
|
|
||||||
var ondisk_card = FileAccess.open(
|
|
||||||
"user://card_cache/" + card_id + "/card.json", FileAccess.READ
|
|
||||||
)
|
|
||||||
var card_json = JSON.parse_string(ondisk_card.get_as_text())
|
|
||||||
|
|
||||||
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 img = Image.new()
|
return OK
|
||||||
img.load("user://card_cache/" + card_id + "/card.jpg")
|
|
||||||
texture = ImageTexture.create_from_image(img)
|
|
||||||
|
|
||||||
ondisk_card = null
|
|
||||||
return true
|
func _load_image() -> Error:
|
||||||
|
# NOTE: Assuming we're going with using the .png cards on board.
|
||||||
|
var cached_img = FileAccess.get_file_as_bytes("user://card_cache/" + card_id + "/card.jpg")
|
||||||
|
|
||||||
|
if cached_img.is_empty():
|
||||||
|
push_error("%sCard on-board image was not found in cache" % _card_error("CACHE"))
|
||||||
|
return FAILED
|
||||||
|
|
||||||
|
var image = Image.new()
|
||||||
|
var image_status: Error = image.load_png_from_buffer(cached_img)
|
||||||
|
|
||||||
|
if image_status != OK:
|
||||||
|
push_error("%sCard on-board image failed to load correctly" % _card_error("IMAGE"))
|
||||||
|
return FAILED
|
||||||
|
|
||||||
|
var image_texture = ImageTexture.new()
|
||||||
|
image_texture.set_image(image)
|
||||||
|
|
||||||
|
texture = image_texture
|
||||||
|
|
||||||
|
return OK
|
||||||
|
12
card.tscn
Normal file
12
card.tscn
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[gd_scene load_steps=2 format=3 uid="uid://cah3mvdnom1xg"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" uid="uid://b3yqd1qu7dyq" path="res://card.gd" id="1_kikvd"]
|
||||||
|
|
||||||
|
[node name="Card" type="TextureRect"]
|
||||||
|
offset_left = 794.0
|
||||||
|
offset_top = 79.0
|
||||||
|
offset_right = 919.0
|
||||||
|
offset_bottom = 254.0
|
||||||
|
expand_mode = 5
|
||||||
|
stretch_mode = 4
|
||||||
|
script = ExtResource("1_kikvd")
|
44
field.gd
Normal file
44
field.gd
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
extends TextureRect
|
||||||
|
|
||||||
|
var _screen_size: Vector2
|
||||||
|
var _colors: Array[Color]
|
||||||
|
|
||||||
|
var _card_scene = preload("res://card.tscn")
|
||||||
|
|
||||||
|
|
||||||
|
# Called when the node enters the scene tree for the first time.
|
||||||
|
func _ready() -> void:
|
||||||
|
# TODO: Calculate this field's scale and position based on which no# field this is.
|
||||||
|
_screen_size = get_viewport_rect().size
|
||||||
|
|
||||||
|
var card = _card_scene.instantiate()
|
||||||
|
|
||||||
|
# TODO: Currently working with an already-cached card with a known ID to load this.
|
||||||
|
# Later on, the cards should be pulling the IDs directly from the library's list of IDs.
|
||||||
|
card.init("d3f10f07-7cfe-4a6f-8de6-373e367a731b")
|
||||||
|
|
||||||
|
add_child(card)
|
||||||
|
|
||||||
|
|
||||||
|
func set_colors(colors: Array[Color]) -> void:
|
||||||
|
_colors = colors
|
||||||
|
|
||||||
|
# TODO: Method to take list of colors, split into this format of dictionary, and apply as gradient.
|
||||||
|
var gradient_data := {
|
||||||
|
0.0: Color.MAROON,
|
||||||
|
1.0: Color.MAROON,
|
||||||
|
}
|
||||||
|
|
||||||
|
var gradient := Gradient.new()
|
||||||
|
gradient.offsets = gradient_data.keys()
|
||||||
|
gradient.colors = gradient_data.values()
|
||||||
|
|
||||||
|
var gradient_texture = GradientTexture1D.new()
|
||||||
|
gradient_texture.gradient = gradient
|
||||||
|
|
||||||
|
texture = gradient_texture
|
||||||
|
|
||||||
|
|
||||||
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
|
func _process(delta: float) -> void:
|
||||||
|
pass
|
1
field.gd.uid
Normal file
1
field.gd.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://cqutu8u3qenu0
|
9
field.tscn
Normal file
9
field.tscn
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[gd_scene load_steps=2 format=3 uid="uid://clnevm4xcexrs"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" uid="uid://cqutu8u3qenu0" path="res://field.gd" id="1_6e7u2"]
|
||||||
|
|
||||||
|
[node name="Field" type="TextureRect"]
|
||||||
|
offset_top = 540.0
|
||||||
|
offset_right = 1920.0
|
||||||
|
offset_bottom = 1080.0
|
||||||
|
script = ExtResource("1_6e7u2")
|
10
library.gd
10
library.gd
@ -2,8 +2,9 @@ extends Node2D
|
|||||||
|
|
||||||
var _card_class = preload("res://card.gd")
|
var _card_class = preload("res://card.gd")
|
||||||
|
|
||||||
var lib_cards
|
# Library cards are represented as an array of card IDs.
|
||||||
var num_cards
|
var lib_cards: Array[String] = []
|
||||||
|
var num_cards: int = 0
|
||||||
|
|
||||||
|
|
||||||
func _load_card_callback(card) -> void:
|
func _load_card_callback(card) -> void:
|
||||||
@ -20,6 +21,11 @@ func _init(card_ids: Array) -> void:
|
|||||||
num_cards += 1
|
num_cards += 1
|
||||||
|
|
||||||
|
|
||||||
|
func init(card_ids: Array[String]) -> void:
|
||||||
|
for id in card_ids:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
func add_cards(cards: Array, top: bool) -> void:
|
func add_cards(cards: Array, top: bool) -> void:
|
||||||
for card in cards:
|
for card in cards:
|
||||||
add_card(card, top)
|
add_card(card, top)
|
||||||
|
31
player.gd
31
player.gd
@ -1,32 +1,19 @@
|
|||||||
extends Node2D
|
extends Node2D
|
||||||
|
|
||||||
var _card_class = preload("res://card.gd")
|
# var _card_class = preload("res://card.gd")
|
||||||
|
|
||||||
var card
|
var field_scene = preload("res://field.tscn")
|
||||||
|
var fields: Array[Node] = []
|
||||||
|
|
||||||
func _on_request_completed(result, response_code, headers, body):
|
|
||||||
var json = JSON.parse_string(body.get_string_from_utf8())
|
|
||||||
print(json["name"])
|
|
||||||
|
|
||||||
|
|
||||||
func _test_func():
|
|
||||||
card.load_card()
|
|
||||||
print(card.card_id)
|
|
||||||
print(card.card_name)
|
|
||||||
print(card.card_type)
|
|
||||||
print(card.oracle_text)
|
|
||||||
|
|
||||||
|
|
||||||
# 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.
|
# The first field in the array will be the player's own field.
|
||||||
card = _card_class.new("d3f10f07-7cfe-4a6f-8de6-373e367a731b")
|
# Might be a better idea to have that in a seperate variable? idk
|
||||||
add_child(card)
|
fields.append(field_scene.instantiate())
|
||||||
|
var colors: Array[Color] = [Color(1, 0, 1)]
|
||||||
card.cache_done.connect(_test_func)
|
fields[0].set_colors(colors)
|
||||||
|
add_child(fields[0])
|
||||||
pass # Replace with function body.
|
|
||||||
|
|
||||||
|
|
||||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
|
12
player.tscn
12
player.tscn
@ -1,22 +1,12 @@
|
|||||||
[gd_scene load_steps=4 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://bc51go8t8uvts" path="res://library.gd" id="2_onrkg"]
|
[ext_resource type="Script" uid="uid://bc51go8t8uvts" path="res://library.gd" id="2_onrkg"]
|
||||||
[ext_resource type="Script" uid="uid://b3yqd1qu7dyq" path="res://card.gd" id="3_i3pqv"]
|
|
||||||
|
|
||||||
[node name="Player" type="Node2D"]
|
[node name="Player" type="Node2D"]
|
||||||
script = ExtResource("1_4flbx")
|
script = ExtResource("1_4flbx")
|
||||||
|
|
||||||
[node name="Field" type="Node2D" parent="."]
|
|
||||||
|
|
||||||
[node name="Hand" type="Node2D" parent="."]
|
[node name="Hand" type="Node2D" parent="."]
|
||||||
|
|
||||||
[node name="Library" type="Node2D" parent="."]
|
[node name="Library" type="Node2D" parent="."]
|
||||||
script = ExtResource("2_onrkg")
|
script = ExtResource("2_onrkg")
|
||||||
|
|
||||||
[node name="TextureRect" type="TextureRect" parent="."]
|
|
||||||
offset_left = -1.0
|
|
||||||
offset_top = 1.0
|
|
||||||
offset_right = 39.0
|
|
||||||
offset_bottom = 41.0
|
|
||||||
script = ExtResource("3_i3pqv")
|
|
||||||
|
Reference in New Issue
Block a user