From 5a55a5508e1f426963621e36e1b3e49332d4278d Mon Sep 17 00:00:00 2001 From: amizing25 Date: Fri, 29 Mar 2024 18:36:02 +0700 Subject: [PATCH] add validation to lineup length --- gameserver/src/net/tools.rs | 51 +++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/gameserver/src/net/tools.rs b/gameserver/src/net/tools.rs index 175fc3d..f1662b4 100644 --- a/gameserver/src/net/tools.rs +++ b/gameserver/src/net/tools.rs @@ -497,7 +497,7 @@ pub struct JsonData { pub main_character: MainCharacter, } -#[derive(Debug, Serialize, Deserialize, Default)] +#[derive(Debug, Serialize, Deserialize)] pub struct JsonData2 { #[serde(default)] pub lineups: BTreeMap, @@ -509,6 +509,23 @@ pub struct JsonData2 { pub main_character: MainCharacter, } +impl Default for JsonData2 { + fn default() -> Self { + let mut lineups = BTreeMap::::new(); + lineups.insert(0, 8006); + lineups.insert(1, 0); + lineups.insert(2, 0); + lineups.insert(3, 0); + + Self { + lineups , + position: Default::default(), + main_character: Default::default(), + scene: Default::default() + } + } +} + #[derive(Serialize, Deserialize, Clone, Debug, Copy)] pub enum MainCharacter { @@ -538,30 +555,42 @@ impl JsonData { }; let json2: JsonData2 = serde_json::from_str(&tokio::fs::read_to_string("persistent").await.unwrap_or_default()).unwrap_or_default(); - json.lineups = json2.lineups; json.position = json2.position; json.scene = json2.scene; json.main_character = json2.main_character; + json.verify_lineup().await; + json } + async fn verify_lineup(&mut self) { + if self.lineups.len() == 0 { + self.lineups = BTreeMap::::from([(0, 8006), (1, 0), (2, 0),(3, 0)]) + } else if self.lineups.len() < 4 { + for i in self.lineups.len()..4 { + self.lineups.insert(i as u32, 0); + } + } + self.save().await; + } + async fn create_dummy() -> Self { let mut db = Self { lightcones: vec![], relics: vec![], avatars: BTreeMap::::new(), - lineups: BTreeMap::::new(), + lineups: BTreeMap::::from([(0, 8006), (1, 0), (2, 0),(3, 0)]), scene: Default::default(), position: Default::default(), battle_config: Default::default(), main_character: Default::default(), }; db.avatars.insert( - 8004, + 8006, AvatarJson { - avatar_id: 8004, + avatar_id: 8006, level: 80, promotion: 6, sp_max: Some(10_000), @@ -571,17 +600,17 @@ impl JsonData { data: AvatarData { rank: 6, skills: BTreeMap::from([ - (800401, 6), - (800402, 10), - (800403, 10), - (800404, 10), - (800405, 1), + (800601, 6), + (800602, 10), + (800603, 10), + (800604, 10), + (800605, 1), ]), }, }, ); - db.lineups.insert(0, 8004); + db.lineups.insert(0, 8006); db.save().await;