add separate sort order field to levels, so they can be reordered without changing the id and invalidating solutions

This commit is contained in:
Crispy 2024-10-07 12:04:55 +02:00
parent 90bc93fa02
commit e43a422708
6 changed files with 27 additions and 11 deletions

View file

@ -1,8 +0,0 @@
{
"id": "00_zeroes",
"name": "Zeroes",
"description": "learn how to output data\nthis is a multi-line test",
"init_board": null,
"inputs": [],
"outputs": [0, 0, 0, 0, 0, 0, 0, 0]
}

View file

@ -1,7 +1,8 @@
{ {
"id": "01_cat", "id": "intro_copy_input",
"sort_order": 12,
"name": "Copy Cat", "name": "Copy Cat",
"description": "learn how to meow", "description": "read input and output the same thing",
"init_board": null, "init_board": null,
"inputs": [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 103, 33], "inputs": [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 103, 33],
"outputs": [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 103, 33] "outputs": [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 103, 33]

9
levels/01_loop.json Normal file
View file

@ -0,0 +1,9 @@
{
"id": "intro_loop",
"sort_order": 11,
"name": "Loop",
"description": "repeated output",
"init_board": "o o\n \n*P \n \n \n ",
"inputs": [],
"outputs": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}

9
levels/01_zero.json Normal file
View file

@ -0,0 +1,9 @@
{
"id": "intro_output",
"sort_order": 10,
"name": "Zero",
"description": "learn how to output data",
"init_board": "o o\n \n*P \n \n \n ",
"inputs": [],
"outputs": [0]
}

View file

@ -3,6 +3,7 @@ use serde::Deserialize;
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct Level { pub struct Level {
id: String, id: String,
sort_order: i32,
name: String, name: String,
description: String, description: String,
init_board: Option<String>, init_board: Option<String>,
@ -15,6 +16,10 @@ impl Level {
&self.id &self.id
} }
pub fn sort_order(&self) -> i32 {
self.sort_order
}
pub fn name(&self) -> &str { pub fn name(&self) -> &str {
&self.name &self.name
} }

View file

@ -236,7 +236,7 @@ fn get_levels() -> Vec<Level> {
levels.push(level); levels.push(level);
} }
} }
levels.sort_by(|a, b| a.id().cmp(b.id())); levels.sort_by(|a, b| a.sort_order().cmp(&b.sort_order()));
levels levels
} }