Unify /buff_complex and /buff

Turns out parse_cmd_args allows omitting arguments, /buff <buff>
[misc_data] will be idential to /buff <buff> [strength] [duration]
[misc_data]
This commit is contained in:
juliancoffee 2024-01-08 19:55:10 +02:00
parent 2746a98f40
commit 296f70c1b2
3 changed files with 34 additions and 62 deletions

View File

@ -64,8 +64,7 @@ command-battlemode-available-modes = Available modes: pvp, pve
command-battlemode-same = Attempted to set the same battlemode command-battlemode-same = Attempted to set the same battlemode
command-battlemode-updated = New battlemode: { $battlemode } command-battlemode-updated = New battlemode: { $battlemode }
command-buff-unknown = Unknown buff: { $buff } command-buff-unknown = Unknown buff: { $buff }
command-buff-complex = /buff doesn't work with [{ $buff }], use /buff_complex command-buff-data = Buff argument '{ $buff }' requires additional data
command-buff-simple = /buff_complex doesn't work with [{ $buff }], use /buff
command-buff-body-unknown = Unknown body spec: { $spec } command-buff-body-unknown = Unknown body spec: { $spec }
command-skillpreset-load-error = Error while loading presets command-skillpreset-load-error = Error while loading presets
command-skillpreset-broken = Skill preset is broken command-skillpreset-broken = Skill preset is broken

View File

@ -200,23 +200,14 @@ lazy_static! {
buff_pack buff_pack
}; };
static ref BUFFS_SIMPLE: Vec<String> = { static ref BUFFS: Vec<String> = {
let mut buff_pack: Vec<String> = BUFF_PARSER let mut buff_pack: Vec<String> = BUFF_PARSER.keys().cloned().collect();
.iter()
.filter_map(|(key, kind)| kind.is_simple().then_some(key.clone()))
.collect();
// Add `all` as valid command // Add `all` as valid command
buff_pack.push("all".to_string()); buff_pack.push("all".to_owned());
buff_pack buff_pack
}; };
static ref BUFFS_COMPLEX: Vec<String> =
BUFF_PARSER
.iter()
.filter_map(|(key, kind)| (!kind.is_simple()).then_some(key.clone()))
.collect();
static ref BLOCK_KINDS: Vec<String> = terrain::block::BlockKind::iter() static ref BLOCK_KINDS: Vec<String> = terrain::block::BlockKind::iter()
.map(|bk| bk.to_string()) .map(|bk| bk.to_string())
.collect(); .collect();
@ -322,7 +313,6 @@ pub enum ServerChatCommand {
BattleModeForce, BattleModeForce,
Body, Body,
Buff, Buff,
BuffComplex,
Build, Build,
Campfire, Campfire,
CreateLocation, CreateLocation,
@ -437,23 +427,14 @@ impl ServerChatCommand {
), ),
ServerChatCommand::Buff => cmd( ServerChatCommand::Buff => cmd(
vec![ vec![
Enum("buff", BUFFS_SIMPLE.clone(), Required), Enum("buff", BUFFS.clone(), Required),
Float("strength", 0.01, Optional), Float("strength", 0.01, Optional),
Float("duration", 10.0, Optional), Float("duration", 10.0, Optional),
Any("buff data spec", Optional),
], ],
"Cast a buff on player", "Cast a buff on player",
Some(Admin), 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( ServerChatCommand::Ban => cmd(
vec![ vec![
PlayerName(Required), PlayerName(Required),
@ -955,7 +936,6 @@ impl ServerChatCommand {
ServerChatCommand::BattleModeForce => "battlemode_force", ServerChatCommand::BattleModeForce => "battlemode_force",
ServerChatCommand::Body => "body", ServerChatCommand::Body => "body",
ServerChatCommand::Buff => "buff", ServerChatCommand::Buff => "buff",
ServerChatCommand::BuffComplex => "buff_complex",
ServerChatCommand::Build => "build", ServerChatCommand::Build => "build",
ServerChatCommand::AreaAdd => "area_add", ServerChatCommand::AreaAdd => "area_add",
ServerChatCommand::AreaList => "area_list", ServerChatCommand::AreaList => "area_list",

View File

@ -131,7 +131,6 @@ fn do_command(
ServerChatCommand::BattleModeForce => handle_battlemode_force, ServerChatCommand::BattleModeForce => handle_battlemode_force,
ServerChatCommand::Body => handle_body, ServerChatCommand::Body => handle_body,
ServerChatCommand::Buff => handle_buff, ServerChatCommand::Buff => handle_buff,
ServerChatCommand::BuffComplex => handle_buff_complex,
ServerChatCommand::Build => handle_build, ServerChatCommand::Build => handle_build,
ServerChatCommand::AreaAdd => handle_area_add, ServerChatCommand::AreaAdd => handle_area_add,
ServerChatCommand::AreaList => handle_area_list, ServerChatCommand::AreaList => handle_area_list,
@ -4141,60 +4140,57 @@ fn handle_buff(
args: Vec<String>, args: Vec<String>,
action: &ServerChatCommand, action: &ServerChatCommand,
) -> CmdResult<()> { ) -> 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())); return Err(Content::Plain(action.help_string()));
}; };
let strength = strength.unwrap_or(0.01); 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" { 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 BUFF_PACK
.iter() .iter()
.filter_map(|kind_key| parse_buffkind(kind_key)) .filter_map(|kind_key| parse_buffkind(kind_key))
.filter(|buffkind| buffkind.is_simple()) .filter(|buffkind| buffkind.is_simple())
.for_each(|buffkind| cast_buff(buffkind, buffdata, server, target)); .for_each(|buffkind| cast_buff(buffkind, buffdata, server, target));
Ok(())
} else { } else {
let buffkind = parse_buffkind(&buff).ok_or_else(|| { let buffkind = parse_buffkind(&buff).ok_or_else(|| {
Content::localized_with_args("command-buff-unknown", [("buff", buff.clone())]) Content::localized_with_args("command-buff-unknown", [("buff", buff.clone())])
})?; })?;
if !buffkind.is_simple() { if buffkind.is_simple() {
return Err(Content::localized_with_args("command-buff-complex", [( let duration = duration.unwrap_or(10.0);
"buff", buff, 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, server: &mut Server,
_client: EcsEntity,
target: EcsEntity, target: EcsEntity,
args: Vec<String>, spec: String,
action: &ServerChatCommand, strength: f32,
duration: f64,
) -> CmdResult<()> { ) -> 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 // explicit match to remember that this function exists
let misc_data = match buffkind { let misc_data = match buffkind {
BuffKind::Polymorphed => { BuffKind::Polymorphed => {
@ -4241,9 +4237,6 @@ fn handle_buff_complex(
| BuffKind::Heatstroke => unreachable!("is_simple() above"), | 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); let buffdata = BuffData::new(strength, Some(Secs(duration))).with_misc_data(misc_data);
cast_buff(buffkind, buffdata, server, target); cast_buff(buffkind, buffdata, server, target);