ravenous_typst/setup/analysis/class_costs.typ

161 lines
4.4 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Pure data analysis for school technique costs.
#import "../generation/radar_graphs.typ": school-sources
#import "../build_config.typ": school-tiers, tier-is-enabled
#let class-display-names = (
artilleur: "Artilleur",
bidouilleur: "Bidouilleur",
einerjar: "Einerjar",
franc-tireur: "Franc-tireur",
guerrier-mage: "Guerrier mage",
specialist-armes-lourdes: "Armes lourdes",
lancier: "Lancier",
mage-bestial: "Mage bestial",
maitre-lames: "Maître-lames",
protecteur: "Protecteur",
sangremage: "Sangremage",
)
#let empty-tier(tier) = (
tier: tier,
techniques: 0,
word-total: 0,
blood-total: 0.0,
blood-count: 0,
gauge-total: 0.0,
gauge-count: 0,
)
// Count prose words while ignoring Typst macro and glossary identifiers. Macro
// arguments remain in place because they often contain text visible to readers.
#let technique-word-count(value) = {
let prose = value
.replace(regex("[#@][A-Za-z_][A-Za-z0-9_:-]*"), " ")
.replace(regex("[^\p{L}'’]+"), " ")
.trim()
if prose == "" { 0 } else { prose.split(regex("\s+")).len() }
}
#let numeric-cost(value) = {
let value = value.trim()
if value.match(regex("^-?(?:\d+(?:\.\d+)?|\.\d+)$")) != none {
float(value)
} else {
none
}
}
#let add-tier-metric(metric, blood, gauge, words) = {
let result = metric
result.techniques += 1
result.word-total += words
if blood != none {
result.blood-total += blood
result.blood-count += 1
}
if gauge != none {
result.gauge-total += gauge
result.gauge-count += 1
}
result
}
#let summarize-tiers(tiers) = {
let total = empty-tier("Total")
for tier in tiers {
total.techniques += tier.techniques
total.word-total += tier.word-total
total.blood-total += tier.blood-total
total.blood-count += tier.blood-count
total.gauge-total += tier.gauge-total
total.gauge-count += tier.gauge-count
}
total
}
// Analyze one school TSV. Columns are resolved from each repeated header, so
// the extra metadata column in specialist_armes_lourdes.tsv is harmless.
#let analyze-class-costs(name, source) = {
let rows = csv(source, delimiter: "\t")
let tiers = school-tiers.map(empty-tier)
let current-tier = none
let blood-column = none
let gauge-column = none
let effect-column = none
for row in rows {
if row.len() == 0 { continue }
let first = row.first().trim()
if first.starts-with("Tier") {
let digits = first.replace(regex("\D"), "")
if digits != "" { current-tier = int(digits) }
continue
}
if first == "Technique" {
blood-column = row.position(cell => cell.trim() == "Sang")
gauge-column = row.position(cell => cell.trim() == "Jauge")
effect-column = row.position(cell => cell.trim() == "Effet")
if blood-column == none or gauge-column == none or effect-column == none {
panic("Missing Sang, Jauge or Effet column in `" + str(source) + "`")
}
continue
}
if first == "" or current-tier == none { continue }
if current-tier < 1 or not tier-is-enabled(current-tier) { continue }
let blood = if blood-column < row.len() {
numeric-cost(row.at(blood-column))
} else { none }
let gauge = if gauge-column < row.len() {
numeric-cost(row.at(gauge-column))
} else { none }
let words = if effect-column < row.len() {
technique-word-count(row.at(effect-column))
} else { 0 }
let index = current-tier - 1
tiers.at(index) = add-tier-metric(tiers.at(index), blood, gauge, words)
}
(
name: name,
display: class-display-names.at(name),
source: source,
tiers: tiers,
total: summarize-tiers(tiers),
)
}
#let all-class-costs = {
let result = (:)
for (name, source) in school-sources {
result.insert(name, analyze-class-costs(name, source))
}
result
}
#let portfolio-tiers(classes: all-class-costs) = {
range(school-tiers.len()).map(index => {
let aggregate = empty-tier(index + 1)
for (_, class-data) in classes {
let tier = class-data.tiers.at(index)
aggregate.techniques += tier.techniques
aggregate.word-total += tier.word-total
aggregate.blood-total += tier.blood-total
aggregate.blood-count += tier.blood-count
aggregate.gauge-total += tier.gauge-total
aggregate.gauge-count += tier.gauge-count
}
aggregate
})
}
#let portfolio-total(classes: all-class-costs) = summarize-tiers(
portfolio-tiers(classes: classes),
)
#let average(total, count) = if count == 0 { none } else { total / count }