files-game/engine/cards.gd
2026-10-01 17:22:18 -04:00

66 lines
2.3 KiB
GDScript

class_name Cards
## Card definitions from the Cider deck CSV (cards.ts).
##
## A CardDef is a Dictionary: {name, kind, bandwidth, cores, health, slots, type, text, set} and,
## when printed, ability: {name, cost, text}. kind is "anomaly", "server", "program", "hardware" or
## "execution". A pool is {all: Array of defs, by_name: Dictionary name -> def}.
const DEFAULT_CSV := "res://data/cards.csv"
const KIND_BY_TEMPLATE := {
"Anomaly Front": "anomaly",
"Server Front": "server",
"Server Front Alt": "server",
"Program Front": "program",
"Hardware Front": "hardware",
"Execution Front": "execution",
}
static func _num(s: String) -> int:
return int(s) if s != "" else 0
## Reads the CSV's text. Reference cards (Turn Sequence) are skipped. By default only set 1 (the base
## game) is loaded; `sets` picks others, or "all". `source` names the file in errors.
static func parse_cards(csv: String, sets: Variant = [1], source := "cards.csv") -> Dictionary:
var all := []
var by_name := {}
for row in Csv.parse_objects(csv):
var kind: String = KIND_BY_TEMPLATE.get(row.get("Front Template", ""), "")
if kind == "":
continue
var set_no := int(row["Set"]) if row.get("Set", "") != "" else 1
if not (sets is String and sets == "all") and not (sets as Array).has(set_no):
continue
var card := {
"name": row["Name"],
"kind": kind,
"bandwidth": _num(row.get("Bandwidth", "")),
"cores": _num(row.get("Cores", "")),
"health": _num(row.get("Health", "")),
"slots": _num(row.get("Expansion Slots", "")),
"type": row.get("Type", ""),
"text": row.get("Description", ""),
"set": set_no,
}
if row.get("Ability Name", "") != "":
card.ability = {
"name": row["Ability Name"],
"cost": _num(row.get("Ability Cost", "")),
"text": row.get("Ability Description", ""),
}
if by_name.has(card.name):
Util.fail("Duplicate card name in %s: %s" % [source, card.name])
continue
all.append(card)
by_name[card.name] = card
return {"all": all, "by_name": by_name}
## Loads the card CSV from disk. See parse_cards for `sets`.
static func load_cards(path := DEFAULT_CSV, sets: Variant = [1]) -> Dictionary:
var text := FileAccess.get_file_as_string(path)
if text == "":
Util.fail("Can't read %s" % path)
return parse_cards(text, sets, path)