rlru 0.1.18

Rocket League replay uploader
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};

use anyhow::{bail, Context, Result};
use reqwest::header::{AUTHORIZATION, LOCATION};
use reqwest::StatusCode;
use serde_json::Value;

use crate::config::{RankUploadConfig, UploadDestinationConfig};
use crate::state_file::write_atomically;

const HISTORY_PER_ACCOUNT: usize = 20;
const MAX_CACHE_SIZE_FACTOR: usize = 2;

#[derive(Debug, Clone)]
pub struct ReplayUploader {
    http: reqwest::Client,
}

impl ReplayUploader {
    pub fn new() -> Self {
        Self {
            http: reqwest::Client::new(),
        }
    }

    pub async fn ping(&self, target: &UploadDestinationConfig) -> Result<()> {
        if !target.ping.enabled {
            return Ok(());
        }
        let auth_header = target.auth.header_value()?;
        self.ping_with_auth_header(target, auth_header).await
    }

    async fn ping_with_auth_header(
        &self,
        target: &UploadDestinationConfig,
        auth_header: Option<String>,
    ) -> Result<()> {
        if !target.ping.enabled {
            return Ok(());
        }

        let response = self
            .request_with_auth_header(
                self.http.get(target.endpoint_url(&target.ping.path)?),
                auth_header,
            )
            .send()
            .await
            .with_context(|| format!("failed to ping {}", target.name))?;
        let status = response.status();
        let body = response.text().await.unwrap_or_default();

        if !status.is_success() {
            bail!("{} ping failed with {status}: {body}", target.name);
        }

        Ok(())
    }

    pub async fn upload_replay(
        &self,
        target: &UploadDestinationConfig,
        file_path: &Path,
    ) -> Result<UploadResult> {
        self.upload_replay_with_match_id(target, file_path, None)
            .await
    }

    pub async fn upload_replay_with_match_id(
        &self,
        target: &UploadDestinationConfig,
        file_path: &Path,
        match_id: Option<&str>,
    ) -> Result<UploadResult> {
        let auth_header = target.auth.header_value()?;
        self.upload_replay_with_auth_header(target, file_path, match_id, None, auth_header, None)
            .await
    }

    /// Uploads a replay file. `upload_name`, when provided, becomes the multipart
    /// filename (most destinations surface it as the replay's display name);
    /// otherwise the name falls back to `{match_id}.replay` or the file's own
    /// name.
    pub async fn upload_replay_with_auth_header(
        &self,
        target: &UploadDestinationConfig,
        file_path: &Path,
        match_id: Option<&str>,
        upload_name: Option<&str>,
        auth_header: Option<String>,
        ranks_bundle: Option<&RankBundle>,
    ) -> Result<UploadResult> {
        if !target.replay_upload.enabled {
            return Ok(UploadResult {
                outcome: UploadOutcome::Skipped,
                location: None,
            });
        }

        self.ping_with_auth_header(target, auth_header.clone())
            .await?;

        let file_name = match (upload_name, match_id) {
            (Some(name), _) => name.to_string(),
            (None, Some(match_id)) => format!("{match_id}.replay"),
            (None, None) => file_path
                .file_name()
                .and_then(|value| value.to_str())
                .context("replay path has no UTF-8 file name")?
                .to_string(),
        };
        let bytes = fs::read(file_path)
            .with_context(|| format!("failed to read replay {}", file_path.display()))?;
        let part = reqwest::multipart::Part::bytes(bytes).file_name(file_name);
        let mut form =
            reqwest::multipart::Form::new().part(target.replay_upload.file_field.clone(), part);

        // Bundle player ranks into the same upload when the destination supports
        // it (Rocket Sense). Replay files do not carry ranks, so this is how the
        // metadata travels alongside the file in a single request.
        if let (RankUploadConfig::Bundled { field }, Some(bundle)) =
            (&target.rank_upload, ranks_bundle)
        {
            if !bundle.players.is_empty() {
                let json =
                    serde_json::to_string(bundle).context("failed to serialize rank bundle")?;
                form = form.text(field.clone(), json);
            }
        }

        let response = self
            .request_with_auth_header(
                self.http
                    .post(target.endpoint_url(&target.replay_upload.path)?)
                    .multipart(form),
                auth_header,
            )
            .send()
            .await
            .with_context(|| format!("failed to upload replay to {}", target.name))?;

        let status = response.status();
        let location_header = response
            .headers()
            .get(LOCATION)
            .and_then(|value| value.to_str().ok())
            .map(ToOwned::to_owned);
        let body = response.text().await.unwrap_or_default();
        let location = upload_location(target, location_header.as_deref(), &body);

        classify_upload_response(target, status, location, &body)
    }

    pub async fn upload_mmr(
        &self,
        target: &UploadDestinationConfig,
        payload: &MmrUpload,
        match_id: &str,
    ) -> Result<()> {
        let auth_header = target.auth.header_value()?;
        self.upload_mmr_with_auth_header(target, payload, match_id, auth_header)
            .await
    }

    /// Posts per-match player rank metadata, mirroring the BakkesMod
    /// AutoReplayUploader plugin's separate `POST /api/v1/mmr` request. No-op
    /// unless the destination uses an MMR endpoint and there is data to send.
    pub async fn upload_mmr_with_auth_header(
        &self,
        target: &UploadDestinationConfig,
        payload: &MmrUpload,
        match_id: &str,
        auth_header: Option<String>,
    ) -> Result<()> {
        let RankUploadConfig::Endpoint { path } = &target.rank_upload else {
            return Ok(());
        };
        if payload.players.is_empty() {
            return Ok(());
        }

        let response = self
            .request_with_auth_header(
                self.http
                    .post(target.endpoint_url_without_query(path)?)
                    .json(payload),
                auth_header,
            )
            .send()
            .await
            .with_context(|| format!("failed to upload MMR for {match_id} to {}", target.name))?;

        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        if !status.is_success() {
            bail!(
                "{} MMR upload for {match_id} failed with {status}: {body}",
                target.name
            );
        }
        Ok(())
    }

    fn request_with_auth_header(
        &self,
        request: reqwest::RequestBuilder,
        auth_header: Option<String>,
    ) -> reqwest::RequestBuilder {
        match auth_header {
            Some(value) => request.header(AUTHORIZATION, value),
            None => request,
        }
    }
}

impl Default for ReplayUploader {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UploadResult {
    pub outcome: UploadOutcome,
    pub location: Option<String>,
}

/// Player rank payload posted alongside a replay, matching the JSON schema the
/// BakkesMod AutoReplayUploader plugin sends to `ballchasing.com/api/v1/mmr`.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct MmrUpload {
    /// Match GUID the ranks belong to (the plugin calls this field `game`).
    pub game: String,
    pub players: Vec<MmrPlayer>,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct MmrPlayer {
    /// Rocket League `OnlinePlatform` enum value (Steam=1, PS4=2, Xbox=4,
    /// Switch=7, Epic=11).
    pub platform_id: i64,
    /// Platform-specific player id (the middle component of the PsyNet PlayerID).
    pub id: String,
    pub before: MmrSkill,
    pub after: MmrSkill,
    /// Free-form debug field; the plugin stores the player name here.
    pub debug: String,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct MmrSkill {
    pub tier: i64,
    pub division: i64,
    pub matches_played: i64,
    pub mmr: f64,
}

/// Rich player rank payload bundled into the replay upload for destinations
/// that accept it (Rocket Sense). Unlike [`MmrUpload`], this carries the full
/// PsyNet skill snapshot — raw TrueSkill `mu`/`sigma` plus the before/after
/// tier/division/mmr — so the server can store everything available.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct RankBundle {
    pub players: Vec<RankBundlePlayer>,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct RankBundlePlayer {
    /// Platform-specific online id (the middle component of the PsyNet PlayerID).
    pub platform_player_id: String,
    /// Player display name from PsyNet match history, when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub player_name: Option<String>,
    /// PsyNet platform name (e.g. `Epic`, `Steam`, `PS4`); the server normalizes.
    pub platform: String,
    /// Playlist (queue) the skill is for.
    pub playlist: i64,
    /// Whether the skill is ranked/meaningful (PsyNet `bValid`).
    pub valid: bool,
    /// Rank after the match (current rank).
    pub after: RankSnapshot,
    /// Rank before the match.
    pub before: RankSnapshot,
    /// Current per-playlist skill snapshot from `Skills/GetPlayersSkills`,
    /// carrying counters the match-history rank metadata lacks (`win_streak`,
    /// `matches_played`, `placement_matches_played`) plus PsyNet's own `mmr`.
    /// Point-in-time as of the sync, NOT the match moment — accurate for fresh
    /// uploads, stale for backfilled replays. Absent when PsyNet returned no
    /// skill for this player/playlist.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub current: Option<CurrentSkill>,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct CurrentSkill {
    pub mmr: f64,
    pub win_streak: i64,
    pub matches_played: i64,
    pub placement_matches_played: i64,
    /// Unix epoch (seconds) when this snapshot was fetched from PsyNet — always
    /// a moment *after* the match was played. The server compares it against the
    /// match timestamp to gauge staleness: a small gap means the counters still
    /// reflect the match; a large gap (backfilled replay) means they don't.
    pub fetched_at: i64,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct RankSnapshot {
    pub tier: i64,
    pub division: i64,
    pub mu: f64,
    pub sigma: f64,
    pub mmr: f64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UploadOutcome {
    Uploaded,
    Duplicate,
    Skipped,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct UploadCacheEntry {
    replay_id: String,
    location: Option<String>,
}

#[derive(Debug, Clone)]
pub struct UploadCache {
    path: PathBuf,
    items: Vec<UploadCacheEntry>,
    index: HashMap<String, Option<String>>,
    max: usize,
}

impl UploadCache {
    pub fn load(path: PathBuf, account_count: usize) -> Result<Self> {
        let max = account_count
            .max(1)
            .saturating_mul(HISTORY_PER_ACCOUNT)
            .saturating_mul(MAX_CACHE_SIZE_FACTOR);
        let mut cache = Self {
            path,
            items: Vec::new(),
            index: HashMap::new(),
            max,
        };

        if !cache.path.exists() {
            return Ok(cache);
        }

        let file = fs::File::open(&cache.path)
            .with_context(|| format!("failed to open upload cache {}", cache.path.display()))?;
        for line in BufReader::new(file).lines() {
            let line = line?;
            if let Some(entry) = UploadCacheEntry::parse(&line) {
                let replay_id = entry.replay_id.clone();
                if let Entry::Vacant(index_entry) = cache.index.entry(replay_id) {
                    index_entry.insert(entry.location.clone());
                    cache.items.push(entry);
                }
            }
        }
        cache.ensure_capacity();
        Ok(cache)
    }

    pub fn contains(&self, replay_id: &str) -> bool {
        self.index.contains_key(replay_id)
    }

    pub fn location(&self, replay_id: &str) -> Option<&str> {
        self.index.get(replay_id).and_then(|value| value.as_deref())
    }

    pub fn add(&mut self, replay_id: impl Into<String>) -> Result<bool> {
        self.add_with_location(replay_id, None)
    }

    pub fn add_with_location(
        &mut self,
        replay_id: impl Into<String>,
        location: Option<String>,
    ) -> Result<bool> {
        let replay_id = replay_id.into();
        if let Some(cached_location) = self.index.get_mut(&replay_id) {
            let updated = location.is_some() && cached_location != &location;
            if updated {
                *cached_location = location.clone();
                if let Some(entry) = self
                    .items
                    .iter_mut()
                    .find(|entry| entry.replay_id == replay_id)
                {
                    entry.location = location;
                }
                self.save()?;
            }
            return Ok(updated);
        }
        self.index.insert(replay_id.clone(), location.clone());
        self.items.push(UploadCacheEntry {
            replay_id,
            location,
        });
        self.ensure_capacity();
        self.save()?;
        Ok(true)
    }

    pub fn save(&self) -> Result<()> {
        let mut content = String::new();
        for item in &self.items {
            content.push_str(&item.serialize());
            content.push('\n');
        }
        write_atomically(&self.path, content)
            .with_context(|| format!("failed to write upload cache {}", self.path.display()))
    }

    fn ensure_capacity(&mut self) {
        while self.items.len() > self.max {
            if let Some(oldest) = self.items.first().cloned() {
                self.items.remove(0);
                self.index.remove(&oldest.replay_id);
            }
        }
    }
}

impl UploadCacheEntry {
    fn parse(line: &str) -> Option<Self> {
        let line = line.trim();
        if line.is_empty() {
            return None;
        }
        let (replay_id, location) = line
            .split_once('\t')
            .map(|(replay_id, location)| {
                let location = (!location.trim().is_empty()).then(|| location.trim().to_string());
                (replay_id.trim().to_string(), location)
            })
            .unwrap_or_else(|| (line.to_string(), None));
        (!replay_id.is_empty()).then_some(Self {
            replay_id,
            location,
        })
    }

    fn serialize(&self) -> String {
        match &self.location {
            Some(location) => format!("{}\t{}", self.replay_id, location),
            None => self.replay_id.clone(),
        }
    }
}

fn upload_location(
    target: &UploadDestinationConfig,
    location_header: Option<&str>,
    body: &str,
) -> Option<String> {
    location_header
        .and_then(|location| absolutize_location(target, location))
        .or_else(|| {
            location_from_body(body).and_then(|location| absolutize_location(target, &location))
        })
        .or_else(|| rocket_sense_location(target, body))
}

fn absolutize_location(target: &UploadDestinationConfig, location: &str) -> Option<String> {
    if location.trim().is_empty() {
        return None;
    }
    target
        .url
        .join(location)
        .ok()
        .map(|url| url.to_string())
        .or_else(|| Some(location.to_string()))
}

fn location_from_body(body: &str) -> Option<String> {
    let value: Value = serde_json::from_str(body).ok()?;
    ["location", "link", "url"]
        .into_iter()
        .find_map(|field| value.get(field).and_then(Value::as_str))
        .map(ToOwned::to_owned)
}

fn rocket_sense_location(target: &UploadDestinationConfig, body: &str) -> Option<String> {
    if target.name != "Rocket Sense" {
        return None;
    }
    let value: Value = serde_json::from_str(body).ok()?;
    let replay_id = value
        .get("replay")
        .and_then(|replay| replay.get("id"))
        .and_then(Value::as_str)?;
    let mut base = target.url.clone();
    if base.path().ends_with("/api/v1") {
        let stripped = base.path().trim_end_matches("/api/v1").to_string();
        base.set_path(&stripped);
    }
    base.join(&format!("replays/{replay_id}"))
        .ok()
        .map(|url| url.to_string())
}

fn classify_upload_response(
    target: &UploadDestinationConfig,
    status: StatusCode,
    location: Option<String>,
    body: &str,
) -> Result<UploadResult> {
    let code = status.as_u16();
    if target.replay_upload.success_statuses.contains(&code) {
        Ok(UploadResult {
            outcome: UploadOutcome::Uploaded,
            location,
        })
    } else if target.replay_upload.duplicate_statuses.contains(&code)
        || rocket_sense_deduplicated(target, status, body)
    {
        Ok(UploadResult {
            outcome: UploadOutcome::Duplicate,
            location,
        })
    } else {
        bail!("{} upload failed with {status}: {body}", target.name)
    }
}

fn rocket_sense_deduplicated(
    target: &UploadDestinationConfig,
    status: StatusCode,
    body: &str,
) -> bool {
    if target.name != "Rocket Sense" || status != StatusCode::OK {
        return false;
    }
    let Ok(value) = serde_json::from_str::<Value>(body) else {
        return false;
    };
    value
        .get("deduplicated")
        .and_then(Value::as_bool)
        .unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn upload_cache_dedupes_and_persists() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("uploaded.txt");
        let mut cache = UploadCache::load(path.clone(), 1).unwrap();

        assert!(cache.add("match-1").unwrap());
        assert!(!cache.add("match-1").unwrap());

        let restored = UploadCache::load(path, 1).unwrap();
        assert!(restored.contains("match-1"));
    }

    #[test]
    fn upload_cache_persists_locations() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("uploaded.txt");
        let mut cache = UploadCache::load(path.clone(), 1).unwrap();

        cache
            .add_with_location("match-1", Some("https://example.com/replay/1".to_string()))
            .unwrap();

        let restored = UploadCache::load(path, 1).unwrap();
        assert_eq!(
            restored.location("match-1"),
            Some("https://example.com/replay/1")
        );
    }

    #[test]
    fn upload_cache_reads_legacy_id_only_lines() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("uploaded.txt");
        fs::write(&path, "match-1\n").unwrap();

        let restored = UploadCache::load(path, 1).unwrap();

        assert!(restored.contains("match-1"));
        assert_eq!(restored.location("match-1"), None);
    }

    #[test]
    fn upload_cache_save_uses_temporary_file_without_leaving_part_files() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("uploaded.txt");
        let mut cache = UploadCache::load(path.clone(), 1).unwrap();

        cache.add("match-1").unwrap();

        let entries = fs::read_dir(tmp.path())
            .unwrap()
            .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
            .collect::<Vec<_>>();
        assert_eq!(entries, vec!["uploaded.txt"]);
    }

    #[test]
    fn rocket_sense_http_200_deduplicated_is_duplicate() {
        let target = UploadDestinationConfig::rocket_sense();
        let result = classify_upload_response(
            &target,
            StatusCode::OK,
            Some("https://rocket-sense.duckdns.org/replays/replay-1".to_string()),
            r#"{"deduplicated":true,"replay":{"id":"replay-1"}}"#,
        )
        .unwrap();

        assert_eq!(result.outcome, UploadOutcome::Duplicate);
        assert_eq!(
            result.location,
            Some("https://rocket-sense.duckdns.org/replays/replay-1".to_string())
        );
    }
}