rust/hedgewars-server/src/server/replaystorage.rs
author unC0Rr
Thu, 29 Sep 2022 16:30:02 +0200
changeset 15882 f185e7367dd3
parent 15881 212e16c60bf5
child 15883 8f2ddbfdc0b4
permissions -rw-r--r--
Add some work towards having checker work with the new server
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15881
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
     1
use crate::core::types::Replay;
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
     2
//use super::demo::load;
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
     3
use std::fs;
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
     4
use std::path::PathBuf;
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
     5
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
     6
pub struct ReplayStorage {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
     7
    borrowed_replays: Vec<ReplayId>,
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
     8
}
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
     9
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    10
#[derive(Clone, PartialEq)]
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    11
pub struct ReplayId {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    12
    path: PathBuf,
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    13
}
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    14
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    15
impl ReplayStorage {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    16
    pub fn new() -> Self {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    17
        ReplayStorage {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    18
            borrowed_replays: vec![],
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    19
        }
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    20
    }
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    21
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    22
    pub fn pick_replay(&mut self, protocol: u16) -> Option<(ReplayId, Replay)> {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    23
        let protocol_suffix = format!(".{}", protocol);
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    24
        let result = fs::read_dir("replays")
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    25
            .ok()?
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    26
            .flat_map(|f| Some(f.ok()?.path()))
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    27
            .filter(|f| {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    28
                f.ends_with(&protocol_suffix) && !self.borrowed_replays.iter().any(|e| &e.path == f)
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    29
            })
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    30
            .next()
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    31
            .and_then(|f| {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    32
                Some((
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    33
                    ReplayId { path: f.clone() },
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    34
                    Replay::load(f.to_str()?).ok()?,
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    35
                ))
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    36
            });
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    37
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    38
        if let Some((ref replay_id, _)) = result {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    39
            self.borrowed_replays.push((*replay_id).clone());
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    40
        }
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    41
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    42
        result
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    43
    }
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    44
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    45
    pub fn move_failed_replay(&mut self, id: &ReplayId) -> std::io::Result<()> {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    46
        self.unborrow(id);
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    47
        self.move_file("failed", id)
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    48
    }
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    49
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    50
    pub fn move_checked_replay(&mut self, id: &ReplayId) -> std::io::Result<()> {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    51
        self.unborrow(id);
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    52
        self.move_file("checked", id)
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    53
    }
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    54
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    55
    pub fn requeue_replay(&mut self, id: &ReplayId) {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    56
        self.unborrow(id)
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    57
    }
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    58
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    59
    fn unborrow(&mut self, id: &ReplayId) {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    60
        self.borrowed_replays.retain(|i| i != id)
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    61
    }
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    62
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    63
    fn move_file(&self, dir: &str, id: &ReplayId) -> std::io::Result<()> {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    64
        let new_name = format!(
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    65
            "{}/{}",
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    66
            dir,
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    67
            id.path
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    68
                .file_name()
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    69
                .and_then(|f| f.to_str())
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    70
                .expect("What's up with your file name?")
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    71
        );
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    72
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    73
        fs::rename(&id.path, new_name)
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    74
    }
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    75
}