mirror of
https://github.com/mr0xb/godot-flashcards.git
synced 2026-08-27 20:44:56 -04:00
117 lines
3.2 KiB
GDScript
117 lines
3.2 KiB
GDScript
class_name Deck
|
|
extends Resource
|
|
|
|
@export var id: String = ""
|
|
@export var name: String = "New Deck"
|
|
@export var description: String = ""
|
|
@export var cards: Array[Flashcard] = []
|
|
|
|
@export var author_id: String = "local"
|
|
@export var author_name: String = "Local User"
|
|
@export var author_created_at: String = ""
|
|
|
|
@export var created_at: String = ""
|
|
@export var updated_at: String = ""
|
|
@export var color_theme: Color = Color.CORNFLOWER_BLUE
|
|
@export var icon: String = ""
|
|
@export var tags: Array[String] = []
|
|
@export var is_public: bool = false
|
|
@export var download_count: int = 0
|
|
@export var rating: float = 0.0
|
|
|
|
func _init() -> void:
|
|
if id == "":
|
|
id = _generate_uuid()
|
|
var now = Time.get_datetime_string_from_system(true, true) + "Z"
|
|
created_at = now
|
|
updated_at = now
|
|
author_created_at = now
|
|
|
|
func to_dict() -> Dictionary:
|
|
var cards_data: Array = []
|
|
for card in cards:
|
|
cards_data.append(card.to_dict())
|
|
|
|
return {
|
|
"version": "1.0",
|
|
"deck": {
|
|
"id": id,
|
|
"name": name,
|
|
"description": description,
|
|
"author": {
|
|
"id": author_id,
|
|
"name": author_name,
|
|
"created_at": author_created_at
|
|
},
|
|
"metadata": {
|
|
"created_at": created_at,
|
|
"updated_at": updated_at,
|
|
"color_theme": color_theme.to_html(),
|
|
"icon": icon,
|
|
"tags": tags.duplicate(),
|
|
"is_public": is_public,
|
|
"download_count": download_count,
|
|
"rating": rating
|
|
},
|
|
"cards": cards_data
|
|
}
|
|
}
|
|
|
|
func from_dict(data: Dictionary) -> void:
|
|
var deck_data = data.get("deck", data)
|
|
|
|
id = deck_data.get("id", _generate_uuid())
|
|
name = deck_data.get("name", "New Deck")
|
|
description = deck_data.get("description", "")
|
|
|
|
var author = deck_data.get("author", {})
|
|
author_id = author.get("id", "local")
|
|
author_name = author.get("name", "Local User")
|
|
author_created_at = author.get("created_at", Time.get_datetime_string_from_system(true, true) + "Z")
|
|
|
|
var metadata = deck_data.get("metadata", {})
|
|
created_at = metadata.get("created_at", Time.get_datetime_string_from_system(true, true) + "Z")
|
|
updated_at = metadata.get("updated_at", created_at)
|
|
color_theme = Color.from_string(metadata.get("color_theme", "#6495ED"), Color.CORNFLOWER_BLUE)
|
|
icon = metadata.get("icon", "")
|
|
tags.assign(metadata.get("tags", []))
|
|
is_public = metadata.get("is_public", false)
|
|
download_count = metadata.get("download_count", 0)
|
|
rating = metadata.get("rating", 0.0)
|
|
|
|
cards.clear()
|
|
var cards_data = deck_data.get("cards", [])
|
|
for card_data in cards_data:
|
|
var card = Flashcard.new()
|
|
card.from_dict(card_data)
|
|
cards.append(card)
|
|
|
|
func add_card(card: Flashcard) -> void:
|
|
cards.append(card)
|
|
_update_modified_date()
|
|
|
|
func remove_card(card_id: String) -> bool:
|
|
for i in range(cards.size()):
|
|
if cards[i].id == card_id:
|
|
cards.remove_at(i)
|
|
_update_modified_date()
|
|
return true
|
|
return false
|
|
|
|
func get_card_by_id(card_id: String) -> Flashcard:
|
|
for card in cards:
|
|
if card.id == card_id:
|
|
return card
|
|
return null
|
|
|
|
func get_card_count() -> int:
|
|
return cards.size()
|
|
|
|
func _update_modified_date() -> void:
|
|
updated_at = Time.get_datetime_string_from_system(true, true) + "Z"
|
|
|
|
func _generate_uuid() -> String:
|
|
return "%x%x-%x-%x-%x-%x%x%x" % [
|
|
randi(), randi(), randi(), randi(),
|
|
randi(), randi(), randi(), randi()
|
|
]
|