From 801455dfcc3e43776783946de8267962306d3e8a Mon Sep 17 00:00:00 2001 From: LionTion Date: Fri, 16 Oct 2026 12:48:36 +0200 Subject: [PATCH] Game Status --- .../ExcaliburTourApplication.java | 2 + .../configurations/MqttConfig.java | 15 +++++ .../controller/CodeController.java | 4 +- .../services/GameStatusMonitor.java | 59 +++++++++++++++++++ .../services/TourCodeService.java | 6 ++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/exodus_hn/excalibur_tour/services/GameStatusMonitor.java diff --git a/src/main/java/de/exodus_hn/excalibur_tour/ExcaliburTourApplication.java b/src/main/java/de/exodus_hn/excalibur_tour/ExcaliburTourApplication.java index ce18ffc..caac3d4 100644 --- a/src/main/java/de/exodus_hn/excalibur_tour/ExcaliburTourApplication.java +++ b/src/main/java/de/exodus_hn/excalibur_tour/ExcaliburTourApplication.java @@ -2,7 +2,9 @@ package de.exodus_hn.excalibur_tour; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; +@EnableScheduling @SpringBootApplication public class ExcaliburTourApplication { diff --git a/src/main/java/de/exodus_hn/excalibur_tour/configurations/MqttConfig.java b/src/main/java/de/exodus_hn/excalibur_tour/configurations/MqttConfig.java index a8770f8..ce69b61 100644 --- a/src/main/java/de/exodus_hn/excalibur_tour/configurations/MqttConfig.java +++ b/src/main/java/de/exodus_hn/excalibur_tour/configurations/MqttConfig.java @@ -133,6 +133,21 @@ public class MqttConfig extends TextWebSocketHandler { } } + public void publishMessage(String topic, String payload) { + try { + if (mqttClient != null && mqttClient.isConnected()) { + MqttMessage mqttMessage = new MqttMessage(payload.getBytes()); + mqttMessage.setQos(2); + mqttClient.publish(topic, mqttMessage); + System.out.println("An MQTT gesendet [" + topic + "]: " + payload); + } else { + System.err.println("MQTT-Client ist nicht verbunden. Nachricht konnte nicht gesendet werden."); + } + } catch (MqttException e) { + e.printStackTrace(); + } + } + public static class BridgeMessage { public String topic; public String payload; diff --git a/src/main/java/de/exodus_hn/excalibur_tour/controller/CodeController.java b/src/main/java/de/exodus_hn/excalibur_tour/controller/CodeController.java index 6f8295b..ae2f765 100644 --- a/src/main/java/de/exodus_hn/excalibur_tour/controller/CodeController.java +++ b/src/main/java/de/exodus_hn/excalibur_tour/controller/CodeController.java @@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.*; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; +import java.util.Locale; @CrossOrigin @RestController @@ -73,9 +74,10 @@ public class CodeController { if (code == null || code.length() < 9) { boolean found = false; for (int i = 0; i < 10; ++i) { - code = randomStringUtils.nextAlphanumeric(9); + code = randomStringUtils.nextAlphanumeric(9).toUpperCase(Locale.ROOT); t = tourCodeService.getTourCodeByCode(code); if (t == null) { + found = true; break; } } diff --git a/src/main/java/de/exodus_hn/excalibur_tour/services/GameStatusMonitor.java b/src/main/java/de/exodus_hn/excalibur_tour/services/GameStatusMonitor.java new file mode 100644 index 0000000..0616662 --- /dev/null +++ b/src/main/java/de/exodus_hn/excalibur_tour/services/GameStatusMonitor.java @@ -0,0 +1,59 @@ +package de.exodus_hn.excalibur_tour.services; + +import de.exodus_hn.excalibur_tour.Utils; +import de.exodus_hn.excalibur_tour.configurations.MqttConfig; +import de.exodus_hn.excalibur_tour.entities.TourCode; +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +@Component +@RequiredArgsConstructor +public class GameStatusMonitor { + + private final TourCodeService tourCodeService; + private final MqttConfig mqttConfig; + + // Remembers the last state: true = gameStarted (players online), false = gameFinished (no one online) + // Default to false (or true depending on initial assumption, false is safer on startup) + private final AtomicBoolean lastStateWasActive = new AtomicBoolean(false); + + private static final String MQTT_TOPIC = "outdoor/king-arthur/sword/set"; + + // Runs every 30 seconds (30000 milliseconds) + @Scheduled(fixedRate = 30000) + public void checkActiveCodes() { + try { + List allCodes = tourCodeService.getAllTourCodes(); + LocalDateTime now = Utils.getDateTimeNow(); + + boolean anyCodeActive = allCodes.stream() + .anyMatch(t -> Boolean.TRUE.equals(t.getActivated()) && + t.getLastHeartbeatTimestamp() != null && + t.getLastHeartbeatTimestamp().plus(3, ChronoUnit.MINUTES).isAfter(now)); + + // Check if state has changed + boolean previousState = lastStateWasActive.get(); + if (anyCodeActive != previousState) { + // State changed! Update memory state and send MQTT message + lastStateWasActive.set(anyCodeActive); + + if (anyCodeActive) { + mqttConfig.publishMessage(MQTT_TOPIC, "gameStarted"); + System.out.println("Status geändert: Mindestens ein Code ist aktiv -> gameStarted gesendet."); + } else { + mqttConfig.publishMessage(MQTT_TOPIC, "gameFinished"); + System.out.println("Status geändert: Keine Codes mehr aktiv -> gameFinished gesendet."); + } + } + } catch (Exception e) { + System.err.println("Fehler beim Überprüfen des Spielstatus: " + e.getMessage()); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/de/exodus_hn/excalibur_tour/services/TourCodeService.java b/src/main/java/de/exodus_hn/excalibur_tour/services/TourCodeService.java index d56dad7..a321bc7 100644 --- a/src/main/java/de/exodus_hn/excalibur_tour/services/TourCodeService.java +++ b/src/main/java/de/exodus_hn/excalibur_tour/services/TourCodeService.java @@ -5,6 +5,8 @@ import de.exodus_hn.excalibur_tour.repository.TourCodeRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.List; + @Service @RequiredArgsConstructor public class TourCodeService { @@ -26,4 +28,8 @@ public class TourCodeService { public TourCode updateCode(TourCode tourCode) { return tourCodeRepository.save(tourCode); } + + public List getAllTourCodes() { + return tourCodeRepository.findAll(); + } }