mirror of
https://github.com/OpenLiberty/liberty-bikes.git
synced 2025-02-17 11:09:39 +08:00
(Not functional) QueuePlayersCount
This commit is contained in:
parent
b2fb1008d4
commit
acfe6a2d42
@ -31,6 +31,8 @@ public class GameMetrics {
|
||||
private int totalPlayers = 0;
|
||||
private int currentRounds = 0;
|
||||
private Context timerContext;
|
||||
private int currentQueuedPlayers = 0;
|
||||
private int currentPartiesCounter = 0;
|
||||
|
||||
@Counted(unit = MetricUnits.NONE,
|
||||
name = "roundNumberCounter",
|
||||
@ -74,6 +76,22 @@ public class GameMetrics {
|
||||
return totalPlayers;
|
||||
}
|
||||
|
||||
@Gauge(unit = MetricUnits.NONE,
|
||||
name = "currentQueuedPlayersCounter",
|
||||
absolute = true,
|
||||
description = "Number of players that are currently waiting in a queue")
|
||||
public int getTotalPlayersQueued() {
|
||||
return currentQueuedPlayers;
|
||||
}
|
||||
|
||||
@Gauge(unit = MetricUnits.NONE,
|
||||
name = "currentPartiesCounter",
|
||||
absolute = true,
|
||||
description = "Number of players that are currently waiting in a queue")
|
||||
public int getCurrentPartiesCounter() {
|
||||
return currentPartiesCounter;
|
||||
}
|
||||
|
||||
// @Timed(unit = MetricUnits.SECONDS,
|
||||
// name = "timer",
|
||||
// absolute = true,
|
||||
@ -99,19 +117,35 @@ public class GameMetrics {
|
||||
}
|
||||
|
||||
public void incMobilePlayerCount() {
|
||||
totalCurrentMobilePlayers = totalCurrentMobilePlayers + 1;
|
||||
totalCurrentMobilePlayers = getMobilePlayerCount() + 1;
|
||||
}
|
||||
|
||||
public void decMobilePlayerCount() {
|
||||
totalCurrentMobilePlayers = totalCurrentMobilePlayers - 1;
|
||||
totalCurrentMobilePlayers = getMobilePlayerCount() - 1;
|
||||
}
|
||||
|
||||
public void incRoundCounter() {
|
||||
currentRounds = currentRounds + 1;
|
||||
currentRounds = getCurrentRoundsCounter() + 1;
|
||||
}
|
||||
|
||||
public void decRoundCounter() {
|
||||
currentRounds = currentRounds - 1;
|
||||
currentRounds = getCurrentRoundsCounter() - 1;
|
||||
}
|
||||
|
||||
public void incCurrentQueuedPlayersCounter() {
|
||||
currentQueuedPlayers = getTotalPlayersQueued() + 1;
|
||||
}
|
||||
|
||||
public void decCurrentQueuedPlayersCounter() {
|
||||
currentQueuedPlayers = getTotalPlayersQueued() - 1;
|
||||
}
|
||||
|
||||
public void incCurrentPartiesCounter() {
|
||||
currentPartiesCounter = getCurrentPartiesCounter() + 1;
|
||||
}
|
||||
|
||||
public void decCurrentPartiesCounter() {
|
||||
currentPartiesCounter = getCurrentPartiesCounter() - 1;
|
||||
}
|
||||
|
||||
// public static Context timerStart(Metadata metricMetadata) {
|
||||
@ -121,26 +155,21 @@ public class GameMetrics {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
public static final Metadata gameRoundTimerMetadata = new MetadataBuilder()
|
||||
.withName("game_round_timer")
|
||||
.withDescription("The Time Game Rounds Last")
|
||||
.withUnit(MetricUnits.SECONDS)
|
||||
.build();
|
||||
// public static final Metadata gameRoundTimerMetadata = new MetadataBuilder()
|
||||
// .withName("game_round_timer")
|
||||
// .withDescription("The Time Game Rounds Last")
|
||||
// .withUnit(MetricUnits.SECONDS)
|
||||
// .build();
|
||||
|
||||
public static final Metadata currentPartiesCounterMetadata = new MetadataBuilder()
|
||||
.withName("current_number_of_parties")
|
||||
.withDescription("Number of parties currently running")
|
||||
.build();
|
||||
|
||||
public static final Metadata currentQueuedPlayersCounter = new MetadataBuilder()
|
||||
.withName("current_num_of_players_in_queue")
|
||||
.withDescription("Number of players that are currently waiting in a queue")
|
||||
.build();
|
||||
|
||||
public static final Metadata openWebsocketTimerMetadata = new MetadataBuilder()
|
||||
.withName("open_game_websocket_timer")
|
||||
.withDescription("The Time Game Round Websockets Are Open")
|
||||
.withUnit(MetricUnits.SECONDS)
|
||||
.build();
|
||||
// public static final Metadata openWebsocketTimerMetadata = new MetadataBuilder()
|
||||
// .withName("open_game_websocket_timer")
|
||||
// .withDescription("The Time Game Round Websockets Are Open")
|
||||
// .withUnit(MetricUnits.SECONDS)
|
||||
// .build();
|
||||
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import jakarta.ws.rs.sse.SseEventSink;
|
||||
//import org.eclipse.microprofile.metrics.annotation.Counted;
|
||||
import org.libertybikes.game.core.GameRound;
|
||||
import org.libertybikes.game.core.GameRound.LifecycleCallback;
|
||||
import org.libertybikes.game.metric.GameMetrics;
|
||||
//import org.libertybikes.game.metric.GameMetrics;
|
||||
import org.libertybikes.game.round.service.GameRoundService;
|
||||
|
||||
@ -27,18 +28,21 @@ public class Party {
|
||||
@JsonbTransient
|
||||
GameRoundService roundService;
|
||||
|
||||
@Inject
|
||||
GameMetrics gameMetrics;
|
||||
|
||||
public final String id;
|
||||
private final PartyQueue queue = new PartyQueue(this);
|
||||
private final PartyQueue queue = new PartyQueue(this, gameMetrics);
|
||||
private volatile GameRound currentRound;
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
//GameMetrics.counterInc(GameMetrics.currentPartiesCounterMetadata);
|
||||
gameMetrics.incCurrentPartiesCounter();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void preDestroy() {
|
||||
//GameMetrics.counterDec(GameMetrics.currentPartiesCounterMetadata);
|
||||
gameMetrics.decCurrentPartiesCounter();
|
||||
}
|
||||
|
||||
@Inject
|
||||
|
@ -12,14 +12,18 @@ import org.libertybikes.game.core.GameRound;
|
||||
import org.libertybikes.game.core.OutboundMessage;
|
||||
import org.libertybikes.game.core.Player;
|
||||
//import org.libertybikes.game.metric.GameMetrics;
|
||||
import org.libertybikes.game.metric.GameMetrics;
|
||||
|
||||
public class PartyQueue {
|
||||
|
||||
GameMetrics gameMetrics;
|
||||
|
||||
private final ConcurrentLinkedDeque<QueuedClient> waitingPlayers = new ConcurrentLinkedDeque<>();
|
||||
private final Party party;
|
||||
|
||||
public PartyQueue(Party p) {
|
||||
public PartyQueue(Party p, GameMetrics gameMetrics) {
|
||||
this.party = p;
|
||||
this.gameMetrics = Objects.requireNonNull(gameMetrics, "gameMetrics");
|
||||
}
|
||||
|
||||
public void add(String playerId, SseEventSink sink, Sse sse) {
|
||||
@ -27,12 +31,12 @@ public class PartyQueue {
|
||||
// If this client was already in the queue, remove them and add them at the end
|
||||
if (waitingPlayers.removeFirstOccurrence(client)) {
|
||||
party.log("Removed client " + playerId + " from queue before adding at end");
|
||||
//GameMetrics.counterDec(GameMetrics.currentQueuedPlayersCounter);
|
||||
gameMetrics.decCurrentQueuedPlayersCounter();
|
||||
}
|
||||
party.log("Adding client " + playerId + " into the queue in position " + client.queuePosition());
|
||||
waitingPlayers.add(client);
|
||||
|
||||
//GameMetrics.counterInc(GameMetrics.currentQueuedPlayersCounter);
|
||||
gameMetrics.incCurrentQueuedPlayersCounter();
|
||||
|
||||
if (party.getCurrentRound().isOpen())
|
||||
promoteClients();
|
||||
@ -47,7 +51,7 @@ public class PartyQueue {
|
||||
QueuedClient first = waitingPlayers.pollFirst();
|
||||
if (first != null) {
|
||||
first.promoteToGame(newRound.id);
|
||||
//GameMetrics.counterDec(GameMetrics.currentQueuedPlayersCounter);
|
||||
gameMetrics.decCurrentQueuedPlayersCounter();
|
||||
}
|
||||
}
|
||||
for (QueuedClient client : waitingPlayers)
|
||||
@ -59,7 +63,7 @@ public class PartyQueue {
|
||||
QueuedClient client = null;
|
||||
while ((client = waitingPlayers.pollFirst()) != null) {
|
||||
client.close();
|
||||
//GameMetrics.counterDec(GameMetrics.currentQueuedPlayersCounter);
|
||||
gameMetrics.decCurrentQueuedPlayersCounter();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,13 +57,15 @@ public class GameRoundWebsocket {
|
||||
//timerContext = GameMetrics.timerStart(GameMetrics.openWebsocketTimerMetadata);
|
||||
}
|
||||
|
||||
// BUG : org.jboss.weld.contexts.ContextNotActiveException: WELD-001303: No active contexts for scope type jakarta.enterprise.context.ApplicationScoped
|
||||
@OnClose
|
||||
public void onClose(@PathParam("roundId") String roundId, Session session) {
|
||||
log(roundId, "Closed a session");
|
||||
|
||||
/*if (timerContext != null)
|
||||
timerContext.close();
|
||||
*/
|
||||
/*
|
||||
* if (timerContext != null)
|
||||
* timerContext.close();
|
||||
*/
|
||||
try {
|
||||
GameRound round = gameSvc.getRound(roundId);
|
||||
if (round != null)
|
||||
@ -75,11 +77,12 @@ public class GameRoundWebsocket {
|
||||
}
|
||||
|
||||
@OnMessage
|
||||
/*@Metered(name = "rate_of_websocket_calls",
|
||||
displayName = "Rate of GameRound Websocket Calls",
|
||||
description = "Rate of incoming messages to the game round websocket",
|
||||
absolute = true)
|
||||
*/
|
||||
/*
|
||||
* @Metered(name = "rate_of_websocket_calls",
|
||||
* displayName = "Rate of GameRound Websocket Calls",
|
||||
* description = "Rate of incoming messages to the game round websocket",
|
||||
* absolute = true)
|
||||
*/
|
||||
public void onMessage(@PathParam("roundId") final String roundId, String message, Session session) {
|
||||
try {
|
||||
final InboundMessage msg = jsonb.fromJson(message, InboundMessage.class);
|
||||
|
@ -15,6 +15,7 @@ import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.enterprise.concurrent.ManagedScheduledExecutorService;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.enterprise.inject.spi.CDI;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.GET;
|
||||
@ -29,6 +30,7 @@ import jakarta.ws.rs.sse.Sse;
|
||||
import jakarta.ws.rs.sse.SseEventSink;
|
||||
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
import org.libertybikes.game.metric.GameMetrics;
|
||||
import org.libertybikes.game.party.Party;
|
||||
|
||||
@Path("/party")
|
||||
@ -43,6 +45,12 @@ public class PartyService {
|
||||
@ConfigProperty(name = "singleParty", defaultValue = "true")
|
||||
private boolean isSingleParty;
|
||||
|
||||
@Inject
|
||||
GameMetrics gameMetrics;
|
||||
|
||||
@Inject
|
||||
Instance<Party> partyInstance;
|
||||
|
||||
@Resource
|
||||
private ManagedScheduledExecutorService exec;
|
||||
|
||||
@ -51,7 +59,8 @@ public class PartyService {
|
||||
if (!isSingleParty)
|
||||
return;
|
||||
|
||||
Party p = CDI.current().select(Party.class).get();
|
||||
// Party p = CDI.current().select(Party.class).get();
|
||||
Party p = partyInstance.get();
|
||||
allParties.put(p.id, p);
|
||||
System.out.println("Created singleton party " + p.id);
|
||||
}
|
||||
@ -77,7 +86,7 @@ public class PartyService {
|
||||
return allParties.values().iterator().next();
|
||||
}
|
||||
|
||||
Party p = CDI.current().select(Party.class).get();
|
||||
Party p = partyInstance.get();
|
||||
allParties.put(p.id, p);
|
||||
// Put a max lifetime of 12 hours on a party
|
||||
exec.schedule(() -> this.deleteParty(p.id), 12, TimeUnit.HOURS);
|
||||
@ -98,6 +107,7 @@ public class PartyService {
|
||||
Party deleted = allParties.remove(partyId);
|
||||
if (deleted != null) {
|
||||
deleted.close();
|
||||
partyInstance.destroy(deleted);
|
||||
System.out.println("Deleted party " + partyId);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user