thrustin 1.0.1

xperience epic adventures by THRUSTING some THRUSTERS into THRUSTEES https://THRUSTIN.rs. E
Documentation
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use argon2;
use chrono::{DateTime, Duration, TimeZone, Utc};
use mongodb::coll::Collection;
use mongodb::db::ThreadedDatabase;
use mongodb::{bson, doc, Array, Bson, Client, Document, ThreadedClient};
use rand::Rng;
use std::collections::HashMap;

#[derive(Debug)]
pub struct Database {
    pub bans: Collection,
    pub users: Collection,
    // Cache bans so we don't have to constantly load from database for each call
    bans_cache: HashMap<String, (i64, DateTime<Utc>)>,
    config: argon2::Config<'static>,
}

impl Database {
    fn find_ban_doc(&self, ip_addr: &str) -> Option<Document> {
        let doc = doc! {
            "ip_addr": ip_addr
        };
        let mut cursor = self
            .bans
            .find(Some(doc.clone()), None)
            .ok()
            .expect("Failed to find ban");
        // Return doc if found, otherwise None
        match cursor.next() {
            Some(Ok(doc)) => Some(doc),
            Some(Err(_)) => None,
            None => None,
        }
    }

    fn find_name_doc(&self, name: &str) -> Option<Document> {
        let doc = doc! {
            "name": name
        };
        let mut cursor = self
            .users
            .find(Some(doc.clone()), None)
            .ok()
            .expect("Failed to find name");
        // Return doc if found, otherwise None
        match cursor.next() {
            Some(Ok(doc)) => Some(doc),
            Some(Err(_)) => None,
            None => None,
        }
    }

    fn find_user_doc(&self, user: &str) -> Option<Document> {
        let doc = doc! {
            "user": user
        };
        let mut cursor = self
            .users
            .find(Some(doc.clone()), None)
            .ok()
            .expect("Failed to find user");
        // Return doc if found, otherwise None
        match cursor.next() {
            Some(Ok(doc)) => Some(doc),
            Some(Err(_)) => None,
            None => None,
        }
    }

    fn hash_password(&self, pass: &str) -> String {
        let mut rng = rand::thread_rng();
        // Damn this was hard
        let salt: Vec<u8> = (0..32).map(|_| rng.gen()).collect();
        let hash = argon2::hash_encoded(pass.as_bytes(), &salt, &self.config)
            .expect("Failed to hash password");
        String::from(hash)
    }

    fn load_bans(&mut self) {
        let cursor = self
            .bans
            .find(Some(doc! {}), None)
            .ok()
            .expect("Failed to load bans");

        self.bans_cache = HashMap::new();
        for doc in cursor {
            if let Ok(doc) = doc {
                let ip_addr = if let Ok(ip_addr) = doc.get_str("ip_addr") {
                    ip_addr.clone()
                } else {
                    ""
                };

                let duration = if let Ok(duration) = doc.get_i64("duration") {
                    duration.clone()
                } else {
                    0
                };

                let end = if let Ok(end) = doc.get_utc_datetime("end") {
                    end.clone()
                } else {
                    Utc.timestamp(0, 0)
                };

                self.bans_cache
                    .insert(String::from(ip_addr), (duration, end));
            }
        }
    }

    fn verify_password(&self, hash: &str, pass: &str) -> bool {
        let matches =
            argon2::verify_encoded(hash, pass.as_bytes()).expect("Failed to verify password");
        matches
    }

    fn strings_to_bson_array(strings: Vec<String>) -> Array {
        let mut array = Vec::new();
        for string in strings {
            array.push(Bson::String(string));
        }
        array
    }

    pub fn account(&self, name: &str) -> Vec<String> {
        let mut messages = Vec::new();
        match self.find_name_doc(&name) {
            Some(doc) => {
                messages.push(String::from("A display of your account information and statistical information. Please enjoy THRUSTIN!"));
                if let Some(Bson::String(user)) = doc.get("user") {
                    messages.push(format!("Username - {}", user));
                }
                messages.push(format!("Name - {}", name));
                messages.push(String::from("Password - [ENCRYPTED_CONTENT__UNVIEWABLE]"));
                if let Some(points) = doc.get("points_gained") {
                    messages.push(format!("Points Earned So Far - {}", points));
                } else {
                    messages.push(String::from("Pointed Earned - 0"));
                }
                if let Some(games) = doc.get("games_played") {
                    messages.push(format!("Games Played So Far - {}", games));
                } else {
                    messages.push(String::from("Games Played So Far - 0"));
                }
                if let Some(games) = doc.get("games_won") {
                    messages.push(format!("Games Won So Far - {}", games));
                } else {
                    messages.push(String::from("Games Won So Far - 0"));
                }
				if let Some(&Bson::I32(level)) = doc.get("level") {
					if let Some(exp) = doc.get("exp") {
						messages.push(format!("Level - {}", level));
						let exponent: f32 = 2.15;
						let exp_needed = (level as f32).powf(exponent).round() as i32;
						messages.push(format!("Experience - {}/{}", exp, exp_needed));
					} else {
						messages.push(String::from("Level - 1"));
						messages.push(String::from("Experience - 0/1"));
					}
				} else {
					messages.push(String::from("Level - 1"));
					messages.push(String::from("Experience - 0/1"));
				}
            }
            None => {
                messages.push(String::from("Yo there's a bit of an epic problem. We couldn't find your account data lmao. What is going on."));
            }
        }
        messages
    }

    pub fn ban(&mut self, ip_addr: &str) -> bool {
        let doc = self.find_ban_doc(&ip_addr);
        match doc {
            // If ban exists, update time
            Some(doc) => {
                let duration = if let Ok(duration) = doc.get_i64("ip_addr") {
                    duration.clone()
                } else {
                    3600
                };
                let end = Utc::now() + Duration::seconds(duration);
                let update = doc! {
                    "$set": {
                        "duration": duration,
                        "end": end,
                    }
                };
                match self.bans.update_one(doc, update, None) {
                    Ok(_) => {
                        self.load_bans();
                        true
                    }
                    _ => false,
                }
            }
            // If ban doesn't exist, add new ban
            None => {
                // in seconds, starting at 1 hour
                let duration = 3600;
                let end = Utc::now() + Duration::seconds(duration);
                let doc = doc! {
                    "ip_addr": ip_addr,
                    "duration": duration,
                    "end": end,
                };
                match self.bans.insert_one(doc.clone(), None) {
                    Ok(_) => {
                        self.load_bans();
                        true
                    }
                    _ => false,
                }
            }
        }
    }

    pub fn bans(&self) -> Vec<String> {
        let mut messages = vec![String::from("Banned fellows from this server. Kill'em.")];
        let mut bans = Vec::new();
        for (ip_addr, (duration, end)) in self.bans_cache.clone() {
            bans.push(format!("{} {} {}", ip_addr, duration, end));
        }
        bans.sort_unstable_by(|a, b| a.cmp(&b));
        messages.append(&mut bans);
        messages
    }

    pub fn bson_array_to_strings(array: Array) -> Vec<String> {
        let mut strings = Vec::new();
        for bson in array {
            if let Bson::String(bson_string) = bson {
                strings.push(bson_string);
            }
        }
        strings
    }

    // lol, this appoints a chieftain
    pub fn chieftain(&self, name: &str) -> bool {
        // Only set chieftain if it's an actual database bro
        if let Some(_) = self.find_name_doc(&name) {
            let filter = doc! {
                "name": name
            };
            let update = doc! {
                "$set": {
                    "is_chieftain": true
                }
            };
            self.users
                .update_one(filter, update, None)
                .expect("Failed to update chieftain");
            true
        } else {
            false
        }
    }

    // Shows a list of chieftains
    pub fn chieftains(&self) -> Vec<String> {
        let mut messages = Vec::new();
        let doc = doc! {
            "is_chieftain": true
        };
        let cursor = self
            .users
            .find(Some(doc.clone()), None)
            .ok()
            .expect("Failed to find chieftains");

        messages.push(String::from("A LIST OF CHIEFTAINS RESPONSIBLE FOR MANAGEMENT OF THIS THRUSTIN SERVER IS AS FOLLOWS."));
        let mut chieftains = Vec::new();
        for doc in cursor {
            if let Ok(doc) = doc {
                if let Ok(name) = doc.get_str("name") {
                    chieftains.push(String::from(name));
                }
            }
        }
        chieftains.sort_unstable_by(|a, b| a.cmp(&b));
        messages.append(&mut chieftains);
        messages
    }

    pub fn color(&self, name: &str, bg: &str, fg: &str) -> bool {
        let filter = doc! {
            "name": name
        };
        let update = doc! {
            "$set": {
                "bg": bg,
                "fg": fg,
            }
        };
        match self.users.update_one(filter, update, None) {
            Ok(_) => true,
            Err(_) => false,
        }
    }

    pub fn does_name_exist(&self, name: &str) -> bool {
        let doc = doc! {
            "name": name
        };
        let mut cursor = self
            .users
            .find(Some(doc.clone()), None)
            .ok()
            .expect("Failed to find login");
        // bool for found
        match cursor.next() {
            Some(Ok(_)) => true,
            Some(Err(_)) => false,
            None => false,
        }
    }

    pub fn is_chieftain(&self, name: &str) -> bool {
        if let Some(doc) = self.find_name_doc(&name) {
            if let Some(&Bson::Boolean(is_chieftain)) = doc.get("is_chieftain") {
                is_chieftain
            } else {
                false
            }
        } else {
            false
        }
    }

    pub fn is_banned(&self, ip_addr: &str) -> Option<&(i64, DateTime<Utc>)> {
        if self.bans_cache.contains_key(&String::from(ip_addr)) {
            self.bans_cache.get(&String::from(ip_addr))
        } else {
            None
        }
    }

    pub fn login(&self, user: &str, pass: &str) -> Option<Document> {
        let doc = doc! {
            "user": user,
        };
        let mut cursor = self
            .users
            .find(Some(doc.clone()), None)
            .ok()
            .expect("Failed to find user");
        let item = cursor.next();
        // Return doc if found, otherwise None
        match item {
            Some(Ok(doc)) => match doc.get_str("pass") {
                Ok(hash) => {
                    if self.verify_password(hash, pass) {
                        Some(doc)
                    } else {
                        None
                    }
                }
                _ => None,
            },
            Some(Err(_)) => None,
            None => None,
        }
    }

    pub fn name(&self, old_name: &str, new_name: &str) -> bool {
        let filter = doc! {
            "name": old_name
        };
        let update = doc! {
            "$set": {
                "name": new_name
            }
        };
        match self.users.update_one(filter, update, None) {
            Ok(_) => true,
            Err(_) => false,
        }
    }

    pub fn new(db_name: &str) -> Database {
        let client =
            Client::connect("localhost", 27017).expect("Failed to initialize database client");
        let db = client.db(db_name);
        let bans = db.collection("bans");
        let users = db.collection("users");
        let config = argon2::Config::default();

        let mut db = Database {
            bans,
            users,
            config,
            bans_cache: HashMap::new(),
        };
        db.load_bans();
        db
    }

    pub fn password(&self, name: &str, pass: &str) -> bool {
        let filter = doc! {
            "name": name
        };
        let hash = self.hash_password(pass);
        let update = doc! {
            "$set": {
                "pass": &hash
            }
        };
        match self.users.update_one(filter, update, None) {
            Ok(_) => true,
            Err(_) => false,
        }
    }

    // Whenever user joins or starts game
    pub fn up_games_played(&self, name: &str) {
        if let Some(doc) = self.find_name_doc(&name) {
            if let Some(&Bson::I32(games)) = doc.get("games_played") {
                let filter = doc! {
                    "name": name
                };
                let update = doc! {
                    "$set": {
                        "games_played": games + 1
                    }
                };
                self.users
                    .update_one(filter, update, None)
                    .expect("Failed to update games played");
            }
        }
    }

    // Whenever user wins game
    pub fn up_games_won(&self, name: &str) {
        if let Some(doc) = self.find_name_doc(&name) {
            if let Some(&Bson::I32(games)) = doc.get("games_won") {
                let filter = doc! {
                    "name": name
                };
                let update = doc! {
                    "$set": {
                        "games_won": games + 1
                    }
                };
                self.users
                    .update_one(filter, update, None)
                    .expect("Failed to update games won");
            }
        }
    }

    // When user gets a point
    pub fn up_points_gained(&self, name: &str) {
        if let Some(doc) = self.find_name_doc(&name) {
            if let Some(&Bson::I32(points)) = doc.get("points_gained") {
                let filter = doc! {
                    "name": name
                };
                let update = doc! {
                    "$set": {
                        "points_gained": points + 1
                    }
                };
                self.users
                    .update_one(filter, update, None)
                    .expect("Failed to update points gained");
            }
        }
    }

	// Whenever a player is in a lobby that has ended, winner or not
	pub fn up_exp(&self, name: &str, exp_gained: i32) {
		if let Some(doc) = self.find_name_doc(&name) {
            if let Some(&Bson::I32(exp)) = doc.get("exp") {
                let filter = doc! {
                    "name": name
                };
                let update = doc! {
                    "$set": {
                        "exp": exp + exp_gained
                    }
                };
                self.users
                    .update_one(filter, update, None)
                    .expect("Failed to update exp");
            }
        }
	}

	// When a player gains enough EXP to level up, level is increased and EXP decreases by amount required to level up
	pub fn up_level(&self, name: &str, exp_to_level: i32) {
        if let Some(doc) = self.find_name_doc(&name) {
			if let Some(&Bson::I32(exp)) = doc.get("exp") {
				if let Some(&Bson::I32(level)) = doc.get("level") {
					let filter = doc! {
						"name": name
					};
					let update = doc! {
						"$set": {
							"level": level + 1,
							"exp": exp - exp_to_level
						}
					};
					self.users
						.update_one(filter, update, None)
						.expect("Failed to update level");
				}
			}
        }
    }

    pub fn register(&self, user: &str, pass: &str) -> bool {
        if self.find_user_doc(user).is_some() {
            return false;
        }
        let hash = self.hash_password(pass);
        let doc = doc! {
            "bg": "b7410e",
            "fg": "000",
            "user": user,
            "pass": &hash,
            "name": user,
            "points_gained": 0,
            "games_played": 0,
            "games_won": 0,
            "is_chieftain": false,
			"exp": 0,
			"level": 1,
        };
        match self.users.insert_one(doc.clone(), None) {
            Ok(_) => true,
            Err(_) => false,
        }
    }

    // For testing purposes
    pub fn register_chieftain(&self) -> bool {
        let hash = self.hash_password("chieftain");
        let doc = doc! {
            "user": "chieftain",
            "pass": &hash,
            "name": "chieftain",
            "points_gained": 0,
            "games_played": 0,
            "games_won": 0,
			"level": 1,
			"exp": 0,
            "is_chieftain": true
        };
        match self.users.insert_one(doc.clone(), None) {
            Ok(_) => true,
            Err(_) => false,
        }
    }

    pub fn unban(&mut self, ip_addr: &str) -> bool {
        let doc = self.find_ban_doc(&ip_addr);
        match doc {
            // Set ban time to now if found
            Some(doc) => {
                let end = Utc::now();
                let update = doc! {
                    "$set": {
                        "end": end,
                    }
                };
                match self.bans.update_one(doc, update, None) {
                    Ok(_) => {
                        self.load_bans();
                        true
                    }
                    _ => false,
                }
            }
            None => false,
        }
    }

    pub fn unchieftain(&self, name: &str) -> bool {
        // Return false if user doesn't exist
        if self.find_name_doc(name).is_none() {
            return false;
        }
        let filter = doc! {
            "name": name
        };
        let update = doc! {
            "$set": {
                "is_chieftain": false
            }
        };
        match self.users.update_one(filter, update, None) {
            Ok(_) => true,
            Err(_) => false,
        }
    }

    pub fn unthrust(&self, name: &str) -> bool {
        let filter = doc! {
            "name": name
        };
        let array: Array = Vec::new();
        let update = doc! {
            "$set": {
                "thrusters": array.clone(),
                "thrustees": array,
            }
        };
        match self.users.update_one(filter, update, None) {
            Ok(_) => true,
            Err(_) => false,
        }
    }

    pub fn username(&self, name: &str, user: &str) -> bool {
        let filter = doc! {
            "name": name
        };
        let update = doc! {
            "$set": {
                "user": user
            }
        };
        match self.users.update_one(filter, update, None) {
            Ok(_) => true,
            Err(_) => false,
        }
    }

    pub fn thrustees(&self, name: &str, thrustees: Vec<String>) -> bool {
        let filter = doc! {
            "name": name
        };
        let array = Database::strings_to_bson_array(thrustees);
        let update = doc! {
            "$set": {
                "thrustees": array
            }
        };
        match self.users.update_one(filter, update, None) {
            Ok(_) => true,
            Err(_) => false,
        }
    }

    pub fn thrusters(&self, name: &str, thrusters: Vec<String>) -> bool {
        let filter = doc! { "name": name };
        let array = Database::strings_to_bson_array(thrusters);
        let update = doc! {
            "$set": {
                "thrusters": array
            }
        };
        match self.users.update_one(filter, update, None) {
            Ok(_) => true,
            Err(_) => false,
        }
    }
}