aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/feature/jisoku.rs25
-rw-r--r--src/feature/mod.rs1
-rw-r--r--src/feature/tasterank.rs5
-rw-r--r--src/main.rs2
4 files changed, 20 insertions, 13 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}
diff --git a/src/feature/mod.rs b/src/feature/mod.rs
index 5e00620..a52bb2c 100644
--- a/src/feature/mod.rs
+++ b/src/feature/mod.rs
@@ -1,5 +1,4 @@
1use std::boxed::Box; 1use std::boxed::Box;
2use std::error::Error;
3 2
4extern crate telegram_bot; 3extern crate telegram_bot;
5use telegram_bot::{Api, Message}; 4use telegram_bot::{Api, Message};
diff --git a/src/feature/tasterank.rs b/src/feature/tasterank.rs
index c4a29f9..13730e1 100644
--- a/src/feature/tasterank.rs
+++ b/src/feature/tasterank.rs
@@ -1,4 +1,3 @@
1use std::option::Option;
2use std::io; 1use std::io;
3use std::io::prelude::*; 2use std::io::prelude::*;
4use std::fs::File; 3use std::fs::File;
@@ -33,7 +32,9 @@ impl Feature for Tasterank {
33 for (i, rank) in self.ranks.iter().enumerate() { 32 for (i, rank) in self.ranks.iter().enumerate() {
34 msg.push_str(&format!("{}. {}\n", i + 1, rank)); 33 msg.push_str(&format!("{}. {}\n", i + 1, rank));
35 } 34 }
36 a.send_message(m.chat.id(), msg, None, None, Some(m.message_id), None); 35 if let Err(e) = a.send_message(m.chat.id(), msg, None, None, Some(m.message_id), None) {
36 println!("{}", e);
37 }
37 return Ok(FeatureResult::Handled); 38 return Ok(FeatureResult::Handled);
38 } 39 }
39 Ok(FeatureResult::Skip) 40 Ok(FeatureResult::Skip)
diff --git a/src/main.rs b/src/main.rs
index eaec1f4..ef5c8e8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,5 @@
1extern crate telegram_bot; 1extern crate telegram_bot;
2use telegram_bot::{Api, ListeningMethod, ListeningAction, Update}; 2use telegram_bot::{Api, ListeningMethod, ListeningAction};
3 3
4mod feature; 4mod feature;
5use feature::{Feature, FeatureResult}; 5use feature::{Feature, FeatureResult};