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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::rc::Rc;

use debris_common::BuildMode;
use debris_llir::{item_id::ItemId, minecraft_utils::Scoreboard};
use rustc_hash::FxHashMap;

use crate::common::ScoreboardPlayer;

/// Holds data about specific scoreboard contexts
#[derive(Debug)]
pub struct ScoreboardContext {
    scoreboard_players: FxHashMap<ScoreboardPlayerId, Rc<str>>,
    pub scoreboards: FxHashMap<Scoreboard, Rc<str>>,
    scoreboard_prefix: Rc<str>,
    player_fmt_string: &'static str,
}

impl ScoreboardContext {
    /// Creates a new scoreboard context with the default scoreboard name
    pub fn new(scoreboard_prefix: String, build_mode: BuildMode) -> Self {
        let player_fmt_string = match build_mode {
            BuildMode::Debug => "var_",
            BuildMode::Release => "#",
        };
        ScoreboardContext {
            scoreboard_prefix: scoreboard_prefix.into(),
            scoreboard_players: Default::default(),
            scoreboards: Default::default(),
            player_fmt_string,
        }
    }

    /// Returns the name of this scoreboard
    ///
    /// Internally creates a scoreboard if it did not exist yet
    /// Clippy reports a false positive here
    #[allow(clippy::map_entry)]
    pub fn get_scoreboard(&mut self, scoreboard: Scoreboard) -> Rc<str> {
        if !self.scoreboards.contains_key(&scoreboard) {
            self.scoreboards
                .insert(scoreboard, self.format_scoreboard(scoreboard));
        }
        self.scoreboards.get(&scoreboard).unwrap().clone()
    }

    /// Gets the scoreboard player that corresponds to this `ItemId`
    pub fn get_scoreboard_player(&mut self, item_id: ItemId) -> Rc<str> {
        let player_id = item_id.into();
        match self.scoreboard_players.get(&player_id) {
            Some(scoreboard_player) => Rc::clone(scoreboard_player),
            None => self.add_player(player_id),
        }
    }

    /// Makes a new scoreboard player and returns it as a `ScoreboardPlayer`
    pub fn get_temporary_player(&mut self) -> ScoreboardPlayer {
        let length = self.scoreboard_players.len();
        let player_id = ScoreboardPlayerId::Temporary(length);
        let player = match self.scoreboard_players.get(&player_id) {
            Some(scoreboard_player) => Rc::clone(scoreboard_player),
            None => self.add_player(player_id),
        };
        let scoreboard = self.get_scoreboard(Scoreboard::Main);

        ScoreboardPlayer { player, scoreboard }
    }

    fn add_player(&mut self, player_id: ScoreboardPlayerId) -> Rc<str> {
        let num_players = self.scoreboard_players.len();
        let string = self.format_player(num_players);
        self.scoreboard_players
            .insert(player_id, Rc::clone(&string));
        string
    }

    fn format_player(&self, id: usize) -> Rc<str> {
        format!("{}{}", self.player_fmt_string, id).into()
    }

    fn format_scoreboard(&self, scoreboard: Scoreboard) -> Rc<str> {
        match scoreboard {
            Scoreboard::Main => self.scoreboard_prefix.clone(),
            Scoreboard::Custom(id) => format!("{}.{}", self.scoreboard_prefix, id).into(),
            Scoreboard::Internal(_) => todo!(),
        }
    }
}

/// Used to differentiate between a generated id and a temporary id created by this backend
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
enum ScoreboardPlayerId {
    Normal(ItemId),
    Temporary(usize),
}

impl From<ItemId> for ScoreboardPlayerId {
    fn from(value: ItemId) -> Self {
        ScoreboardPlayerId::Normal(value)
    }
}