aboutsummaryrefslogtreecommitdiff
path: root/src/feature
diff options
context:
space:
mode:
authorjan <jan@ruken.pw>2016-04-06 17:54:12 (UTC)
committerjan <jan@ruken.pw>2016-04-06 17:54:12 (UTC)
commit314047dda73387fff1fd4ccdb35f5db6ea6d7d5a (patch)
treef6b0d4c52db0a93c9afa9989b4624f23193b3a28 /src/feature
parentac1f32f068a8c1e856b6db00a8058d9bf372a006 (diff)
FEATURES!
Diffstat (limited to 'src/feature')
-rw-r--r--src/feature/jisoku.rs55
-rw-r--r--src/feature/mod.rs23
-rw-r--r--src/feature/tasterank.rs61
3 files changed, 139 insertions, 0 deletions
diff --git a/src/feature/jisoku.rs b/src/feature/jisoku.rs
new file mode 100644
index 0000000..70bc886
--- /dev/null
+++ b/src/feature/jisoku.rs
@@ -0,0 +1,55 @@
1extern crate telegram_bot;
2use telegram_bot::{Api, Message, MessageType};
3use std::sync::{Arc, Mutex};
4use std::thread;
5use std::time::Duration;
6
7use std::ascii::AsciiExt;
8
9use feature::FeatureResult;
10use feature::Feature;
11
12pub struct Jisoku {
13 running: bool,
14}
15
16const MESSAGES: [&'static str; 5] = ["JISOKU METER FURIKIRI UBAU GAME",
17 "I AM A CHASER DARE YORI HAYAI RACE",
18 "LIFE IS FIRE TSUKIRU MADE HIGH PACE",
19 "LETS GET READY TO RUMBLE MEZAMERO PASSION",
20 "*insert dubstep*"];
21
22impl Feature for Jisoku {
23 fn name(&self) -> &'static str {
24 "Jisoku Meter"
25 }
26 fn init(&mut self) {}
27 fn handle(&mut self, a: Api, m: Message) -> Result<FeatureResult, String> {
28 if let MessageType::Text(ref s) = m.msg {
29 if !(&s).contains("JISOKU METER") {
30 return Ok(FeatureResult::Skip);
31 }
32 } else {
33 return Ok(FeatureResult::Skip);
34 }
35
36 if self.running {
37 return Ok(FeatureResult::Skip);
38 }
39 self.running = true;
40 thread::spawn(move || {
41 for s in MESSAGES.into_iter() {
42 a.send_message(m.chat.id(), (*s).to_owned(), None, None, None, None);
43 thread::sleep(Duration::new(1, 0));
44 }
45 });
46 Ok(FeatureResult::Handled)
47 }
48}
49
50impl Jisoku {
51 pub fn new() -> Jisoku {
52 let mut j = Jisoku { running: false };
53 j
54 }
55}
diff --git a/src/feature/mod.rs b/src/feature/mod.rs
new file mode 100644
index 0000000..5e00620
--- /dev/null
+++ b/src/feature/mod.rs
@@ -0,0 +1,23 @@
1use std::boxed::Box;
2use std::error::Error;
3
4extern crate telegram_bot;
5use telegram_bot::{Api, Message};
6
7pub mod jisoku;
8pub mod tasterank;
9
10pub enum FeatureResult {
11 Handled,
12 Skip,
13}
14
15pub trait Feature {
16 fn name(&self) -> &'static str;
17 fn init(&mut self);
18 fn handle(&mut self, Api, Message) -> Result<FeatureResult, String>;
19}
20
21pub fn init() -> Vec<Box<Feature>> {
22 vec![Box::new(tasterank::Tasterank::new()), Box::new(jisoku::Jisoku::new())]
23}
diff --git a/src/feature/tasterank.rs b/src/feature/tasterank.rs
new file mode 100644
index 0000000..c4a29f9
--- /dev/null
+++ b/src/feature/tasterank.rs
@@ -0,0 +1,61 @@
1use std::option::Option;
2use std::io;
3use std::io::prelude::*;
4use std::fs::File;
5
6use std::ascii::AsciiExt;
7
8extern crate telegram_bot;
9use telegram_bot::{Api, Message, MessageType};
10
11use feature::FeatureResult;
12use feature::Feature;
13
14pub struct Tasterank {
15 ranks: Vec<String>,
16}
17
18impl Feature for Tasterank {
19 fn name(&self) -> &'static str {
20 "Tasterank"
21 }
22 fn init(&mut self) {
23 if let Err(e) = self.update_ranks() {
24 panic!(e);
25 }
26 }
27 fn handle(&mut self, a: Api, m: Message) -> Result<FeatureResult, String> {
28 if let MessageType::Text(s) = m.msg {
29 if !(&s).to_ascii_lowercase().contains("luggas tasterank") {
30 return Ok(FeatureResult::Skip);
31 }
32 let mut msg = String::new();
33 for (i, rank) in self.ranks.iter().enumerate() {
34 msg.push_str(&format!("{}. {}\n", i + 1, rank));
35 }
36 a.send_message(m.chat.id(), msg, None, None, Some(m.message_id), None);
37 return Ok(FeatureResult::Handled);
38 }
39 Ok(FeatureResult::Skip)
40 }
41}
42
43impl Tasterank {
44 pub fn new() -> Tasterank {
45 let mut t = Tasterank { ranks: vec![] };
46 t.init();
47 t
48 }
49 fn update_ranks(&mut self) -> Result<(), io::Error> {
50 let mut f = try!(File::open("data/tasterank.list"));
51 let mut buf = String::new();
52 try!(f.read_to_string(&mut buf));
53 let temp: Vec<&str> = (&buf).split("\n").collect();
54 self.ranks.clear();
55 for r in &temp {
56 self.ranks.push(r.to_string());
57 }
58 self.ranks.pop().unwrap();
59 Ok(())
60 }
61}