record-daemon: remove data in state machine
record-daemon / Build, check and test (push) Successful in 2m11s
record-daemon / Build, check and test (push) Successful in 2m11s
This commit is contained in:
2 files changed
+101
-168
No files matched your search
@@ -1,4 +1,7 @@
|
||||
//! Daemon state machine implementation.
|
||||
//!
|
||||
//! Only tracks state
|
||||
//! All game data is stored in events and processed at game end.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -6,7 +9,7 @@ use parking_lot::RwLock;
|
||||
use tracing::{info, trace, warn};
|
||||
|
||||
use super::DaemonStatus;
|
||||
use crate::lqp::{GameEndInfo, GameEvent, GameflowPhase, ItemBuild, RunePage, SummonerSpells};
|
||||
use crate::lqp::{GameEvent, GameflowPhase};
|
||||
|
||||
/// Internal daemon state.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -36,36 +39,19 @@ impl From<DaemonState> for DaemonStatus {
|
||||
}
|
||||
|
||||
/// State transition event.
|
||||
///
|
||||
/// Simplified: Only tracks state changes, no data passing.
|
||||
/// Data is stored in events and processed at game end.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum StateTransition {
|
||||
/// League Client started.
|
||||
ClientStarted,
|
||||
/// League Client stopped.
|
||||
ClientStopped,
|
||||
/// Game started.
|
||||
GameStarted {
|
||||
game_id: u64,
|
||||
champion: Option<String>,
|
||||
queue_type: Option<String>,
|
||||
queue_id: Option<u32>,
|
||||
game_mode: Option<String>,
|
||||
map_name: Option<String>,
|
||||
team: Option<u32>,
|
||||
summoner_name: Option<String>,
|
||||
/// Player's PUUID
|
||||
puuid: Option<String>,
|
||||
/// Rune page at game start
|
||||
runes: Option<RunePage>,
|
||||
/// Summoner spells
|
||||
summoner_spells: Option<SummonerSpells>,
|
||||
},
|
||||
/// Game ended.
|
||||
GameEnded {
|
||||
/// Game end info with stats from WebSocket.
|
||||
game_end_info: Option<GameEndInfo>,
|
||||
/// Final items
|
||||
final_items: Option<ItemBuild>,
|
||||
},
|
||||
/// Game started
|
||||
GameStarted,
|
||||
/// Game ended
|
||||
GameEnded,
|
||||
/// Error occurred.
|
||||
Error(String),
|
||||
/// Error recovered.
|
||||
@@ -153,13 +139,11 @@ impl DaemonStateMachine {
|
||||
|
||||
// Update related state
|
||||
match &transition {
|
||||
StateTransition::GameStarted {
|
||||
game_id, champion, ..
|
||||
} => {
|
||||
*self.current_game_id.write() = Some(*game_id);
|
||||
*self.current_champion.write() = champion.clone();
|
||||
StateTransition::GameStarted => {
|
||||
// Game ID and champion are now tracked in events, not state
|
||||
}
|
||||
StateTransition::GameEnded { .. } => {
|
||||
StateTransition::GameEnded => {
|
||||
// Clear state tracking
|
||||
*self.current_game_id.write() = None;
|
||||
*self.current_champion.write() = None;
|
||||
}
|
||||
@@ -189,20 +173,14 @@ impl DaemonStateMachine {
|
||||
|
||||
// From Monitoring
|
||||
(DaemonState::Monitoring, StateTransition::ClientStopped) => Some(DaemonState::Idle),
|
||||
(DaemonState::Monitoring, StateTransition::GameStarted { .. }) => {
|
||||
Some(DaemonState::Recording)
|
||||
}
|
||||
(DaemonState::Monitoring, StateTransition::GameStarted) => Some(DaemonState::Recording),
|
||||
(DaemonState::Monitoring, StateTransition::Error(_)) => Some(DaemonState::Error),
|
||||
(DaemonState::Monitoring, StateTransition::Shutdown) => Some(DaemonState::ShuttingDown),
|
||||
|
||||
// From Recording
|
||||
(DaemonState::Recording, StateTransition::GameEnded { .. }) => {
|
||||
Some(DaemonState::Monitoring)
|
||||
}
|
||||
(DaemonState::Recording, StateTransition::GameEnded) => Some(DaemonState::Monitoring),
|
||||
// Allow GameStarted from Recording (handles case where GameEnded wasn't received)
|
||||
(DaemonState::Recording, StateTransition::GameStarted { .. }) => {
|
||||
Some(DaemonState::Recording)
|
||||
}
|
||||
(DaemonState::Recording, StateTransition::GameStarted) => Some(DaemonState::Recording),
|
||||
(DaemonState::Recording, StateTransition::ClientStopped) => Some(DaemonState::Idle),
|
||||
(DaemonState::Recording, StateTransition::Error(_)) => Some(DaemonState::Error),
|
||||
(DaemonState::Recording, StateTransition::Shutdown) => Some(DaemonState::ShuttingDown),
|
||||
@@ -224,6 +202,9 @@ impl DaemonStateMachine {
|
||||
}
|
||||
|
||||
/// Process a game event and potentially trigger a transition.
|
||||
///
|
||||
/// Only returns state transitions
|
||||
/// All data is stored in events in the timeline and processed at game end.
|
||||
pub fn process_event(&self, event: &GameEvent) -> Option<StateTransition> {
|
||||
trace!(
|
||||
"Processing event in state {:?}: {:?}",
|
||||
@@ -232,33 +213,13 @@ impl DaemonStateMachine {
|
||||
);
|
||||
|
||||
match event {
|
||||
GameEvent::GameStart(info) => {
|
||||
Some(StateTransition::GameStarted {
|
||||
game_id: info.game_id,
|
||||
champion: info.champion.clone(),
|
||||
queue_type: info.queue_type.clone(),
|
||||
queue_id: info.queue_id,
|
||||
game_mode: info.game_mode.clone(),
|
||||
map_name: info.map_name.clone(),
|
||||
team: info.team,
|
||||
summoner_name: info.summoner_name.clone(),
|
||||
puuid: None, // Will be populated from client.fetch_player_game_metadata()
|
||||
runes: None, // Will be populated from client.fetch_player_game_metadata()
|
||||
summoner_spells: None, // Will be populated from client.fetch_player_game_metadata()
|
||||
})
|
||||
}
|
||||
GameEvent::GameEnd(info) => Some(StateTransition::GameEnded {
|
||||
game_end_info: Some(info.clone()),
|
||||
final_items: None, // Will be populated from client.fetch_final_items()
|
||||
}),
|
||||
GameEvent::GameStart(_) => Some(StateTransition::GameStarted),
|
||||
GameEvent::GameEnd(_) => Some(StateTransition::GameEnded),
|
||||
GameEvent::PhaseChange(info) => {
|
||||
// Only trigger GameEnded on EndOfGame phase (stats are available by then)
|
||||
// The actual GameEnd event with stats comes from /lol-end-of-game/v1/eog-stats-block
|
||||
if info.phase == "EndOfGame" && self.is_recording() {
|
||||
Some(StateTransition::GameEnded {
|
||||
game_end_info: None,
|
||||
final_items: None,
|
||||
})
|
||||
Some(StateTransition::GameEnded)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -303,23 +264,9 @@ mod tests {
|
||||
let machine = DaemonStateMachine::new();
|
||||
|
||||
machine.transition(StateTransition::ClientStarted);
|
||||
let new_state = machine.transition(StateTransition::GameStarted {
|
||||
game_id: 12345,
|
||||
champion: Some("Ahri".to_string()),
|
||||
queue_type: None,
|
||||
queue_id: None,
|
||||
game_mode: None,
|
||||
map_name: None,
|
||||
team: None,
|
||||
summoner_name: None,
|
||||
puuid: None,
|
||||
runes: None,
|
||||
summoner_spells: None,
|
||||
});
|
||||
let new_state = machine.transition(StateTransition::GameStarted);
|
||||
|
||||
assert_eq!(new_state, Some(DaemonState::Recording));
|
||||
assert_eq!(machine.current_game_id(), Some(12345));
|
||||
assert_eq!(machine.current_champion(), Some("Ahri".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -327,19 +274,7 @@ mod tests {
|
||||
let machine = DaemonStateMachine::new();
|
||||
|
||||
// Can't start recording from Idle
|
||||
let result = machine.transition(StateTransition::GameStarted {
|
||||
game_id: 12345,
|
||||
champion: None,
|
||||
queue_type: None,
|
||||
queue_id: None,
|
||||
game_mode: None,
|
||||
map_name: None,
|
||||
team: None,
|
||||
summoner_name: None,
|
||||
puuid: None,
|
||||
runes: None,
|
||||
summoner_spells: None,
|
||||
});
|
||||
let result = machine.transition(StateTransition::GameStarted);
|
||||
|
||||
assert_eq!(result, None);
|
||||
assert_eq!(machine.current_state(), DaemonState::Idle);
|
||||
|
||||
Reference in new issue
Block a user