rust/chat_sanitizer/src/letter_repeat.rs
author Wuzzy <Wuzzy2@mail.ru>
Sun, 07 Apr 2019 19:26:16 +0200
changeset 14755 ab7bf5036314
parent 14510 ba29aa03db87
permissions -rw-r--r--
Render mine timer if gear info is active
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
     3
use itertools::Itertools;
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
     4
use std::marker::PhantomData;
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
     5
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
     6
struct LetterRepeatChecker<T> {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
     7
    threshold: usize,
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
     8
    player_id_type: PhantomData<T>,
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
     9
}
14508
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    10
14510
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    11
impl<T> LetterRepeatChecker<T> {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    12
    pub fn new(threshold: usize) -> Self {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    13
        Self {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    14
            threshold,
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    15
            player_id_type: PhantomData,
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    16
        }
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    17
    }
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    18
}
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    19
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    20
impl<T> MessageChecker<T> for LetterRepeatChecker<T> {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    21
    fn check(&self, _player_id: T, message: &str) -> Severity {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    22
        for (_key, group) in &message.chars().into_iter().group_by(|c| *c) {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    23
            if group.count() >= self.threshold {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    24
                return Severity::Warn;
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    25
            }
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    26
        }
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    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
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    31
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    32
#[cfg(test)]
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    33
mod tests {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    34
    use super::*;
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    35
    #[test]
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    36
    fn it_works() {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    37
        let checker = LetterRepeatChecker::new(3);
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    38
        assert_eq!(checker.check(0, "Hello world!"), Severity::Pass);
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    39
        assert_eq!(checker.check(0, "ooops"), Severity::Warn);
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    40
        assert_eq!(
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    41
            checker.check(0, "жираф - длинношеее животное"),
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    42
            Severity::Warn
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    43
        );
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    44
    }
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14508
diff changeset
    45
}