Compare commits

3 Commits

9 changed files with 137 additions and 64 deletions

55
card.gd
View File

@ -1,4 +1,4 @@
extends Node extends TextureRect
## The card class [br][br] ## The card class [br][br]
## ##
## ##
@ -9,39 +9,47 @@ extends Node
# in the helper text box, but thats for the future # in the helper text box, but thats for the future
const ManaCosts = preload("res://data/mana.gd") const ManaCosts = preload("res://data/mana.gd")
var cached = false signal cache_done
var card_id = "placedholder_id" var card_id = "placedholder_id"
var card_name = "placeholder_name" var card_name = "placeholder_name"
var card_type = "placeholder_card_type" var card_type = "placeholder_card_type"
var oracle_text = "placeholder_oracle_text" var oracle_text = "placeholder_oracle_text"
var png_path = "placeholder_image_path" var _png_path = "placeholder_image_path"
var jpg_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 "CARD::" + card_id + "::" + error_type + "\n"
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):
return
await _do_cache_grab() await _do_cache_grab()
func _do_cache_grab() -> void: func _do_cache_grab() -> void:
await _do_http_request_card() await _do_http_request_card()
await _do_http_request_imgs(png_path, true) await _do_http_request_imgs(_png_path, true)
await _do_http_request_imgs(jpg_path, false) await _do_http_request_imgs(_jpg_path, false)
cache_done.emit()
func _check_cache(id: String) -> bool: func _check_cache(id: String) -> bool:
if (!FileAccess.file_exists("user://card_cache/" + id + "/card.json")): if !FileAccess.file_exists("user://card_cache/" + id + "/card.json"):
return false return false
if (!FileAccess.file_exists("user://card_cache/" + id + "/card.png")): if !FileAccess.file_exists("user://card_cache/" + id + "/card.png"):
return false return false
if (!FileAccess.file_exists("user://card_cache/" + id + "/card.jpg")): if !FileAccess.file_exists("user://card_cache/" + id + "/card.jpg"):
return false return false
return true return true
func _do_http_request_imgs(image_path: String, png: bool) -> void: func _do_http_request_imgs(image_path: String, png: bool) -> void:
var httpr = HTTPRequest.new() var httpr = HTTPRequest.new()
add_child(httpr) add_child(httpr)
@ -64,6 +72,7 @@ func _do_http_request_imgs(image_path: String, png: bool) -> void:
img.save_png("user://card_cache/" + card_id + ("/card.png" if png else "/card.jpg")) img.save_png("user://card_cache/" + card_id + ("/card.png" if png else "/card.jpg"))
img = null img = null
func _do_http_request_card() -> void: func _do_http_request_card() -> void:
var httpr = HTTPRequest.new() var httpr = HTTPRequest.new()
add_child(httpr) add_child(httpr)
@ -75,27 +84,29 @@ func _do_http_request_card() -> void:
var response = await httpr.request_completed var response = await httpr.request_completed
if response[0] != HTTPRequest.RESULT_SUCCESS: if response[0] != HTTPRequest.RESULT_SUCCESS:
push_error(_card_error("GET_REQUEST") + "Failed to fetch card data from Scryfall") push_error(_card_error("GET_REQUEST") + "Failed to fetch card data from Scryfall")
return return
var unprocessed_body = response[3].get_string_from_utf8() var unprocessed_body = response[3].get_string_from_utf8()
var card_content = JSON.parse_string(unprocessed_body) var card_content = JSON.parse_string(unprocessed_body)
if (card_content == null): if card_content == null:
push_error(_card_error("PARSING") + "Failed to parse the Scryfall card results.") push_error(_card_error("PARSING") + "Failed to parse the Scryfall card results.")
var dir = DirAccess.open("user://") var dir = DirAccess.open("user://")
dir.make_dir_recursive("user://card_cache/" + card_id + "/") # lets ensure the path is there dir.make_dir_recursive("user://card_cache/" + card_id + "/") # lets ensure the path is there
dir = null dir = null
var card_cache = FileAccess.open("user://card_cache/" + card_id + "/card.json", FileAccess.WRITE) 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.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"] var image_uris = card_content["image_uris"]
png_path = image_uris["png"] _png_path = image_uris["png"]
jpg_path = image_uris["normal"] _jpg_path = image_uris["normal"]
## load_card ## load_card
## ##
@ -105,16 +116,26 @@ func _do_http_request_card() -> void:
func load_card() -> bool: func load_card() -> bool:
if !_check_cache(card_id): if !_check_cache(card_id):
await _do_cache_grab() await _do_cache_grab()
push_error(_card_error("CACHE_FAIL") + "Cache wasn't ready, this card will need to be reinitialized") push_error(
(
_card_error("CACHE_FAIL")
+ "Cache wasn't ready, this card will need to be reinitialized"
)
)
return false return false
var ondisk_card = FileAccess.open(
var ondisk_card = FileAccess.open("user://card_cache/" + card_id + "/card.json", FileAccess.READ) "user://card_cache/" + card_id + "/card.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())
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()
img.load("user://card_cache/" + card_id + "/card.jpg")
texture = ImageTexture.create_from_image(img)
ondisk_card = null ondisk_card = null
return true return true

View File

@ -1,10 +1 @@
enum ManaCosts { enum ManaCosts { WHITE, BLUE, BLACK, RED, GREEN, COLOURLESS, GENERIC, LIFE }
WHITE,
BLUE,
BLACK,
RED,
GREEN,
COLOURLESS,
GENERIC,
LIFE
}

43
library.gd Normal file
View File

@ -0,0 +1,43 @@
extends Node2D
var _card_class = preload("res://card.gd")
var lib_cards
var num_cards
func _load_card_callback(card) -> void:
card.load_card()
func _init(card_ids: Array) -> void:
lib_cards = Array()
var temp_card
for id in card_ids:
temp_card = _card_class.new(id)
temp_card.cache_done.connect(_load_card_callback.bind(temp_card))
lib_cards.push_back(temp_card)
num_cards += 1
func add_cards(cards: Array, top: bool) -> void:
for card in cards:
add_card(card, top)
func add_card(card, top: bool) -> void:
if top:
lib_cards.push_front(card)
else:
lib_cards.push_back(card)
func shuffle() -> void:
lib_cards.shuffle()
func draw_cards(num) -> Array:
var ret = Array()
for i in num:
ret.push_back(lib_cards.pop_front())
return ret

1
library.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://bc51go8t8uvts

View File

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

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=3 format=3 uid="uid://cx0vga81xwckh"] [gd_scene load_steps=4 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"] [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")
@ -9,8 +10,13 @@ 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="."]
script = ExtResource("2_onrkg")
[node name="HTTPRequest" type="HTTPRequest" parent="."] [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")

View File

@ -15,6 +15,12 @@ run/main_scene="uid://b4ldtb3gw0jlu"
config/features=PackedStringArray("4.4", "Forward Plus") config/features=PackedStringArray("4.4", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[display]
window/size/viewport_width=1920
window/size/viewport_height=1080
window/size/mode=3
[editor_plugins] [editor_plugins]
enabled=PackedStringArray("res://addons/godot_vim/plugin.cfg") enabled=PackedStringArray()

View File

@ -3,7 +3,6 @@ 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.

View File

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