codegame/player/
mod.rs

1use super::*;
2
3mod stream;
4mod tcp;
5
6pub use stream::*;
7pub use tcp::*;
8
9#[derive(Debug, Error)]
10pub enum PlayerError {
11    #[error("IO error: {0}")]
12    IOError(#[from] std::io::Error),
13}
14
15pub trait Player<G: Game>: Send {
16    fn get_action(
17        &mut self,
18        player_view: &G::PlayerView,
19        debug_interface: Option<&PlayerDebugInterface<G>>,
20    ) -> Result<G::Action, PlayerError>;
21    fn debug_update(
22        &mut self,
23        player_view: &G::PlayerView,
24        debug_interface: &PlayerDebugInterface<G>,
25    ) -> Result<(), PlayerError>;
26}
27
28pub struct EmptyPlayer;
29
30#[derive(Debug, Serialize, Deserialize, Clone)]
31pub struct EmptyPlayerOptions;
32
33impl<G: Game> Player<G> for EmptyPlayer
34where
35    G::Action: Default,
36{
37    fn get_action(
38        &mut self,
39        _: &G::PlayerView,
40        _: Option<&PlayerDebugInterface<G>>,
41    ) -> Result<G::Action, PlayerError> {
42        Ok(default())
43    }
44    fn debug_update(
45        &mut self,
46        _: &G::PlayerView,
47        _: &PlayerDebugInterface<G>,
48    ) -> Result<(), PlayerError> {
49        Ok(())
50    }
51}
52
53impl<G: Game, T: Player<G> + ?Sized> Player<G> for Box<T> {
54    fn get_action(
55        &mut self,
56        view: &G::PlayerView,
57        debug_interface: Option<&PlayerDebugInterface<G>>,
58    ) -> Result<G::Action, PlayerError> {
59        (**self).get_action(view, debug_interface)
60    }
61    fn debug_update(
62        &mut self,
63        player_view: &G::PlayerView,
64        debug_interface: &PlayerDebugInterface<G>,
65    ) -> Result<(), PlayerError> {
66        (**self).debug_update(player_view, debug_interface)
67    }
68}
69
70pub struct ErroredPlayer(pub String);
71
72impl<G: Game> Player<G> for ErroredPlayer {
73    fn get_action(
74        &mut self,
75        _: &G::PlayerView,
76        _: Option<&PlayerDebugInterface<G>>,
77    ) -> Result<G::Action, PlayerError> {
78        Err(PlayerError::IOError(std::io::Error::new(
79            std::io::ErrorKind::Other,
80            self.0.as_str(),
81        )))
82    }
83    fn debug_update(
84        &mut self,
85        _: &G::PlayerView,
86        _: &PlayerDebugInterface<G>,
87    ) -> Result<(), PlayerError> {
88        Err(PlayerError::IOError(std::io::Error::new(
89            std::io::ErrorKind::Other,
90            self.0.as_str(),
91        )))
92    }
93}