codegame/debug/
mod.rs

1use super::*;
2
3/// Debug commands that can be sent while debugging with the app
4#[trans_doc = "ru:Команды, которые могут быть отправлены приложению для помощи в отладке"]
5#[derive(Serialize, Deserialize, Trans)]
6#[trans(no_generics_in_name)]
7pub enum DebugCommand<G: Game> {
8    /// Add debug data to current tick
9    #[trans_doc = "ru:Добавить отладочные данные в текущий тик"]
10    Add {
11        /// Data to add
12        #[trans_doc = "ru:Данные для добавления"]
13        data: G::DebugData,
14    },
15    /// Clear current tick's debug data
16    #[trans_doc = "ru:Очистить отладочные данные текущего тика"]
17    Clear,
18    /// Enable/disable auto performing of commands
19    #[trans_doc = "ru:Включить/выключить автоматическое выполнение команд"]
20    SetAutoFlush {
21        /// Enable/disable autoflush
22        #[trans_doc = "ru:Включить/выключить автоматическое выполнение"]
23        enable: bool,
24    },
25    /// Perform all previously sent commands
26    #[trans_doc = "ru:Выполнить все присланные ранее команды"]
27    Flush,
28}
29
30pub struct DebugInterface<G: Game> {
31    pub(crate) debug_command_handler: Box<dyn Fn(usize, bool, DebugCommand<G>) + Send>,
32    pub(crate) debug_state: Box<dyn Fn(usize) -> G::DebugState + Send>,
33}
34
35impl<G: Game> DebugInterface<G> {
36    pub fn for_player(&self, player_index: usize, global: bool) -> PlayerDebugInterface<G> {
37        PlayerDebugInterface {
38            player_index,
39            global,
40            debug_interface: self,
41        }
42    }
43}
44
45pub struct PlayerDebugInterface<'a, G: Game> {
46    player_index: usize,
47    global: bool,
48    debug_interface: &'a DebugInterface<G>,
49}
50
51impl<G: Game> PlayerDebugInterface<'_, G> {
52    pub fn send(&self, command: DebugCommand<G>) {
53        (self.debug_interface.debug_command_handler)(self.player_index, self.global, command);
54    }
55    pub fn state(&self) -> G::DebugState {
56        (self.debug_interface.debug_state)(self.player_index)
57    }
58}