tauri-app: initial tests
Some checks failed
record-daemon / Build, check and test (push) Failing after 53s
Some checks failed
record-daemon / Build, check and test (push) Failing after 53s
This commit is contained in:
@@ -1,14 +1,232 @@
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// A timestamped event in the timeline.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TimestampedEvent {
|
||||
/// Video timestamp (offset from recording start) in seconds.
|
||||
pub video_timestamp: i64,
|
||||
/// Game timestamp (in-game time) in seconds.
|
||||
pub game_timestamp: Option<i64>,
|
||||
/// Real-world timestamp.
|
||||
pub timestamp: DateTime<Utc>,
|
||||
/// Event type name.
|
||||
pub event_type: String,
|
||||
/// Human-readable description.
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
/// A timeline of events for a recording.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Timeline {
|
||||
/// Recording ID.
|
||||
pub recording_id: Uuid,
|
||||
/// Recording start time.
|
||||
pub start_time: DateTime<Utc>,
|
||||
/// Recording end time.
|
||||
pub end_time: Option<DateTime<Utc>>,
|
||||
/// Total duration in seconds.
|
||||
pub duration_secs: i64,
|
||||
/// Events in the timeline.
|
||||
pub events: Vec<TimestampedEvent>,
|
||||
}
|
||||
|
||||
/// Game history item for display in the UI.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct GameHistoryItem {
|
||||
/// Recording ID.
|
||||
pub id: String,
|
||||
/// Game start time.
|
||||
pub start_time: DateTime<Utc>,
|
||||
/// Game end time.
|
||||
pub end_time: Option<DateTime<Utc>>,
|
||||
/// Duration in seconds.
|
||||
pub duration_secs: i64,
|
||||
/// Formatted duration string (e.g., "32:15").
|
||||
pub duration_formatted: String,
|
||||
/// Number of events.
|
||||
pub event_count: usize,
|
||||
/// Champion played (if available).
|
||||
pub champion: Option<String>,
|
||||
/// Game result (if available).
|
||||
pub result: Option<String>,
|
||||
/// Video file path.
|
||||
pub video_path: Option<String>,
|
||||
}
|
||||
|
||||
/// Get the default output directory for recordings.
|
||||
fn get_default_output_dir() -> Option<PathBuf> {
|
||||
dirs::video_dir()
|
||||
.map(|p| p.join("LeagueRecorder"))
|
||||
.or_else(|| dirs::home_dir().map(|p| p.join("Videos").join("LeagueRecorder")))
|
||||
}
|
||||
|
||||
/// Get the timeline storage directory.
|
||||
fn get_timeline_dir() -> PathBuf {
|
||||
get_default_output_dir()
|
||||
.map(|p| p.join("timelines"))
|
||||
.unwrap_or_else(|| PathBuf::from("./recordings/timelines"))
|
||||
}
|
||||
|
||||
/// Load all timelines from disk.
|
||||
fn load_timelines() -> Vec<Timeline> {
|
||||
let timeline_dir = get_timeline_dir();
|
||||
let mut timelines = Vec::new();
|
||||
|
||||
if !timeline_dir.exists() {
|
||||
return timelines;
|
||||
}
|
||||
|
||||
if let Ok(entries) = fs::read_dir(&timeline_dir) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.extension().map(|e| e == "json").unwrap_or(false) {
|
||||
if let Ok(contents) = fs::read_to_string(&path) {
|
||||
if let Ok(timeline) = serde_json::from_str::<Timeline>(&contents) {
|
||||
timelines.push(timeline);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by start time, newest first
|
||||
timelines.sort_by(|a, b| b.start_time.cmp(&a.start_time));
|
||||
timelines
|
||||
}
|
||||
|
||||
/// Format duration as MM:SS or HH:MM:SS.
|
||||
fn format_duration(secs: i64) -> String {
|
||||
let hours = secs / 3600;
|
||||
let minutes = (secs % 3600) / 60;
|
||||
let seconds = secs % 60;
|
||||
|
||||
if hours > 0 {
|
||||
format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
|
||||
} else {
|
||||
format!("{:02}:{:02}", minutes, seconds)
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract game result from events.
|
||||
fn extract_game_result(timeline: &Timeline) -> Option<String> {
|
||||
for event in &timeline.events {
|
||||
if event.event_type == "GameEnd" {
|
||||
// Check description for win/loss
|
||||
if event.description.to_lowercase().contains("win") {
|
||||
return Some("Victory".to_string());
|
||||
} else if event.description.to_lowercase().contains("loss")
|
||||
|| event.description.to_lowercase().contains("defeat")
|
||||
{
|
||||
return Some("Defeat".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Extract champion from events.
|
||||
fn extract_champion(timeline: &Timeline) -> Option<String> {
|
||||
for event in &timeline.events {
|
||||
if event.event_type == "GameStart" {
|
||||
// Try to extract champion from description
|
||||
let desc = &event.description;
|
||||
if desc.contains("Playing as ") {
|
||||
return Some(desc.replace("Playing as ", "").trim().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Extract video file path from timeline.
|
||||
fn extract_video_path(timeline: &Timeline) -> Option<String> {
|
||||
// Look for video file in the recordings directory
|
||||
let output_dir = get_default_output_dir()?;
|
||||
let video_dir = output_dir.join("videos");
|
||||
|
||||
if video_dir.exists() {
|
||||
if let Ok(entries) = fs::read_dir(&video_dir) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
let file_stem = path.file_stem()?.to_string_lossy().to_string();
|
||||
if file_stem.contains(&timeline.recording_id.to_string()) {
|
||||
return Some(path.to_string_lossy().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Also check for files named with the recording ID
|
||||
let recording_id = timeline.recording_id.to_string();
|
||||
if let Ok(entries) = fs::read_dir(&output_dir) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
let file_stem = path.file_stem()?.to_string_lossy().to_string();
|
||||
if file_stem.contains(&recording_id) {
|
||||
return Some(path.to_string_lossy().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||
fn get_game_history() -> Vec<GameHistoryItem> {
|
||||
let timelines = load_timelines();
|
||||
|
||||
timelines
|
||||
.into_iter()
|
||||
.map(|timeline| GameHistoryItem {
|
||||
id: timeline.recording_id.to_string(),
|
||||
start_time: timeline.start_time,
|
||||
end_time: timeline.end_time,
|
||||
duration_secs: timeline.duration_secs,
|
||||
duration_formatted: format_duration(timeline.duration_secs),
|
||||
event_count: timeline.events.len(),
|
||||
champion: extract_champion(&timeline),
|
||||
result: extract_game_result(&timeline),
|
||||
video_path: extract_video_path(&timeline),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_timeline(recording_id: String) -> Option<Timeline> {
|
||||
let timeline_dir = get_timeline_dir();
|
||||
let path = timeline_dir.join(format!("{}.json", recording_id));
|
||||
|
||||
if path.exists() {
|
||||
if let Ok(contents) = fs::read_to_string(&path) {
|
||||
return serde_json::from_str::<Timeline>(&contents).ok();
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_recordings_dir() -> String {
|
||||
get_default_output_dir()
|
||||
.map(|p| p.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|| "./recordings".to_string())
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(tauri::generate_handler![greet])
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
get_game_history,
|
||||
get_timeline,
|
||||
get_recordings_dir
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user