From 0720a83bcdc22684c68759755afb007579e0ebcc Mon Sep 17 00:00:00 2001 From: jan Date: Thu, 7 Apr 2016 14:30:25 +0200 Subject: keine warnings beim compilen mehr. Jisoku kann jetzt auch mehrfach verwendet werden anstatt fuer die Ewigkeit geblockt zu sein. 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 @@ extern crate telegram_bot; use telegram_bot::{Api, Message, MessageType}; -use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; - -use std::ascii::AsciiExt; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; use feature::FeatureResult; use feature::Feature; pub struct Jisoku { - running: bool, + running: Arc, } const MESSAGES: [&'static str; 5] = ["JISOKU METER FURIKIRI UBAU GAME", @@ -33,15 +32,24 @@ impl Feature for Jisoku { return Ok(FeatureResult::Skip); } - if self.running { + if self.running.compare_and_swap(false, true, Ordering::SeqCst) { return Ok(FeatureResult::Skip); } - self.running = true; + let running = self.running.clone(); thread::spawn(move || { for s in MESSAGES.into_iter() { - a.send_message(m.chat.id(), (*s).to_owned(), None, None, None, None); + if let Err(e) = a.send_message(m.chat.id(), + (*s).to_owned(), + None, + None, + None, + None) { + println!("{}", e); + break; + }; thread::sleep(Duration::new(1, 0)); } + running.store(false, Ordering::SeqCst); }); Ok(FeatureResult::Handled) } @@ -49,7 +57,6 @@ impl Feature for Jisoku { impl Jisoku { pub fn new() -> Jisoku { - let mut j = Jisoku { running: false }; - j + Jisoku { running: Arc::new(AtomicBool::new(false)) } } } 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 @@ use std::boxed::Box; -use std::error::Error; extern crate telegram_bot; use 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 @@ -use std::option::Option; use std::io; use std::io::prelude::*; use std::fs::File; @@ -33,7 +32,9 @@ impl Feature for Tasterank { for (i, rank) in self.ranks.iter().enumerate() { msg.push_str(&format!("{}. {}\n", i + 1, rank)); } - a.send_message(m.chat.id(), msg, None, None, Some(m.message_id), None); + if let Err(e) = a.send_message(m.chat.id(), msg, None, None, Some(m.message_id), None) { + println!("{}", e); + } return Ok(FeatureResult::Handled); } 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 @@ extern crate telegram_bot; -use telegram_bot::{Api, ListeningMethod, ListeningAction, Update}; +use telegram_bot::{Api, ListeningMethod, ListeningAction}; mod feature; use feature::{Feature, FeatureResult}; -- cgit v0.10.1