feat: add disconnect and reconnect methods to lobby

This commit is contained in:
punchready 2021-06-07 08:51:38 +02:00
parent ca0999d06b
commit 0d1af0ed62
2 changed files with 38 additions and 11 deletions

View File

@ -72,9 +72,7 @@ public class Lobby {
partyConfig.maxRoundTime, partyConfig.maxRoundTime,
this::turnTimeout); this::turnTimeout);
var eventsDescribingGameStart = this.game.startGame(player1Characters, player2Characters); this.connection.broadcastEvents(this.game.startGame(player1Characters, player2Characters));
this.connection.broadcastEvents(eventsDescribingGameStart);
updateTimer(); updateTimer();
} }
@ -115,6 +113,22 @@ public class Lobby {
updateTimer(); updateTimer();
} }
/**
* Called by {@link LobbyConnection} when a client disconnects
* @param source the player disconnecting
*/
public synchronized void handleDisconnect(Participant source) {
}
/**
* Called by {@link LobbyConnection} when a client reconnects
* @param source the player reconnecting
*/
public synchronized void handleReconnect(Participant source) {
}
/** /**
* This method is called at the end of receiveRequests, to start a timer. The active player has now a specific * This method is called at the end of receiveRequests, to start a timer. The active player has now a specific
* amount of time to do his moves. * amount of time to do his moves.

View File

@ -14,6 +14,8 @@ import uulm.teamname.marvelous.server.lobby.Lobby;
import uulm.teamname.marvelous.server.netconnector.SUID; import uulm.teamname.marvelous.server.netconnector.SUID;
import uulm.teamname.marvelous.server.netconnector.UserManager; import uulm.teamname.marvelous.server.netconnector.UserManager;
import javax.management.MBeanParameterInfo;
import java.time.OffsetDateTime;
import java.util.*; import java.util.*;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
@ -89,6 +91,7 @@ public class LobbyConnection implements Runnable {
} }
} }
public Participant getPlayer1() { public Participant getPlayer1() {
return player1; return player1;
} }
@ -116,10 +119,16 @@ public class LobbyConnection implements Runnable {
public void handleDisconnect(Participant participant) { public void handleDisconnect(Participant participant) {
participant.disconnected = true; participant.disconnected = true;
if(lobby != null) {
lobby.handleDisconnect(participant);
}
} }
public void handleReconnect(Participant participant) { public void handleReconnect(Participant participant) {
participant.disconnected = false; participant.disconnected = false;
if(lobby != null) {
lobby.handleReconnect(participant);
}
} }
@ -160,6 +169,7 @@ public class LobbyConnection implements Runnable {
state = LobbyConnectionState.Aborted; state = LobbyConnectionState.Aborted;
} }
private void sendGameStructure() { private void sendGameStructure() {
GameStructureMessage gameStructureMessage = new GameStructureMessage(); GameStructureMessage gameStructureMessage = new GameStructureMessage();
gameStructureMessage.playerOneName = player1.id.getName(); gameStructureMessage.playerOneName = player1.id.getName();
@ -211,6 +221,17 @@ public class LobbyConnection implements Runnable {
spectators.forEach(spectator -> spectator.sendMessage(message)); spectators.forEach(spectator -> spectator.sendMessage(message));
} }
private void broadcastToAllExcept(Participant except, BasicMessage message) {
if (!except.equals(player1)) player1.sendMessage(message);
if (!except.equals(player2)) player2.sendMessage(message);
for(Participant spectator: spectators) {
if(!except.equals(spectator)) {
spectator.sendMessage(message);
}
}
}
public void sendEvents(Participant recipient, Event... events) { public void sendEvents(Participant recipient, Event... events) {
EventMessage message = new EventMessage(); EventMessage message = new EventMessage();
message.messages = events; message.messages = events;
@ -235,12 +256,4 @@ public class LobbyConnection implements Runnable {
broadcastToAllExcept(except, message); broadcastToAllExcept(except, message);
} }
private void broadcastToAllExcept(Participant except, BasicMessage message) {
if (!except.equals(player1)) player1.sendMessage(message);
if (!except.equals(player2)) player2.sendMessage(message);
spectators.stream()
.filter(spectator -> !except.equals(spectator))
.forEach(spectator -> spectator.sendMessage(message));
}
} }