From 296f70c1b2c17667e10a8756cdcb2ce82f89bfd2 Mon Sep 17 00:00:00 2001 From: juliancoffee Date: Mon, 8 Jan 2024 19:55:10 +0200 Subject: [PATCH] Unify /buff_complex and /buff Turns out parse_cmd_args allows omitting arguments, /buff [misc_data] will be idential to /buff [strength] [duration] [misc_data] --- assets/voxygen/i18n/en/command.ftl | 3 +- common/src/cmd.rs | 30 +++----------- server/src/cmd.rs | 63 +++++++++++++----------------- 3 files changed, 34 insertions(+), 62 deletions(-) diff --git a/assets/voxygen/i18n/en/command.ftl b/assets/voxygen/i18n/en/command.ftl index 963caa7360..4b76ba0b6a 100644 --- a/assets/voxygen/i18n/en/command.ftl +++ b/assets/voxygen/i18n/en/command.ftl @@ -64,8 +64,7 @@ command-battlemode-available-modes = Available modes: pvp, pve command-battlemode-same = Attempted to set the same battlemode command-battlemode-updated = New battlemode: { $battlemode } command-buff-unknown = Unknown buff: { $buff } -command-buff-complex = /buff doesn't work with [{ $buff }], use /buff_complex -command-buff-simple = /buff_complex doesn't work with [{ $buff }], use /buff +command-buff-data = Buff argument '{ $buff }' requires additional data command-buff-body-unknown = Unknown body spec: { $spec } command-skillpreset-load-error = Error while loading presets command-skillpreset-broken = Skill preset is broken diff --git a/common/src/cmd.rs b/common/src/cmd.rs index a7d7228490..b26059550e 100644 --- a/common/src/cmd.rs +++ b/common/src/cmd.rs @@ -200,23 +200,14 @@ lazy_static! { buff_pack }; - static ref BUFFS_SIMPLE: Vec = { - let mut buff_pack: Vec = BUFF_PARSER - .iter() - .filter_map(|(key, kind)| kind.is_simple().then_some(key.clone())) - .collect(); + static ref BUFFS: Vec = { + let mut buff_pack: Vec = BUFF_PARSER.keys().cloned().collect(); // Add `all` as valid command - buff_pack.push("all".to_string()); + buff_pack.push("all".to_owned()); buff_pack }; - static ref BUFFS_COMPLEX: Vec = - BUFF_PARSER - .iter() - .filter_map(|(key, kind)| (!kind.is_simple()).then_some(key.clone())) - .collect(); - static ref BLOCK_KINDS: Vec = terrain::block::BlockKind::iter() .map(|bk| bk.to_string()) .collect(); @@ -322,7 +313,6 @@ pub enum ServerChatCommand { BattleModeForce, Body, Buff, - BuffComplex, Build, Campfire, CreateLocation, @@ -437,23 +427,14 @@ impl ServerChatCommand { ), ServerChatCommand::Buff => cmd( vec![ - Enum("buff", BUFFS_SIMPLE.clone(), Required), + Enum("buff", BUFFS.clone(), Required), Float("strength", 0.01, Optional), Float("duration", 10.0, Optional), + Any("buff data spec", Optional), ], "Cast a buff on player", Some(Admin), ), - ServerChatCommand::BuffComplex => cmd( - vec![ - Enum("buff", BUFFS_COMPLEX.clone(), Required), - Any("buff data spec", Required), - Float("strength", 0.01, Optional), - Float("duration", 10.0, Optional), - ], - "Cast a complex buff on player", - Some(Admin), - ), ServerChatCommand::Ban => cmd( vec![ PlayerName(Required), @@ -955,7 +936,6 @@ impl ServerChatCommand { ServerChatCommand::BattleModeForce => "battlemode_force", ServerChatCommand::Body => "body", ServerChatCommand::Buff => "buff", - ServerChatCommand::BuffComplex => "buff_complex", ServerChatCommand::Build => "build", ServerChatCommand::AreaAdd => "area_add", ServerChatCommand::AreaList => "area_list", diff --git a/server/src/cmd.rs b/server/src/cmd.rs index afe0a243cc..a478ca4108 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -131,7 +131,6 @@ fn do_command( ServerChatCommand::BattleModeForce => handle_battlemode_force, ServerChatCommand::Body => handle_body, ServerChatCommand::Buff => handle_buff, - ServerChatCommand::BuffComplex => handle_buff_complex, ServerChatCommand::Build => handle_build, ServerChatCommand::AreaAdd => handle_area_add, ServerChatCommand::AreaList => handle_area_list, @@ -4141,60 +4140,57 @@ fn handle_buff( args: Vec, action: &ServerChatCommand, ) -> CmdResult<()> { - let (Some(buff), strength, duration) = parse_cmd_args!(args, String, f32, f64) else { + let (Some(buff), strength, duration, misc_data_spec) = + parse_cmd_args!(args, String, f32, f64, String) + else { return Err(Content::Plain(action.help_string())); }; let strength = strength.unwrap_or(0.01); - let duration = duration.unwrap_or(1.0); - let buffdata = BuffData::new(strength, Some(Secs(duration))); if buff == "all" { + let duration = duration.unwrap_or(5.0); + let buffdata = BuffData::new(strength, Some(Secs(duration))); + + // apply every(*) non-complex buff + // + // (*) BUFF_PACK contains all buffs except + // invulnerability BUFF_PACK .iter() .filter_map(|kind_key| parse_buffkind(kind_key)) .filter(|buffkind| buffkind.is_simple()) .for_each(|buffkind| cast_buff(buffkind, buffdata, server, target)); + Ok(()) } else { let buffkind = parse_buffkind(&buff).ok_or_else(|| { Content::localized_with_args("command-buff-unknown", [("buff", buff.clone())]) })?; - if !buffkind.is_simple() { - return Err(Content::localized_with_args("command-buff-complex", [( - "buff", buff, - )])); + if buffkind.is_simple() { + let duration = duration.unwrap_or(10.0); + let buffdata = BuffData::new(strength, Some(Secs(duration))); + cast_buff(buffkind, buffdata, server, target); + Ok(()) + } else { + // default duration is longer for complex buffs + let duration = duration.unwrap_or(20.0); + let spec = misc_data_spec.ok_or_else(|| { + Content::localized_with_args("command-buff-data", [("buff", buff.clone())]) + })?; + cast_buff_complex(buffkind, server, target, spec, strength, duration) } - - cast_buff(buffkind, buffdata, server, target); } - - Ok(()) } -fn handle_buff_complex( +fn cast_buff_complex( + buffkind: BuffKind, server: &mut Server, - _client: EcsEntity, target: EcsEntity, - args: Vec, - action: &ServerChatCommand, + spec: String, + strength: f32, + duration: f64, ) -> CmdResult<()> { - let (Some(buff), Some(spec), strength, duration) = - parse_cmd_args!(args, String, String, f32, f64) - else { - return Err(Content::Plain(action.help_string())); - }; - - let buffkind = parse_buffkind(&buff).ok_or_else(|| { - Content::localized_with_args("command-buff-unknown", [("buff", buff.clone())]) - })?; - - if buffkind.is_simple() { - return Err(Content::localized_with_args("command-buff-simple", [( - "buff", buff, - )])); - } - // explicit match to remember that this function exists let misc_data = match buffkind { BuffKind::Polymorphed => { @@ -4241,9 +4237,6 @@ fn handle_buff_complex( | BuffKind::Heatstroke => unreachable!("is_simple() above"), }; - let strength = strength.unwrap_or(0.01); - let duration = duration.unwrap_or(20.0); - let buffdata = BuffData::new(strength, Some(Secs(duration))).with_misc_data(misc_data); cast_buff(buffkind, buffdata, server, target);