1
// This file is part of hnefatafl-copenhagen.
2
//
3
// hnefatafl-copenhagen is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU Affero General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// hnefatafl-copenhagen is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU Affero General Public License for more details.
12
//
13
// You should have received a copy of the GNU Affero General Public License
14
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15

            
16
use std::{
17
    collections::{HashMap, HashSet},
18
    fmt,
19
};
20

            
21
use hnefatafl_copenhagen::{email::Email, glicko::Rating};
22
use serde::{Deserialize, Serialize};
23

            
24
use crate::Id;
25

            
26
impl fmt::Display for Accounts {
27
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28
        let mut accounts = Vec::new();
29
        for (name, account) in &self.0 {
30
            accounts.push(format!("{name} {account}"));
31
        }
32
        accounts.sort_unstable();
33
        let accounts = accounts.join(" ");
34

            
35
        write!(f, "{accounts}")
36
    }
37
}
38

            
39
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
40
pub(crate) struct Account {
41
    #[serde(default)]
42
    pub email: Option<Email>,
43
    #[serde(default)]
44
    pub email_sent: i64,
45
    #[serde(default)]
46
    pub password: String,
47
    #[serde(default)]
48
    pub logged_in: Option<usize>,
49
    #[serde(default)]
50
    pub draws: u64,
51
    #[serde(default)]
52
    pub wins: u64,
53
    #[serde(default)]
54
    pub losses: u64,
55
    #[serde(default)]
56
    pub rating: Rating,
57
    #[serde(default)]
58
    pub send_emails: bool,
59
    #[serde(skip)]
60
    pub pending_games: HashSet<Id>,
61
}
62

            
63
impl fmt::Display for Account {
64
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65
        if self.logged_in.is_some() {
66
            write!(
67
                f,
68
                "{} {} {} {} logged_in",
69
                self.wins, self.losses, self.draws, self.rating
70
            )
71
        } else {
72
            write!(
73
                f,
74
                "{} {} {} {} logged_out",
75
                self.wins, self.losses, self.draws, self.rating
76
            )
77
        }
78
    }
79
}
80

            
81
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
82
pub(crate) struct Accounts(pub HashMap<String, Account>);