use std::io; use std::io::prelude::*; use std::fs::File; use std::ascii::AsciiExt; extern crate telegram_bot; use telegram_bot::{Api, Message, MessageType}; extern crate time; use feature::FeatureResult; use feature::Feature; pub struct Tasterank { ranks: Vec, } impl Feature for Tasterank { fn name(&self) -> &'static str { "Tasterank" } fn init(&mut self) {} fn handle(&mut self, a: Api, m: Message) -> Result { if let MessageType::Text(s) = m.msg { if !(&s).to_ascii_lowercase().contains("luggas tasterank") { return Ok(FeatureResult::Skip); } let start = time::precise_time_ns(); if let Err(e) = self.update_ranks() { println!("{}", e); return Ok(FeatureResult::Skip); } println!("(benchm) updating ranks: {}ms", (time::precise_time_ns() - start) / 1000000); let mut msg = String::new(); let mut i = 1; for rank in &self.ranks { msg.push_str(&format!("{}. {}\n", i, rank)); i += 1; } println!("(benchm) tasterank before send: {}ms", (time::precise_time_ns() - start) / 1000000); if let Err(e) = a.send_message(m.chat.id(), msg, None, None, Some(m.message_id), None) { println!("{}", e); } println!("(benchm) tasterank: {}ms", (time::precise_time_ns() - start) / 1000000); return Ok(FeatureResult::Handled); } Ok(FeatureResult::Skip) } } impl Tasterank { pub fn new() -> Tasterank { let mut t = Tasterank { ranks: vec![] }; t.init(); t } fn update_ranks(&mut self) -> Result<(), io::Error> { let mut f = try!(File::open("data/tasterank.list")); let mut buf = String::new(); try!(f.read_to_string(&mut buf)); let temp: Vec<&str> = (&buf).split("\n").collect(); self.ranks.clear(); for r in &temp { self.ranks.push(r.to_string()); } self.ranks.pop().unwrap(); Ok(()) } }