Skip to content

Commit

Permalink
Turns out the game name could be a map if you want to submit undecisi…
Browse files Browse the repository at this point in the history
…ve yamls

Handle that as bes we can
  • Loading branch information
Eijebong committed Feb 6, 2024
1 parent b212404 commit 3671d7f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use crate::diesel_uuid::Uuid as DieselUuid;
use crate::error::Result;
use crate::schema::{rooms, yamls};
Expand Down Expand Up @@ -50,9 +52,16 @@ pub struct Yaml {
pub game: String,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Debug)]
#[serde(untagged)]
pub enum YamlGame {
Name(String),
Map(HashMap<String, f64>),
}

#[derive(serde::Deserialize, Debug)]
pub struct YamlFile {
pub game: String,
pub game: YamlGame,
pub name: String,
}

Expand Down Expand Up @@ -111,14 +120,24 @@ pub fn add_yaml_to_room(
ctx: &State<Context>,
) -> Result<()> {
let mut conn = ctx.db_pool.get()?;
let game_name = match &parsed.game {
YamlGame::Name(name) => name.clone(),
YamlGame::Map(map) => {
if map.len() == 1 {
map.keys().next().unwrap().clone()
} else {
"Unknown".to_string()
}
}
};

let new_yaml = NewYaml {
id: DieselUuid::random(),
owner_id: DieselUuid(owner_id),
room_id: DieselUuid(uuid),
content,
player_name: &parsed.name,
game: &parsed.game,
game: &game_name,
};
diesel::insert_into(yamls::table)
.values(new_yaml)
Expand Down

0 comments on commit 3671d7f

Please sign in to comment.