use super::types::SubjectId;
use crate::event::Event;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReputationChangeRequested {
pub subject_id: SubjectId,
pub delta: f32,
pub category: Option<String>,
pub reason: Option<String>,
}
impl Event for ReputationChangeRequested {}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReputationSetRequested {
pub subject_id: SubjectId,
pub score: f32,
pub category: Option<String>,
}
impl Event for ReputationSetRequested {}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReputationChangedEvent {
pub subject_id: SubjectId,
pub old_score: f32,
pub new_score: f32,
pub delta: f32,
pub category: Option<String>,
pub reason: Option<String>,
}
impl Event for ReputationChangedEvent {}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReputationThresholdCrossedEvent {
pub subject_id: SubjectId,
pub old_threshold: Option<String>,
pub new_threshold: String,
pub score: f32,
pub category: Option<String>,
}
impl Event for ReputationThresholdCrossedEvent {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_reputation_change_requested() {
let event = ReputationChangeRequested {
subject_id: SubjectId::new("player", "kingdom"),
delta: 15.0,
category: None,
reason: Some("Completed quest".into()),
};
assert_eq!(event.subject_id.observer, "player");
assert_eq!(event.subject_id.target, "kingdom");
assert_eq!(event.delta, 15.0);
assert_eq!(event.reason, Some("Completed quest".into()));
}
#[test]
fn test_reputation_set_requested() {
let event = ReputationSetRequested {
subject_id: SubjectId::new("player", "npc_alice"),
score: 75.0,
category: Some("romance".into()),
};
assert_eq!(event.score, 75.0);
assert_eq!(event.category, Some("romance".into()));
}
#[test]
fn test_reputation_changed_event() {
let event = ReputationChangedEvent {
subject_id: SubjectId::new("player", "kingdom"),
old_score: 50.0,
new_score: 65.0,
delta: 15.0,
category: None,
reason: Some("Completed quest".into()),
};
assert_eq!(event.old_score, 50.0);
assert_eq!(event.new_score, 65.0);
assert_eq!(event.delta, 15.0);
}
#[test]
fn test_threshold_crossed_event() {
let event = ReputationThresholdCrossedEvent {
subject_id: SubjectId::new("player", "kingdom"),
old_threshold: Some("Neutral".into()),
new_threshold: "Friendly".into(),
score: 75.0,
category: None,
};
assert_eq!(event.old_threshold, Some("Neutral".into()));
assert_eq!(event.new_threshold, "Friendly");
assert_eq!(event.score, 75.0);
}
#[test]
fn test_serialization() {
let event = ReputationChangeRequested {
subject_id: SubjectId::new("player", "kingdom"),
delta: 10.0,
category: None,
reason: None,
};
let json = serde_json::to_string(&event).unwrap();
let deserialized: ReputationChangeRequested = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.subject_id.observer, "player");
assert_eq!(deserialized.delta, 10.0);
}
}