The daemon's protocol says whether it is recording and which game it is on, and nothing else, so the app could not say what was being recorded or stop it. A Recording screen now opens by itself when a recording starts, and at launch if one is already running, and a button in the title bar brings it back from any other screen. It shows the champion, the queue and the summoner, when the game began, two clocks, the live scoreline and both teams, and the clip being written as it grows on disk. The game's own data is read once a second on a thread of its own, from the endpoint the daemon polls for its events, which needs no login. Without an answer the screen keeps what it has and says it is waiting. Stop recording and Stop and delete end the game. A stop over the protocol is not the stop the recorder gives a game that ends by itself. The video lands in a new timeline while the open one is left behind, which showed in the match history as a game that never happened, next to one with no champion. Stopping from the app closes the open timeline into the one holding the video, and deleting removes the clip and both. The history skips timelines that have no end meanwhile, so a recording in progress is not listed as a game.
1064 lines
36 KiB
Rust
1064 lines
36 KiB
Rust
//! Recording: the game the daemon is recording, and the way to stop it.
|
|
//!
|
|
//! The daemon's protocol says whether it is recording and which game it is on, and nothing
|
|
//! about the game itself. The screen reads the rest where it is: the game's own live data,
|
|
//! at the loopback endpoint the daemon polls for its events, and the timeline the recorder
|
|
//! opened for this game, which holds the champion, the queue and the summoner it fetched at
|
|
//! the start.
|
|
|
|
use std::io::Read;
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::mpsc::{Receiver, TryRecvError, channel};
|
|
use std::time::Duration;
|
|
|
|
use chrono::{DateTime, Utc};
|
|
use rune::tok::{TextStyle, radius, space};
|
|
use rune::{El, col, el, row, text};
|
|
use serde_json::Value;
|
|
use ureq::tls::TlsConfig;
|
|
|
|
use crate::app::{App, Screen};
|
|
use crate::format;
|
|
use crate::history::{self, Timeline};
|
|
use crate::settings::Settings;
|
|
use crate::ui::{self, Paint, body, heading};
|
|
|
|
/// The game's own data for the game it is running.
|
|
///
|
|
/// Served by the game client on the loopback address, with a certificate it made for itself:
|
|
/// there is no name to verify and nothing on the network in between, so verification is off.
|
|
/// The daemon polls the events of the same server, and this is what it does not pass on.
|
|
const LIVE_URL: &str = "https://127.0.0.1:2999/liveclientdata/allgamedata";
|
|
|
|
/// How often the screen asks the game and the disk, in seconds.
|
|
const REFRESH: f64 = 1.0;
|
|
|
|
/// A figure the screen reads off: monospaced, so the clocks do not jiggle as they count.
|
|
fn mono(size: f32) -> TextStyle {
|
|
TextStyle::new(size, 500.0).mono()
|
|
}
|
|
|
|
/// What the screen knows of the recording it is showing.
|
|
#[derive(Default)]
|
|
pub struct State {
|
|
/// The game's live data, as last read.
|
|
pub live: Option<Live>,
|
|
/// The timeline the recorder is filling in: what it learned about the game at the start.
|
|
pub open: Option<Timeline>,
|
|
/// The clip being written, with the bytes on disk so far.
|
|
pub file: Option<(PathBuf, u64)>,
|
|
/// Why the live data could not be read, when it could not.
|
|
pub error: Option<String>,
|
|
/// When the recording began, as the status flip or the timeline said.
|
|
pub started: Option<DateTime<Utc>>,
|
|
/// The read on its way to `poll`, and when the last one went out.
|
|
rx: Option<Receiver<Snapshot>>,
|
|
at: f64,
|
|
/// The whole second the clocks last showed, so a frame redraws when it ticks and not
|
|
/// before.
|
|
second: u64,
|
|
/// The "stop and delete" confirmation is open.
|
|
pub confirm_delete: bool,
|
|
/// The stop on its way is a stop and delete.
|
|
pub deleting: bool,
|
|
}
|
|
|
|
impl State {
|
|
/// The daemon started recording: the last game's readings go, and the clock starts.
|
|
pub fn begin(&mut self) {
|
|
*self = State {
|
|
started: Some(Utc::now()),
|
|
..State::default()
|
|
};
|
|
}
|
|
|
|
/// The recording ended: forget what the screen read of it.
|
|
///
|
|
/// A stop the app asked for keeps `deleting`: the daemon's status can reach the app before
|
|
/// the answer to the stop, and the answer is what deletes anything.
|
|
pub fn forget(&mut self) {
|
|
*self = State {
|
|
deleting: self.deleting,
|
|
..State::default()
|
|
};
|
|
}
|
|
|
|
/// When the recording began: the recorder's own start once its timeline is on disk.
|
|
pub fn started_at(&self) -> Option<DateTime<Utc>> {
|
|
self.open
|
|
.as_ref()
|
|
.map(|timeline| timeline.start_time)
|
|
.or(self.started)
|
|
}
|
|
|
|
/// How long the recorder has been recording, in seconds.
|
|
pub fn elapsed(&self) -> f64 {
|
|
self.started_at()
|
|
.map(|at| Utc::now().signed_duration_since(at).num_seconds().max(0) as f64)
|
|
.unwrap_or(0.0)
|
|
}
|
|
|
|
/// The champion being recorded: the game's own answer first, then the recorder's.
|
|
pub fn champion(&self) -> Option<String> {
|
|
self.live
|
|
.as_ref()
|
|
.and_then(Live::mine)
|
|
.map(|player| player.champion.clone())
|
|
.filter(|name| !name.is_empty())
|
|
.or_else(|| self.open.as_ref().and_then(Timeline::champion))
|
|
}
|
|
}
|
|
|
|
/// What one read of the game and the disk answered.
|
|
struct Snapshot {
|
|
live: Result<Live, String>,
|
|
open: Option<Timeline>,
|
|
file: Option<(PathBuf, u64)>,
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Reading
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Read the game and the disk on a thread; `tick` takes the answer.
|
|
fn snapshot(game_id: Option<u64>, dir: &Path, container: &str, since: DateTime<Utc>) -> Snapshot {
|
|
let open = history::open_timeline(game_id).map(|(_, timeline)| timeline);
|
|
let file = history::writing_file(dir, container, since);
|
|
let live = fetch().and_then(|value| {
|
|
Live::parse(&value).ok_or_else(|| "the game has not reached the map yet".to_string())
|
|
});
|
|
Snapshot { live, open, file }
|
|
}
|
|
|
|
/// The game's live data, as the game client serves it.
|
|
///
|
|
/// What goes wrong here is what a game that is still loading looks like: the endpoint answers
|
|
/// with nothing, or refuses until the map is up. The screen says so in those words; the
|
|
/// transport's own message goes to the shell, where a run from one can see it.
|
|
fn fetch() -> Result<Value, String> {
|
|
let answer = ureq::get(LIVE_URL)
|
|
.config()
|
|
.tls_config(TlsConfig::builder().disable_verification(true).build())
|
|
.timeout_global(Some(Duration::from_secs(2)))
|
|
.build()
|
|
.call();
|
|
let mut response = match answer {
|
|
Ok(response) => response,
|
|
Err(e) => {
|
|
eprintln!("live data: {e}");
|
|
return Err("the game is not answering yet".to_string());
|
|
}
|
|
};
|
|
let mut text = String::new();
|
|
if let Err(e) = response.body_mut().as_reader().read_to_string(&mut text) {
|
|
eprintln!("live data: {e}");
|
|
return Err("the game's answer did not read".to_string());
|
|
}
|
|
serde_json::from_str(&text).map_err(|e| {
|
|
eprintln!("live data: {e}");
|
|
"the game's answer did not read".to_string()
|
|
})
|
|
}
|
|
|
|
/// One player in the game being recorded.
|
|
#[derive(Debug, Clone, Default, PartialEq)]
|
|
pub struct Player {
|
|
pub name: String,
|
|
pub champion: String,
|
|
pub level: u32,
|
|
pub kills: u32,
|
|
pub deaths: u32,
|
|
pub assists: u32,
|
|
pub creep_score: u32,
|
|
pub vision: f64,
|
|
/// On the blue side.
|
|
pub order: bool,
|
|
pub dead: bool,
|
|
pub respawn: f64,
|
|
}
|
|
|
|
/// A count the game reports, in the range the screen draws.
|
|
fn count(value: Option<&Value>) -> u32 {
|
|
value
|
|
.and_then(Value::as_u64)
|
|
.map_or(0, |n| n.min(u64::from(u32::MAX)) as u32)
|
|
}
|
|
|
|
/// One game as its client reports it while it runs.
|
|
#[derive(Debug, Clone, Default, PartialEq)]
|
|
pub struct Live {
|
|
/// How long the game has been running, in seconds.
|
|
pub game_time: f64,
|
|
/// Everyone in it, both sides.
|
|
pub players: Vec<Player>,
|
|
/// Which of them is being recorded.
|
|
pub me: Option<usize>,
|
|
/// The recorded player's gold, which the game reports apart from the scores.
|
|
pub gold: u64,
|
|
}
|
|
|
|
impl Live {
|
|
/// Read `/liveclientdata/allgamedata`.
|
|
pub fn parse(value: &Value) -> Option<Live> {
|
|
let game = value.get("gameData")?;
|
|
let game_time = game.get("gameTime").and_then(Value::as_f64)?;
|
|
let mut players = Vec::new();
|
|
for player in value.get("allPlayers")?.as_array()? {
|
|
let champion = player
|
|
.get("championName")
|
|
.and_then(Value::as_str)
|
|
.unwrap_or_default();
|
|
// A slot with no champion has not picked yet, or is not in this game at all.
|
|
if champion.is_empty() {
|
|
continue;
|
|
}
|
|
let score = |key: &str| count(player.pointer(&format!("/scores/{key}")));
|
|
players.push(Player {
|
|
name: name_of(player),
|
|
champion: champion.to_string(),
|
|
level: count(player.get("level")),
|
|
kills: score("kills"),
|
|
deaths: score("deaths"),
|
|
assists: score("assists"),
|
|
creep_score: score("creepScore"),
|
|
vision: player
|
|
.pointer("/scores/wardScore")
|
|
.and_then(Value::as_f64)
|
|
.unwrap_or(0.0),
|
|
order: player.get("team").and_then(Value::as_str) == Some("ORDER"),
|
|
dead: player
|
|
.get("isDead")
|
|
.and_then(Value::as_bool)
|
|
.unwrap_or(false),
|
|
respawn: player
|
|
.get("respawnTimer")
|
|
.and_then(Value::as_f64)
|
|
.unwrap_or(0.0),
|
|
});
|
|
}
|
|
let active = value.get("activePlayer");
|
|
let champion = active
|
|
.and_then(|active| active.get("championName"))
|
|
.and_then(Value::as_str)
|
|
.filter(|name| !name.is_empty());
|
|
let summoner = active
|
|
.and_then(|active| active.get("summonerName"))
|
|
.and_then(Value::as_str);
|
|
// The active player's own row is the one for the champion, and failing that for the
|
|
// name: the game names the player being watched in both places.
|
|
let me = champion
|
|
.and_then(|name| players.iter().position(|player| player.champion == name))
|
|
.or_else(|| {
|
|
summoner.and_then(|name| players.iter().position(|player| player.name == name))
|
|
});
|
|
Some(Live {
|
|
game_time,
|
|
players,
|
|
me,
|
|
gold: active
|
|
.and_then(|active| active.get("currentGold"))
|
|
.and_then(Value::as_f64)
|
|
.unwrap_or(0.0)
|
|
.max(0.0) as u64,
|
|
})
|
|
}
|
|
|
|
/// The player being recorded, once the game says which one it is.
|
|
pub fn mine(&self) -> Option<&Player> {
|
|
self.me.and_then(|at| self.players.get(at))
|
|
}
|
|
}
|
|
|
|
/// A player's name, as the game writes it: `Name#TAG` when it has a tag.
|
|
fn name_of(player: &Value) -> String {
|
|
if let Some(name) = player.get("riotId").and_then(Value::as_str)
|
|
&& !name.is_empty()
|
|
{
|
|
return name.to_string();
|
|
}
|
|
let game_name = player
|
|
.get("riotIdGameName")
|
|
.or_else(|| player.get("summonerName"))
|
|
.and_then(Value::as_str)
|
|
.unwrap_or_default();
|
|
let tag = player
|
|
.get("riotIdTagLine")
|
|
.and_then(Value::as_str)
|
|
.unwrap_or_default();
|
|
if tag.is_empty() {
|
|
game_name.to_string()
|
|
} else {
|
|
format!("{game_name}#{tag}")
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Polling
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Take the read that landed and start the next one, while the game runs.
|
|
///
|
|
/// Returns whether the view has something new to draw: the clock on screen ticks with the
|
|
/// frames the app wakes for anyway, so a running recording always redraws.
|
|
pub fn tick(app: &mut App, t: f64) -> bool {
|
|
let mut changed = false;
|
|
if let Some(rx) = app.recording.rx.as_ref() {
|
|
match rx.try_recv() {
|
|
Ok(snapshot) => {
|
|
app.recording.rx = None;
|
|
app.recording.live = snapshot.live.as_ref().ok().cloned();
|
|
app.recording.error = snapshot.live.err();
|
|
app.recording.open = snapshot.open;
|
|
app.recording.file = snapshot.file;
|
|
changed = true;
|
|
}
|
|
Err(TryRecvError::Empty) => {}
|
|
Err(TryRecvError::Disconnected) => app.recording.rx = None,
|
|
}
|
|
}
|
|
// Nothing to read when nothing runs: the screen is only here on the way back from a game.
|
|
// A snapshot writes the sample game, and there is nothing behind it to ask.
|
|
if !app.status.is_recording || app.sample {
|
|
return changed;
|
|
}
|
|
// The clocks tick once a second: the rest of the screen only changes when a read lands.
|
|
let second = app.recording.elapsed().floor() as u64;
|
|
if second != app.recording.second {
|
|
app.recording.second = second;
|
|
changed = true;
|
|
}
|
|
if app.recording.rx.is_none() && t - app.recording.at >= REFRESH {
|
|
app.recording.at = t;
|
|
let game_id = app.status.current_game_id;
|
|
let since = app.recording.started_at().unwrap_or_else(Utc::now);
|
|
let dir = app.output_dir();
|
|
let container = app.container();
|
|
let (tx, rx) = channel();
|
|
std::thread::spawn(move || {
|
|
let _ = tx.send(snapshot(game_id, &dir, &container, since));
|
|
});
|
|
app.recording.rx = Some(rx);
|
|
}
|
|
changed
|
|
}
|
|
|
|
/// The daemon answered a stop: close what the app's own stop left open, then leave the screen.
|
|
///
|
|
/// The answer carries the clip the recorder wrote. A game the recorder ends by itself is one
|
|
/// timeline; stopped over the protocol it writes the video into a second one and leaves the
|
|
/// open one behind, which [`history::close_stopped`] tidies up, and which also deletes both
|
|
/// when the stop was the app's "stop and delete".
|
|
pub fn stopped(app: &mut App, data: &Value) {
|
|
let deleting = std::mem::take(&mut app.recording.deleting);
|
|
app.recording.confirm_delete = false;
|
|
let video = data.get("path").and_then(Value::as_str).map(PathBuf::from);
|
|
match video {
|
|
Some(video) => {
|
|
let open = history::open_timeline(app.status.current_game_id).map(|(path, _)| path);
|
|
match history::close_stopped(open.as_deref(), &video, deleting) {
|
|
Ok(()) if deleting => app.notify("Recording deleted"),
|
|
Ok(()) => app.notify("Recording stopped"),
|
|
// The recording is over whatever the tidying up made of it, and the row on
|
|
// screen is read from disk again below.
|
|
Err(e) => app.notify_error(e),
|
|
}
|
|
}
|
|
None => app.notify("Recording stopped"),
|
|
}
|
|
app.recording.forget();
|
|
app.refresh_games();
|
|
if app.screen == Screen::Recording {
|
|
app.jump(Screen::History);
|
|
}
|
|
}
|
|
|
|
/// The dialog's "Yes": stop, and delete what the stop writes.
|
|
pub fn delete_confirmed(app: &mut App) {
|
|
app.recording.confirm_delete = false;
|
|
app.recording.deleting = true;
|
|
app.daemon.stop_recording();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The screen
|
|
// ---------------------------------------------------------------------------
|
|
|
|
pub fn view(app: &App, p: Paint) -> El {
|
|
let mut screen = col()
|
|
.key("recording")
|
|
.w_full()
|
|
.padding(space::S6)
|
|
.gap(space::S3)
|
|
.child(header(app, p));
|
|
if !app.status.is_recording {
|
|
screen = screen.child(
|
|
col()
|
|
.key("idle")
|
|
.w_full()
|
|
.py(96.0)
|
|
.center()
|
|
.gap(6.0)
|
|
.child(
|
|
text("Nothing is being recorded")
|
|
.key("idle-title")
|
|
.font(heading(15.0))
|
|
.color(p.t.text),
|
|
)
|
|
.child(
|
|
text("The recorder starts on its own when a game begins.")
|
|
.key("idle-detail")
|
|
.font(body(12.0))
|
|
.color(p.muted()),
|
|
),
|
|
);
|
|
} else {
|
|
screen = screen.child(hero(app, p)).child(
|
|
row()
|
|
.key("halves")
|
|
.w_full()
|
|
.gap(space::S3)
|
|
.child(
|
|
col()
|
|
.key("teams")
|
|
.grow(1.0)
|
|
.min_w(0.0)
|
|
.gap(space::S3)
|
|
.child(team(app, p, true))
|
|
.child(team(app, p, false)),
|
|
)
|
|
.child(
|
|
col()
|
|
.key("side")
|
|
.w(320.0)
|
|
.min_w(260.0)
|
|
.child(details(app, p)),
|
|
),
|
|
);
|
|
}
|
|
// A short window scrolls the screen rather than cutting the side column off.
|
|
app.recording_scroll.follow(
|
|
"recording-scroller",
|
|
col()
|
|
.w_full()
|
|
.grow(1.0)
|
|
.min_h(0.0)
|
|
.scroll(rune::ScrollSpec {
|
|
stick_bottom: false,
|
|
})
|
|
.child(screen),
|
|
)
|
|
}
|
|
|
|
fn header(app: &App, p: Paint) -> El {
|
|
let mut title = row().key("title-line").gap(space::S2).center_y().child(
|
|
text("Recording")
|
|
.key("title")
|
|
.font(heading(20.0))
|
|
.color(p.t.text),
|
|
);
|
|
if app.status.is_recording {
|
|
title = title.child(ui::dot(p.danger));
|
|
}
|
|
let subtitle = match (&app.recording.open, app.status.is_recording) {
|
|
(_, false) => "Nothing is being recorded".to_string(),
|
|
(Some(timeline), true) => match timeline.summoner() {
|
|
Some(summoner) => format!("{} · {summoner}", timeline.queue_label()),
|
|
None => timeline.queue_label(),
|
|
},
|
|
(None, true) => "Waiting for the game".to_string(),
|
|
};
|
|
let mut actions = row().key("actions").gap(space::S2).center_y();
|
|
if app.status.is_recording {
|
|
actions = actions
|
|
.child(ui::button(
|
|
p,
|
|
"stop-recording",
|
|
"Stop recording",
|
|
true,
|
|
|app: &mut App| app.daemon.stop_recording(),
|
|
))
|
|
.child(ui::button_danger(
|
|
p,
|
|
"stop-delete",
|
|
"Stop and delete",
|
|
|app: &mut App| app.recording.confirm_delete = true,
|
|
));
|
|
}
|
|
row()
|
|
.key("head")
|
|
.w_full()
|
|
.justify_between()
|
|
.center_y()
|
|
.child(
|
|
col().key("title-block").gap(2.0).child(title).child(
|
|
text(subtitle)
|
|
.key("subtitle")
|
|
.font(body(11.5))
|
|
.color(p.muted()),
|
|
),
|
|
)
|
|
.child(actions)
|
|
}
|
|
|
|
/// The game on screen now: who is being recorded, the clocks, and the scoreline.
|
|
fn hero(app: &App, p: Paint) -> El {
|
|
let state = &app.recording;
|
|
let live = state.live.as_ref();
|
|
let me = live.and_then(Live::mine);
|
|
let champion = state.champion();
|
|
let game_time = live
|
|
.map(|live| live.game_time)
|
|
.unwrap_or_else(|| state.elapsed());
|
|
let started = state
|
|
.started_at()
|
|
.map(|at| format!("Started {}", format::game_start(at)))
|
|
.unwrap_or_else(|| "Starting".to_string());
|
|
let queue = state
|
|
.open
|
|
.as_ref()
|
|
.map(Timeline::queue_label)
|
|
.unwrap_or_else(|| "—".to_string());
|
|
let summoner = state.open.as_ref().and_then(Timeline::summoner);
|
|
|
|
let mut scores = row().key("scores").w_full().gap(30.0).center_y();
|
|
if let Some(player) = me {
|
|
scores = scores
|
|
.child(stat(
|
|
p,
|
|
&format!("{}/{}/{}", player.kills, player.deaths, player.assists),
|
|
"K / D / A",
|
|
))
|
|
.child(stat(p, &player.creep_score.to_string(), "CS"))
|
|
.child(stat(
|
|
p,
|
|
&format::number(live.map_or(0, |live| live.gold)),
|
|
"Gold",
|
|
))
|
|
.child(stat(p, &format!("{:.0}", player.vision), "Vision"))
|
|
.child(stat(p, &player.level.to_string(), "Level"));
|
|
} else {
|
|
let detail = state
|
|
.error
|
|
.clone()
|
|
.unwrap_or_else(|| "The game has not reported a scoreline yet.".into());
|
|
scores = scores.child(
|
|
col()
|
|
.key("waiting")
|
|
.gap(1.0)
|
|
.child(
|
|
text("Waiting for the game's live data")
|
|
.key("waiting-title")
|
|
.font(body(12.0))
|
|
.color(p.muted()),
|
|
)
|
|
.child(
|
|
text(detail)
|
|
.key("waiting-detail")
|
|
.font(body(11.0))
|
|
.color(p.muted())
|
|
.shrink()
|
|
.truncate(),
|
|
),
|
|
);
|
|
}
|
|
|
|
el().key("hero")
|
|
.w_full()
|
|
.radius(radius::LG)
|
|
.fill(p.t.card)
|
|
.border(p.hair, p.t.card_border)
|
|
.shadow(1.0, 2.0, p.t.shadow)
|
|
.shadow(8.0, 24.0, p.t.shadow_soft)
|
|
.child(
|
|
col()
|
|
.key("hero-body")
|
|
.w_full()
|
|
.child(
|
|
row()
|
|
.key("hero-head")
|
|
.w_full()
|
|
.gap(space::S3)
|
|
.px(space::S6)
|
|
.py(space::S3)
|
|
.center_y()
|
|
.child(ui::champion_portrait(app, p, champion.as_deref(), 56.0))
|
|
.child(
|
|
col()
|
|
.key("who")
|
|
.grow(1.0)
|
|
.min_w(0.0)
|
|
.gap(2.0)
|
|
.child(
|
|
text(champion.unwrap_or_else(|| "Recording".into()))
|
|
.key("champion")
|
|
.font(heading(18.0))
|
|
.color(p.t.text)
|
|
.shrink()
|
|
.truncate(),
|
|
)
|
|
.child(
|
|
text(match &summoner {
|
|
Some(summoner) => format!("{queue} · {summoner}"),
|
|
None => queue.clone(),
|
|
})
|
|
.key("queue")
|
|
.font(body(11.5))
|
|
.color(p.muted())
|
|
.shrink()
|
|
.truncate(),
|
|
)
|
|
.child(
|
|
text(started)
|
|
.key("started")
|
|
.font(body(11.0))
|
|
.color(p.muted()),
|
|
),
|
|
)
|
|
.child(clock(p, game_time, "Game time"))
|
|
.child(clock(p, state.elapsed(), "Recorded")),
|
|
)
|
|
.child(
|
|
el().key("hero-sep")
|
|
.w_full()
|
|
.h(p.hair)
|
|
.fill(p.t.separator)
|
|
.a11y_hidden(),
|
|
)
|
|
.child(
|
|
row()
|
|
.key("hero-scores")
|
|
.w_full()
|
|
.px(space::S6)
|
|
.py(space::S2)
|
|
.center_y()
|
|
.child(scores),
|
|
),
|
|
)
|
|
}
|
|
|
|
/// One of the two clocks: the value over what it counts.
|
|
fn clock(p: Paint, secs: f64, label: &str) -> El {
|
|
col()
|
|
.key(format!("clock-{label}"))
|
|
.gap(0.0)
|
|
.items_end()
|
|
.child(
|
|
text(format::clock(secs))
|
|
.key("value")
|
|
.font(mono(20.0))
|
|
.color(p.t.text),
|
|
)
|
|
.child(text(label).key("label").font(body(10.5)).color(p.muted()))
|
|
}
|
|
|
|
fn stat(p: Paint, value: &str, label: &str) -> El {
|
|
col()
|
|
.key(format!("stat-{label}"))
|
|
.gap(0.0)
|
|
.child(text(value).key("value").font(mono(14.0)).color(p.t.text))
|
|
.child(text(label).key("label").font(body(10.5)).color(p.muted()))
|
|
}
|
|
|
|
/// One side of the game, champion by champion.
|
|
fn team(app: &App, p: Paint, order: bool) -> El {
|
|
let mut rows = Vec::new();
|
|
if let Some(live) = &app.recording.live {
|
|
let mut players: Vec<&Player> = live
|
|
.players
|
|
.iter()
|
|
.filter(|player| player.order == order)
|
|
.collect();
|
|
if !order {
|
|
players.reverse();
|
|
}
|
|
for player in players {
|
|
rows.push(player_row(app, p, player));
|
|
}
|
|
}
|
|
if rows.is_empty() {
|
|
rows.push(
|
|
text("Waiting for the game's players…")
|
|
.key("no-players")
|
|
.font(body(11.5))
|
|
.color(p.muted())
|
|
.padding(space::S2),
|
|
);
|
|
}
|
|
ui::card(p, Some(if order { "Blue team" } else { "Red team" }), rows)
|
|
}
|
|
|
|
fn player_row(app: &App, p: Paint, player: &Player) -> El {
|
|
let mut trailing = row().key("score").gap(space::S2).center_y().child(
|
|
text(format!(
|
|
"{}/{}/{}",
|
|
player.kills, player.deaths, player.assists
|
|
))
|
|
.key("kda")
|
|
.font(mono(11.5))
|
|
.color(p.t.text),
|
|
);
|
|
if player.dead {
|
|
trailing = trailing.child(
|
|
text(format!("{}s", player.respawn.ceil().max(0.0) as u64))
|
|
.key("respawn")
|
|
.font(mono(11.5))
|
|
.color(p.danger),
|
|
);
|
|
}
|
|
row()
|
|
.key(format!("player-{}", player.champion))
|
|
.w_full()
|
|
.h(30.0)
|
|
.px(space::S2)
|
|
.gap(space::S2)
|
|
.center_y()
|
|
.child(ui::champion_portrait(app, p, Some(&player.champion), 20.0))
|
|
.child(
|
|
text(&player.name)
|
|
.key("name")
|
|
.font(body(11.5))
|
|
.color(if player.dead { p.muted() } else { p.t.text })
|
|
.grow(1.0)
|
|
.min_w(0.0)
|
|
.shrink()
|
|
.truncate(),
|
|
)
|
|
.child(col().key("kda-col").w(58.0).items_end().child(trailing))
|
|
.child(
|
|
col().key("cs-col").w(44.0).items_end().child(
|
|
text(player.creep_score.to_string())
|
|
.key("cs")
|
|
.font(mono(11.5))
|
|
.color(p.muted()),
|
|
),
|
|
)
|
|
.child(
|
|
col().key("level-col").w(30.0).items_end().child(
|
|
text(player.level.to_string())
|
|
.key("level")
|
|
.font(mono(11.5))
|
|
.color(p.muted()),
|
|
),
|
|
)
|
|
}
|
|
|
|
/// What is being written, and how it is being written.
|
|
fn details(app: &App, p: Paint) -> El {
|
|
let state = &app.recording;
|
|
let settings = app.settings.as_ref();
|
|
let file = state.file.as_ref().and_then(|(path, bytes)| {
|
|
path.file_name()
|
|
.and_then(|name| name.to_str())
|
|
.map(|name| (name.to_string(), *bytes))
|
|
});
|
|
let rows = vec![
|
|
detail_row(p, "recorded", "Recorded", format::clock(state.elapsed())),
|
|
detail_row(
|
|
p,
|
|
"started",
|
|
"Started",
|
|
state
|
|
.started_at()
|
|
.map(format::game_start)
|
|
.unwrap_or_else(|| "—".to_string()),
|
|
),
|
|
detail_row(
|
|
p,
|
|
"file",
|
|
"File",
|
|
file.as_ref()
|
|
.map(|(name, _)| name.clone())
|
|
.unwrap_or_else(|| "not written yet".to_string()),
|
|
),
|
|
detail_row(
|
|
p,
|
|
"size",
|
|
"On disk",
|
|
file.as_ref()
|
|
.map(|(_, bytes)| format::bytes(*bytes))
|
|
.unwrap_or_else(|| "—".to_string()),
|
|
),
|
|
detail_row(
|
|
p,
|
|
"quality",
|
|
"Quality",
|
|
match settings {
|
|
Some(settings) => format!(
|
|
"{} · {} fps",
|
|
settings.video.quality.label(),
|
|
settings.video.frame_rate
|
|
),
|
|
None => "—".to_string(),
|
|
},
|
|
),
|
|
detail_row(
|
|
p,
|
|
"encoder",
|
|
"Encoder",
|
|
settings
|
|
.map(|settings| settings.video.encoder_preset.label().to_string())
|
|
.unwrap_or_else(|| "—".to_string()),
|
|
),
|
|
detail_row(p, "audio", "Audio", audio_label(settings)),
|
|
detail_row(
|
|
p,
|
|
"folder",
|
|
"Folder",
|
|
app.output_dir().display().to_string(),
|
|
),
|
|
];
|
|
ui::card(p, Some("This recording"), rows)
|
|
}
|
|
|
|
/// A label on the left, its value on the right, in a row of the details card.
|
|
fn detail_row(p: Paint, key: &str, label: &str, value: String) -> El {
|
|
row()
|
|
.key(format!("detail-{key}"))
|
|
.w_full()
|
|
.h(30.0)
|
|
.px(space::S2)
|
|
.gap(space::S2)
|
|
.center_y()
|
|
.justify_between()
|
|
.label(format!("{label}: {value}"))
|
|
.child(
|
|
text(label)
|
|
.key("label")
|
|
.font(body(11.5))
|
|
.color(p.muted())
|
|
.a11y_hidden(),
|
|
)
|
|
.child(
|
|
text(value)
|
|
.key("value")
|
|
.font(mono(11.5))
|
|
.color(p.t.text)
|
|
.shrink()
|
|
.truncate()
|
|
.a11y_hidden(),
|
|
)
|
|
}
|
|
|
|
/// What the video carries, in the order the recorder lays the tracks down.
|
|
fn audio_label(settings: Option<&Settings>) -> String {
|
|
let Some(settings) = settings else {
|
|
return "—".to_string();
|
|
};
|
|
if !settings.audio.enabled {
|
|
return "No sound".to_string();
|
|
}
|
|
let mut parts = Vec::new();
|
|
if settings.audio.capture_game {
|
|
parts.push("Game".to_string());
|
|
}
|
|
if settings.audio.capture_mic {
|
|
parts.push("Microphone".to_string());
|
|
}
|
|
for source in &settings.audio.capture_apps {
|
|
if source.enabled {
|
|
parts.push(source.name.clone());
|
|
}
|
|
}
|
|
if parts.is_empty() {
|
|
"Nothing".to_string()
|
|
} else {
|
|
parts.join(", ")
|
|
}
|
|
}
|
|
|
|
/// The confirmation "Stop and delete" opens: stopping is the point of no return.
|
|
pub fn confirm_dialog(app: &App, p: Paint, layer: &mut Vec<El>) {
|
|
let panel = col()
|
|
.key("stop-delete-dialog")
|
|
.role("dialog")
|
|
.label("Stop and delete this recording")
|
|
.w(380.0)
|
|
.padding(space::S6)
|
|
.gap(space::S3)
|
|
.radius(radius::LG)
|
|
.fill(p.t.card)
|
|
.border(p.hair, p.t.card_border)
|
|
.shadow(4.0, 12.0, p.t.shadow)
|
|
.shadow(24.0, 48.0, p.t.shadow_soft)
|
|
.modal(true, |app: &mut App| app.recording.confirm_delete = false)
|
|
.child(
|
|
text("Stop recording and delete it?")
|
|
.key("stop-delete-question")
|
|
.font(heading(15.0))
|
|
.color(p.t.text),
|
|
)
|
|
.child(
|
|
text("The video and its timeline are removed from disk. Nothing of this game is kept.")
|
|
.key("stop-delete-note")
|
|
.font(body(12.0))
|
|
.color(p.muted()),
|
|
)
|
|
.child(
|
|
row()
|
|
.key("stop-delete-actions")
|
|
.w_full()
|
|
.gap(space::S2)
|
|
.justify_end()
|
|
.child(ui::button(
|
|
p,
|
|
"stop-delete-no",
|
|
"No",
|
|
false,
|
|
|app: &mut App| {
|
|
app.recording.confirm_delete = false;
|
|
},
|
|
))
|
|
.child(ui::button_danger(
|
|
p,
|
|
"stop-delete-yes",
|
|
"Stop and delete",
|
|
delete_confirmed,
|
|
)),
|
|
);
|
|
|
|
layer.push(
|
|
el().key("stop-delete-scrim")
|
|
.absolute(0.0, 0.0)
|
|
.w_full()
|
|
.h_full()
|
|
.fill(rune::Color::BLACK.alpha(if app.dark { 0.55 } else { 0.28 }))
|
|
.a11y_hidden(),
|
|
);
|
|
layer.push(
|
|
el().key("stop-delete-layer")
|
|
.absolute(0.0, 0.0)
|
|
.w_full()
|
|
.h_full()
|
|
.padding(16.0)
|
|
.center()
|
|
.child(panel),
|
|
);
|
|
}
|
|
|
|
/// A recording for `--scene recording-live`: twelve minutes in, one team ahead, a file on disk.
|
|
pub fn demo(app: &mut App) {
|
|
let mut live = Live {
|
|
game_time: 754.0,
|
|
gold: 12_840,
|
|
..Live::default()
|
|
};
|
|
let mut add = |name: &str,
|
|
champion: &str,
|
|
level: u32,
|
|
(kills, deaths, assists): (u32, u32, u32),
|
|
cs: u32,
|
|
order: bool| {
|
|
live.players.push(Player {
|
|
name: name.to_string(),
|
|
champion: champion.to_string(),
|
|
level,
|
|
kills,
|
|
deaths,
|
|
assists,
|
|
creep_score: cs,
|
|
vision: 18.0,
|
|
order,
|
|
dead: false,
|
|
respawn: 0.0,
|
|
});
|
|
};
|
|
add("Demo#EUW", "Kai'Sa", 11, (9, 3, 7), 214, true);
|
|
add("Nautilus Main", "Nautilus", 9, (2, 5, 14), 42, true);
|
|
add("Mid Lane", "Azir", 12, (6, 2, 9), 231, true);
|
|
add("Jungle Diff", "Sejuani", 10, (3, 3, 12), 138, true);
|
|
add("Top Lane", "Camille", 11, (7, 4, 5), 189, true);
|
|
add("Enemy ADC", "Jinx", 10, (5, 6, 4), 205, false);
|
|
add("Enemy Support", "Thresh", 8, (1, 8, 10), 34, false);
|
|
add("Enemy Mid", "Zed", 11, (4, 5, 6), 198, false);
|
|
add("Enemy Jungle", "Lee Sin", 9, (2, 7, 8), 121, false);
|
|
add("Enemy Top", "Renekton", 10, (5, 5, 3), 176, false);
|
|
live.me = Some(0);
|
|
app.recording.live = Some(live);
|
|
app.recording.started = Some(Utc::now() - chrono::Duration::seconds(766));
|
|
app.recording.file = Some((
|
|
PathBuf::from(r"C:\Users\you\Videos\League\2026-11-22_20-31-08_7000000.mp4"),
|
|
1_288_000_000,
|
|
));
|
|
if let Some(mut sample) = crate::history::sample().into_iter().next() {
|
|
sample.start_time = Utc::now() - chrono::Duration::seconds(766);
|
|
sample.end_time = None;
|
|
sample.video_file = None;
|
|
sample.video_path = None;
|
|
app.recording.open = Some(sample);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
const ALL_GAME_DATA: &str = r#"{
|
|
"activePlayer": {
|
|
"championName": "Kai'Sa",
|
|
"currentGold": 12840.5,
|
|
"level": 11,
|
|
"summonerName": "Demo"
|
|
},
|
|
"allPlayers": [
|
|
{
|
|
"championName": "Kai'Sa",
|
|
"riotId": "Demo#EUW",
|
|
"riotIdGameName": "Demo",
|
|
"riotIdTagLine": "EUW",
|
|
"level": 11,
|
|
"team": "ORDER",
|
|
"isDead": false,
|
|
"respawnTimer": 0.0,
|
|
"scores": {"kills": 9, "deaths": 3, "assists": 7, "creepScore": 214, "wardScore": 18.0}
|
|
},
|
|
{
|
|
"championName": "Zed",
|
|
"summonerName": "Enemy Mid",
|
|
"level": 12,
|
|
"team": "CHAOS",
|
|
"isDead": true,
|
|
"respawnTimer": 7.4,
|
|
"scores": {"kills": 4, "deaths": 5, "assists": 6, "creepScore": 198}
|
|
},
|
|
{
|
|
"championName": "",
|
|
"team": "CHAOS",
|
|
"scores": {}
|
|
}
|
|
],
|
|
"gameData": {"gameMode": "CLASSIC", "gameTime": 754.2}
|
|
}"#;
|
|
|
|
#[test]
|
|
fn the_game_reports_who_is_playing_and_how_it_is_going() {
|
|
let value: Value = serde_json::from_str(ALL_GAME_DATA).unwrap();
|
|
let live = Live::parse(&value).unwrap();
|
|
assert!((live.game_time - 754.2).abs() < 1e-9);
|
|
// The empty slot is not a player.
|
|
assert_eq!(live.players.len(), 2);
|
|
assert_eq!(live.gold, 12_840);
|
|
|
|
let me = live.mine().unwrap();
|
|
assert_eq!(me.champion, "Kai'Sa");
|
|
assert_eq!(me.name, "Demo#EUW");
|
|
assert_eq!((me.kills, me.deaths, me.assists), (9, 3, 7));
|
|
assert_eq!(me.creep_score, 214);
|
|
assert_eq!(me.vision, 18.0);
|
|
assert!(me.order && !me.dead);
|
|
|
|
// A name without a tag is read without one, and a dead player says when they are back.
|
|
let enemy = &live.players[1];
|
|
assert_eq!(enemy.name, "Enemy Mid");
|
|
assert!(!enemy.order);
|
|
assert!(enemy.dead);
|
|
assert!((enemy.respawn - 7.4).abs() < 1e-9);
|
|
}
|
|
|
|
#[test]
|
|
fn a_game_that_is_not_on_the_map_reports_nothing() {
|
|
// The game answers with an error document until the map has loaded.
|
|
let value = serde_json::json!({"errorCode": "RESOURCE_NOT_FOUND"});
|
|
assert!(Live::parse(&value).is_none());
|
|
}
|
|
}
|