ravenous_typst/chapters/craft/forge-data.typ

152 lines
4.0 KiB
Plaintext

#import "@preview/typsqlite:0.1.0": sqlite
// Data access and composition for forged equipment.
//
// Keep this module free of presentation code: category-specific rules can be
// supplied through `post-process` without changing how the database is read or
// how shapes and natures are matched.
#let db = sqlite(read("/data/src/db/weapons.db", encoding: none))
#let _json-scalar(value) = {
if value == none { none } else { json(bytes(value)) }
}
#let _records(sql) = {
let result = (db.query)(sql)
result.rows.map(row => {
let record = (:)
for (column, value) in result.columns.zip(row) {
record.insert(column, value)
}
// These columns are stored as JSON scalars inside SQLite text columns.
for field in ("category", "target", "apply_category") {
record.insert(field, _json-scalar(record.at(field)))
}
record
})
}
#let shapes = _records(
"SELECT * FROM Equipements WHERE meta_category = 'Shape' ORDER BY id",
)
#let natures = _records(
"SELECT * FROM Equipements WHERE meta_category = 'Nature' ORDER BY id",
)
#let numeric-fields = (
"rarity",
"cost",
"difficulty",
"tier",
"damage",
"p_damage",
"precision",
"rmax",
"armor_break",
"armor_pierce",
"antimaterial",
"protection",
"integrity",
"dodge",
"parry",
"speed",
"stealth",
"action_cost",
"range",
"agility_bonus",
)
#let _json-list(value) = {
if value == none or value == "" or value == "null" {
()
} else {
let decoded = json(bytes(value))
if type(decoded) == array { decoded } else { (decoded,) }
}
}
// Torque uses JSON-like text in the current database (for example "6" or
// "null"). A missing torque means that grip is unavailable and stays missing.
#let _add-torque(left, right) = {
if left == none or right == none or left == "null" or right == "null" {
none
} else {
int(left) + int(right)
}
}
#let compatible-natures(shape, from: natures) = {
if shape.apply_category == none {
()
} else {
from.filter(nature => nature.target == shape.apply_category)
}
}
#let compose(shape, nature) = {
assert.eq(shape.meta_category, "Shape")
assert.eq(nature.meta_category, "Nature")
assert.eq(
nature.target,
shape.apply_category,
message: "The nature does not apply to this shape",
)
// Start from the shape so fields which describe its fundamental behaviour
// remain available. Every overwritten field below is intentional.
let equipment = shape
equipment.insert("id", none)
equipment.insert("shape_id", shape.id)
equipment.insert("nature_id", nature.id)
equipment.insert("meta_category", "Equipment")
equipment.insert("name", shape.name + " " + nature.adjective)
equipment.insert("adjective", nature.adjective)
equipment.insert("shape_category", shape.category)
equipment.insert("nature_category", nature.category)
equipment.insert("category", shape.category)
equipment.insert("target", none)
equipment.insert("apply_category", shape.apply_category)
for field in numeric-fields {
equipment.insert(field, shape.at(field, default: 0) + nature.at(field, default: 0))
}
equipment.insert(
"torque_two_handed",
_add-torque(shape.torque_two_handed, nature.torque_two_handed),
)
equipment.insert(
"torque_one_handed",
_add-torque(shape.torque_one_handed, nature.torque_one_handed),
)
equipment.insert(
"damage_type",
_json-list(shape.damage_type) + _json-list(nature.damage_type),
)
equipment.insert(
"special",
_json-list(shape.special) + _json-list(nature.special),
)
equipment
}
// `post-process` is deliberately the final step. It may inspect the preserved
// shape/nature categories and return a modified equipment dictionary.
#let build-equipments(
shapes: shapes,
natures: natures,
post-process: equipment => equipment,
) = {
shapes.fold((), (result, shape) => {
result + compatible-natures(shape, from: natures).map(nature => {
post-process(compose(shape, nature))
})
})
}
#let equipments = build-equipments()