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
//! List of players which are not allowed to use client side physics, to punish
//! abuse

use super::{editable::Version, EditableSetting, SERVER_PHYSICS_FORCE_FILENAME as FILENAME};
use serde::{Deserialize, Serialize};
use std::convert::Infallible;
pub use v0::*;

#[derive(Deserialize, Serialize)]
pub enum ServerPhysicsForceListRaw {
    V0(ServerPhysicsForceList),
}

impl TryFrom<ServerPhysicsForceListRaw> for (Version, ServerPhysicsForceList) {
    type Error = <ServerPhysicsForceList as EditableSetting>::Error;

    fn try_from(value: ServerPhysicsForceListRaw) -> Result<Self, Self::Error> {
        use ServerPhysicsForceListRaw::*;
        Ok(match value {
            V0(mut value) => (value.validate()?, value),
        })
    }
}

impl From<ServerPhysicsForceList> for ServerPhysicsForceListRaw {
    fn from(value: ServerPhysicsForceList) -> Self { Self::V0(value) }
}

impl EditableSetting for ServerPhysicsForceList {
    type Error = Infallible;
    type Legacy = ServerPhysicsForceList;
    type Setting = ServerPhysicsForceListRaw;

    const FILENAME: &'static str = FILENAME;
}

type Latest = ServerPhysicsForceList;

mod v0 {
    use super::Latest;
    use authc::Uuid;
    use serde::{Deserialize, Serialize};
    use std::{
        collections::HashMap,
        ops::{Deref, DerefMut},
    };

    use crate::settings::{editable::Version, EditableSetting};

    #[derive(Clone, Deserialize, Serialize, Debug)]
    pub struct ServerPhysicsForceRecord {
        /// Moderator/Admin who forced the player to server authoritative
        /// physics, none if applied via the server (currently not possible)
        pub by: Option<(Uuid, String)>,
        pub reason: Option<String>,
    }

    #[derive(Clone, Deserialize, Serialize, Default)]
    pub struct ServerPhysicsForceList(HashMap<Uuid, ServerPhysicsForceRecord>);

    impl Deref for ServerPhysicsForceList {
        type Target = HashMap<Uuid, ServerPhysicsForceRecord>;

        fn deref(&self) -> &Self::Target { &self.0 }
    }

    impl DerefMut for ServerPhysicsForceList {
        fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
    }

    impl ServerPhysicsForceList {
        pub(super) fn validate(&mut self) -> Result<Version, <Latest as EditableSetting>::Error> {
            Ok(Version::Latest)
        }
    }
}