Game Status
This commit is contained in:
1 parent
fc77c7d8aa
commit
801455dfcc
5 files changed
+85
-1
No files matched your search
@@ -2,7 +2,9 @@ package de.exodus_hn.excalibur_tour;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
|
@EnableScheduling
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class ExcaliburTourApplication {
|
public class ExcaliburTourApplication {
|
||||||
|
|
||||||
|
|||||||
@@ -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 static class BridgeMessage {
|
||||||
public String topic;
|
public String topic;
|
||||||
public String payload;
|
public String payload;
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
@CrossOrigin
|
@CrossOrigin
|
||||||
@RestController
|
@RestController
|
||||||
@@ -73,9 +74,10 @@ public class CodeController {
|
|||||||
if (code == null || code.length() < 9) {
|
if (code == null || code.length() < 9) {
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
for (int i = 0; i < 10; ++i) {
|
for (int i = 0; i < 10; ++i) {
|
||||||
code = randomStringUtils.nextAlphanumeric(9);
|
code = randomStringUtils.nextAlphanumeric(9).toUpperCase(Locale.ROOT);
|
||||||
t = tourCodeService.getTourCodeByCode(code);
|
t = tourCodeService.getTourCodeByCode(code);
|
||||||
if (t == null) {
|
if (t == null) {
|
||||||
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<TourCode> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,8 @@ import de.exodus_hn.excalibur_tour.repository.TourCodeRepository;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class TourCodeService {
|
public class TourCodeService {
|
||||||
@@ -26,4 +28,8 @@ public class TourCodeService {
|
|||||||
public TourCode updateCode(TourCode tourCode) {
|
public TourCode updateCode(TourCode tourCode) {
|
||||||
return tourCodeRepository.save(tourCode);
|
return tourCodeRepository.save(tourCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<TourCode> getAllTourCodes() {
|
||||||
|
return tourCodeRepository.findAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user