rust/hedgewars-server/src/server/replaystorage.rs
author alfadur
Sun, 26 Mar 2023 03:53:49 +0300
changeset 15941 8035f7452b48
parent 15883 8f2ddbfdc0b4
permissions -rw-r--r--
optimize type mask iteration
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()))
15883
8f2ddbfdc0b4 Simplify code
unC0Rr
parents: 15881
diff changeset
    27
            .find(|f| {
15881
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
            .and_then(|f| {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    31
                Some((
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    32
                    ReplayId { path: f.clone() },
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    33
                    Replay::load(f.to_str()?).ok()?,
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    34
                ))
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
        if let Some((ref replay_id, _)) = result {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    38
            self.borrowed_replays.push((*replay_id).clone());
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    39
        }
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
        result
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    42
    }
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
    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
    45
        self.unborrow(id);
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    46
        self.move_file("failed", id)
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    47
    }
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
    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
    50
        self.unborrow(id);
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    51
        self.move_file("checked", id)
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    52
    }
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
    pub fn requeue_replay(&mut self, id: &ReplayId) {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    55
        self.unborrow(id)
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    56
    }
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
    fn unborrow(&mut self, id: &ReplayId) {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    59
        self.borrowed_replays.retain(|i| i != id)
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    60
    }
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
    fn move_file(&self, dir: &str, id: &ReplayId) -> std::io::Result<()> {
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    63
        let new_name = format!(
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    64
            "{}/{}",
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    65
            dir,
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    66
            id.path
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    67
                .file_name()
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    68
                .and_then(|f| f.to_str())
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    69
                .expect("What's up with your file name?")
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    70
        );
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
        fs::rename(&id.path, new_name)
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    73
    }
212e16c60bf5 Add replay storage to the new server implementation
unC0Rr
parents:
diff changeset
    74
}