Commit c2e48355 authored by Delorus's avatar Delorus
Browse files

Merge branch 'limit-time-for-user' into 'develop'

#32 Add command to set limittime for each user.

See merge request !48
parents b3f41f0a d5ab4e48
Pipeline #537 passed with stages
in 4 minutes and 8 seconds
package page.devnet.telegrambot;
import lombok.Setter;
import lombok.Value;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.methods.ParseMode;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
......@@ -12,18 +11,16 @@ import org.telegram.telegrambots.meta.api.objects.User;
import page.devnet.database.repository.UnsubscribeRepository;
import page.devnet.pluginmanager.Plugin;
import page.devnet.telegrambot.util.ChatDateTime;
import page.devnet.telegrambot.util.CommandUtils;
import page.devnet.telegrambot.util.ParserMessage;
import java.time.Duration;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* @author maksim
......@@ -32,6 +29,7 @@ import java.util.regex.Pattern;
@Slf4j
public class WordLimiterPlugin implements Plugin<Update, List<PartialBotApiMethod<?>>> {
@Override
public String getPluginId() {
return "limitPlug";
......@@ -40,6 +38,7 @@ public class WordLimiterPlugin implements Plugin<Update, List<PartialBotApiMetho
private static final Pattern WORD_PATTERN = Pattern.compile("\\w+", Pattern.UNICODE_CHARACTER_CLASS);
private final ConcurrentMap<String, WordCount> countWordsByUser = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, List<Integer>> limitSettingByUser = new ConcurrentHashMap<>();
private final UnsubscribeRepository unsubscribeRepository;
......@@ -48,9 +47,6 @@ public class WordLimiterPlugin implements Plugin<Update, List<PartialBotApiMetho
}
@Setter
private CommandUtils commandUtils = new CommandUtils();
@Override
public List<PartialBotApiMethod<?>> onEvent(Update event) {
if (!event.hasMessage() || !event.getMessage().hasText()) {
......@@ -58,8 +54,7 @@ public class WordLimiterPlugin implements Plugin<Update, List<PartialBotApiMetho
}
if (event.getMessage().isCommand()) {
executeCommand(event.getMessage());
return Collections.emptyList();
return executeCommand(event.getMessage());
}
Message message = event.getMessage();
......@@ -72,6 +67,7 @@ public class WordLimiterPlugin implements Plugin<Update, List<PartialBotApiMetho
int wordsCount = parseWords(message.getText()).size();
var wc = countWordsByUser.merge(formattedUserName, WordCount.ofCount(wordsCount), (old, next) -> {
var fromStartDayToNow = Duration.between(new ChatDateTime(next.timestamp).fromFixHoursTime(3), next.timestamp);
if (Duration.between(old.timestamp, next.timestamp).compareTo(fromStartDayToNow) >= 0) {
return next;
......@@ -80,20 +76,13 @@ public class WordLimiterPlugin implements Plugin<Update, List<PartialBotApiMetho
}
});
String msg = "";
if (wc.inRange(400)) {
msg = "0YHQtdCz0L7QtNC90Y8g0JLRiyDQtNC+0YHRgtCw0YLQvtGH0L3QviDQvdCw0L/QuNGB0LDQu9C4LCDQvtGC0LTQvtGF0L3QuNGC0LUu";
} else if (wc.inRange(600)) {
msg = "0JLRiyDRg9C20LUg0LTQvtCy0L7Qu9GM0L3QviDQvNC90L7Qs9C+INC90LDQv9C40YHQsNC70LgsINC80L7QttC10YIg0L/QvtGA0LAg0L7RgdGC0LDQvdC+0LLQuNGC0YzRgdGPPw==";
} else if (wc.inRange(1000)) {
msg = "0L/QvtGB0LvQtdC00L3QtdC1INC/0YDQtdC00YPQv9GA0LXQttC00LXQvdC40LUsINCS0Ysg0YPQttC1INC/0L7Rh9GC0Lgg0LTQvtGB0YLQuNCz0LvQuCDQv9C10YDQstC+0LPQviDQvNC10YHRgtCwINCyINC90LjQutGH0LXQvNC90L7RgdGC0LgsINGF0LLQsNGC0LjRgi4=";
} else {
int limit = 1300;
if (wc.inRange(limit) || wc.count > limit && (wc.count - wc.prevCount >= 10)) {
msg = "KirQntCh0KLQkNCd0J7QktCY0KHQrCEhISoq";
}
if (!limitSettingByUser.containsKey(formattedUserName)) {
limitSettingByUser.put(formattedUserName, Arrays.asList(600, 800, 1000, 1300));
}
String msg = "";
msg = checkWCRange(formattedUserName, wc);
if (msg.isEmpty()) {
return Collections.emptyList();
}
......@@ -107,17 +96,53 @@ public class WordLimiterPlugin implements Plugin<Update, List<PartialBotApiMetho
.build());
}
private void executeCommand(Message message) {
var command = commandUtils.normalizeCmdMsg(message.getText());
private void setLimitRange(String formatedUserName, String commandParameter) {
List<Integer> warningsValue = Arrays.stream(commandParameter.split(" "))
.map(Integer::valueOf)
.collect(Collectors.toList());
limitSettingByUser.compute(formatedUserName, (u, w) -> warningsValue);
}
private String checkWCRange(String username, WordCount wc) {
List<Integer> rangeValue = limitSettingByUser.get(username);
if (wc.inRange(rangeValue.get(0))) {
return "0YHQtdCz0L7QtNC90Y8g0JLRiyDQtNC+0YHRgtCw0YLQvtGH0L3QviDQvdCw0L/QuNGB0LDQu9C4LCDQvtGC0LTQvtGF0L3QuNGC0LUu";
} else if (wc.inRange(rangeValue.get(1))) {
return "0JLRiyDRg9C20LUg0LTQvtCy0L7Qu9GM0L3QviDQvNC90L7Qs9C+INC90LDQv9C40YHQsNC70LgsINC80L7QttC10YIg0L/QvtGA0LAg0L7RgdGC0LDQvdC+0LLQuNGC0YzRgdGPPw==";
} else if (wc.inRange(rangeValue.get(2))) {
return "0L/QvtGB0LvQtdC00L3QtdC1INC/0YDQtdC00YPQv9GA0LXQttC00LXQvdC40LUsINCS0Ysg0YPQttC1INC/0L7Rh9GC0Lgg0LTQvtGB0YLQuNCz0LvQuCDQv9C10YDQstC+0LPQviDQvNC10YHRgtCwINCyINC90LjQutGH0LXQvNC90L7RgdGC0LgsINGF0LLQsNGC0LjRgi4=";
} else {
if (wc.inRange(rangeValue.get(3)) || wc.count > rangeValue.get(4) && (wc.count - wc.prevCount >= 10)) {
return "KirQntCh0KLQkNCd0J7QktCY0KHQrCEhISoq";
}
}
return "";
}
private List<PartialBotApiMethod<?>> executeCommand(Message message) {
ParserMessage parserMessage = new ParserMessage();
var command = parserMessage.getCommandFromMessage(message.getText());
var commandParameter = parserMessage.getCommandParameterFromMessage(message.getText());
switch (command) {
case "unsubscribe": {
case UNSUBSCRIBE: {
unsubscribeRepository.createOrUpdate(message.getFrom().getId().intValue(), message.getFrom().getId().intValue());
break;
return Collections.emptyList();
}
case "subscribe": {
case SUBSCRIBE: {
unsubscribeRepository.delete(message.getFrom().getId().intValue());
break;
return Collections.emptyList();
}
case LIMITTIME: {
if (commandParameter.split(" ").length == 4) {
setLimitRange(formatUserName(message.getFrom()), commandParameter);
return List.of(new SendMessage(String.valueOf(message.getChatId()), "It's set"));
} else {
return List.of(new SendMessage(String.valueOf(message.getChatId()), "Sorry, message must be in format: 'number number number number'"));
}
}
default:
return Collections.emptyList();
}
}
......@@ -146,15 +171,20 @@ public class WordLimiterPlugin implements Plugin<Update, List<PartialBotApiMetho
return result;
}
@Value
private static class WordCount {
@RequiredArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
@Setter
private static final class WordCount {
public static WordCount ofCount(int count) {
return new WordCount(count, 0, ZonedDateTime.now(ZoneId.of("Asia/Yekaterinburg")));
}
int count;
int prevCount;
ZonedDateTime timestamp;
private final int count;
private final int prevCount;
private final ZonedDateTime timestamp;
public WordCount add(WordCount wc) {
return new WordCount(count + wc.count, count, timestamp);
......
......@@ -2,5 +2,12 @@ package page.devnet.telegrambot.util;
public enum Command {
NONE, ENABLE, DISABLE, WORKPLUG, ALLPLUG;
NONE,
ENABLE,
DISABLE,
WORKPLUG,
ALLPLUG,
UNSUBSCRIBE,
SUBSCRIBE,
LIMITTIME;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment