diff options
Diffstat (limited to 'src/feature')
-rw-r--r-- | src/feature/jisoku.rs | 55 | ||||
-rw-r--r-- | src/feature/mod.rs | 23 | ||||
-rw-r--r-- | src/feature/tasterank.rs | 61 |
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 @@ | |||
1 | extern crate telegram_bot; | ||
2 | use telegram_bot::{Api, Message, MessageType}; | ||
3 | use std::sync::{Arc, Mutex}; | ||
4 | use std::thread; | ||
5 | use std::time::Duration; | ||
6 | |||
7 | use std::ascii::AsciiExt; | ||
8 | |||
9 | use feature::FeatureResult; | ||
10 | use feature::Feature; | ||
11 | |||
12 | pub struct Jisoku { | ||
13 | running: bool, | ||
14 | } | ||
15 | |||
16 | const 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 | |||
22 | impl 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 | |||
50 | impl 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 @@ | |||
1 | use std::boxed::Box; | ||
2 | use std::error::Error; | ||
3 | |||
4 | extern crate telegram_bot; | ||
5 | use telegram_bot::{Api, Message}; | ||
6 | |||
7 | pub mod jisoku; | ||
8 | pub mod tasterank; | ||
9 | |||
10 | pub enum FeatureResult { | ||
11 | Handled, | ||
12 | Skip, | ||
13 | } | ||
14 | |||
15 | pub 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 | |||
21 | pub 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 @@ | |||
1 | use std::option::Option; | ||
2 | use std::io; | ||
3 | use std::io::prelude::*; | ||
4 | use std::fs::File; | ||
5 | |||
6 | use std::ascii::AsciiExt; | ||
7 | |||
8 | extern crate telegram_bot; | ||
9 | use telegram_bot::{Api, Message, MessageType}; | ||
10 | |||
11 | use feature::FeatureResult; | ||
12 | use feature::Feature; | ||
13 | |||
14 | pub struct Tasterank { | ||
15 | ranks: Vec<String>, | ||
16 | } | ||
17 | |||
18 | impl 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 | |||
43 | impl 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 | } | ||