|
| 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 | +} |
0 commit comments