Skip to main content

State

Struct State 

Source
pub struct State {
    pub version: String,
    pub username: String,
    pub workspace: PathBuf,
    pub current_level: String,
    pub start_time: NaiveDateTime,
    pub levels_complete: IndexMap<String, LevelCompletion>,
    pub level_attempts: IndexMap<String, u32>,
    pub level_hints_used: IndexMap<String, u32>,
    pub level_started_at: IndexMap<String, NaiveDateTime>,
    pub completed_at: Option<NaiveDateTime>,
}
Expand description

Everything the game remembers about one player: the contents of state.json.

The fields are written in declaration order, which is the order pydantic uses. The four per-level maps are keyed by level ID and keep insertion order.

§Level ID migration

Versions 1.0 and 2.0 numbered the sections differently. When such a file is loaded, the IDs in current_level and in the keys of the four per-level maps are remapped (2.7 and 2.8 swap, 6.7 and 6.8 swap, then section 7 becomes 8, 8 becomes 9, 9 becomes 10 and 10 becomes 7), so every level keeps its content and its credit.

The migration, the version check and the Czech error messages belong to StateStore::load; deserializing a State directly bypasses them.

Fields§

§version: String

Schema version. CURRENT_STATE_VERSION once loaded or created.

§username: String

Name of the player; also names the default workspace.

§workspace: PathBuf

Directory in which the level fixtures are built.

§current_level: String

ID of the level the player is on, for example "1.12".

§start_time: NaiveDateTime

When the game was started.

§levels_complete: IndexMap<String, LevelCompletion>

Finished levels, in the order they were finished.

§level_attempts: IndexMap<String, u32>

Failed submissions per level.

§level_hints_used: IndexMap<String, u32>

Number of revealed hints per level.

§level_started_at: IndexMap<String, NaiveDateTime>

When the player first saw each level.

§completed_at: Option<NaiveDateTime>

When the player finished the last level; None while the game is still running.

Implementations§

Source§

impl State

Source

pub fn new( username: impl Into<String>, workspace: impl AsRef<Path>, now: NaiveDateTime, ) -> Self

A fresh game on the introduction 0.0 (Python’s StateManager.create).

workspace is normalized like a Python Path. The timer of the first level starts at now.

Source

pub fn record_attempt(&mut self, id: &str)

Counts one failed submission on level id. Successful ones are never recorded here.

Source

pub fn ensure_level_started(&mut self, id: &str, now: NaiveDateTime) -> bool

Starts the timer of level id at now, unless it is already running.

Returns whether the timer was started, that is, whether the state changed and needs saving.

Source

pub fn record_completion( &mut self, id: &str, now: NaiveDateTime, ) -> LevelCompletion

Records level id as finished at now and returns the new record.

The time is the number of whole seconds since the level was started, never negative and 0 when the start was never recorded. Hints and attempts are copied from the per-level counters.

Source

pub fn hint_status(&self, id: &str, total_hints: usize) -> usize

How many of the level’s total_hints hints the player has seen already.

Source

pub fn reveal_next_hint( &mut self, id: &str, total_hints: usize, ) -> Option<usize>

Reveals the next hint of level id.

Returns the index of the newly revealed hint, or None when all total_hints hints have been revealed already (the state is then left unchanged).

Source

pub fn is_finished(&self) -> bool

Whether the player has finished the whole game.

Source

pub fn reset_level_progress(&mut self, id: &str, now: NaiveDateTime)

Forgets everything recorded for level id and starts its timer anew at now.

Also clears completed_at, so a finished game becomes playable again. Meant for dev jump, which must be able to replay any level from scratch. Other levels keep their entries and their order.

Trait Implementations§

Source§

impl Clone for State

Source§

fn clone(&self) -> State

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for State

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for State

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for State

Source§

impl PartialEq for State

Source§

fn eq(&self, other: &State) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for State

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for State

Auto Trait Implementations§

§

impl Freeze for State

§

impl RefUnwindSafe for State

§

impl Send for State

§

impl Sync for State

§

impl Unpin for State

§

impl UnsafeUnpin for State

§

impl UnwindSafe for State

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.