rudimentary card loading

This commit is contained in:
ShyProton 2025-04-23 22:04:04 -04:00
parent 77da7cf6b2
commit fafaf404ab
7 changed files with 109 additions and 63 deletions

View File

@ -2,12 +2,14 @@ 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
@ -17,15 +19,17 @@ func _check_cache(id: String) -> bool:
return false
return true
func fetch_card(id: String) -> void:
if _check_cache(id):
return
await _do_http_request_card(id)
await _do_http_request_imgs(id, _png_path, true)
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)
@ -73,9 +77,7 @@ func _do_http_request_card(id: String) -> void:
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
)
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

86
card.gd
View File

@ -1,43 +1,83 @@
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.
## 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
# TODO: Implement card utilities such as mana cost, land value, etc using api data.
const ManaCosts = preload("res://data/mana.gd")
var card_id = "placedholder_id"
var card_name = "placeholder_name"
var card_type = "placeholder_card_type"
var oracle_text = "placeholder_oracle_text"
var card_id: String
var card_name: String
var card_type: String
var oracle_text: 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
func _ready() -> void:
var err = load_card()
if err == false:
var load_status = _load_card()
if load_status != OK:
# 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 load_card() -> bool:
var ondisk_card = FileAccess.open(
"user://card_cache/" + card_id + "/card.json", FileAccess.READ
)
if ondisk_card == null:
push_error("ERROR::CRITICAL::CACHE\nCard being accessed wasn't cached, yell at the coders plz")
return false
var card_json = JSON.parse_string(ondisk_card.get_as_text())
func _load_card() -> Error:
if _load_data() != OK:
return FAILED
if _load_image() != OK:
return FAILED
return OK
func _load_data() -> Error:
var cached_json = FileAccess.get_file_as_string("user://card_cache/" + card_id + "/card.json")
if cached_json.is_empty():
push_error("%s\nCard json data was not found in cache" % _card_error("CACHE"))
return FAILED
var card_json = JSON.parse_string(cached_json)
if card_json == null:
push_error("%s\nCard json data is could not be parsed as valid json" % _card_error("DATA"))
return FAILED
card_name = card_json["name"]
card_type = card_json["type_line"]
oracle_text = card_json["oracle_text"]
ondisk_card = null
return true
return OK
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
View 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")

View File

@ -3,12 +3,26 @@ 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
# TODO: Calculate this field's scale and position based on which no# field this is.
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,
@ -18,16 +32,12 @@ func _ready() -> void:
var gradient := Gradient.new()
gradient.offsets = gradient_data.keys()
gradient.colors = gradient_data.values()
var gradient_texture = GradientTexture1D.new()
gradient_texture.gradient = gradient
# gradient_texture.width = _screen_size.x
texture = gradient_texture
func set_colors(colors: Array[Color]) -> void:
_colors = colors
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:

View File

@ -1,18 +1,9 @@
[gd_scene load_steps=3 format=3 uid="uid://clnevm4xcexrs"]
[gd_scene load_steps=2 format=3 uid="uid://clnevm4xcexrs"]
[ext_resource type="Script" uid="uid://cqutu8u3qenu0" path="res://field.gd" id="1_6e7u2"]
[ext_resource type="Script" uid="uid://b3yqd1qu7dyq" path="res://card.gd" id="2_ujqnb"]
[node name="Field" type="TextureRect"]
offset_top = 540.0
offset_right = 1920.0
offset_bottom = 1080.0
script = ExtResource("1_6e7u2")
[node name="Card" type="TextureRect" parent="."]
layout_mode = 0
offset_left = -1.0
offset_top = 1.0
offset_right = 39.0
offset_bottom = 41.0
script = ExtResource("2_ujqnb")

View File

@ -2,8 +2,9 @@ extends Node2D
var _card_class = preload("res://card.gd")
var lib_cards
var num_cards
# Library cards are represented as an array of card IDs.
var lib_cards: Array[String] = []
var num_cards: int = 0
func _load_card_callback(card) -> void:
@ -20,6 +21,11 @@ func _init(card_ids: Array) -> void:
num_cards += 1
func init(card_ids: Array[String]) -> void:
for id in card_ids:
pass
func add_cards(cards: Array, top: bool) -> void:
for card in cards:
add_card(card, top)

View File

@ -6,25 +6,10 @@ 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.
func _ready() -> void:
# card = _card_class.new("d3f10f07-7cfe-4a6f-8de6-373e367a731b")
# add_child(card)
# card.cache_done.connect(_test_func)
# The first field in the array will be the player's own field.
# Might be a better idea to have that in a seperate variable? idk
fields.append(field_scene.instantiate())
var colors: Array[Color] = [Color(1, 0, 1)]
fields[0].set_colors(colors)