From 879021ca910c6f2982e64aba84e246ea414e0de8 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 20 Aug 2026 01:15:49 +0200 Subject: [PATCH] Initial forge --- chapters/craft/forge-data.typ | 151 +++++++++++++++++++++++++++++++++ chapters/craft/forge-table.typ | 24 ++++-- forge-data-test.typ | 23 +++++ 3 files changed, 189 insertions(+), 9 deletions(-) create mode 100644 chapters/craft/forge-data.typ create mode 100644 forge-data-test.typ diff --git a/chapters/craft/forge-data.typ b/chapters/craft/forge-data.typ new file mode 100644 index 0000000..40b41bb --- /dev/null +++ b/chapters/craft/forge-data.typ @@ -0,0 +1,151 @@ +#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() diff --git a/chapters/craft/forge-table.typ b/chapters/craft/forge-table.typ index 519e0af..0c1b94d 100644 --- a/chapters/craft/forge-table.typ +++ b/chapters/craft/forge-table.typ @@ -1,12 +1,19 @@ -#import "@preview/typsqlite:0.1.0": sqlite, sqlite-table +#import "forge-data.typ": equipments -#let db = sqlite(read("/data/src/db/weapons.db", encoding: none)) -// #panic( -// #sqlite-table( -// (db.query)("SELECT * FROM equipements"), -// fill: (x, y) => if y == 0 { gray.lighten(70%) }, -// ), -// ) +// This is intentionally only a diagnostic table. The forge rules live in +// `forge-data.typ`; richer category-specific rendering can be added here. +#table( + columns: 6, + table.header([Forme], [Catégorie], [Dégâts], [Précision], [Protection], [Intégrité]), + ..equipments.map(item => ( + [#item.name], + [#item.shape_category], + [#item.damage], + [#item.precision], + [#item.protection], + [#item.integrity], + )).flatten(), +) // \begin{landscape}\begin{center} @@ -81,4 +88,3 @@ // \end{landscape} // - diff --git a/forge-data-test.typ b/forge-data-test.typ new file mode 100644 index 0000000..2f062e9 --- /dev/null +++ b/forge-data-test.typ @@ -0,0 +1,23 @@ +#import "chapters/craft/forge-data.typ": * + +#assert.eq(equipments.len(), 36) + +#let griffes-legeres = equipments.find(item => item.shape_id == 1 and item.nature_id == 33) +#assert.eq(griffes-legeres.name, "Griffes légère") +#assert.eq(griffes-legeres.damage, 3) +#assert.eq(griffes-legeres.p_damage, 0) +#assert.eq(griffes-legeres.precision, 2) +#assert.eq(griffes-legeres.torque_one_handed, 4) +#assert.eq(griffes-legeres.torque_two_handed, none) + +#let bouclier-leger = equipments.find(item => item.shape_id == 8 and item.nature_id == 90) +#assert.eq(bouclier-leger.protection, 2) +#assert.eq(bouclier-leger.parry, 3) +#assert.eq(bouclier-leger.torque_one_handed, 6) + +#let armure-legere = equipments.find(item => item.shape_id == 9 and item.nature_id == 39) +#assert.eq(armure-legere.protection, 3) +#assert.eq(armure-legere.integrity, 5) +#assert.eq(armure-legere.torque_two_handed, 9) + +Forge data tests passed.