author | Wuzzy <Wuzzy2@mail.ru> |
Tue, 14 May 2019 22:42:19 +0200 | |
changeset 14953 | f27f00fabc0d |
parent 14510 | ba29aa03db87 |
permissions | -rw-r--r-- |
14508
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 |
|
14510 | 3 |
use itertools::Itertools; |
4 |
use std::marker::PhantomData; |
|
5 |
||
6 |
struct LetterRepeatChecker<T> { |
|
7 |
threshold: usize, |
|
8 |
player_id_type: PhantomData<T>, |
|
9 |
} |
|
14508
831ecafd74c6
Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff
changeset
|
10 |
|
14510 | 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 |
||
14508
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 |
} |
14510 | 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 |
} |