Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.rubasace.linkedin.games.ldrbot.group;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

import java.time.LocalDateTime;

@Getter
public class StartTimeResetEvent extends ApplicationEvent {
private final Long chatId;
private final LocalDateTime resetTime;

public StartTimeResetEvent(final Object source, final Long chatId, final LocalDateTime resetTime) {
super(source);
this.chatId = chatId;
this.startDate = resetTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.time.LocalDateTime;

@NoArgsConstructor
@Setter
Expand All @@ -41,6 +42,8 @@ public class TelegramGroup {

private String groupName;

private LocalDateTime startDate;

@Column(nullable = false)
private ZoneId timezone = ZoneId.of(DEFAULT_ZONE);

Expand All @@ -67,6 +70,7 @@ public class TelegramGroup {
public TelegramGroup(final Long chatId, final String groupName) {
this.chatId = chatId;
this.groupName = groupName;
this.startDate = new LocalDateTime();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -162,4 +163,13 @@ public void setTimezone(final Long chatId, final String timeZone) throws GroupNo
telegramGroupRepository.save(telegramGroup);
applicationEventPublisher.publishEvent(new TimezoneChangedEvent(this, chatId, timezone));
}

@Transactional
public void resetStartTime(final Long chatId) throws GroupNotFoundException {
TelegramGroup telegramGroup = findGroupOrThrow(chatId);
LocalDateTime now = new LocalDateTime();
telegramGroup.setStartTime(now);
telegramGroupRepository.save(telegramGroup);
applicationEventPublisher.publishEvent(new StartTimeResetEvent(this, chatId, now));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ private void configure(BaseAbilityBot baseAbilityBot, Update update) {
case "timezone":
showTimezoneConfig(chatId, messageId);
return;
case "reset-group-stats":
showResetGroupStatsConfig(chatId, messageId);
return;
case "back":
showMainConfig(chatId, messageId);
return;
Expand Down Expand Up @@ -139,6 +142,7 @@ private void showMainConfig(final Long chatId, final Integer messageId) {
InlineKeyboardMarkup buttons = KeyboardMarkupUtils.createTwoColumnLayout(getPrefix(),
KeyboardMarkupUtils.ButtonData.of("tracked-games", "Tracked Games"),
KeyboardMarkupUtils.ButtonData.of("timezone", "Timezone"),
KeyboardMarkupUtils.ButtonData.of("reset-group-stats", "Reset Group Stats"),
EXIT_BUTTON);

customTelegramClient.sendOrEditMessage(chatId, "Configuration - Choose an option:", buttons, messageId);
Expand Down Expand Up @@ -168,6 +172,23 @@ private void showTimezoneConfig(final Long chatId, final Integer messageId) {
customTelegramClient.sendMessage("Please send me your timezone (for example: <code>Europe/London</code> or <code>America/New_York</code>):", chatId);
}

private void showResetGroupStatsConfig(final Long chatId, final Integer messageId) {
InlineKeyboardMarkup buttons = KeyboardMarkupUtils.createTwoColumnLayout(getPrefix(),
KeyboardMarkupUtils.ButtonData.of("yes", "Yes"),
KeyboardMarkupUtils.ButtonData.of("back", "<< Back to Main Configuration"));

customTelegramClient.sendOrEditMessage(chatId, "Are you sure you want to reset group stats?", buttons, messageId);
}

private void handleResetGroupStats(final Long chatId, final Integer messageId, final String action) {
if ("yes".equals(action)) {
telegramGroupService.resetStartTime(chatId);
showMainConfig(chatId, messageId);
} else if ("back".equals(action)) {
showMainConfig(chatId, messageId);
}
}

private KeyboardMarkupUtils.ButtonData gameTypeToAction(final GameType gameType, final Set<GameType> trackedGames) {
String icon = trackedGames.contains(gameType) ? "✅ " : "❌ ";
return KeyboardMarkupUtils.ButtonData.of(gameType.name(), icon + StringUtils.capitalize(gameType.name().toLowerCase()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface StatsRepository extends Repository<GameSession, Long> {
JOIN s.gameSession gs
WHERE s.group.uuid = :groupId
AND s.game IN :trackedGames
AND gs.registeredAt > s.group.startDate
ORDER BY s.game, s.gameDay ASC, gs.registeredAt ASC
""")
Stream<GameSessionProjection> findSessionsPerGame(@Param("groupId") String groupId, @Param("trackedGames") Set<GameType> trackedGames);
Expand Down