feat: add return of table state
This commit is contained in:
@@ -51,6 +51,9 @@ public class CompetitionWS {
|
||||
@Inject
|
||||
RTeam rTeam;
|
||||
|
||||
@Inject
|
||||
RState rState;
|
||||
|
||||
@Inject
|
||||
SecurityCtx securityCtx;
|
||||
|
||||
@@ -95,6 +98,7 @@ public class CompetitionWS {
|
||||
getWSReceiverMethods(RRegister.class, rRegister);
|
||||
getWSReceiverMethods(RCard.class, rCard);
|
||||
getWSReceiverMethods(RTeam.class, rTeam);
|
||||
getWSReceiverMethods(RState.class, rState);
|
||||
|
||||
executor = notifyExecutor;
|
||||
}
|
||||
@@ -141,6 +145,7 @@ public class CompetitionWS {
|
||||
LOGGER.debugf("Active connections: %d", connection.getOpenConnections().size());
|
||||
|
||||
waitingResponse.remove(connection);
|
||||
rState.removeConnection(connection);
|
||||
}
|
||||
|
||||
private MessageOut makeReply(MessageIn message, Object data) {
|
||||
@@ -230,6 +235,30 @@ public class CompetitionWS {
|
||||
});
|
||||
}
|
||||
|
||||
public static void sendNotifyState(WebSocketConnection connection, String code, Object data) {
|
||||
String uuid = connection.pathParam("uuid");
|
||||
|
||||
List<Uni<Void>> queue = new ArrayList<>();
|
||||
queue.add(Uni.createFrom().voidItem()); // For avoid empty queue
|
||||
|
||||
connection.getOpenConnections().forEach(c -> {
|
||||
Boolean s = c.userData().get(UserData.TypedKey.forBoolean("needState"));
|
||||
if (uuid.equals(c.pathParam("uuid")) && s != null && s) {
|
||||
queue.add(c.sendText(new MessageOut(UUID.randomUUID(), code, MessageType.NOTIFY, data)));
|
||||
}
|
||||
});
|
||||
|
||||
Uni.join().all(queue)
|
||||
.andCollectFailures()
|
||||
.runSubscriptionOn(executor)
|
||||
.subscribeAsCompletionStage()
|
||||
.whenComplete((v, t) -> {
|
||||
if (t != null) {
|
||||
LOGGER.error("Error sending ws_out message", t);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@OnError
|
||||
Uni<Void> error(WebSocketConnection connection, ForbiddenException t) {
|
||||
return connection.close(CloseReason.INTERNAL_SERVER_ERROR);
|
||||
|
||||
@@ -58,6 +58,8 @@ public class RCard {
|
||||
|
||||
@WSReceiver(code = "getCardForMatch", permission = PermLevel.VIEW)
|
||||
public Uni<List<CardModel>> getCardForMatch(WebSocketConnection connection, Long matchId) {
|
||||
if (matchId == null)
|
||||
return Uni.createFrom().nullItem();
|
||||
return getById(matchId, connection).chain(matchModel -> cardService.getForMatch(matchModel));
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,9 @@ public class RMatch {
|
||||
@Inject
|
||||
TradService trad;
|
||||
|
||||
@Inject
|
||||
RState rState;
|
||||
|
||||
private Uni<MatchModel> getById(long id, WebSocketConnection connection) {
|
||||
return matchRepository.findById(id)
|
||||
.invoke(Unchecked.consumer(o -> {
|
||||
@@ -195,6 +198,7 @@ public class RMatch {
|
||||
return Panache.withTransaction(() -> matchRepository.persist(mm));
|
||||
})
|
||||
.invoke(mm -> toSend.add(MatchEntity.fromModel(mm)))
|
||||
.invoke(mm -> rState.setMatchEnd(connection, matchEnd))
|
||||
.chain(mm -> updateEndAndTree(mm, toSend))
|
||||
.invoke(__ -> SSMatch.sendMatch(connection, toSend))
|
||||
.replaceWithVoid();
|
||||
|
||||
149
src/main/java/fr/titionfire/ffsaf/ws/recv/RState.java
Normal file
149
src/main/java/fr/titionfire/ffsaf/ws/recv/RState.java
Normal file
@@ -0,0 +1,149 @@
|
||||
package fr.titionfire.ffsaf.ws.recv;
|
||||
|
||||
import fr.titionfire.ffsaf.ws.PermLevel;
|
||||
import fr.titionfire.ffsaf.ws.send.SSState;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import io.quarkus.websockets.next.UserData;
|
||||
import io.quarkus.websockets.next.WebSocketConnection;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@ApplicationScoped
|
||||
@RegisterForReflection
|
||||
public class RState {
|
||||
|
||||
private static final HashMap<WebSocketConnection, TableState> tableStates = new HashMap<>();
|
||||
|
||||
@WSReceiver(code = "subscribeToState", permission = PermLevel.VIEW)
|
||||
public Uni<List<TableState>> sendCurrentScore(WebSocketConnection connection, Boolean subscribe) {
|
||||
connection.userData().put(UserData.TypedKey.forBoolean("needState"), subscribe);
|
||||
|
||||
if (subscribe) {
|
||||
String uuid = connection.pathParam("uuid");
|
||||
return Uni.createFrom().item(() ->
|
||||
tableStates.values().stream().filter(s -> s.getCompetitionUuid().equals(uuid)).toList()
|
||||
);
|
||||
}
|
||||
return Uni.createFrom().nullItem();
|
||||
}
|
||||
|
||||
@WSReceiver(code = "sendState", permission = PermLevel.TABLE)
|
||||
public Uni<Void> sendState(WebSocketConnection connection, TableState tableState) {
|
||||
tableState.setCompetitionUuid(connection.pathParam("uuid"));
|
||||
|
||||
if (tableStates.containsKey(connection))
|
||||
tableState.setId(tableStates.get(connection).getId());
|
||||
if (tableState.getChronoState().isRunning() && tableState.getChronoState().state == 0)
|
||||
tableState.setState(MatchState.IN_PROGRESS);
|
||||
tableStates.put(connection, tableState);
|
||||
SSState.sendStateFull(connection, tableState);
|
||||
return Uni.createFrom().voidItem();
|
||||
}
|
||||
|
||||
@WSReceiver(code = "sendSelectCategory", permission = PermLevel.TABLE)
|
||||
public Uni<Void> sendSelectCategory(WebSocketConnection connection, Long catId) {
|
||||
TableState tableState = tableStates.get(connection);
|
||||
if (tableState != null) {
|
||||
tableState.setSelectedCategory(catId);
|
||||
tableState.setState(MatchState.NOT_STARTED);
|
||||
SSState.sendStateFull(connection, tableState);
|
||||
}
|
||||
return Uni.createFrom().voidItem();
|
||||
}
|
||||
|
||||
@WSReceiver(code = "sendSelectMatch", permission = PermLevel.TABLE)
|
||||
public Uni<Void> sendSelectMatch(WebSocketConnection connection, Long matchId) {
|
||||
TableState tableState = tableStates.get(connection);
|
||||
if (tableState != null) {
|
||||
tableState.setSelectedMatch(matchId);
|
||||
tableState.setState(MatchState.NOT_STARTED);
|
||||
SSState.sendStateFull(connection, tableState);
|
||||
}
|
||||
return Uni.createFrom().voidItem();
|
||||
}
|
||||
|
||||
@WSReceiver(code = "sendCurentChrono", permission = PermLevel.TABLE)
|
||||
public Uni<Void> sendCurentChrono(WebSocketConnection connection, ChronoState chronoState) {
|
||||
TableState tableState = tableStates.get(connection);
|
||||
if (tableState != null) {
|
||||
tableState.setChronoState(chronoState);
|
||||
if (chronoState.isRunning())
|
||||
tableState.setState(MatchState.IN_PROGRESS);
|
||||
SSState.sendStateFull(connection, tableState);
|
||||
}
|
||||
return Uni.createFrom().voidItem();
|
||||
}
|
||||
|
||||
@WSReceiver(code = "sendLicenceName", permission = PermLevel.TABLE)
|
||||
public Uni<Void> sendCurrentScore(WebSocketConnection connection, String name) {
|
||||
TableState tableState = tableStates.get(connection);
|
||||
if (tableState != null) {
|
||||
tableState.setLiceName(name);
|
||||
SSState.sendStateFull(connection, tableState);
|
||||
}
|
||||
return Uni.createFrom().voidItem();
|
||||
}
|
||||
|
||||
@WSReceiver(code = "sendCurrentScore", permission = PermLevel.TABLE)
|
||||
public Uni<Void> sendCurrentScore(WebSocketConnection connection, ScoreState scoreState) {
|
||||
TableState tableState = tableStates.get(connection);
|
||||
if (tableState != null) {
|
||||
tableState.setScoreState(scoreState);
|
||||
SSState.sendStateFull(connection, tableState);
|
||||
}
|
||||
return Uni.createFrom().voidItem();
|
||||
}
|
||||
|
||||
public void removeConnection(WebSocketConnection connection) {
|
||||
if (tableStates.containsKey(connection)) {
|
||||
SSState.sendRmStateFull(connection, tableStates.get(connection).getId());
|
||||
tableStates.remove(connection);
|
||||
}
|
||||
}
|
||||
|
||||
public void setMatchEnd(WebSocketConnection connection, RMatch.MatchEnd matchEnd) {
|
||||
if (tableStates.containsKey(connection)) {
|
||||
TableState tableState = tableStates.get(connection);
|
||||
if (matchEnd.end())
|
||||
tableState.setState(MatchState.ENDED);
|
||||
else
|
||||
tableState.setState(MatchState.IN_PROGRESS);
|
||||
SSState.sendStateFull(connection, tableState);
|
||||
}
|
||||
}
|
||||
|
||||
@RegisterForReflection
|
||||
public record ChronoState(long time, long startTime, long configTime, long configPause, int state) {
|
||||
public boolean isRunning() {
|
||||
return startTime != 0 || state != 0;
|
||||
}
|
||||
}
|
||||
|
||||
@RegisterForReflection
|
||||
public record ScoreState(int scoreRouge, int scoreBleu) {
|
||||
}
|
||||
|
||||
@Data
|
||||
@RegisterForReflection
|
||||
public static class TableState {
|
||||
UUID id = UUID.randomUUID();
|
||||
String competitionUuid;
|
||||
Long selectedCategory;
|
||||
Long selectedMatch;
|
||||
ChronoState chronoState;
|
||||
ScoreState scoreState;
|
||||
String liceName = "???";
|
||||
MatchState state = MatchState.NOT_STARTED;
|
||||
}
|
||||
|
||||
public enum MatchState {
|
||||
NOT_STARTED,
|
||||
IN_PROGRESS,
|
||||
ENDED
|
||||
}
|
||||
}
|
||||
19
src/main/java/fr/titionfire/ffsaf/ws/send/SSState.java
Normal file
19
src/main/java/fr/titionfire/ffsaf/ws/send/SSState.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package fr.titionfire.ffsaf.ws.send;
|
||||
|
||||
import fr.titionfire.ffsaf.ws.CompetitionWS;
|
||||
import fr.titionfire.ffsaf.ws.recv.RState;
|
||||
import io.quarkus.websockets.next.WebSocketConnection;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class SSState {
|
||||
|
||||
public static void sendStateFull(WebSocketConnection connection, RState.TableState state) {
|
||||
CompetitionWS.sendNotifyState(connection, "sendStateFull", state);
|
||||
}
|
||||
|
||||
public static void sendRmStateFull(WebSocketConnection connection, UUID id) {
|
||||
CompetitionWS.sendNotifyState(connection, "rmStateFull", id);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user