From 062c290e49f3a6d27b7789df4480637d9fbd7c1a Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 4 May 2021 09:22:10 -0400 Subject: [PATCH] Made buff commands exhaustive --- Cargo.lock | 2 ++ assets/voxygen/i18n/en/hud/chat.ron | 1 + common/Cargo.toml | 6 +++- common/src/cmd.rs | 56 ++++++++++++++++++++--------- common/src/comp/buff.rs | 5 ++- server/src/cmd.rs | 43 +++------------------- voxygen/src/hud/chat.rs | 4 ++- 7 files changed, 60 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5600abfcae..4c86d1e3ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5542,6 +5542,8 @@ dependencies = [ "specs-idvs", "spin_sleep", "structopt", + "strum", + "strum_macros", "tracing", "tracing-subscriber", "uuid", diff --git a/assets/voxygen/i18n/en/hud/chat.ron b/assets/voxygen/i18n/en/hud/chat.ron index 1e46dea342..394db4c2dc 100644 --- a/assets/voxygen/i18n/en/hud/chat.ron +++ b/assets/voxygen/i18n/en/hud/chat.ron @@ -7,6 +7,7 @@ "hud.outcome.burning": "died of: burning", "hud.outcome.curse": "died of: curse", "hud.outcome.bleeding": "died of: bleeding", + "hud.outcome.crippled": "died of: crippled", // Chat outputs "hud.chat.online_msg": "[{name}] is online now", diff --git a/common/Cargo.toml b/common/Cargo.toml index 533a966a6f..2d8f2ed24e 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -49,7 +49,7 @@ ron = { version = "0.6", default-features = false } serde_json = "1.0.50" serde_repr = "0.1.6" -# esv export +# csv export csv = { version = "1.1.3", optional = true } structopt = { version = "0.3.13", optional = true } @@ -59,6 +59,10 @@ slotmap = { version = "1.0", features = ["serde"] } indexmap = "1.3.0" slab = "0.4.2" +# Strum +strum = "0.20" +strum_macros = "0.20" + # ECS specs = { git = "https://github.com/amethyst/specs.git", features = ["serde", "storage-event-control", "nightly"], rev = "5a9b71035007be0e3574f35184acac1cd4530496" } specs-idvs = { git = "https://gitlab.com/veloren/specs-idvs.git", rev = "b65fb220e94f5d3c9bc30074a076149763795556" } diff --git a/common/src/cmd.rs b/common/src/cmd.rs index 008bf3f89d..911f30d32c 100644 --- a/common/src/cmd.rs +++ b/common/src/cmd.rs @@ -1,4 +1,8 @@ -use crate::{assets, comp, npc, terrain}; +use crate::{ + assets, + comp::{self, buff::BuffKind}, + npc, terrain, +}; use assets::AssetExt; use hashbrown::HashMap; use lazy_static::lazy_static; @@ -8,6 +12,7 @@ use std::{ path::Path, str::FromStr, }; +use strum::IntoEnumIterator; use tracing::warn; /// Struct representing a command that a user can run from server chat. @@ -210,21 +215,40 @@ lazy_static! { .map(|s| s.to_string()) .collect(); - static ref BUFFS: Vec = vec![ - // Debuffs - "burning", "bleeding", "curse", - // Heal - "regeneration", "saturation", "potion", "campfire_heal", - // Outmaxing stats - "increase_max_energy", "increase_max_health", - // Defensive buffs - "invulnerability", "protecting_ward", - // One command to rule them all - "all", - ] - .iter() - .map(|b| b.to_string()) - .collect(); + pub static ref BUFF_PARSER: HashMap = { + let string_from_buff = |kind| match kind { + BuffKind::Burning => "burning", + BuffKind::Regeneration => "regeration", + BuffKind::Saturation => "saturation", + BuffKind::Bleeding => "bleeding", + BuffKind::Cursed => "cursed", + BuffKind::Potion => "potion", + BuffKind::CampfireHeal => "campfire_heal", + BuffKind::IncreaseMaxEnergy => "increase_max_energy", + BuffKind::IncreaseMaxHealth => "increase_max_health", + BuffKind::Invulnerability => "invulnerability", + BuffKind::ProtectingWard => "protecting_ward", + BuffKind::Frenzied => "frenzied", + BuffKind::Crippled => "crippled", + }; + let mut buff_parser = HashMap::new(); + BuffKind::iter().for_each(|kind| {buff_parser.insert(string_from_buff(kind).to_string(), kind);}); + buff_parser + }; + + pub static ref BUFF_PACK: Vec = { + let mut buff_pack: Vec<_> = BUFF_PARSER.keys().cloned().collect(); + // Remove invulnerability as it removes debuffs + buff_pack.retain(|kind| kind != "invulnerability"); + buff_pack + }; + + 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 + }; static ref BLOCK_KINDS: Vec = terrain::block::BLOCK_KINDS .keys() diff --git a/common/src/comp/buff.rs b/common/src/comp/buff.rs index 2e938937ed..b7bbe5bacb 100644 --- a/common/src/comp/buff.rs +++ b/common/src/comp/buff.rs @@ -9,10 +9,13 @@ use specs::{Component, DerefFlaggedStorage}; use specs_idvs::IdvStorage; #[cfg(not(target_arch = "wasm32"))] use std::{cmp::Ordering, time::Duration}; +use strum_macros::EnumIter; /// De/buff Kind. /// This is used to determine what effects a buff will have -#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize, PartialOrd, Ord)] +#[derive( + Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize, PartialOrd, Ord, EnumIter, +)] pub enum BuffKind { // Buffs /// Restores health/time for some period diff --git a/server/src/cmd.rs b/server/src/cmd.rs index ec604c7ba8..bf1ecafd1a 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -11,7 +11,7 @@ use authc::Uuid; use chrono::{NaiveTime, Timelike}; use common::{ assets, - cmd::{ChatCommand, CHAT_COMMANDS, CHAT_SHORTCUTS}, + cmd::{ChatCommand, BUFF_PACK, BUFF_PARSER, CHAT_COMMANDS, CHAT_SHORTCUTS}, comp::{ self, aura::{Aura, AuraKind, AuraTarget}, @@ -36,13 +36,11 @@ use common_net::{ }; use common_state::{BuildAreaError, BuildAreas}; use core::{convert::TryFrom, ops::Not, time::Duration}; -use hashbrown::HashSet; +use hashbrown::{HashMap, HashSet}; use rand::Rng; use specs::{Builder, Entity as EcsEntity, Join, WorldExt}; -use wiring::{Circuit, Wire, WiringAction, WiringActionEffect, WiringElement}; - -use hashbrown::HashMap; use vek::*; +use wiring::{Circuit, Wire, WiringAction, WiringActionEffect, WiringElement}; use world::util::Sampler; use crate::{client::Client, login_provider::LoginProvider, wiring}; @@ -2615,22 +2613,6 @@ fn handle_apply_buff( args: String, action: &ChatCommand, ) -> CmdResult<()> { - const BUFF_PACK: &[&str] = &[ - // Debuffs - "burning", - "bleeding", - "curse", - // Healing - "regeneration", - "saturation", - "potion", - "campfire_heal", - // Outmaxing stats - "increase_max_energy", - "increase_max_health", - // Defensive buffs (invulnerability is skipped because it ruins all debuffs) - "protecting_ward", - ]; if let (Some(buff), strength, duration) = scan_fmt_some!(&args, &action.arg_fmt(), String, f32, f64) { @@ -2640,7 +2622,7 @@ fn handle_apply_buff( if buff != "all" { cast_buff(&buff, buffdata, server, target) } else { - for kind in BUFF_PACK { + for kind in BUFF_PACK.iter() { cast_buff(kind, buffdata, server, target)?; } Ok(()) @@ -2663,19 +2645,4 @@ fn cast_buff(kind: &str, data: BuffData, server: &mut Server, target: EcsEntity) } } -fn parse_buffkind(buff: &str) -> Option { - match buff { - "burning" => Some(BuffKind::Burning), - "regeneration" => Some(BuffKind::Regeneration), - "saturation" => Some(BuffKind::Saturation), - "bleeding" => Some(BuffKind::Bleeding), - "curse" => Some(BuffKind::Cursed), - "potion" => Some(BuffKind::Potion), - "campfire_heal" => Some(BuffKind::CampfireHeal), - "increase_max_energy" => Some(BuffKind::IncreaseMaxEnergy), - "increase_max_health" => Some(BuffKind::IncreaseMaxHealth), - "invulnerability" => Some(BuffKind::Invulnerability), - "protecting_ward" => Some(BuffKind::ProtectingWard), - _ => None, - } -} +fn parse_buffkind(buff: &str) -> Option { BUFF_PARSER.get(buff).copied() } diff --git a/voxygen/src/hud/chat.rs b/voxygen/src/hud/chat.rs index f91a5a0182..0795e3c930 100644 --- a/voxygen/src/hud/chat.rs +++ b/voxygen/src/hud/chat.rs @@ -628,6 +628,7 @@ fn insert_killing_buff(buff: BuffKind, localized_strings: &Localization, templat BuffKind::Burning => localized_strings.get("hud.outcome.burning"), BuffKind::Bleeding => localized_strings.get("hud.outcome.bleeding"), BuffKind::Cursed => localized_strings.get("hud.outcome.curse"), + BuffKind::Crippled => localized_strings.get("hud.outcome.crippled"), BuffKind::Regeneration | BuffKind::Saturation | BuffKind::Potion @@ -635,7 +636,8 @@ fn insert_killing_buff(buff: BuffKind, localized_strings: &Localization, templat | BuffKind::IncreaseMaxEnergy | BuffKind::IncreaseMaxHealth | BuffKind::Invulnerability - | BuffKind::ProtectingWard => { + | BuffKind::ProtectingWard + | BuffKind::Frenzied => { tracing::error!("Player was killed by a positive buff!"); localized_strings.get("hud.outcome.mysterious") },