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-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

View File

@ -200,23 +200,14 @@ lazy_static! {
buff_pack
};
static ref BUFFS_SIMPLE: Vec<String> = {
let mut buff_pack: Vec<String> = BUFF_PARSER
.iter()
.filter_map(|(key, kind)| kind.is_simple().then_some(key.clone()))
.collect();
static ref BUFFS: Vec<String> = {
let mut buff_pack: Vec<String> = 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<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()
.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",

View File

@ -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<String>,
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<String>,
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);