User-facing text only; code names, JSON keys and the project's internal name (and so the user:// folder) are unchanged. The Collection card kind keeps its name until a new one is chosen. The window title says Table Tarot; the top bar shows the theme's logo (the fortune teller, drawn from a copy of the art without its background square); the app icon is the mascot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
271 lines
7.2 KiB
GDScript
271 lines
7.2 KiB
GDScript
class_name TowerBoard
|
|
extends RefCounted
|
|
## A collection: every table on the board, plus loading and saving as JSON.
|
|
## Files carry a format_version; older versions are upgraded by migrate().
|
|
|
|
const SAVE_PATH := "user://board.json" # The single-board file of format version 1.
|
|
const FORMAT_VERSION := 2
|
|
|
|
static var _counter: int = 0
|
|
static var _salt: String = ""
|
|
|
|
## Stable slug used as the file name and as the key for local state.
|
|
var id: String = ""
|
|
## Display name; free to change.
|
|
var name: String = ""
|
|
var tables: Array[RollTable] = []
|
|
## Group cards (frames around blocks of tables). Optional; a table is in at
|
|
## most one group. See TableGroup.
|
|
var groups: Array[TableGroup] = []
|
|
|
|
|
|
static func new_id(prefix: String) -> String:
|
|
if _salt == "":
|
|
_salt = "%x" % (int(Time.get_unix_time_from_system()) % 0xfffff)
|
|
_counter += 1
|
|
return "%s%s_%d" % [prefix, _salt, _counter]
|
|
|
|
|
|
func get_table(id: String) -> RollTable:
|
|
for t in tables:
|
|
if t.id == id:
|
|
return t
|
|
return null
|
|
|
|
|
|
## Removes a table, every pipe that pointed at it and its group membership.
|
|
func remove_table(id: String) -> void:
|
|
for i in range(tables.size() - 1, -1, -1):
|
|
if tables[i].id == id:
|
|
tables.remove_at(i)
|
|
for t in tables:
|
|
for e in t.entries:
|
|
e.pipes.erase(id)
|
|
for g in groups:
|
|
g.members.erase(id)
|
|
|
|
|
|
# ---------- Groups ----------
|
|
|
|
func get_group(gid: String) -> TableGroup:
|
|
for g in groups:
|
|
if g.id == gid:
|
|
return g
|
|
return null
|
|
|
|
|
|
## The group a table belongs to, or null.
|
|
func group_of(table_id: String) -> TableGroup:
|
|
for g in groups:
|
|
if g.members.has(table_id):
|
|
return g
|
|
return null
|
|
|
|
|
|
## True when the table is hidden inside a collapsed group.
|
|
func is_hidden(table_id: String) -> bool:
|
|
var g := group_of(table_id)
|
|
return g != null and g.collapsed
|
|
|
|
|
|
## Makes a group of the given tables (taking them out of any other group).
|
|
func add_group(gname: String, member_ids: Array, gid: String = "") -> TableGroup:
|
|
var g := TableGroup.new(gid if gid != "" else new_id("g"), gname)
|
|
groups.append(g)
|
|
for tid: Variant in member_ids:
|
|
add_to_group(str(tid), g.id)
|
|
return g
|
|
|
|
|
|
## Puts a table in a group, taking it out of its old one. False if either is missing.
|
|
func add_to_group(table_id: String, gid: String) -> bool:
|
|
var g := get_group(gid)
|
|
if g == null or get_table(table_id) == null:
|
|
return false
|
|
var old := group_of(table_id)
|
|
if old == g:
|
|
return true
|
|
if old:
|
|
old.members.erase(table_id)
|
|
g.members.append(table_id)
|
|
return true
|
|
|
|
|
|
func remove_from_group(table_id: String) -> void:
|
|
var g := group_of(table_id)
|
|
if g:
|
|
g.members.erase(table_id)
|
|
|
|
|
|
## Removes the group only; its cards stay on the board.
|
|
func ungroup(gid: String) -> void:
|
|
for i in range(groups.size() - 1, -1, -1):
|
|
if groups[i].id == gid:
|
|
groups.remove_at(i)
|
|
|
|
|
|
## Removes the group and every table in it. Returns the removed table ids.
|
|
func delete_group(gid: String) -> Array[String]:
|
|
var g := get_group(gid)
|
|
var gone: Array[String] = []
|
|
if g == null:
|
|
return gone
|
|
gone.assign(g.members.duplicate())
|
|
ungroup(gid)
|
|
for tid in gone:
|
|
remove_table(tid)
|
|
return gone
|
|
|
|
|
|
## Moves a group's frame and every member table by `delta`.
|
|
func move_group(gid: String, delta: Vector2) -> void:
|
|
var g := get_group(gid)
|
|
if g == null:
|
|
return
|
|
g.position += delta
|
|
for tid in g.members:
|
|
var t := get_table(tid)
|
|
if t:
|
|
t.position += delta
|
|
|
|
|
|
## Drops members that aren't tables, keeps each table in its first group only,
|
|
## and gives groups unique ids. Empty groups are kept (they can be refilled).
|
|
func clean_groups() -> void:
|
|
var ids := {}
|
|
for t in tables:
|
|
ids[t.id] = true
|
|
var taken := {}
|
|
var gids := {}
|
|
for g in groups:
|
|
if g.id == "" or gids.has(g.id):
|
|
g.id = new_id("g")
|
|
gids[g.id] = true
|
|
var keep: Array[String] = []
|
|
for m in g.members:
|
|
if ids.has(m) and not taken.has(m):
|
|
taken[m] = true
|
|
keep.append(m)
|
|
g.members = keep
|
|
|
|
|
|
## Upgrades a parsed file to FORMAT_VERSION, one version step at a time.
|
|
## To add a version, bump FORMAT_VERSION and add a _migrate_N_to_N1 step here.
|
|
static func migrate(d: Dictionary) -> Dictionary:
|
|
var v: int = int(d.get("format_version", 1))
|
|
while v < FORMAT_VERSION:
|
|
match v:
|
|
1:
|
|
d = _migrate_1_to_2(d)
|
|
_:
|
|
break
|
|
v += 1
|
|
d["format_version"] = v
|
|
return d
|
|
|
|
|
|
## Version 1 had no id or name; they are filled in by whoever imports the file.
|
|
static func _migrate_1_to_2(d: Dictionary) -> Dictionary:
|
|
d["format_version"] = 2
|
|
if not d.has("id"):
|
|
d["id"] = ""
|
|
if not d.has("name"):
|
|
d["name"] = ""
|
|
return d
|
|
|
|
|
|
func to_dict() -> Dictionary:
|
|
var list: Array = []
|
|
for t in tables:
|
|
list.append(t.to_dict())
|
|
var d := {"format_version": FORMAT_VERSION, "id": id, "name": name, "tables": list}
|
|
if not groups.is_empty():
|
|
var gl: Array = []
|
|
for g in groups:
|
|
gl.append(g.to_dict())
|
|
d["groups"] = gl
|
|
return d
|
|
|
|
|
|
func to_json() -> String:
|
|
return JSON.stringify(to_dict(), " ")
|
|
|
|
|
|
## Replaces the board's tables (and id/name, when the text has them) from JSON
|
|
## text of any known version. Returns false if the text isn't a board.
|
|
func load_json(text: String) -> bool:
|
|
var parsed: Variant = JSON.parse_string(text)
|
|
if not (parsed is Dictionary) or not (parsed.get("tables") is Array):
|
|
return false
|
|
return load_dict(parsed as Dictionary)
|
|
|
|
|
|
## Same as load_json for an already parsed Dictionary.
|
|
func load_dict(src: Dictionary) -> bool:
|
|
if not (src.get("tables") is Array):
|
|
return false
|
|
var d: Dictionary = migrate(src.duplicate())
|
|
var loaded: Array[RollTable] = []
|
|
for td in d["tables"]:
|
|
if td is Dictionary:
|
|
loaded.append(RollTable.from_dict(td))
|
|
tables = loaded
|
|
var loaded_groups: Array[TableGroup] = []
|
|
var src_groups: Variant = d.get("groups", [])
|
|
if src_groups is Array:
|
|
for gd: Variant in src_groups:
|
|
if gd is Dictionary:
|
|
loaded_groups.append(TableGroup.from_dict(gd))
|
|
groups = loaded_groups
|
|
clean_groups()
|
|
if str(d.get("id", "")) != "":
|
|
id = str(d["id"])
|
|
if str(d.get("name", "")) != "":
|
|
name = str(d["name"])
|
|
return true
|
|
|
|
|
|
## Writes text to path via a temp file and a rename, so a crash can't leave a
|
|
## half-written file. Creates the folder if needed.
|
|
static func write_text_atomic(path: String, text: String) -> Error:
|
|
var dir := path.get_base_dir()
|
|
if not DirAccess.dir_exists_absolute(dir):
|
|
var mk := DirAccess.make_dir_recursive_absolute(dir)
|
|
if mk != OK:
|
|
return mk
|
|
var tmp := path + ".tmp"
|
|
var f := FileAccess.open(tmp, FileAccess.WRITE)
|
|
if f == null:
|
|
return FileAccess.get_open_error()
|
|
f.store_string(text)
|
|
var werr := f.get_error()
|
|
f.close()
|
|
if werr != OK:
|
|
DirAccess.remove_absolute(tmp)
|
|
return werr
|
|
var err := DirAccess.rename_absolute(tmp, path)
|
|
if err != OK and FileAccess.file_exists(path):
|
|
# Some platforms won't rename over an existing file.
|
|
DirAccess.remove_absolute(path)
|
|
err = DirAccess.rename_absolute(tmp, path)
|
|
if err != OK:
|
|
DirAccess.remove_absolute(tmp)
|
|
return err
|
|
|
|
|
|
func save(path: String = SAVE_PATH) -> bool:
|
|
var err := write_text_atomic(path, to_json())
|
|
if err != OK:
|
|
push_warning("Couldn't save the spread to %s: %s" % [path, error_string(err)])
|
|
return false
|
|
return true
|
|
|
|
|
|
## Loads a board file, or returns null if there isn't a usable one.
|
|
static func load_saved(path: String = SAVE_PATH) -> TowerBoard:
|
|
if not FileAccess.file_exists(path):
|
|
return null
|
|
var b := TowerBoard.new()
|
|
if not b.load_json(FileAccess.get_file_as_string(path)) or b.tables.is_empty():
|
|
return null
|
|
return b
|