Skip to content

Commit

Permalink
修复初次加载配置不生效的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
eigeen committed Mar 21, 2024
1 parent 18790bd commit 4b85f43
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 29 deletions.
3 changes: 0 additions & 3 deletions src/conditions/insect_glaive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ impl InsectGlaiveCondition {
if let TriggerCondition::InsectGlaiveLight { red, white, yellow } = cond {
Box::new(move |event| {
if let Event::InsectGlaive { ctx } = event {
if ctx.weapon_type != 10 {
return false;
}
compare_cfg_ctx(
&red,
ctx.insect_glaive.attack_timer,
Expand Down
5 changes: 1 addition & 4 deletions src/conditions/longsword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ impl LongswordCondition {
if let Event::LongswordLevelChanged {
new: new_event,
old: old_event,
ctx,
ctx: _,
} = event
{
if ctx.weapon_type != 3 {
return false;
}
if let Some(new) = &new {
if new != new_event {
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/conditions/weapon_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl WeaponTypeCondition {
let trigger_fn: Box<dyn Fn(&Event) -> bool + Send> = if let TriggerCondition::WeaponType { value } = cond {
Box::new(move |event| {
if let Event::WeaponTypeChanged { new, .. } = event {
&value == new
&value == &new.as_i32()
} else {
false
}
Expand All @@ -36,7 +36,7 @@ impl WeaponTypeCondition {
pub fn new_check(cond: &CheckCondition) -> Self {
let cond = cond.clone();
let check_fn: Box<dyn Fn(&Context) -> bool + Send> = if let CheckCondition::WeaponType { value } = cond {
Box::new(move |ctx| value == ctx.weapon_type)
Box::new(move |ctx| value == ctx.weapon_type.as_i32())
} else {
error!("internal: WeaponTypeCondition cmp_fn 参数不正确");
Box::new(|_| false)
Expand Down
8 changes: 8 additions & 0 deletions src/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ where

#[cfg(test)]
mod tests {
use mhw_toolkit::game_util::WeaponType;

use super::*;

const FILE_PATH_1: &str = "mas-config.toml";
Expand Down Expand Up @@ -280,4 +282,10 @@ mod tests {
let cfg = load_config(FILE_PATH_1).unwrap();
eprintln!("{:?}", cfg);
}

#[test]
fn test_valuecmp_weapon() {
let longsword = WeaponType::LongSword;
assert!(ValueCmp::EqInt(3) == longsword.as_i32());
}
}
12 changes: 8 additions & 4 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use log::{debug, info};
use mhw_toolkit::{game_util, util};
use mhw_toolkit::{
game_util::{self, WeaponType},
util,
};
use once_cell::sync::Lazy;

use crate::game_context::{ChargeBlade, ChatCommand, Fsm, InsectGlaive, SpecializedTool};
Expand Down Expand Up @@ -53,11 +56,12 @@ pub fn get_longsword_level() -> i32 {
util::get_value_with_offset(WEAPON_DATA_BASE, LONGSWORD_OFFSETS).unwrap_or(99)
}

pub fn get_weapon_type() -> i32 {
match util::get_value_with_offset(WEAPON_DATA_BASE, WEAPON_OFFSETS) {
pub fn get_weapon_type() -> WeaponType {
let weapon_type_id = match util::get_value_with_offset(WEAPON_DATA_BASE, WEAPON_OFFSETS) {
Some(w) => w,
None => 0,
}
};
WeaponType::from_i32(weapon_type_id)
}

pub fn get_fsm() -> Fsm {
Expand Down
2 changes: 1 addition & 1 deletion src/game_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Context {
pub chat_command: Option<ChatCommand>,
pub quest_state: i32,
pub longsword_level: i32,
pub weapon_type: i32,
pub weapon_type: WeaponType,
pub fsm: Fsm,
pub use_item_id: i32,
pub insect_glaive: InsectGlaive,
Expand Down
6 changes: 3 additions & 3 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ pub async fn event_listener(tx: Sender<Event>) {
}));
}
if ctx.weapon_type != last_ctx.weapon_type {
debug!("on weapon_type changed");
debug!("on weapon_type changed from {:?} to {:?}", last_ctx.weapon_type, ctx.weapon_type);
tx_send_or_break!(tx.send(Event::WeaponTypeChanged {
new: ctx.weapon_type,
old: last_ctx.weapon_type,
ctx: ctx.clone()
}));
}
if ctx.fsm != last_ctx.fsm {
debug!("on fsm_id changed from {:?} to {:?}", ctx.fsm, last_ctx.fsm);
debug!("on fsm_id changed from {:?} to {:?}", last_ctx.fsm, ctx.fsm);
tx_send_or_break!(tx.send(Event::FsmChanged {
new: ctx.fsm,
old: last_ctx.fsm,
Expand All @@ -73,7 +73,7 @@ pub async fn event_listener(tx: Sender<Event>) {
}
if WeaponType::LongSword == ctx.weapon_type {
if ctx.longsword_level != last_ctx.longsword_level {
debug!("on longsword_level changed");
debug!("on longsword_level changed from {} to {}", last_ctx.longsword_level, ctx.longsword_level);
tx_send_or_break!(tx.send(Event::LongswordLevelChanged {
new: ctx.longsword_level,
old: last_ctx.longsword_level,
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ fn main_entry() -> Result<(), String> {
// 首次自动加载配置文件
match handlers::load_triggers() {
Ok(trigger_mgr) => {
let _ = tx.send(triggers::Event::LoadTriggers { trigger_mgr });
if let Err(e) = tx.send(triggers::Event::LoadTriggers { trigger_mgr }).await {
error!("加载配置失败:{}", e);
};
}
Err(e) => {
error!("加载配置失败:{}", e);
Expand Down
47 changes: 36 additions & 11 deletions src/triggers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::atomic::{AtomicI32, Ordering};

use log::error;
use mhw_toolkit::game_util;
use log::{debug, error};
use mhw_toolkit::game_util::{self, WeaponType};
use once_cell::sync::Lazy;
use rand::Rng;

Expand All @@ -19,14 +19,39 @@ static CHAT_MESSAGE_SENDER: Lazy<game_util::ChatMessageSender> = Lazy::new(|| ga

#[derive(Debug)]
pub enum Event {
LoadTriggers { trigger_mgr: TriggerManager },
LongswordLevelChanged { new: i32, old: i32, ctx: Context },
WeaponTypeChanged { new: i32, old: i32, ctx: Context },
QuestStateChanged { new: i32, old: i32, ctx: Context },
FsmChanged { new: Fsm, old: Fsm, ctx: Context },
UseItem { item_id: i32, ctx: Context },
InsectGlaive { ctx: Context },
ChargeBlade { ctx: Context },
LoadTriggers {
trigger_mgr: TriggerManager,
},
LongswordLevelChanged {
new: i32,
old: i32,
ctx: Context,
},
WeaponTypeChanged {
new: WeaponType,
old: WeaponType,
ctx: Context,
},
QuestStateChanged {
new: i32,
old: i32,
ctx: Context,
},
FsmChanged {
new: Fsm,
old: Fsm,
ctx: Context,
},
UseItem {
item_id: i32,
ctx: Context,
},
InsectGlaive {
ctx: Context,
},
ChargeBlade {
ctx: Context,
},
}

impl Event {
Expand Down Expand Up @@ -176,7 +201,7 @@ pub fn parse_trigger(t_cfg: &configs::Trigger) -> Trigger {
t_cfg.check.iter().map(parse_check_condition).for_each(|c| t.add_check_condition(c));
t_cfg.action.iter().filter_map(parse_event).for_each(|e| t.add_action(e));

log::debug!("注册trigger check({}), action({})", t.check_conditions.len(), t.actions.len());
debug!("注册trigger check({}), action({})", t.check_conditions.len(), t.actions.len());

t
}
Expand Down

0 comments on commit 4b85f43

Please sign in to comment.