Skip to main content
Verticopolis engine

verticopolis_engine/
snapshot.rs

1//! The hashed state view: the saved game without its prose, which the
2//! conformance suite hashes at every checkpoint. Hash it with
3//! [`crate::canonical::digest`] to tell whether two games are in the same
4//! state.
5//!
6//! ```
7//! use verticopolis_engine::canonical::digest;
8//! use verticopolis_engine::clock::GameMode;
9//! use verticopolis_engine::sim::Simulation;
10//! use verticopolis_engine::snapshot::state_view;
11//!
12//! let mut a = Simulation::new_game(9, GameMode::Classic);
13//! let mut b = Simulation::new_game(9, GameMode::Classic);
14//! a.tick(120.0);
15//! b.tick(60.0);
16//! b.tick(60.0);
17//! assert_eq!(digest(&state_view(&a)), digest(&state_view(&b)));
18//! ```
19use serde_json::Value;
20
21use crate::sim::Simulation;
22
23/// The saved game with every unit listed on its own
24/// ([`Simulation::serialize_unfolded`], so the floor-run encoding never moves
25/// a hash), without the log messages' text and the pending choice's message,
26/// so wording changes never move a hash, and without the crowd, which the
27/// conformance suite hashes as its own channel ([`crate::crowd::Crowd::view`]),
28/// the cars' timers mid-stop, the housekeeping tallies, the day's move-ins or
29/// a first-day game's period marks, which the hash has never held. They show
30/// in the hash through what they decide: whether the morning checkout line,
31/// the housekeeping nudges and the evening's "New tenants:" line are logged,
32/// and the money the first day's end bills.
33pub fn state_view(sim: &Simulation) -> Value {
34    let mut v = sim.serialize_unfolded_without(&[
35        "crowd",
36        "dispatch",
37        "housekeeping",
38        "moveIns",
39        "periodMarks",
40    ]);
41    if let Some(log) = v.get_mut("log").and_then(Value::as_array_mut) {
42        for e in log {
43            if let Some(o) = e.as_object_mut() {
44                o.remove("text");
45            }
46        }
47    }
48    if let Some(p) = v
49        .get_mut("events")
50        .and_then(|e| e.get_mut("pending"))
51        .and_then(Value::as_object_mut)
52    {
53        p.remove("message");
54    }
55    v
56}