Launcher: play time per instance

Every run of the game started from the window or `horizon-launcher play`
adds its length to its instance's play time in instances.json (by id, so a
renamed instance keeps it), with when it ended. Home's Instance row shows
"3 h 06 min in total · last played today"; `instance list` shows it too,
and its JSON gives played_secs and last_played_ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-27 02:02:15 +01:00
parent 4b761abcb2
commit ea0cc65aea
7 changed files with 150 additions and 12 deletions
+13 -3
View File
@@ -740,7 +740,12 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> {
cmd.arg("--creative");
}
cmd.args(arg);
attached(cmd)?
let started = std::time::Instant::now();
let played = attached(cmd);
if let Err(e) = instances::record_play(&instance.id, started.elapsed()) {
eprintln!("Play time not saved: {e}");
}
played?
}
Cmd::Server {
dir,
@@ -1470,7 +1475,8 @@ fn instance(action: InstanceCmd, as_json: bool) -> Result<(), String> {
serde_json::json!({
"id": i.id, "name": i.name, "kind": format!("{:?}", i.kind).to_lowercase(),
"home": i.home, "icon": i.icon, "group": i.group, "selected": selected,
"version": i.version,
"version": i.version, "played_secs": i.played.as_secs(),
"last_played_ms": i.last_played.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()).map(|d| d.as_millis() as u64),
})
};
let line = |i: &instances::Instance, selected: bool| {
@@ -1485,8 +1491,12 @@ fn instance(action: InstanceCmd, as_json: bool) -> Result<(), String> {
.as_ref()
.map(|v| format!(" {v}"))
.unwrap_or_default();
let played = match i.last_played {
Some(_) => format!(" {} played", instances::play_time(i.played)),
None => String::new(),
};
println!(
"{mark} {} ({}){pinned}{group}\n {}",
"{mark} {} ({}){pinned}{group}{played}\n {}",
i.name,
i.id,
i.home.display()