From bd38d7f46a6b97b20ff5ef0c0c0c7ccd1e6b00d2 Mon Sep 17 00:00:00 2001 From: David Li Date: Thu, 28 Jan 2016 08:54:19 -0500 Subject: feat(emote): randomly choose appropriate emote --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ src/main.rs | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 5ba7bad..41f015c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,8 @@ version = "0.1.0" dependencies = [ "hyper 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "irc 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde_macros 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 2f1150b..eef1655 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,8 @@ authors = ["David Li "] [dependencies] hyper = "0.7.2" irc = "0.9.2" +lazy_static = "0.1.*" +rand = "0.3" serde = "*" serde_json = "*" serde_macros = "*" diff --git a/src/main.rs b/src/main.rs index 03f9e02..4abd4b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,9 @@ #![plugin(serde_macros)] extern crate hyper; extern crate irc; +#[macro_use] +extern crate lazy_static; +extern crate rand; extern crate serde; extern crate serde_json; @@ -15,6 +18,22 @@ use hyper::header::Connection; use irc::client::prelude::*; +use rand::{thread_rng, sample}; + +lazy_static! { + static ref EMOTE_HAPPY: Vec<&'static str> = vec![":D", ":)", "^_^"]; + + static ref EMOTES: HashMap<&'static str, &'static [&'static str]> = { + let mut m = HashMap::new(); + m.insert("happy", &(*EMOTE_HAPPY)[..]); + m.insert("smile", &(*EMOTE_HAPPY)[..]); + m.insert(":)", &(*EMOTE_HAPPY)[..]); + m.insert(":D", &(*EMOTE_HAPPY)[..]); + + m + }; +} + #[derive(Debug, PartialEq, Serialize, Deserialize)] struct WikiPage { ns: u32, @@ -66,6 +85,10 @@ fn emote_action(action: &str) -> Option<&str> { if action == "shrugs" { Some("¯\\_(ツ)_/¯") } + else if let Some(x) = EMOTES.get(action) { + let mut rng = thread_rng(); + Some(sample(&mut rng, *x, 1)[0]) + } else { None } -- cgit v1.2.3