author | alfadur |
Sat, 11 Jul 2020 23:06:29 +0300 | |
changeset 15742 | b743adf298d8 |
parent 14526 | ba29aa03db87 |
permissions | -rw-r--r-- |
14524
831ecafd74c6
Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff
changeset
|
1 |
use crate::{MessageChecker, Severity}; |
831ecafd74c6
Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff
changeset
|
2 |
|
14526 | 3 |
use itertools::Itertools; |
4 |
use std::marker::PhantomData; |
|
5 |
||
6 |
struct LetterRepeatChecker<T> { |
|
7 |
threshold: usize, |
|
8 |
player_id_type: PhantomData<T>, |
|
9 |
} |
|
14524
831ecafd74c6
Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff
changeset
|
10 |
|
14526 | 11 |
impl<T> LetterRepeatChecker<T> { |
12 |
pub fn new(threshold: usize) -> Self { |
|
13 |
Self { |
|
14 |
threshold, |
|
15 |
player_id_type: PhantomData, |
|
16 |
} |
|
17 |
} |
|
18 |
} |
|
19 |
||
20 |
impl<T> MessageChecker<T> for LetterRepeatChecker<T> { |
|
21 |
fn check(&self, _player_id: T, message: &str) -> Severity { |
|
22 |
for (_key, group) in &message.chars().into_iter().group_by(|c| *c) { |
|
23 |
if group.count() >= self.threshold { |
|
24 |
return Severity::Warn; |
|
25 |
} |
|
26 |
} |
|
27 |
||
14524
831ecafd74c6
Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff
changeset
|
28 |
Severity::Pass |
831ecafd74c6
Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff
changeset
|
29 |
} |
831ecafd74c6
Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff
changeset
|
30 |
} |
14526 | 31 |
|
32 |
#[cfg(test)] |
|
33 |
mod tests { |
|
34 |
use super::*; |
|
35 |
#[test] |
|
36 |
fn it_works() { |
|
37 |
let checker = LetterRepeatChecker::new(3); |
|
38 |
assert_eq!(checker.check(0, "Hello world!"), Severity::Pass); |
|
39 |
assert_eq!(checker.check(0, "ooops"), Severity::Warn); |
|
40 |
assert_eq!( |
|
41 |
checker.check(0, "жираф - длинношеее животное"), |
|
42 |
Severity::Warn |
|
43 |
); |
|
44 |
} |
|
45 |
} |