74 lines
1.7 KiB
GDScript
74 lines
1.7 KiB
GDScript
extends Node
|
|
|
|
var _caching = preload("res://caching.gd")
|
|
|
|
var _decklist
|
|
|
|
|
|
func _init() -> void:
|
|
_decklist = Dictionary()
|
|
|
|
|
|
func _write_to_decks(_decks: Array) -> void:
|
|
if FileAccess.file_exists("user://decks.json"):
|
|
DirAccess.remove_absolute("user://decks.json")
|
|
|
|
var file = FileAccess.open("user://decks.json", FileAccess.WRITE)
|
|
file.store_line(JSON.stringify(_decks))
|
|
file.close()
|
|
|
|
|
|
func load_decks() -> Array:
|
|
if !FileAccess.file_exists("user://decks.json"):
|
|
return []
|
|
var file = FileAccess.open("user://decks.json", FileAccess.READ)
|
|
var file_text = file.get_as_text()
|
|
file.close()
|
|
return JSON.parse_string(file_text)
|
|
|
|
|
|
func _convert_mtgo_to_cache_lookup(decklist: String) -> Dictionary:
|
|
var _cards = {}
|
|
var lines = decklist.split("\n")
|
|
for line in lines:
|
|
var words = line.split(" ", false, 1)
|
|
if words.size() != 2:
|
|
continue
|
|
|
|
_cards[words[1]] = words[0]
|
|
return _cards
|
|
|
|
|
|
func _do_free(cache) -> void:
|
|
if !cache.has_emitted_all():
|
|
return
|
|
cache.queue_free()
|
|
|
|
|
|
func add_new_deck(cards: String, _name: String, about = "") -> void:
|
|
var _queries = _convert_mtgo_to_cache_lookup(cards)
|
|
_do_decklist_cache(_queries)
|
|
|
|
var _decks = load_decks()
|
|
_decks.push_back({"name": _name, "about": about, "decklist": _queries})
|
|
_write_to_decks(_decks)
|
|
|
|
|
|
func _do_decklist_cache(_queries: Dictionary) -> void:
|
|
var cache = _caching.new()
|
|
add_child(cache)
|
|
cache.setup()
|
|
|
|
for query in _queries:
|
|
var entry = cache.get_card_data_from_name(query)
|
|
if entry.size() == 0:
|
|
push_error("Failed to find card: " + query)
|
|
continue
|
|
_decklist[entry["id"]] = _queries[query]
|
|
cache.fetch_done.connect(_do_free.bind(cache))
|
|
|
|
|
|
func _show_decklist() -> void:
|
|
for card in _decklist:
|
|
print(card + " : " + _decklist[card])
|