aboutsummaryrefslogtreecommitdiff
path: root/src/feature/jisoku.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/feature/jisoku.rs')
-rw-r--r--src/feature/jisoku.rs25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/feature/jisoku.rs b/src/feature/jisoku.rs
index 70bc886..8f88e81 100644
--- a/src/feature/jisoku.rs
+++ b/src/feature/jisoku.rs
@@ -1,16 +1,15 @@
1extern crate telegram_bot; 1extern crate telegram_bot;
2use telegram_bot::{Api, Message, MessageType}; 2use telegram_bot::{Api, Message, MessageType};
3use std::sync::{Arc, Mutex};
4use std::thread; 3use std::thread;
5use std::time::Duration; 4use std::time::Duration;
6 5use std::sync::Arc;
7use std::ascii::AsciiExt; 6use std::sync::atomic::{AtomicBool, Ordering};
8 7
9use feature::FeatureResult; 8use feature::FeatureResult;
10use feature::Feature; 9use feature::Feature;
11 10
12pub struct Jisoku { 11pub struct Jisoku {
13 running: bool, 12 running: Arc<AtomicBool>,
14} 13}
15 14
16const MESSAGES: [&'static str; 5] = ["JISOKU METER FURIKIRI UBAU GAME", 15const MESSAGES: [&'static str; 5] = ["JISOKU METER FURIKIRI UBAU GAME",
@@ -33,15 +32,24 @@ impl Feature for Jisoku {
33 return Ok(FeatureResult::Skip); 32 return Ok(FeatureResult::Skip);
34 } 33 }
35 34
36 if self.running { 35 if self.running.compare_and_swap(false, true, Ordering::SeqCst) {
37 return Ok(FeatureResult::Skip); 36 return Ok(FeatureResult::Skip);
38 } 37 }
39 self.running = true; 38 let running = self.running.clone();
40 thread::spawn(move || { 39 thread::spawn(move || {
41 for s in MESSAGES.into_iter() { 40 for s in MESSAGES.into_iter() {
42 a.send_message(m.chat.id(), (*s).to_owned(), None, None, None, None); 41 if let Err(e) = a.send_message(m.chat.id(),
42 (*s).to_owned(),
43 None,
44 None,
45 None,
46 None) {
47 println!("{}", e);
48 break;
49 };
43 thread::sleep(Duration::new(1, 0)); 50 thread::sleep(Duration::new(1, 0));
44 } 51 }
52 running.store(false, Ordering::SeqCst);
45 }); 53 });
46 Ok(FeatureResult::Handled) 54 Ok(FeatureResult::Handled)
47 } 55 }
@@ -49,7 +57,6 @@ impl Feature for Jisoku {
49 57
50impl Jisoku { 58impl Jisoku {
51 pub fn new() -> Jisoku { 59 pub fn new() -> Jisoku {
52 let mut j = Jisoku { running: false }; 60 Jisoku { running: Arc::new(AtomicBool::new(false)) }
53 j
54 } 61 }
55} 62}