blastmud/blastmud_game/src/services.rs

43 lines
2.0 KiB
Rust
Raw Normal View History

use crate::{
2023-01-15 23:16:02 +11:00
models::item::{Item, SkillType},
db::DBTrans,
DResult
};
2023-01-15 23:16:02 +11:00
use rand::{self, Rng};
pub async fn broadcast_to_room(trans: &DBTrans, location: &str, from_item: Option<&Item>,
message_explicit_ok: &str, message_nonexplicit: Option<&str>) -> DResult<()> {
for item in trans.find_items_by_location(location).await? {
if item.item_type != "player" {
continue;
}
if let Some((session, session_dat)) = trans.find_session_for_player(&item.item_code).await? {
if session_dat.less_explicit_mode && Some(&item.item_code) != from_item.map(|i| &i.item_code) {
if let Some(msg) = message_nonexplicit {
trans.queue_for_session(&session, Some(msg)).await?;
}
return Ok(());
}
trans.queue_for_session(&session, Some(message_explicit_ok)).await?;
}
}
Ok(())
}
2023-01-15 23:16:02 +11:00
// Rolls the die to determine if a player pulls off something that requires a skill.
// It is a number between -1 and 1.
// Non-negative numbers mean they pulled it off, positive mean they didn't, with
// more positive numbers meaning they did a better job, and more negative numbers
// meaning it went really badly.
// If level = raw skill, there is a 50% chance of succeeding.
// level = raw skill + 1, there is a 75% chance of succeeding.
// level = raw skill - 1, there is a 25% chance of succeeding.
// Past those differences, it follows the logistic function:
// Difference: -5 -4 -3 -2 -1 0 1 2 3 4 5
// Probability: 0.4% 1.2% 3.5% 10% 25% 50% 75% 90% 96% 99% 99.6%
pub fn skill_check(who: &Item, skill: &SkillType, level: i64) -> f64 {
let user_level = who.total_skills.get(skill).unwrap_or(&0);
let level_gap = level - user_level.clone() as i64;
const K: f64 = 1.0986122886681098; // log 3
rand::thread_rng().gen::<f64>() - 1.0 / (1.0 + (-K * (level_gap as f64)).exp())
}