refactor: changed Client field accessors to getters / setters

This commit is contained in:
Yannik Bretschneider 2021-06-08 02:47:17 +02:00
parent 7f717f5fe2
commit be08c6f1c4
5 changed files with 59 additions and 39 deletions

View File

@ -51,7 +51,7 @@ public class LobbyConnection implements Runnable {
public void addParticipant(Participant participant) {
if (this.state == LobbyConnectionState.Started) {
Logger.trace("Set client state to playing");
participant.getClient().state = ClientState.Playing;
participant.getClient().setState(ClientState.Playing);
participant.state = ParticipantState.Playing;
}
if (participant.type == ParticipantType.Spectator) {

View File

@ -41,14 +41,14 @@ public class LobbyManager {
* AtomicBoolean}
*/
public boolean handleConnect(Client client, AtomicBoolean running) {
if (participants.containsKey(client.id)) {
if (participants.containsKey(client.getId())) {
running.set(true);
}
return true;
}
public boolean handleReady(Client client, PlayerReadyMessage message) {
if (participants.containsKey(client.id)) {
if (participants.containsKey(client.getId())) {
return false;
}
@ -57,11 +57,11 @@ public class LobbyManager {
}
public boolean handleReconnect(Client client) {
if (!participants.containsKey(client.id)) {
if (!participants.containsKey(client.getId())) {
return false;
}
Participant participant = participants.get(client.id);
Participant participant = participants.get(client.getId());
participant.setClient(client);
LobbyConnection lobby = lobbies.get(participant.lobby);
@ -80,10 +80,10 @@ public class LobbyManager {
* isn't free, the participant is disconnected.
*/
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 (!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" +
"lobby number. Please connect as a spectator instead.");
return;
@ -95,7 +95,7 @@ public class LobbyManager {
LobbyConnection lobby = lobbies.get(lobbyID);
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()
.removeClient(client, "The lobby your requested is already full. " +
"Please connect as a spectator instead.");
@ -110,21 +110,21 @@ public class LobbyManager {
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);
participants.put(client.id, participant);
participants.put(client.getId(), participant);
lobby.addParticipant(participant);
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();
response.gameID = lobby.gameID;
response.characterSelection = lobby.options.get(type);
participant.sendMessage(response);
} 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();
response.gameID = lobby.gameID;
participant.sendMessage(response);
@ -141,12 +141,12 @@ public class LobbyManager {
*/
public boolean handleSelection(Client client, CharacterSelectionMessage message) {
Logger.debug("Handling characterSelection...");
if (!participants.containsKey(client.id)) {
if (!participants.containsKey(client.getId())) {
Logger.trace("Participant didn't exist, returning...");
return false;
}
Participant participant = participants.get(client.id);
Participant participant = participants.get(client.getId());
if (participant.state != ParticipantState.Assigned) {
Logger.trace("Participant wasn't assigned, exiting...");
@ -186,9 +186,9 @@ public class LobbyManager {
participant.sendMessage(response);
if (complete) {
lobby.getPlayer1().getClient().state = ClientState.Playing;
lobby.getPlayer2().getClient().state = ClientState.Playing;
lobby.getSpectators().forEach(spectator -> spectator.getClient().state = ClientState.Playing);
lobby.getPlayer1().getClient().setState(ClientState.Playing);
lobby.getPlayer2().getClient().setState(ClientState.Playing);
lobby.getSpectators().forEach(spectator -> spectator.getClient().setState(ClientState.Playing));
LobbyRunner.getInstance().startLobby(lobby);
}
@ -203,11 +203,11 @@ public class LobbyManager {
* @return true if handled successfully, and false otherwise
*/
public boolean handleRequests(Client client, RequestMessage message) {
if (!participants.containsKey(client.id)) {
if (!participants.containsKey(client.getId())) {
return false;
}
Participant participant = participants.get(client.id);
Participant participant = participants.get(client.getId());
if (participant.state != ParticipantState.Playing) {
return false;
@ -230,11 +230,11 @@ public class LobbyManager {
*/
public void handleDisconnect(Client client, boolean byRemote) {
Logger.trace("Handling disconnect of Client");
if (!participants.containsKey(client.id)) {
if (!participants.containsKey(client.getId())) {
return;
}
Participant participant = participants.get(client.id);
Participant participant = participants.get(client.getId());
LobbyConnection lobby = lobbies.get(participant.lobby);

View File

@ -16,7 +16,7 @@ public class Participant {
public Participant(Client client, String lobby, ParticipantType type) {
this.client = client;
this.id = client.id;
this.id = client.getId();
this.lobby = lobby;
this.type = type;
}

View File

@ -8,8 +8,8 @@ import java.util.Optional;
public class Client {
public final WebSocket socket;
public SUID id;
public ClientState state = ClientState.Blank;
private SUID id;
private ClientState state = ClientState.Blank;
public Client(WebSocket socket) {
this.socket = socket;
@ -35,4 +35,24 @@ public class Client {
socket.send(data.get());
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;
}
}

View File

@ -141,13 +141,13 @@ public class UserManager {
/** Handles a HelloServerMessage */
private void handleHelloServerMessage(Client client, HelloServerMessage message) {
if(client.state != ClientState.Blank) {
Logger.debug("Disconnecting client as ClientState isn't Blank but {}", client.state);
if(client.getState() != ClientState.Blank) {
Logger.debug("Disconnecting client as ClientState isn't Blank but {}", client.getState());
client.sendError("Invalid message, as handshake already completed.");
return;
}
client.id = new SUID(message.name, message.deviceID);
client.setId(new SUID(message.name, message.deviceID));
Logger.trace("forwarding message to the LobbyManager");
AtomicBoolean running = new AtomicBoolean(false);
@ -155,9 +155,9 @@ public class UserManager {
var clientHasRunningGame = running.get();
if (clientHasRunningGame) {
client.state = ClientState.Reconnect;
client.setState(ClientState.Reconnect);
} else {
client.state = ClientState.Ready;
client.setState(ClientState.Ready);
}
HelloClientMessage response = new HelloClientMessage();
@ -170,7 +170,7 @@ public class UserManager {
/** Handles a reconnectMessage, and reconnects the client if needed */
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");
return;
}
@ -179,22 +179,22 @@ public class UserManager {
Logger.trace("Reconnecting to lobby. Forwarding reconnect instruction to the LobbyManager");
if(LobbyManager.getInstance().handleReconnect(client)) {
Logger.trace("Successfully reconnected client, changing state to Playing...");
client.state = ClientState.Playing;
client.setState(ClientState.Playing);
} else {
Logger.debug("The client couldn't be reconnected properly");
client.sendError("You could not be reconnected to the Lobby");
}
} else {
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 */
private void handlePlayerReadyMessage(Client client, PlayerReadyMessage message) {
Logger.trace("Handing PlayerReadyMessage...");
if(client.state != ClientState.Ready) {
Logger.debug("Client wasn't in Ready state but instead in {} state, sending error...", client.state);
if(client.getState() != ClientState.Ready) {
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");
return;
}
@ -202,7 +202,7 @@ public class UserManager {
Logger.trace("Relaying message to LobbyManager");
if(message.startGame) {
if(LobbyManager.getInstance().handleReady(client, message)) {
client.state = ClientState.Assigned;
client.setState(ClientState.Assigned);
} else {
Logger.trace("Sending error to client as message couldn't be processed properly");
client.sendError("Invalid message.");
@ -216,9 +216,9 @@ public class UserManager {
/** Handles a characterSelectionMessage, and forwards it to the LobbyManager if valid */
private void handleCharacterSelectionMessage(Client client, CharacterSelectionMessage message) {
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 {}",
client.state);
client.getState());
client.sendError("Cannot select character, as client is not in the Character Selection phase");
return;
}
@ -234,9 +234,9 @@ public class UserManager {
/** Handles a RequestMessage, and forwards it to the LobbyManager if valid */
private void handleRequestsMessage(Client client, RequestMessage message) {
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 {}",
client.state);
client.getState());
client.sendError("Invalid message, as client is not ingame");
return;
}