Skip to content

Instantly share code, notes, and snippets.

@aNNiMON

aNNiMON/Rule5_Fixed.java

Last active Nov 7, 2020
Embed
What would you like to do?
@Override
public void processUpdates(List<Update> updates) {
for (Update update : updates) {
processUpdate(update);
}
}
private void processUpdate(Update update) {
if (update == null) {
return;
}
Message message = update.getMessage();
// don't process old messages
long current = System.currentTimeMillis() / 1000;
if (message.getDate() + 60 < current) {
return;
}
processMessage(message);
}
private void processMessage(Message message) {
long chatId = message.getChatId();
if (chatId == config.tournamentChat()) {
processTournamentChat(message);
} else {
processRegularCommand(message);
}
}
private void processTournamentChat(Message message) {
long chatId = message.getChatId();
if ("/tournament".equals(message.getText())) {
processTournamentCommand(chatId);
}
}
private void processTournamentCommand(long chatId) {
for (Members member : members) {
if (member.isBlocked()) {
Log.warning("Member {} is blocked", member);
tournamentMembers.remove(member);
if (tournamentMembers.size() > MIN_MEMBERS_COUNT) {
tournamentEnabled = false;
}
continue;
}
Member opponent = tournamentMembers.pop();
tournamentEnabled = true;
Message.create()
.withText("Tournament started. Your opponent is {}", opponent)
.toChat(member.getId())
.send();
Message.create()
.withText("Tournament started. Your opponent is {}", member)
.toChat(opponent.getId())
.send();
Message.create()
.withText("Tournament started. {} vs {}", member, opponent)
.toChat(chatId)
.send();
}
}
private void processRegularCommand(Message message) {
long chatId = message.getChatId();
String text = message.getText();
if (text == null) {
Log.error("Empty command");
return;
}
// 20 more lines before this
String[] params = text.split(" ");
members.clear()
members.add(params[1]);
members.add(params[2]);
tournamentEnabled = true;
markup = new Markup();
rows = List.of(members);
markup.setRows(rows);
markup.setAction("tournament");
status = Message.create()
.withText("Let's go!")
.withMarkup(markup)
.toChat(chatId)
.send();
validateStatus(status);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment