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
// SPDX-License-Identifier: AGPL-3.0-or-later
17
// SPDX-FileCopyrightText: 2025 David Campbell <david@hnefatafl.org>
18

            
19
use hnefatafl_copenhagen::glicko::{CONFIDENCE_INTERVAL_95, Rating};
20
use log::error;
21

            
22
#[derive(Clone, Debug)]
23
pub(crate) struct User {
24
    pub name: String,
25
    pub wins: String,
26
    pub losses: String,
27
    pub draws: String,
28
    pub rating: Rating,
29
    pub logged_in: bool,
30
}
31

            
32
impl From<&[&str; 6]> for User {
33
    fn from(user: &[&str; 6]) -> Self {
34
        let [name, wins, losses, draws, rating, logged_in] = *user;
35

            
36
        let (mut rating, mut deviation) = rating.split_once("±").unwrap_or_else(|| {
37
            error!("The ratings has this form: {rating}");
38
            unreachable!();
39
        });
40

            
41
        rating = rating.trim();
42
        deviation = deviation.trim();
43

            
44
        let (Ok(rating), Ok(deviation)) = (rating.parse::<f64>(), deviation.parse::<f64>()) else {
45
            error!("The ratings has this form: ({rating}, {deviation})");
46
            unreachable!();
47
        };
48

            
49
        let logged_in = "logged_in" == logged_in;
50

            
51
        User {
52
            name: name.to_string(),
53
            wins: wins.to_string(),
54
            losses: losses.to_string(),
55
            draws: draws.to_string(),
56
            rating: Rating {
57
                rating,
58
                rd: deviation / CONFIDENCE_INTERVAL_95,
59
            },
60
            logged_in,
61
        }
62
    }
63
}