|
extern crate actix; |
|
extern crate futures; |
|
|
|
use actix::dev::{MessageResponse, ResponseChannel}; |
|
use actix::prelude::*; |
|
use futures::stream::repeat; |
|
|
|
use std::io; |
|
|
|
/// Race representation |
|
enum Race { |
|
Saiyan, |
|
Namekian, |
|
Human, |
|
} |
|
|
|
/// times_by_race return a multiply of ki by race |
|
fn times_by_race(race: &Race) -> usize { |
|
match race { |
|
Race::Human => 2, |
|
Race::Namekian => 4, |
|
Race::Saiyan => 7, |
|
} |
|
} |
|
|
|
/// Ki Message representation |
|
struct Ki(usize); |
|
|
|
impl Message for Ki { |
|
type Result = Ki; |
|
} |
|
|
|
impl<A, M> MessageResponse<A, M> for Ki |
|
where |
|
A: Actor, |
|
M: Message<Result = Ki>, |
|
{ |
|
fn handle<R: ResponseChannel<M>>(self, _: &mut A::Context, tx: Option<R>) { |
|
if let Some(tx) = tx { |
|
tx.send(self); |
|
} |
|
} |
|
} |
|
|
|
/// RaisePower Message representation; |
|
#[derive(Clone)] |
|
struct RaisePower; |
|
|
|
impl Message for RaisePower { |
|
type Result = Ki; |
|
} |
|
|
|
/// Fighter Actor representation |
|
struct Fighter { |
|
name: String, |
|
ki: Ki, |
|
race: Race, |
|
} |
|
|
|
/// Implementation of Actor for Fighter |
|
impl Actor for Fighter { |
|
type Context = Context<Self>; |
|
|
|
fn started(&mut self, ctx: &mut Context<Self>) { |
|
Self::add_stream(repeat::<RaisePower, io::Error>(RaisePower), ctx); |
|
} |
|
} |
|
|
|
impl StreamHandler<RaisePower, io::Error> for Fighter { |
|
fn handle(&mut self, _msg: RaisePower, _ctx: &mut Context<Self>) { |
|
println!("raising my power: {}", self.name); |
|
self.ki.0 += times_by_race(&self.race); |
|
} |
|
} |
|
|
|
impl Handler<RaisePower> for Fighter { |
|
type Result = Ki; |
|
|
|
fn handle(&mut self, _msg: RaisePower, _ctx: &mut Context<Self>) -> Self::Result { |
|
self.ki.0 += times_by_race(&self.race); |
|
Ki(self.ki.0) |
|
} |
|
} |
|
|
|
/// Scouter Actor representation |
|
struct Scouter; |
|
|
|
impl Actor for Scouter { |
|
type Context = Context<Self>; |
|
} |
|
|
|
impl Handler<Ki> for Scouter { |
|
type Result = Ki; |
|
|
|
fn handle(&mut self, msg: Ki, _ctx: &mut Context<Self>) -> Self::Result { |
|
if msg.0 > 9000 { |
|
println!("eita porra! estorou a bagaça aqui vei..."); |
|
System::current().stop() |
|
} |
|
println!("esse nojento é fraco: {}", msg.0); |
|
msg |
|
} |
|
} |
|
|
|
fn get_zfighters() -> Vec<Fighter> { |
|
vec![ |
|
Fighter { |
|
name: "Goku".into(), |
|
ki: Ki(400), |
|
race: Race::Saiyan, |
|
}, |
|
Fighter { |
|
name: "Kurilin".into(), |
|
ki: Ki(100), |
|
race: Race::Human, |
|
}, |
|
Fighter { |
|
name: "Piccolo".into(), |
|
ki: Ki(300), |
|
race: Race::Namekian, |
|
}, |
|
] |
|
} |
|
|
|
fn main() { |
|
let system = System::new("actor-scouter-poc"); |
|
|
|
Arbiter::start(|ctx: &mut Context<Scouter>| { |
|
for fighter in get_zfighters() { |
|
let fighter_addr = fighter.start(); |
|
let fighter_future = fighter_addr.send(RaisePower); |
|
|
|
ctx.spawn( |
|
fighter_future |
|
.into_actor(&Scouter) |
|
.map(|r, _, c| { |
|
println!("message {}", r.0); |
|
c.notify(r) |
|
}) |
|
.map_err(|e, _, _| eprintln!("error: {}", e)), |
|
); |
|
} |
|
|
|
Scouter |
|
}); |
|
|
|
system.run(); |
|
} |