This commit is contained in:
1 parent
ff713da1e8
commit
fcfa55d0aa
13 files changed
+848
-2043
No files matched your search
@@ -3,8 +3,6 @@
|
||||
use chrono::{DateTime, Duration, Utc};
|
||||
use tracing::debug;
|
||||
|
||||
use crate::lqp::GameEvent;
|
||||
|
||||
/// Event mapper that tracks recording start time and maps events to video timestamps.
|
||||
pub struct EventMapper {
|
||||
/// Recording start time.
|
||||
@@ -45,8 +43,8 @@ impl EventMapper {
|
||||
self.start_time.is_some()
|
||||
}
|
||||
|
||||
/// Map a game event to video and game timestamps.
|
||||
pub fn map_event(&self, _event: &GameEvent) -> Option<(Duration, Option<Duration>)> {
|
||||
/// Map an event to video and game timestamps.
|
||||
pub fn map_event(&self) -> Option<(Duration, Option<Duration>)> {
|
||||
let start_time = self.start_time?;
|
||||
let now = Utc::now();
|
||||
|
||||
@@ -60,25 +58,23 @@ impl EventMapper {
|
||||
self.synchronizer.adjust_game_timestamp(raw_ts)
|
||||
});
|
||||
|
||||
// Update game start time if this is a game start event
|
||||
// (handled separately in handle_event)
|
||||
|
||||
Some((video_timestamp, game_timestamp))
|
||||
}
|
||||
|
||||
/// Handle a game event and return mapped timestamps.
|
||||
pub fn handle_event(&mut self, event: &GameEvent) -> Option<(Duration, Option<Duration>)> {
|
||||
/// event_type is used to detect game start for timestamp tracking.
|
||||
pub fn handle_event(&mut self, event_type: &str) -> Option<(Duration, Option<Duration>)> {
|
||||
if !self.is_active() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Track game start time
|
||||
if let GameEvent::GameStart(_) = event {
|
||||
if event_type == crate::lqp::EVENT_TYPE_GAME_START {
|
||||
self.game_start_time = Some(Utc::now());
|
||||
debug!("Game start time recorded: {:?}", self.game_start_time);
|
||||
}
|
||||
|
||||
let result = self.map_event(event);
|
||||
let result = self.map_event();
|
||||
|
||||
// Add sync point if we have both timestamps
|
||||
if let Some((video_ts, Some(game_ts))) = result {
|
||||
|
||||
@@ -10,8 +10,6 @@ use chrono::{DateTime, Duration, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::lqp::GameEvent;
|
||||
|
||||
/// A timeline of events for a recording.
|
||||
/// Stores raw API responses for maximum flexibility and future-proofing.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -79,31 +77,11 @@ impl Timeline {
|
||||
}
|
||||
}
|
||||
|
||||
/// Add an event to the timeline.
|
||||
/// Add an event to the timeline with raw data.
|
||||
pub fn add_event(
|
||||
&mut self,
|
||||
event: GameEvent,
|
||||
video_timestamp: Duration,
|
||||
game_timestamp: Option<Duration>,
|
||||
) {
|
||||
let timestamped = TimestampedEvent {
|
||||
video_timestamp,
|
||||
game_timestamp,
|
||||
timestamp: Utc::now(),
|
||||
event_type: event_type_name(&event),
|
||||
description: event.description(),
|
||||
event,
|
||||
raw_data: None,
|
||||
uri: None,
|
||||
};
|
||||
|
||||
self.events.push(timestamped);
|
||||
}
|
||||
|
||||
/// Add an event to the timeline with raw data.
|
||||
pub fn add_event_with_raw(
|
||||
&mut self,
|
||||
event: GameEvent,
|
||||
event_type: &str,
|
||||
description: &str,
|
||||
video_timestamp: Duration,
|
||||
game_timestamp: Option<Duration>,
|
||||
raw_data: serde_json::Value,
|
||||
@@ -113,11 +91,10 @@ impl Timeline {
|
||||
video_timestamp,
|
||||
game_timestamp,
|
||||
timestamp: Utc::now(),
|
||||
event_type: event_type_name(&event),
|
||||
description: event.description(),
|
||||
event,
|
||||
raw_data: Some(raw_data),
|
||||
uri: Some(uri),
|
||||
event_type: event_type.to_string(),
|
||||
description: description.to_string(),
|
||||
raw_data,
|
||||
uri,
|
||||
};
|
||||
|
||||
self.events.push(timestamped);
|
||||
@@ -192,29 +169,9 @@ fn format_timestamp(duration: Duration) -> String {
|
||||
format!("{:02}:{:02}:{:02}.{:03}", hours, minutes, seconds, millis)
|
||||
}
|
||||
|
||||
/// Get the event type name.
|
||||
fn event_type_name(event: &GameEvent) -> String {
|
||||
match event {
|
||||
GameEvent::MatchFound(_) => "match_found",
|
||||
GameEvent::ChampSelectStart(_) => "champ_select_start",
|
||||
GameEvent::ChampionPick(_) => "champion_pick",
|
||||
GameEvent::GameStart(_) => "game_start",
|
||||
GameEvent::Kill(_) => "kill",
|
||||
GameEvent::Death(_) => "death",
|
||||
GameEvent::Objective(_) => "objective",
|
||||
GameEvent::StatsUpdate(_) => "stats_update",
|
||||
GameEvent::GameEnd(_) => "game_end",
|
||||
GameEvent::PhaseChange(_) => "phase_change",
|
||||
GameEvent::LpChange(_) => "lp_change",
|
||||
GameEvent::Unknown => "unknown",
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::lqp::KillEvent;
|
||||
|
||||
#[test]
|
||||
fn test_timeline_creation() {
|
||||
@@ -230,21 +187,22 @@ mod tests {
|
||||
let id = Uuid::new_v4();
|
||||
let mut timeline = Timeline::new(id);
|
||||
|
||||
let event = GameEvent::Kill(KillEvent {
|
||||
killer: "Player1".to_string(),
|
||||
killer_champion: Some("Ahri".to_string()),
|
||||
victim: "Player2".to_string(),
|
||||
victim_champion: Some("Lux".to_string()),
|
||||
solo_kill: true,
|
||||
assists: 0,
|
||||
position: None,
|
||||
game_time: Some(120.0),
|
||||
timestamp: Utc::now(),
|
||||
let raw_data = serde_json::json!({
|
||||
"killer": "Player1",
|
||||
"victim": "Player2"
|
||||
});
|
||||
|
||||
timeline.add_event(event, Duration::seconds(5), Some(Duration::seconds(120)));
|
||||
timeline.add_event(
|
||||
"kill",
|
||||
"Player1 killed Player2",
|
||||
Duration::seconds(5),
|
||||
Some(Duration::seconds(120)),
|
||||
raw_data,
|
||||
"/lol-game-events/v1/events".to_string(),
|
||||
);
|
||||
|
||||
assert_eq!(timeline.event_count(), 1);
|
||||
assert_eq!(timeline.events[0].event_type, "kill");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -9,32 +9,30 @@ use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::{Result, TimelineError};
|
||||
use crate::lqp::GameEvent;
|
||||
use crate::recording::RecordingResult;
|
||||
|
||||
/// A timestamped event in the timeline.
|
||||
/// Stores raw API data only — no typed event parsing.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TimestampedEvent {
|
||||
/// Video timestamp (offset from recording start).
|
||||
// #[serde(with = "chrono::serde::seconds")]
|
||||
pub video_timestamp: Duration,
|
||||
/// Game timestamp (in-game time).
|
||||
// #[serde(with = "chrono::serde::seconds_option")]
|
||||
pub game_timestamp: Option<Duration>,
|
||||
/// Real-world timestamp.
|
||||
pub timestamp: DateTime<Utc>,
|
||||
/// Event type name (derived from URI).
|
||||
/// Event type string derived from URI (e.g. "game_start", "lp_change", "kill").
|
||||
pub event_type: String,
|
||||
/// Human-readable description.
|
||||
pub description: String,
|
||||
/// The actual event data.
|
||||
pub event: GameEvent,
|
||||
/// Raw JSON data from the API (for flexibility).
|
||||
/// Human-readable description for logging.
|
||||
#[serde(default)]
|
||||
pub raw_data: Option<serde_json::Value>,
|
||||
pub description: String,
|
||||
/// The raw JSON data from the API — the single source of truth.
|
||||
/// The tauri-app parses the values it needs from this data.
|
||||
#[serde(default)]
|
||||
pub raw_data: serde_json::Value,
|
||||
/// URI of the endpoint that triggered this event.
|
||||
#[serde(default)]
|
||||
pub uri: Option<String>,
|
||||
pub uri: String,
|
||||
}
|
||||
|
||||
/// Metadata for a recording.
|
||||
|
||||
Reference in new issue
Block a user