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 core::fmt;
17
use std::{
18
    collections::{HashMap, HashSet},
19
    sync::{Arc, Mutex},
20
};
21

            
22
use chrono::{DateTime, Utc};
23
use serde::{Deserialize, Serialize};
24

            
25
use crate::Id;
26

            
27
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
28
pub struct Tournament {
29
    pub players: HashSet<String>,
30
    pub date: DateTime<Utc>,
31
    pub tree: Option<TournamentTree>,
32
}
33

            
34
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
35
pub struct TournamentTree {
36
    pub active_games: HashMap<Id, Arc<Mutex<Players>>>,
37
    pub rounds: Vec<Vec<Status>>,
38
}
39

            
40
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
41
pub struct Players {
42
    pub round: usize,
43
    pub chunk: usize,
44
    pub player_1: Wins,
45
    pub player_2: Wins,
46
}
47

            
48
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
49
pub struct Wins {
50
    pub name: String,
51
    pub attacker: u8,
52
    pub defender: u8,
53
}
54

            
55
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
56
pub enum Status {
57
    Lost(Player),
58
    #[default]
59
    None,
60
    Playing(Player),
61
    Ready(Player),
62
    Waiting,
63
    Won(Player),
64
}
65

            
66
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
67
pub struct Player {
68
    pub name: String,
69
    pub rating: f64,
70
}
71

            
72
impl fmt::Display for Player {
73
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74
        write!(f, "{} {:0}", self.name, self.rating)
75
    }
76
}