refactor: changed Client field accessors to getters / setters
This commit is contained in:
parent
7f717f5fe2
commit
be08c6f1c4
@ -51,7 +51,7 @@ public class LobbyConnection implements Runnable {
|
|||||||
public void addParticipant(Participant participant) {
|
public void addParticipant(Participant participant) {
|
||||||
if (this.state == LobbyConnectionState.Started) {
|
if (this.state == LobbyConnectionState.Started) {
|
||||||
Logger.trace("Set client state to playing");
|
Logger.trace("Set client state to playing");
|
||||||
participant.getClient().state = ClientState.Playing;
|
participant.getClient().setState(ClientState.Playing);
|
||||||
participant.state = ParticipantState.Playing;
|
participant.state = ParticipantState.Playing;
|
||||||
}
|
}
|
||||||
if (participant.type == ParticipantType.Spectator) {
|
if (participant.type == ParticipantType.Spectator) {
|
||||||
|
@ -41,14 +41,14 @@ public class LobbyManager {
|
|||||||
* AtomicBoolean}
|
* AtomicBoolean}
|
||||||
*/
|
*/
|
||||||
public boolean handleConnect(Client client, AtomicBoolean running) {
|
public boolean handleConnect(Client client, AtomicBoolean running) {
|
||||||
if (participants.containsKey(client.id)) {
|
if (participants.containsKey(client.getId())) {
|
||||||
running.set(true);
|
running.set(true);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean handleReady(Client client, PlayerReadyMessage message) {
|
public boolean handleReady(Client client, PlayerReadyMessage message) {
|
||||||
if (participants.containsKey(client.id)) {
|
if (participants.containsKey(client.getId())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,11 +57,11 @@ public class LobbyManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean handleReconnect(Client client) {
|
public boolean handleReconnect(Client client) {
|
||||||
if (!participants.containsKey(client.id)) {
|
if (!participants.containsKey(client.getId())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Participant participant = participants.get(client.id);
|
Participant participant = participants.get(client.getId());
|
||||||
participant.setClient(client);
|
participant.setClient(client);
|
||||||
|
|
||||||
LobbyConnection lobby = lobbies.get(participant.lobby);
|
LobbyConnection lobby = lobbies.get(participant.lobby);
|
||||||
@ -80,10 +80,10 @@ public class LobbyManager {
|
|||||||
* isn't free, the participant is disconnected.
|
* isn't free, the participant is disconnected.
|
||||||
*/
|
*/
|
||||||
private void addParticipant(Client client, String lobbyID, RoleEnum role) {
|
private void addParticipant(Client client, String lobbyID, RoleEnum role) {
|
||||||
Logger.trace("Adding participant '{}' to the lobby '{}'", client.id.getName(), lobbyID);
|
Logger.trace("Adding participant '{}' to the lobby '{}'", client.getId().getName(), lobbyID);
|
||||||
if (!lobbies.containsKey(lobbyID)) {
|
if (!lobbies.containsKey(lobbyID)) {
|
||||||
if (!LobbyRunner.getInstance().canAddLobby()) {
|
if (!LobbyRunner.getInstance().canAddLobby()) {
|
||||||
Logger.info("Rejecting participant '{}' as server is already full", client.id.getName());
|
Logger.info("Rejecting participant '{}' as server is already full", client.getId().getName());
|
||||||
UserManager.getInstance().removeClient(client, "The server has currently its maximum" +
|
UserManager.getInstance().removeClient(client, "The server has currently its maximum" +
|
||||||
"lobby number. Please connect as a spectator instead.");
|
"lobby number. Please connect as a spectator instead.");
|
||||||
return;
|
return;
|
||||||
@ -95,7 +95,7 @@ public class LobbyManager {
|
|||||||
LobbyConnection lobby = lobbies.get(lobbyID);
|
LobbyConnection lobby = lobbies.get(lobbyID);
|
||||||
|
|
||||||
if (!lobby.hasFreePlayerSlot() && role != RoleEnum.SPECTATOR) {
|
if (!lobby.hasFreePlayerSlot() && role != RoleEnum.SPECTATOR) {
|
||||||
Logger.debug("No free player slots available, disconnecting client '{}'", client.id.getName());
|
Logger.debug("No free player slots available, disconnecting client '{}'", client.getId().getName());
|
||||||
UserManager.getInstance()
|
UserManager.getInstance()
|
||||||
.removeClient(client, "The lobby your requested is already full. " +
|
.removeClient(client, "The lobby your requested is already full. " +
|
||||||
"Please connect as a spectator instead.");
|
"Please connect as a spectator instead.");
|
||||||
@ -110,21 +110,21 @@ public class LobbyManager {
|
|||||||
type = lobby.freeSlot();
|
type = lobby.freeSlot();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.trace("New participant '{}' has the role '{}'", client.id.getName(), type);
|
Logger.trace("New participant '{}' has the role '{}'", client.getId().getName(), type);
|
||||||
|
|
||||||
Participant participant = new Participant(client, lobbyID, type);
|
Participant participant = new Participant(client, lobbyID, type);
|
||||||
participants.put(client.id, participant);
|
participants.put(client.getId(), participant);
|
||||||
|
|
||||||
lobby.addParticipant(participant);
|
lobby.addParticipant(participant);
|
||||||
|
|
||||||
if (type != ParticipantType.Spectator) {
|
if (type != ParticipantType.Spectator) {
|
||||||
Logger.debug("Sending GameAssignment message to user '{}'", client.id.getName());
|
Logger.debug("Sending GameAssignment message to user '{}'", client.getId().getName());
|
||||||
GameAssignmentMessage response = new GameAssignmentMessage();
|
GameAssignmentMessage response = new GameAssignmentMessage();
|
||||||
response.gameID = lobby.gameID;
|
response.gameID = lobby.gameID;
|
||||||
response.characterSelection = lobby.options.get(type);
|
response.characterSelection = lobby.options.get(type);
|
||||||
participant.sendMessage(response);
|
participant.sendMessage(response);
|
||||||
} else {
|
} else {
|
||||||
Logger.debug("Sending GeneralAssignment message to user '{}'", client.id.getName());
|
Logger.debug("Sending GeneralAssignment message to user '{}'", client.getId().getName());
|
||||||
GeneralAssignmentMessage response = new GeneralAssignmentMessage();
|
GeneralAssignmentMessage response = new GeneralAssignmentMessage();
|
||||||
response.gameID = lobby.gameID;
|
response.gameID = lobby.gameID;
|
||||||
participant.sendMessage(response);
|
participant.sendMessage(response);
|
||||||
@ -141,12 +141,12 @@ public class LobbyManager {
|
|||||||
*/
|
*/
|
||||||
public boolean handleSelection(Client client, CharacterSelectionMessage message) {
|
public boolean handleSelection(Client client, CharacterSelectionMessage message) {
|
||||||
Logger.debug("Handling characterSelection...");
|
Logger.debug("Handling characterSelection...");
|
||||||
if (!participants.containsKey(client.id)) {
|
if (!participants.containsKey(client.getId())) {
|
||||||
Logger.trace("Participant didn't exist, returning...");
|
Logger.trace("Participant didn't exist, returning...");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Participant participant = participants.get(client.id);
|
Participant participant = participants.get(client.getId());
|
||||||
|
|
||||||
if (participant.state != ParticipantState.Assigned) {
|
if (participant.state != ParticipantState.Assigned) {
|
||||||
Logger.trace("Participant wasn't assigned, exiting...");
|
Logger.trace("Participant wasn't assigned, exiting...");
|
||||||
@ -186,9 +186,9 @@ public class LobbyManager {
|
|||||||
participant.sendMessage(response);
|
participant.sendMessage(response);
|
||||||
|
|
||||||
if (complete) {
|
if (complete) {
|
||||||
lobby.getPlayer1().getClient().state = ClientState.Playing;
|
lobby.getPlayer1().getClient().setState(ClientState.Playing);
|
||||||
lobby.getPlayer2().getClient().state = ClientState.Playing;
|
lobby.getPlayer2().getClient().setState(ClientState.Playing);
|
||||||
lobby.getSpectators().forEach(spectator -> spectator.getClient().state = ClientState.Playing);
|
lobby.getSpectators().forEach(spectator -> spectator.getClient().setState(ClientState.Playing));
|
||||||
LobbyRunner.getInstance().startLobby(lobby);
|
LobbyRunner.getInstance().startLobby(lobby);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,11 +203,11 @@ public class LobbyManager {
|
|||||||
* @return true if handled successfully, and false otherwise
|
* @return true if handled successfully, and false otherwise
|
||||||
*/
|
*/
|
||||||
public boolean handleRequests(Client client, RequestMessage message) {
|
public boolean handleRequests(Client client, RequestMessage message) {
|
||||||
if (!participants.containsKey(client.id)) {
|
if (!participants.containsKey(client.getId())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Participant participant = participants.get(client.id);
|
Participant participant = participants.get(client.getId());
|
||||||
|
|
||||||
if (participant.state != ParticipantState.Playing) {
|
if (participant.state != ParticipantState.Playing) {
|
||||||
return false;
|
return false;
|
||||||
@ -230,11 +230,11 @@ public class LobbyManager {
|
|||||||
*/
|
*/
|
||||||
public void handleDisconnect(Client client, boolean byRemote) {
|
public void handleDisconnect(Client client, boolean byRemote) {
|
||||||
Logger.trace("Handling disconnect of Client");
|
Logger.trace("Handling disconnect of Client");
|
||||||
if (!participants.containsKey(client.id)) {
|
if (!participants.containsKey(client.getId())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Participant participant = participants.get(client.id);
|
Participant participant = participants.get(client.getId());
|
||||||
|
|
||||||
LobbyConnection lobby = lobbies.get(participant.lobby);
|
LobbyConnection lobby = lobbies.get(participant.lobby);
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ public class Participant {
|
|||||||
|
|
||||||
public Participant(Client client, String lobby, ParticipantType type) {
|
public Participant(Client client, String lobby, ParticipantType type) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.id = client.id;
|
this.id = client.getId();
|
||||||
this.lobby = lobby;
|
this.lobby = lobby;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,8 @@ import java.util.Optional;
|
|||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
public final WebSocket socket;
|
public final WebSocket socket;
|
||||||
public SUID id;
|
private SUID id;
|
||||||
public ClientState state = ClientState.Blank;
|
private ClientState state = ClientState.Blank;
|
||||||
|
|
||||||
public Client(WebSocket socket) {
|
public Client(WebSocket socket) {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
@ -35,4 +35,24 @@ public class Client {
|
|||||||
socket.send(data.get());
|
socket.send(data.get());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public WebSocket getSocket() {
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientState getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SUID getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(SUID id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setState(ClientState state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,13 +141,13 @@ public class UserManager {
|
|||||||
|
|
||||||
/** Handles a HelloServerMessage */
|
/** Handles a HelloServerMessage */
|
||||||
private void handleHelloServerMessage(Client client, HelloServerMessage message) {
|
private void handleHelloServerMessage(Client client, HelloServerMessage message) {
|
||||||
if(client.state != ClientState.Blank) {
|
if(client.getState() != ClientState.Blank) {
|
||||||
Logger.debug("Disconnecting client as ClientState isn't Blank but {}", client.state);
|
Logger.debug("Disconnecting client as ClientState isn't Blank but {}", client.getState());
|
||||||
client.sendError("Invalid message, as handshake already completed.");
|
client.sendError("Invalid message, as handshake already completed.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
client.id = new SUID(message.name, message.deviceID);
|
client.setId(new SUID(message.name, message.deviceID));
|
||||||
|
|
||||||
Logger.trace("forwarding message to the LobbyManager");
|
Logger.trace("forwarding message to the LobbyManager");
|
||||||
AtomicBoolean running = new AtomicBoolean(false);
|
AtomicBoolean running = new AtomicBoolean(false);
|
||||||
@ -155,9 +155,9 @@ public class UserManager {
|
|||||||
|
|
||||||
var clientHasRunningGame = running.get();
|
var clientHasRunningGame = running.get();
|
||||||
if (clientHasRunningGame) {
|
if (clientHasRunningGame) {
|
||||||
client.state = ClientState.Reconnect;
|
client.setState(ClientState.Reconnect);
|
||||||
} else {
|
} else {
|
||||||
client.state = ClientState.Ready;
|
client.setState(ClientState.Ready);
|
||||||
}
|
}
|
||||||
|
|
||||||
HelloClientMessage response = new HelloClientMessage();
|
HelloClientMessage response = new HelloClientMessage();
|
||||||
@ -170,7 +170,7 @@ public class UserManager {
|
|||||||
|
|
||||||
/** Handles a reconnectMessage, and reconnects the client if needed */
|
/** Handles a reconnectMessage, and reconnects the client if needed */
|
||||||
private void handleReconnectMessage(Client client, ReconnectMessage message) {
|
private void handleReconnectMessage(Client client, ReconnectMessage message) {
|
||||||
if(client.state != ClientState.Reconnect) {
|
if(client.getState() != ClientState.Reconnect) {
|
||||||
client.sendError("Invalid message, as client is not in reconnect-ready state");
|
client.sendError("Invalid message, as client is not in reconnect-ready state");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -179,22 +179,22 @@ public class UserManager {
|
|||||||
Logger.trace("Reconnecting to lobby. Forwarding reconnect instruction to the LobbyManager");
|
Logger.trace("Reconnecting to lobby. Forwarding reconnect instruction to the LobbyManager");
|
||||||
if(LobbyManager.getInstance().handleReconnect(client)) {
|
if(LobbyManager.getInstance().handleReconnect(client)) {
|
||||||
Logger.trace("Successfully reconnected client, changing state to Playing...");
|
Logger.trace("Successfully reconnected client, changing state to Playing...");
|
||||||
client.state = ClientState.Playing;
|
client.setState(ClientState.Playing);
|
||||||
} else {
|
} else {
|
||||||
Logger.debug("The client couldn't be reconnected properly");
|
Logger.debug("The client couldn't be reconnected properly");
|
||||||
client.sendError("You could not be reconnected to the Lobby");
|
client.sendError("You could not be reconnected to the Lobby");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Logger.trace("No reconnect requested, setting client to ready to connect state");
|
Logger.trace("No reconnect requested, setting client to ready to connect state");
|
||||||
client.state = ClientState.Ready;
|
client.setState(ClientState.Ready);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Handles a PlayerReadyMessage, and connects a client to a lobby if valid */
|
/** Handles a PlayerReadyMessage, and connects a client to a lobby if valid */
|
||||||
private void handlePlayerReadyMessage(Client client, PlayerReadyMessage message) {
|
private void handlePlayerReadyMessage(Client client, PlayerReadyMessage message) {
|
||||||
Logger.trace("Handing PlayerReadyMessage...");
|
Logger.trace("Handing PlayerReadyMessage...");
|
||||||
if(client.state != ClientState.Ready) {
|
if(client.getState() != ClientState.Ready) {
|
||||||
Logger.debug("Client wasn't in Ready state but instead in {} state, sending error...", client.state);
|
Logger.debug("Client wasn't in Ready state but instead in {} state, sending error...", client.getState());
|
||||||
client.sendError("Invalid message, as client is not in Ready state");
|
client.sendError("Invalid message, as client is not in Ready state");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -202,7 +202,7 @@ public class UserManager {
|
|||||||
Logger.trace("Relaying message to LobbyManager");
|
Logger.trace("Relaying message to LobbyManager");
|
||||||
if(message.startGame) {
|
if(message.startGame) {
|
||||||
if(LobbyManager.getInstance().handleReady(client, message)) {
|
if(LobbyManager.getInstance().handleReady(client, message)) {
|
||||||
client.state = ClientState.Assigned;
|
client.setState(ClientState.Assigned);
|
||||||
} else {
|
} else {
|
||||||
Logger.trace("Sending error to client as message couldn't be processed properly");
|
Logger.trace("Sending error to client as message couldn't be processed properly");
|
||||||
client.sendError("Invalid message.");
|
client.sendError("Invalid message.");
|
||||||
@ -216,9 +216,9 @@ public class UserManager {
|
|||||||
/** Handles a characterSelectionMessage, and forwards it to the LobbyManager if valid */
|
/** Handles a characterSelectionMessage, and forwards it to the LobbyManager if valid */
|
||||||
private void handleCharacterSelectionMessage(Client client, CharacterSelectionMessage message) {
|
private void handleCharacterSelectionMessage(Client client, CharacterSelectionMessage message) {
|
||||||
Logger.trace("Handling CharacterSelectionMessage");
|
Logger.trace("Handling CharacterSelectionMessage");
|
||||||
if(client.state != ClientState.Assigned) {
|
if(client.getState() != ClientState.Assigned) {
|
||||||
Logger.debug("Couldn't handle CharacterSelectionMessage as client wasn't in assignedState but in {}",
|
Logger.debug("Couldn't handle CharacterSelectionMessage as client wasn't in assignedState but in {}",
|
||||||
client.state);
|
client.getState());
|
||||||
client.sendError("Cannot select character, as client is not in the Character Selection phase");
|
client.sendError("Cannot select character, as client is not in the Character Selection phase");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -234,9 +234,9 @@ public class UserManager {
|
|||||||
/** Handles a RequestMessage, and forwards it to the LobbyManager if valid */
|
/** Handles a RequestMessage, and forwards it to the LobbyManager if valid */
|
||||||
private void handleRequestsMessage(Client client, RequestMessage message) {
|
private void handleRequestsMessage(Client client, RequestMessage message) {
|
||||||
Logger.trace("Handling RequestMessage");
|
Logger.trace("Handling RequestMessage");
|
||||||
if(client.state != ClientState.Playing) {
|
if(client.getState() != ClientState.Playing) {
|
||||||
Logger.debug("Couldn't handle RequestMessage as client wasn't in playingState but in {}",
|
Logger.debug("Couldn't handle RequestMessage as client wasn't in playingState but in {}",
|
||||||
client.state);
|
client.getState());
|
||||||
client.sendError("Invalid message, as client is not ingame");
|
client.sendError("Invalid message, as client is not ingame");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user