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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use futures::prelude::*;
use organelle::{Axon, Constraint, Impulse, Soma};
use url::Url;

use super::{Error, Result};
use data::{Difficulty, GameSetup, PlayerSetup, Race};
use launcher::GamePorts;
use melee::{MeleeCompetitor, MeleeContract, MeleeDendrite, UpdateScheme};
use synapses::{Dendrite, Synapse};

/// build a built-in AI opponent
pub struct ComputerBuilder {
    race: Race,
    difficulty: Difficulty,
}

impl ComputerBuilder {
    /// create the builder
    pub fn new() -> Self {
        Self {
            race: Race::Random,
            difficulty: Difficulty::Medium,
        }
    }

    /// set the race of the AI (default is Random)
    pub fn race(self, race: Race) -> Self {
        Self { race: race, ..self }
    }

    /// set the difficulty of the AI (default is Medium)
    pub fn difficulty(self, difficulty: Difficulty) -> Self {
        Self {
            difficulty: difficulty,
            ..self
        }
    }

    /// build the built-in AI
    pub fn create(self) -> Result<Computer> {
        Ok(Computer {
            0: ComputerSoma::axon(self.race, self.difficulty)?,
        })
    }
}

impl MeleeCompetitor for Computer {
    type Soma = Axon<ComputerSoma>;

    fn into_soma(self) -> Self::Soma {
        self.0
    }
}

/// a built-in AI opponent soma
pub struct Computer(Axon<ComputerSoma>);

pub struct ComputerSoma {
    setup: PlayerSetup,
    melee: Option<MeleeDendrite>,
}

impl ComputerSoma {
    fn axon(race: Race, difficulty: Difficulty) -> Result<Axon<Self>> {
        Ok(Axon::new(
            Self {
                setup: PlayerSetup::Computer(race, difficulty),
                melee: None,
            },
            vec![Constraint::One(Synapse::Melee)],
            vec![],
        ))
    }
}

impl Soma for ComputerSoma {
    type Synapse = Synapse;
    type Error = Error;

    #[async(boxed)]
    fn update(self, imp: Impulse<Self::Synapse>) -> Result<Self> {
        match imp {
            Impulse::AddDendrite(_, Synapse::Melee, Dendrite::Melee(melee)) => {
                Ok(Self {
                    melee: Some(melee),

                    ..self
                })
            },

            Impulse::Start(_, main_tx, handle) => {
                handle.spawn(
                    self.melee
                        .unwrap()
                        .wrap(ComputerDendrite::new(self.setup))
                        .or_else(move |e| {
                            main_tx
                                .send(Impulse::Error(e.into()))
                                .map(|_| ())
                                .map_err(|_| ())
                        }),
                );

                Ok(Self {
                    melee: None,
                    ..self
                })
            },
            _ => bail!("unexpected impulse"),
        }
    }
}

struct ComputerDendrite {
    setup: PlayerSetup,
}

impl MeleeContract for ComputerDendrite {
    type Error = Error;

    #[async(boxed)]
    fn get_player_setup(self, _: GameSetup) -> Result<(Self, PlayerSetup)> {
        let setup = self.setup;
        Ok((self, setup))
    }
    /// connect to an instance
    #[async(boxed)]
    fn connect(self, _: Url) -> Result<Self> {
        Ok(self)
    }

    /// create a game
    #[async(boxed)]
    fn create_game(self, _: GameSetup, _: Vec<PlayerSetup>) -> Result<Self> {
        Ok(self)
    }

    /// join a game
    #[async(boxed)]
    fn join_game(self, _: PlayerSetup, _: Option<GamePorts>) -> Result<Self> {
        Ok(self)
    }

    /// run the game
    #[async(boxed)]
    fn run_game(self, _: UpdateScheme) -> Result<Self> {
        Ok(self)
    }
}

impl ComputerDendrite {
    fn new(setup: PlayerSetup) -> Self {
        Self { setup: setup }
    }
}