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 hnefatafl_copenhagen::{
17
    board::Board, game::PreviousBoards, play::Plays, role::Role, server_game::ArchivedGame,
18
    status::Status, tree::Tree,
19
};
20

            
21
#[derive(Clone, Debug)]
22
pub(crate) struct ArchivedGameHandle {
23
    pub boards: Tree,
24
    pub game: ArchivedGame,
25
    pub play: usize,
26
}
27

            
28
impl ArchivedGameHandle {
29
    #[must_use]
30
    #[allow(clippy::missing_panics_doc)]
31
    #[allow(clippy::unwrap_used)]
32
    pub(crate) fn new(game: &ArchivedGame) -> ArchivedGameHandle {
33
        let mut board = Board::new(game.board_size);
34
        let mut boards = Tree::new(game.board_size);
35
        let mut turn = Role::default();
36

            
37
        let plays = match &game.plays {
38
            Plays::PlayRecordsTimed(plays) => {
39
                plays.iter().map(|record| record.play.clone()).collect()
40
            }
41
            Plays::PlayRecords(plays) => plays.clone(),
42
        };
43

            
44
        for play in &plays {
45
            if let Some(play) = &play {
46
                board
47
                    .play(
48
                        play,
49
                        &Status::Ongoing,
50
                        &turn,
51
                        &mut PreviousBoards::default(),
52
                    )
53
                    .unwrap();
54

            
55
                boards.insert(&board);
56
                turn = match turn {
57
                    Role::Attacker => Role::Defender,
58
                    Role::Roleless => Role::Roleless,
59
                    Role::Defender => Role::Attacker,
60
                };
61
            }
62
        }
63

            
64
        boards.backward_all();
65

            
66
        ArchivedGameHandle {
67
            boards,
68
            game: game.clone(),
69
            play: 0,
70
        }
71
    }
72
}