aboutsummaryrefslogtreecommitdiff
path: root/src/game.rs
blob: 431776e8624d63635b29e6459ba9a00402a5c550 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use std::iter;

use failure::err_msg;
use fnv::FnvHashMap;
use itertools::Itertools;
use serenity::{
    framework::standard::{
        Args,
        StandardFramework,
    },
    model::{
        channel::Message,
        id::UserId,
    },
    prelude::*,
};
use url::Url;

use crate::{
    commands::send,
    must_env_lookup,
    Result,
    VOICE_CHANNEL_ID,
};

lazy_static! {
    static ref SHEETS_API_KEY: String = must_env_lookup("SHEETS_API_KEY");
    static ref SPREADSHEET_ID: String = must_env_lookup("SPREADSHEET_ID");
    static ref MAX_SHEET_COLUMN: String = must_env_lookup("MAX_SHEET_COLUMN");
}

pub fn register(s: StandardFramework) -> StandardFramework {
    s
        .command("game", |c| c
            .known_as("gaem")
            .known_as("installedgaem")
            .known_as("installedgame")
            .desc("what game should we play?")
            .exec(installedgame)
        )
        .command("ownedgame", |c| c
            .known_as("ownedgaem")
            .desc("what games does everyone have?")
            .exec(ownedgame)
        )
}

lazy_static! {
    static ref USER_MAP: FnvHashMap<UserId, String> = {
        use serde_json::Value;
        use std::str;

        let map_bytes = include_bytes!("../user_id_mapping.json");

        let v: Value = serde_json::from_str(str::from_utf8(&map_bytes[..]).unwrap()).unwrap();
        match v {
            Value::Object(m) => {
                m.iter()
                    .map(|(k, v)| match v {
                         Value::Number(n) => (UserId(n.as_u64().unwrap()), k.clone()),
                         _ => panic!("non-number in user id mapping"),
                    })
                    .collect()
            },
            _ => panic!("couldn't read user id mapping"),
        }
    };
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd)]
enum GameStatus {
    Installed,
    NotInstalled,
    NotOwned,
    Unknown,
}

fn installedgame(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> {
    game(ctx, msg, args, GameStatus::Installed)
}

fn ownedgame(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> {
    game(ctx, msg, args, GameStatus::NotInstalled)
}

fn game(_ctx: &mut Context, msg: &Message, args: Args, min_status: GameStatus) -> Result<()> {
    use fnv::{
        FnvHashMap,
        FnvHashSet,
    };

    let guild = msg.channel_id.to_channel()?
        .guild()
        .ok_or(err_msg("couldn't find guild"))?;

    let guild = guild.read()
        .guild()
        .ok_or(err_msg("couldn't find guild"))?;

    let guild = guild
        .read();

    let user_args = if args.rest().is_empty() {
        Vec::new()
    } else {
        args.multiple_quoted::<String>()?
    };

    let mut users = user_args
        .into_iter()
        .map(|u| u.trim_start_matches("@").to_owned())
        .filter_map(|u| {
            let mut possible = guild.members_nick_containing(&u, false, false);
            possible.extend(guild.members_username_containing(&u, false, false));

            let possible = possible.into_iter()
                .map(|member| member.user_id())
                .collect::<FnvHashSet<_>>();

            match possible.len() {
                0 => {
                    let _ = send(msg.channel_id, &format!("didn't recognize {}", u), msg.tts);
                    None
                },
                1 => Some(possible.into_iter().next().unwrap()),
                x => {
                    let _ = send(msg.channel_id, &format!("too many matches ({}) for {}", x, u), msg.tts);
                    None
                },
            }
        })
        .filter_map(|uid| {
            let res = USER_MAP.get(&uid).map(|s| s.to_lowercase());

            if let None = res {
                let _ = info!("user {} is not recognized", uid);
            }

            res
        })
        .collect::<FnvHashSet<_>>();

    let inferred = users.len() == 0;

    if users.len() == 0 {
        let pairs = guild
            .voice_states
            .iter()
            .filter_map(|(uid, voice)| {
                voice.channel_id.map(|cid| (*uid, cid))
            })
            .collect::<FnvHashMap<_, _>>();

        let channel = pairs.get(&msg.author.id).unwrap_or(&*VOICE_CHANNEL_ID);

        users = pairs
            .iter()
            .filter_map(|(uid, cid)| {
                if cid == channel {
                    USER_MAP.get(uid).map(|s| s.to_lowercase())
                } else { None }
            })
            .collect::<FnvHashSet<_>>();
    }

    if inferred && users.len() < 2 || !inferred && users.len() < 1 {
        info!("too few known users to make game comparison");
        send(msg.channel_id, "yer too lonely", msg.tts)?;
        return Ok(());
    }

    let mut u = Url::parse(
        &format!("https://sheets.googleapis.com/v4/spreadsheets/{}/values:batchGet", *SPREADSHEET_ID))?;

    u.query_pairs_mut()
        .append_pair("ranges", &format!("a1:{}", &*MAX_SHEET_COLUMN))
        .append_pair("valueRenderOption", "FORMATTED_VALUE")
        .append_pair("majorDimension", "COLUMNS")
        .append_pair("key", &*SHEETS_API_KEY);

    let req = reqwest::Request::new(reqwest::Method::GET, u);

    let client = reqwest::Client::new();

    let mut resp = client.execute(req)?;

    #[derive(Deserialize)]
    struct Resp {
        #[serde(rename = "valueRanges")]
        value_ranges: Vec<Inner>,
    }

    #[derive(Deserialize)]
    struct Inner {
        values: Vec<Vec<String>>,
    }

    let data = &resp.json::<Resp>()?.value_ranges[0].values;

    let user_indexes = (0..data.len())
        .filter_map(|i| {
            let user = data[i][0].to_lowercase();

            if users.contains(&user) {
                Some((user, i))
            } else { None }
        })
        .collect::<FnvHashMap<_, _>>();

    let user_games = user_indexes
        .iter()
        .map(|(user, col)| {
            let empty_hash_set: FnvHashSet<_> = vec![].into_iter().collect();

            let mut game_map = vec! [
                (GameStatus::Installed, empty_hash_set.clone()),
                (GameStatus::NotInstalled, empty_hash_set.clone()),
                (GameStatus::NotOwned, empty_hash_set.clone()),
                (GameStatus::Unknown, empty_hash_set),
            ]
                .into_iter()
                .collect::<FnvHashMap<_, _>>();

            (1..data[*col].len())
                .for_each(|i| {
                    let status = &data[*col][i];

                    let game = &data[0][i];
                    if status.starts_with("y") {
                        game_map.get_mut(&GameStatus::Installed).unwrap().insert(game);
                    } else if status.starts_with("n/i") {
                        game_map.get_mut(&GameStatus::NotInstalled).unwrap().insert(game);
                    } else if status.starts_with("n") {
                        game_map.get_mut(&GameStatus::NotOwned).unwrap().insert(game);
                    } else {
                        game_map.get_mut(&GameStatus::Unknown).unwrap().insert(game);
                    }
                });

            (user, game_map)
        })
        .collect::<FnvHashMap<_, _>>();

    let statuses = vec![GameStatus::Installed, GameStatus::NotOwned, GameStatus::NotInstalled, GameStatus::Unknown]
        .into_iter()
        .filter(|s| s <= &min_status)
        .collect::<Vec<_>>();

    let mut games_in_common = {
        let game_map = user_games.values().nth(0).unwrap();

        statuses
            .iter()
            .fold(iter::empty().collect::<FnvHashSet<_>>(), |acc, s| {
                acc.union(&game_map[s]).cloned().collect()
            })
    };

    for (_user, game_map) in user_games.iter() {
        let relevant_games = statuses
            .iter()
            .fold(iter::empty().collect::<FnvHashSet<_>>(), |acc, s| {
                acc.union(&game_map[s]).cloned().collect()
            });

        games_in_common = games_in_common.intersection(&relevant_games).cloned().collect();
    }

    let mut games_formatted = games_in_common.iter().sorted_by(|a, b| {
        a.to_lowercase().cmp(&b.to_lowercase())
    }).join("\n");

    if games_formatted.is_empty() {
        games_formatted = "**LITERALLY NOTHING**".to_owned();
    }

    send(msg.channel_id, &games_formatted, msg.tts)
}