Skip to content

Instantly share code, notes, and snippets.

@marioidival
Last active August 1, 2018 22:11
Show Gist options
  • Save marioidival/395c721acc70320e275004093618906c to your computer and use it in GitHub Desktop.
Save marioidival/395c721acc70320e275004093618906c to your computer and use it in GitHub Desktop.
Actix DBZ Scouter

ACTIX Dragon Ball Scouter

  • The "system" send RaisePower to Fighters, this "trigger" they to raise your Ki until Scouter stop the event loop.[1]
  • The result of [1] is a Ki of Fighter, this Ki is send to Scouter.
  • The Scouter just verify if : Ki > 9000, if true, Scouter just stop the event loop System::current().stop();

I'm trying 2 way...

  • 1° just execute action (RaisePower) to one fighter and blocked the event loop
  • 2° just execute action (RaisePower) to all, but once.

snippets: https://gist.github.com/marioidival/395c721acc70320e275004093618906c

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();
}
/// This code just RaisePower once to all Fighters
extern crate actix;
extern crate futures;
use actix::dev::{MessageResponse, ResponseChannel};
use actix::prelude::*;
/// Race representation
#[derive(Clone)]
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
#[derive(Clone)]
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
#[derive(Clone)]
struct Fighter {
name: String,
ki: Ki,
race: Race,
}
/// Implementation of Actor for Fighter
impl Actor for Fighter {
type Context = Context<Self>;
}
impl Handler<RaisePower> for Fighter {
type Result = Ki;
fn handle(&mut self, _msg: RaisePower, _ctx: &mut Context<Self>) -> Self::Result {
println!("raising my power: {}", self.name);
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 f in get_zfighters() {
let fighter_addr = f.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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment