Skip to content

Commit d641140

Browse files
authored
Add TheRun.gg Integration (#903)
This ports the TheRun.gg integration from the original LiveSplit. All the same features are supported.
1 parent 1f63444 commit d641140

File tree

7 files changed

+470
-0
lines changed

7 files changed

+470
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ livesplit-auto-splitting = { path = "crates/livesplit-auto-splitting", version =
9090
arc-swap = { version = "1.7.1", optional = true }
9191
log = { version = "0.4.14", default-features = false, optional = true }
9292

93+
# therun.gg
94+
reqwest = { version = "0.13.2", default-features = false, features = ["json", "rustls"], optional = true }
95+
9396
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
9497
# WebAssembly in the Web
9598
js-sys = { version = "0.3.55", optional = true }
@@ -184,6 +187,7 @@ wasm-web = [
184187
"web-sys",
185188
]
186189
auto-splitting = ["std", "livesplit-auto-splitting", "arc-swap", "log"]
190+
therun-gg = ["std", "reqwest", "time/formatting"]
187191

188192
[lib]
189193
bench = false

capi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ wasm-web = ["livesplit-core/wasm-web", "wasm-bindgen", "wasm-bindgen-futures", "
2828
auto-splitting = ["livesplit-core/auto-splitting"]
2929
assume-str-parameters-are-utf8 = []
3030
web-rendering = ["wasm-web", "livesplit-core/web-rendering"]
31+
therun-gg = ["wasm-web", "livesplit-core/therun-gg"]

capi/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ pub mod total_playtime_component;
9090
pub mod web_command_sink;
9191
#[cfg(all(target_family = "wasm", feature = "web-rendering"))]
9292
pub mod web_rendering;
93+
#[cfg(all(target_family = "wasm", feature = "therun-gg"))]
94+
pub mod web_therun_gg;
9395

9496
use crate::{
9597
run_metadata_custom_variable::RunMetadataCustomVariable,

capi/src/web_therun_gg.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! Provides a therun.gg client for the web that communicates with therun.gg to
2+
//! provide live tracking and stats uploading for speedruns.
3+
4+
use livesplit_core::{Timer, event::Event, networking::therun_gg};
5+
use wasm_bindgen::prelude::*;
6+
use wasm_bindgen_futures::spawn_local;
7+
8+
/// A client for communicating with therun.gg. It handles live split tracking
9+
/// and stats uploading. The upload key is a 36-character key obtained from
10+
/// therun.gg.
11+
#[wasm_bindgen]
12+
pub struct TheRunClient {
13+
inner: therun_gg::Client,
14+
}
15+
16+
#[wasm_bindgen]
17+
impl TheRunClient {
18+
/// Creates a new therun.gg client with the given upload key and settings.
19+
#[wasm_bindgen(constructor)]
20+
pub fn new(
21+
upload_key: &str,
22+
is_live_tracking_enabled: bool,
23+
is_stats_uploading_enabled: bool,
24+
) -> Self {
25+
Self {
26+
inner: therun_gg::Client::new(
27+
upload_key.to_owned(),
28+
is_live_tracking_enabled,
29+
is_stats_uploading_enabled,
30+
),
31+
}
32+
}
33+
34+
/// Sets whether live tracking is enabled. When enabled, live split data is
35+
/// sent to therun.gg after every split action.
36+
pub fn setLiveTrackingEnabled(&mut self, enabled: bool) {
37+
self.inner.set_live_tracking_enabled(enabled);
38+
}
39+
40+
/// Sets whether stats uploading is enabled. When enabled, the .lss file is
41+
/// uploaded to therun.gg after every reset or finished run.
42+
pub fn setStatsUploadingEnabled(&mut self, enabled: bool) {
43+
self.inner.set_stats_uploading_enabled(enabled);
44+
}
45+
46+
/// Handles a timer event. Pass the event code and a pointer to the timer.
47+
/// The event codes correspond to the `Event` enum variants (e.g. 0 =
48+
/// Started, 6 = Paused, 7 = Resumed, etc.).
49+
pub unsafe fn handleEvent(&mut self, event: u32, timer: *const Timer) {
50+
// SAFETY: The caller must ensure that the pointer is valid.
51+
let snapshot = unsafe { &*timer }.snapshot();
52+
if let Some(future) = self.inner.handle_event(Event::from(event), &snapshot) {
53+
spawn_local(future);
54+
}
55+
}
56+
}

src/networking/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
66
#[cfg(feature = "std")]
77
pub mod server_protocol;
8+
#[cfg(feature = "therun-gg")]
9+
pub mod therun_gg;

0 commit comments

Comments
 (0)