This commit is contained in:
TotallyNot 2025-11-03 16:36:45 +01:00
parent e7d6b74aab
commit 35413b563c
Signed by: pyrite
GPG key ID: 7F1BA9170CD35D15
33 changed files with 10238 additions and 1891 deletions

View file

@ -1,23 +1,24 @@
use std::sync::Mutex;
use bevy_ecs::{prelude::*, system::SystemParam};
use crate::{
entity_registry::EntityRegistry,
hierarchy::Children,
player::stats::{
AdditiveBonus, BaselineStat, CritRate, DamageBonus, Defence, Dexterity, EffectiveStat,
MultiplicativeBonus, SimpleStatBaseline, SimpleStatBonus, SimpleStatEffective,
SimpleStatMarker, Speed, StatMarker, Strength, WeaponAccuracy,
use bevy_ecs::{prelude::*, query::QuerySingleError, system::SystemParam};
use proxisim_models::{
bundle::{
stat::{
AdditiveBonus, BaselineStat, ClipSize, Clips, CritRate, DamageBonus, Defence,
Dexterity, EffectiveStat, MultiplicativeBonus, SimpleStatBaseline, SimpleStatBonus,
SimpleStatEffective, SimpleStatMarker, Speed, StatMarker, Strength, WeaponAccuracy,
},
weapon::WeaponVerb,
},
weapon::WeaponVerb,
Stages,
hierarchy::Children,
};
use crate::{Stages, entity_registry::EntityRegistry};
#[derive(Resource)]
pub struct Logging(pub bool);
#[derive(Event)]
#[derive(Message)]
struct LogEvent(Mutex<Option<DynamicLogMessage>>);
impl From<DynamicLogMessage> for LogEvent {
@ -38,66 +39,71 @@ pub struct WeaponInfo {
}
#[derive(Clone)]
pub enum LogValue<'a> {
pub enum LogValue {
Float(f32),
Unsigned(u32),
Signed(i32),
Bool(bool),
String(String),
Entity(Entity),
OptionNone,
Display(&'a (dyn std::fmt::Display + Send + Sync)),
Debug(&'a (dyn std::fmt::Debug + Send + Sync)),
Player(Entity),
Weapon(Entity),
Array(Vec<LogValue<'a>>),
Map(Vec<(&'static str, LogValue<'a>)>),
Array(Vec<LogValue>),
Map(Vec<(&'static str, LogValue)>),
}
impl<'a> From<String> for LogValue<'a> {
impl From<String> for LogValue {
fn from(value: String) -> Self {
Self::String(value)
}
}
impl<'a> From<&'a str> for LogValue<'static> {
impl<'a> From<&'a str> for LogValue {
fn from(value: &'a str) -> Self {
Self::String(value.to_owned())
}
}
impl<'a> From<f32> for LogValue<'a> {
impl From<f32> for LogValue {
fn from(value: f32) -> Self {
Self::Float(value)
}
}
impl<'a> From<u32> for LogValue<'a> {
impl From<u32> for LogValue {
fn from(value: u32) -> Self {
Self::Unsigned(value)
}
}
impl<'a> From<u16> for LogValue<'a> {
impl From<u16> for LogValue {
fn from(value: u16) -> Self {
Self::Unsigned(value.into())
}
}
impl From<bool> for LogValue<'static> {
impl From<i16> for LogValue {
fn from(value: i16) -> Self {
Self::Signed(value.into())
}
}
impl From<bool> for LogValue {
fn from(value: bool) -> Self {
Self::Bool(value)
}
}
impl From<Entity> for LogValue<'static> {
impl From<Entity> for LogValue {
fn from(value: Entity) -> Self {
Self::Entity(value)
}
}
impl<'a, T> From<Option<T>> for LogValue<'a>
impl<T> From<Option<T>> for LogValue
where
T: Into<LogValue<'a>>,
T: Into<LogValue>,
{
fn from(value: Option<T>) -> Self {
match value {
@ -107,26 +113,38 @@ where
}
}
impl<'a, V> From<Vec<V>> for LogValue<'a>
impl<V> From<Vec<V>> for LogValue
where
V: Into<LogValue<'a>>,
V: Into<LogValue>,
{
fn from(value: Vec<V>) -> Self {
LogValue::Array(value.into_iter().map(Into::into).collect())
}
}
impl<'a, V> From<Vec<(&'static str, V)>> for LogValue<'a>
impl<V> From<Vec<(&'static str, V)>> for LogValue
where
V: Into<LogValue<'a>>,
V: Into<LogValue>,
{
fn from(value: Vec<(&'static str, V)>) -> Self {
LogValue::Map(value.into_iter().map(|(k, v)| (k, v.into())).collect())
}
}
impl<V> From<Result<V, QuerySingleError>> for LogValue
where
V: Into<LogValue>,
{
fn from(value: Result<V, QuerySingleError>) -> Self {
match value {
Ok(value) => value.into(),
Err(_) => LogValue::String("No found".to_owned()),
}
}
}
#[cfg(feature = "json")]
impl<'a> LogValue<'a> {
impl LogValue {
fn to_value(&self, entity_registry: &EntityRegistry) -> serde_json::Value {
match self {
LogValue::OptionNone => serde_json::Value::Null,
@ -136,8 +154,7 @@ impl<'a> LogValue<'a> {
LogValue::String(val) => serde_json::Value::String(val.clone()),
LogValue::Bool(val) => serde_json::Value::Bool(*val),
LogValue::Unsigned(val) => serde_json::Value::Number(serde_json::Number::from(*val)),
LogValue::Debug(boxed) => serde_json::Value::String(format!("{boxed:?}")),
LogValue::Display(boxed) => serde_json::Value::String(format!("{boxed}")),
LogValue::Signed(val) => serde_json::Value::Number(serde_json::Number::from(*val)),
LogValue::Player(id) => {
serde_json::to_value(entity_registry.0.get(id).unwrap()).unwrap()
}
@ -162,7 +179,7 @@ impl<'a> LogValue<'a> {
trait LogMessage: Send + Sync + 'static {
fn tag(&self) -> &'static str;
fn entries(&self) -> Vec<(&'static str, LogValue<'_>)>;
fn entries(&self) -> Vec<(&'static str, LogValue)>;
}
#[derive(Resource, Default)]
@ -212,11 +229,10 @@ impl std::fmt::Display for Log {
LogValue::Float(val) => write!(f, "{val}")?,
LogValue::Bool(val) => write!(f, "{val}")?,
LogValue::Unsigned(val) => write!(f, "{val}")?,
LogValue::Signed(val) => write!(f, "{val}")?,
LogValue::OptionNone => write!(f, "None")?,
LogValue::Display(val) => write!(f, "\"{val}\"")?,
LogValue::Debug(val) => write!(f, "\"{val:?}\"")?,
LogValue::Player(id) | LogValue::Weapon(id) | LogValue::Entity(id) => {
write!(f, "{:?}", id)?
write!(f, "{id}")?
}
LogValue::Array(_) | LogValue::Map(_) => (),
};
@ -421,7 +437,7 @@ impl std::fmt::Display for Log {
#[derive(SystemParam)]
pub struct Logger<'w> {
event_writer: EventWriter<'w, LogEvent>,
event_writer: MessageWriter<'w, LogEvent>,
logging: Res<'w, Logging>,
}
@ -431,7 +447,7 @@ impl<'w> Logger<'w> {
B: FnOnce() -> DynamicLogMessage,
{
if self.logging.0 {
self.event_writer.send(body().into());
self.event_writer.write(body().into());
}
}
}
@ -440,7 +456,7 @@ fn logging_enabled(logging: Res<Logging>) -> bool {
logging.0
}
fn append_log_messages(mut events: EventReader<LogEvent>, mut log: ResMut<Log>) {
fn append_log_messages(mut events: MessageReader<LogEvent>, mut log: ResMut<Log>) {
for event in events.read() {
if let Some(entry) = event.0.lock().unwrap().take() {
log.entries.push(entry);
@ -450,7 +466,7 @@ fn append_log_messages(mut events: EventReader<LogEvent>, mut log: ResMut<Log>)
pub struct DynamicLogMessage {
pub label: &'static str,
pub entries: Vec<(&'static str, LogValue<'static>)>,
pub entries: Vec<(&'static str, LogValue)>,
}
impl LogMessage for DynamicLogMessage {
@ -458,7 +474,7 @@ impl LogMessage for DynamicLogMessage {
self.label
}
fn entries(&self) -> Vec<(&'static str, LogValue<'_>)> {
fn entries(&self) -> Vec<(&'static str, LogValue)> {
self.entries.clone()
}
}
@ -549,8 +565,8 @@ fn log_simple_stat_changes<Stat: SimpleStatMarker>(
bonus_q: Query<&SimpleStatBonus<Stat>>,
mut logger: Logger,
) where
Stat::ValueType: Into<LogValue<'static>>,
Stat::BonusType: Into<LogValue<'static>>,
Stat::ValueType: Into<LogValue>,
Stat::BonusType: Into<LogValue>,
{
for (target, baseline, effective, children) in stat_q.iter() {
let bonuses: Vec<_> = bonus_q
@ -588,6 +604,8 @@ pub(crate) fn configure(stages: &mut Stages) {
log_simple_stat_changes::<CritRate>,
log_simple_stat_changes::<WeaponAccuracy>,
log_simple_stat_changes::<DamageBonus>,
log_simple_stat_changes::<Clips>,
log_simple_stat_changes::<ClipSize>,
)
.run_if(logging_enabled),
);