Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 424ef3e8fd |
@@ -1,111 +0,0 @@
|
|||||||
//! Level 8 — tags and newlines. The scroll preserves its lines and
|
|
||||||
//! demands explicit types.
|
|
||||||
//!
|
|
||||||
//! Paired design note: `l08.md`.
|
|
||||||
//!
|
|
||||||
//! The target value uses:
|
|
||||||
//! - a multi-line string for `scroll:` (block scalar `|` round-trips through
|
|
||||||
//! serde_yaml as a `Value::String` with embedded newlines),
|
|
||||||
//! - a fractional float for `weight:` (forces float type without needing the
|
|
||||||
//! `!!float` tag in the target text — but the player can use either),
|
|
||||||
//! - a string of digits for `title:` (player needs quotes or `!!str` to
|
|
||||||
//! avoid the integer interpretation).
|
|
||||||
|
|
||||||
use rand::seq::SliceRandom;
|
|
||||||
use rand::{Rng, SeedableRng};
|
|
||||||
use rand_chacha::ChaCha8Rng;
|
|
||||||
use serde::Serialize;
|
|
||||||
use serde_yaml::{Mapping, Value};
|
|
||||||
|
|
||||||
use crate::describe::Describer;
|
|
||||||
|
|
||||||
use super::{Generated, Level};
|
|
||||||
|
|
||||||
pub struct Tags;
|
|
||||||
|
|
||||||
const VERBS: &[&str] = &["Beware", "Avoid", "Heed", "Mark"];
|
|
||||||
const NOUNS: &[&str] = &[
|
|
||||||
"the path that turns twice",
|
|
||||||
"the third spring",
|
|
||||||
"the silent statue",
|
|
||||||
"the moonlit door",
|
|
||||||
];
|
|
||||||
const TITLES: &[&str] = &["1024", "2048", "4096", "8192"];
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
struct DescCtx {
|
|
||||||
scroll_lines: Vec<String>,
|
|
||||||
weight: f64,
|
|
||||||
title: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Level for Tags {
|
|
||||||
fn id(&self) -> u8 {
|
|
||||||
8
|
|
||||||
}
|
|
||||||
|
|
||||||
fn name(&self) -> &'static str {
|
|
||||||
"Scroll of Tags"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate(&self, seed: u64) -> Generated {
|
|
||||||
let mut rng = ChaCha8Rng::seed_from_u64(seed ^ 0x0000_0000_0000_0008);
|
|
||||||
|
|
||||||
let line_n = 2;
|
|
||||||
let scroll_lines: Vec<String> = (0..line_n)
|
|
||||||
.map(|_| {
|
|
||||||
let v = VERBS.choose(&mut rng).unwrap();
|
|
||||||
let n = NOUNS.choose(&mut rng).unwrap();
|
|
||||||
format!("{v} {n}.")
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
// Block scalar `|` produces a string ending in `\n` after the last line.
|
|
||||||
let scroll_text = format!("{}\n", scroll_lines.join("\n"));
|
|
||||||
|
|
||||||
// Fractional weight so serde_yaml's float serialisation keeps the
|
|
||||||
// `.5` and matches the player's submission unambiguously.
|
|
||||||
let weight = rng.gen_range(1..=20) as f64 + 0.5;
|
|
||||||
let title = TITLES.choose(&mut rng).unwrap().to_string();
|
|
||||||
|
|
||||||
let mut top = Mapping::new();
|
|
||||||
top.insert(
|
|
||||||
Value::String("scroll".to_string()),
|
|
||||||
Value::String(scroll_text),
|
|
||||||
);
|
|
||||||
top.insert(Value::String("weight".to_string()), Value::from(weight));
|
|
||||||
top.insert(
|
|
||||||
Value::String("title".to_string()),
|
|
||||||
Value::String(title.clone()),
|
|
||||||
);
|
|
||||||
|
|
||||||
let target_yaml =
|
|
||||||
serde_yaml::to_string(&Value::Mapping(top)).expect("serialise mapping");
|
|
||||||
|
|
||||||
let mut d = Describer::new();
|
|
||||||
d.register(
|
|
||||||
"l08",
|
|
||||||
"A scroll demands its types. Match exactly:\n\
|
|
||||||
scroll: multi-line text{% for line in scroll_lines %}\n {{ line }}{% endfor %}\n\
|
|
||||||
weight: must parse as the float {{ weight }}\n\
|
|
||||||
title: must parse as the string \"{{ title }}\"\n\
|
|
||||||
💡 Block scalar `|` preserves newlines; `!!str` (or quotes) forces a digit-only string.",
|
|
||||||
)
|
|
||||||
.expect("register template");
|
|
||||||
let description = d
|
|
||||||
.render(
|
|
||||||
"l08",
|
|
||||||
&DescCtx {
|
|
||||||
scroll_lines,
|
|
||||||
weight,
|
|
||||||
title,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.expect("render template");
|
|
||||||
|
|
||||||
Generated {
|
|
||||||
target_yaml,
|
|
||||||
description,
|
|
||||||
flavor: "📜 An ancient scroll insists on its precise form.".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
101
src/levels/l10_dynamic.rs
Normal file
101
src/levels/l10_dynamic.rs
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
//! Level 10 — dynamic values. The vault ledger accepts numbers and
|
||||||
|
//! dates in many forms.
|
||||||
|
//!
|
||||||
|
//! Paired design note: `l10.md`.
|
||||||
|
//!
|
||||||
|
//! The target is canonical (decimal ints, decimal float, ISO date
|
||||||
|
//! string). The lesson is that the player can write equivalent values
|
||||||
|
//! in hex / octal / exponent forms — all parse to the same number.
|
||||||
|
|
||||||
|
use rand::{Rng, SeedableRng};
|
||||||
|
use rand_chacha::ChaCha8Rng;
|
||||||
|
use serde::Serialize;
|
||||||
|
use serde_yaml::{Mapping, Value};
|
||||||
|
|
||||||
|
use crate::describe::Describer;
|
||||||
|
|
||||||
|
use super::{Generated, Level};
|
||||||
|
|
||||||
|
pub struct Dynamic;
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct DescCtx {
|
||||||
|
gold_dec: i64,
|
||||||
|
gold_hex: String,
|
||||||
|
silver_dec: i64,
|
||||||
|
silver_oct: String,
|
||||||
|
experience: f64,
|
||||||
|
date: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Level for Dynamic {
|
||||||
|
fn id(&self) -> u8 {
|
||||||
|
10
|
||||||
|
}
|
||||||
|
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"Vault Ledger"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate(&self, seed: u64) -> Generated {
|
||||||
|
let mut rng = ChaCha8Rng::seed_from_u64(seed ^ 0x0000_0000_0000_000A);
|
||||||
|
|
||||||
|
let gold = rng.gen_range(0x100..=0xFFFi64);
|
||||||
|
let silver = rng.gen_range(0o100..=0o777i64);
|
||||||
|
// Fractional float so the canonical serialisation keeps the `.5`.
|
||||||
|
let experience = rng.gen_range(10..=99) as f64 + 0.5;
|
||||||
|
let year = rng.gen_range(1100..=2050i64);
|
||||||
|
let month = rng.gen_range(1..=12i64);
|
||||||
|
let day = rng.gen_range(1..=28i64);
|
||||||
|
let date = format!("{year:04}-{month:02}-{day:02}");
|
||||||
|
|
||||||
|
let mut vault = Mapping::new();
|
||||||
|
vault.insert(Value::String("gold".to_string()), Value::from(gold));
|
||||||
|
vault.insert(Value::String("silver".to_string()), Value::from(silver));
|
||||||
|
vault.insert(
|
||||||
|
Value::String("experience".to_string()),
|
||||||
|
Value::from(experience),
|
||||||
|
);
|
||||||
|
vault.insert(
|
||||||
|
Value::String("date".to_string()),
|
||||||
|
Value::String(date.clone()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut top = Mapping::new();
|
||||||
|
top.insert(Value::String("vault".to_string()), Value::Mapping(vault));
|
||||||
|
|
||||||
|
let target_yaml =
|
||||||
|
serde_yaml::to_string(&Value::Mapping(top)).expect("serialise mapping");
|
||||||
|
|
||||||
|
let mut d = Describer::new();
|
||||||
|
d.register(
|
||||||
|
"l10",
|
||||||
|
"The vault ledger demands its values:\n\
|
||||||
|
gold: {{ gold_dec }} (try hex: 0x{{ gold_hex }})\n\
|
||||||
|
silver: {{ silver_dec }} (try octal: 0o{{ silver_oct }})\n\
|
||||||
|
experience: {{ experience }} (any equivalent float form passes)\n\
|
||||||
|
date: {{ date }} (an ISO-8601 date string)\n\
|
||||||
|
💡 Any equivalent numeric form passes — what matters is the parsed value.",
|
||||||
|
)
|
||||||
|
.expect("register template");
|
||||||
|
let description = d
|
||||||
|
.render(
|
||||||
|
"l10",
|
||||||
|
&DescCtx {
|
||||||
|
gold_dec: gold,
|
||||||
|
gold_hex: format!("{:X}", gold),
|
||||||
|
silver_dec: silver,
|
||||||
|
silver_oct: format!("{:o}", silver),
|
||||||
|
experience,
|
||||||
|
date,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.expect("render template");
|
||||||
|
|
||||||
|
Generated {
|
||||||
|
target_yaml,
|
||||||
|
description,
|
||||||
|
flavor: "🪙 A vault ledger awaits in many ciphers.".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
pub mod l01_minimum;
|
pub mod l01_minimum;
|
||||||
pub mod l02_kv;
|
pub mod l02_kv;
|
||||||
pub mod l03_dict;
|
pub mod l03_dict;
|
||||||
pub mod l08_tags;
|
pub mod l10_dynamic;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user