TableTower/core/table_entry_io.gd

155 lines
5.9 KiB
GDScript3
Raw Normal View History

class_name TableEntryIO
extends RefCounted
## Saves a TableEntry (one result on a card) to a plain Dictionary for the
## board file, and builds one back from such a Dictionary. Only values that
## differ from the defaults are written, so files stay small. TableEntry keeps
## to_dict(), from_dict() and clean_repeat() as one-line delegators.
## The entry as a Dictionary (the board file's form).
static func to_dict(entry: TableEntry) -> Dictionary:
var data := {"id": entry.id, "text": entry.text}
if entry.details != "":
data["details"] = entry.details
if entry.weight != 1:
data["weight"] = entry.weight
if entry.effects != "":
data["effects"] = entry.effects
if entry.when.strip_edges() != "":
data["when"] = entry.when.strip_edges()
if entry.show_if.strip_edges() != "":
data["show_if"] = entry.show_if.strip_edges()
if not entry.pipes.is_empty():
var list: Array = []
for target in entry.pipes:
list.append(_pipe_to_value(entry, target))
data["pipes"] = list
if entry.has_range:
data["range"] = [entry.range_lo, entry.range_hi]
if not entry.cells.is_empty():
data["cells"] = entry.cells.duplicate()
if not entry.repeat.is_empty():
data["repeat"] = entry.repeat.duplicate(true)
if entry.page != "":
data["page"] = entry.page
if entry.image != "":
data["image"] = entry.image
if entry.cost_value.strip_edges() != "":
data["cost"] = {"value": entry.cost_value.strip_edges(), "amount": entry.cost_amount.strip_edges()}
return data
## One pipe as saved: just the target id when every option is the default,
## else a Dictionary with "table" and the options that are set.
static func _pipe_to_value(entry: TableEntry, target: String) -> Variant:
var expr := entry.count_expr(target)
var count := entry.count_for(target)
var save_as := entry.save_for(target)
var condition := entry.when_for(target)
var otherwise := entry.is_otherwise(target)
var with_values: Variant = entry.pipe_with.get(target)
var color := str(entry.pipe_color.get(target, ""))
var combine := entry.combine_for(target)
var paused := entry.pipe_pause.has(target)
var dice := entry.dice_for(target)
var reroll := entry.reroll_for(target)
if expr == "" and count == 1 and save_as == "" and condition == "" and not otherwise \
and with_values == null and color == "" and combine == "all" and not paused \
and dice == "" and reroll.is_empty():
return target
var options := {"table": target}
if expr != "":
options["count"] = expr
elif count != 1:
options["count"] = count
if save_as != "":
options["save_as"] = save_as
if condition != "":
options["when"] = condition
elif otherwise:
options["otherwise"] = true
if with_values is Dictionary and not (with_values as Dictionary).is_empty():
options["with"] = (with_values as Dictionary).duplicate()
if color != "":
options["color"] = color
if combine != "all":
options["combine"] = combine
if paused:
options["pause"] = true
if dice != "":
options["dice"] = dice
if not reroll.is_empty():
options["reroll"] = reroll
return options
## A "repeat" value from JSON or the UI: {count >= 1, reroll?: [ids]}, or {}.
static func clean_repeat(value: Variant) -> Dictionary:
if not (value is Dictionary):
return {}
var count := int(JsonIO._num((value as Dictionary).get("count", 2), 2))
if count < 1:
return {}
var out := {"count": count}
var ids: Array[String] = []
if (value as Dictionary).get("reroll") is Array:
for item: Variant in value["reroll"]:
if str(item) != "" and not ids.has(str(item)):
ids.append(str(item))
if not ids.is_empty():
out["reroll"] = ids
return out
## Builds an entry from its saved Dictionary.
static func from_dict(data: Dictionary) -> TableEntry:
var entry := TableEntry.new(str(data.get("id", "")), str(data.get("text", "")))
entry.details = str(data.get("details", ""))
entry.weight = maxi(0, int(data.get("weight", 1)))
entry.effects = str(data.get("effects", ""))
entry.page = str(data.get("page", ""))
entry.image = str(data.get("image", ""))
var cost: Variant = data.get("cost")
if cost is Dictionary:
entry.cost_value = str(cost.get("value", ""))
entry.cost_amount = str(cost.get("amount", ""))
entry.when = str(data.get("when", "")).strip_edges() if data.get("when") != null else ""
entry.show_if = str(data.get("show_if", "")).strip_edges() if data.get("show_if") != null else ""
for pipe: Variant in data.get("pipes", []):
if pipe is Dictionary:
_read_pipe(entry, pipe)
elif str(pipe) != "":
entry.add_pipe(str(pipe))
if data.get("cells") is Array:
for cell: Variant in data["cells"]:
entry.cells.append(str(cell) if cell != null else "")
entry.repeat = clean_repeat(data.get("repeat"))
var range_value: Variant = data.get("range")
if range_value is Array and range_value.size() == 2:
entry.has_range = true
entry.range_lo = int(range_value[0])
entry.range_hi = int(range_value[1])
return entry
## Adds one saved pipe Dictionary ({"table": id, options...}) to `entry`.
static func _read_pipe(entry: TableEntry, pipe: Dictionary) -> void:
var target := str(pipe.get("table", ""))
if target == "":
return
var count: Variant = pipe.get("count", 1)
entry.add_pipe(target, count if count is String else int(count), str(pipe.get("save_as", "")))
entry.set_pipe_when(target, str(pipe.get("when", "")) if pipe.get("when") != null else "")
entry.set_pipe_otherwise(target, bool(pipe.get("otherwise", false)))
if pipe.get("with") is Dictionary:
entry.set_pipe_with(target, pipe["with"])
if str(pipe.get("color", "")) != "":
entry.pipe_color[target] = str(pipe["color"])
if RollCombine.MODES.has(str(pipe.get("combine", "all"))) and str(pipe.get("combine", "all")) != "all":
entry.pipe_combine[target] = str(pipe["combine"])
if bool(pipe.get("pause", false)):
entry.pipe_pause[target] = true
entry.set_pipe_dice(target, str(pipe.get("dice", "")) if pipe.get("dice") != null else "")
if pipe.get("reroll") is Array:
entry.set_pipe_reroll(target, pipe["reroll"])