mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Added functionality to automatically select a server
- Added functionality on the DiscoveryServer to return a random server to the player - Added elements to the UI to support auto-selecting a server - Added client side code to request a random server Tags: #story[1281]
This commit is contained in:
@@ -9,10 +9,12 @@ import seng302.model.stream.packets.PacketType;
|
||||
import seng302.discoveryServer.util.ServerListing;
|
||||
import seng302.discoveryServer.util.ServerRepoStreamParser;
|
||||
import seng302.discoveryServer.util.ServerTable;
|
||||
import seng302.visualiser.ServerListener;
|
||||
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class DiscoveryServer {
|
||||
@@ -20,6 +22,7 @@ public class DiscoveryServer {
|
||||
public static final String ANSI_YELLOW = "\u001B[33m";
|
||||
public static final String ANSI_BLUE = "\u001B[34m";
|
||||
public static final String ANSI_RESET = "\u001B[0m";
|
||||
private static final int MAX_SERVER_TRIES = 10;
|
||||
public static String DISCOVERY_SERVER = "party.sydney.srv.michaelrausch.nz";
|
||||
|
||||
private ServerTable serverTable;
|
||||
@@ -98,15 +101,22 @@ public class DiscoveryServer {
|
||||
|
||||
case ROOM_CODE_REQUEST:
|
||||
String desiredRoomCode = parser.getRoomCode();
|
||||
ServerListing serverListing;
|
||||
|
||||
if (desiredRoomCode.equals("0000")){
|
||||
serverListing = getRandomFreeServer();
|
||||
}
|
||||
else {
|
||||
serverListing = serverTable.getServerByRoomCode(desiredRoomCode);
|
||||
}
|
||||
|
||||
ServerListing serverListing = serverTable.getServerByRoomCode(desiredRoomCode);
|
||||
Message response;
|
||||
|
||||
if (serverListing != null){
|
||||
response = new ServerRegistrationMessage(serverListing.getServerName(), serverListing.getMapName(), serverListing.getAddress(), serverListing.getPortNumber(), 0, 0, desiredRoomCode);
|
||||
}
|
||||
else{
|
||||
response = new ServerRegistrationMessage("", "", "", 0, 0, 0, "");
|
||||
response = ServerRegistrationMessage.getEmptyRegistration();
|
||||
}
|
||||
|
||||
clientSocket.getOutputStream().write(response.getBuffer());
|
||||
@@ -114,4 +124,29 @@ public class DiscoveryServer {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ServerListing getRandomFreeServer() {
|
||||
ServerListing serverToJoin;
|
||||
|
||||
List<ServerListing> servers = serverTable.getAllServers();
|
||||
|
||||
if (servers.size() <= 0){
|
||||
return null;
|
||||
}
|
||||
|
||||
if (servers.size() == 1){
|
||||
return servers.get(0);
|
||||
}
|
||||
|
||||
serverToJoin = servers.get(new Random().nextInt(servers.size()));
|
||||
|
||||
int tries = 0;
|
||||
|
||||
while (serverToJoin != null && serverToJoin.isMaxPlayersReached() && tries < MAX_SERVER_TRIES){
|
||||
serverToJoin = servers.get(new Random().nextInt(servers.size()));
|
||||
tries++;
|
||||
}
|
||||
|
||||
return serverToJoin;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import seng302.model.stream.packets.PacketType;
|
||||
import seng302.discoveryServer.util.ServerListing;
|
||||
import seng302.discoveryServer.util.ServerRepoStreamParser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
@@ -81,6 +82,31 @@ public class DiscoveryServerClient {
|
||||
return parser.getServerListing();
|
||||
}
|
||||
|
||||
public ServerListing getRandomServer() throws Exception {
|
||||
Socket socket = new Socket(DiscoveryServer.DISCOVERY_SERVER, DiscoveryServer.PORT_NUMBER);
|
||||
ServerRepoStreamParser parser = new ServerRepoStreamParser(socket.getInputStream());
|
||||
|
||||
Message request = new RoomCodeRequest("0000");
|
||||
socket.getOutputStream().write(request.getBuffer());
|
||||
|
||||
PacketType packetType = parser.parse();
|
||||
|
||||
if (packetType != PacketType.SERVER_REGISTRATION){
|
||||
logger.error("Incorrect packet type received");
|
||||
return null;
|
||||
}
|
||||
|
||||
socket.close();
|
||||
|
||||
ServerListing serverListing = parser.getServerListing();
|
||||
|
||||
if (serverListing == null || serverListing.equals(ServerRegistrationMessage.getEmptyRegistration())){
|
||||
return null;
|
||||
}
|
||||
|
||||
return serverListing;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a registration update to the discovery server.
|
||||
*
|
||||
|
||||
@@ -110,4 +110,8 @@ public class ServerListing {
|
||||
public void setTtl(Integer ttl){
|
||||
this.ttl = ttl;
|
||||
}
|
||||
|
||||
public boolean isMaxPlayersReached() {
|
||||
return players >= capacity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,10 @@ public class ServerRegistrationMessage extends Message {
|
||||
createMessage(serverName, mapName, address, port, players, capacity, roomCode);
|
||||
}
|
||||
|
||||
public static ServerRegistrationMessage getEmptyRegistration() {
|
||||
return new ServerRegistrationMessage("","","",0,0,0,"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return size;
|
||||
|
||||
@@ -27,6 +27,7 @@ import seng302.discoveryServer.DiscoveryServer;
|
||||
import seng302.gameServer.ServerDescription;
|
||||
import seng302.discoveryServer.util.ServerListing;
|
||||
import seng302.discoveryServer.DiscoveryServerClient;
|
||||
import seng302.gameServer.messages.RoomCodeRequest;
|
||||
import seng302.utilities.Sounds;
|
||||
import seng302.visualiser.ServerListener;
|
||||
import seng302.visualiser.ServerListenerDelegate;
|
||||
@@ -58,6 +59,8 @@ public class ServerListController implements Initializable, ServerListenerDelega
|
||||
private JFXButton roomConnectButton;
|
||||
@FXML
|
||||
private JFXTextField roomNumber;
|
||||
@FXML
|
||||
private JFXButton autoSelectGame;
|
||||
//---------FXML END---------//
|
||||
|
||||
private Label noServersFound;
|
||||
@@ -101,6 +104,29 @@ public class ServerListController implements Initializable, ServerListenerDelega
|
||||
textField.getValidators().add(validator);
|
||||
}
|
||||
|
||||
autoSelectGame.setOnMouseReleased(e -> {
|
||||
try {
|
||||
ServerListing listing = new DiscoveryServerClient().getRandomServer();
|
||||
|
||||
if (listing == null){
|
||||
Alert alert = new Alert(Alert.AlertType.ERROR);
|
||||
alert.setHeaderText("Error finding game");
|
||||
alert.setContentText("No servers are up");
|
||||
alert.showAndWait();
|
||||
}
|
||||
else{
|
||||
ViewManager.getInstance().getGameClient().runAsClient(listing.getAddress(), listing.getPortNumber());
|
||||
}
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
logger.error("Error getting listing");
|
||||
Alert alert = new Alert(Alert.AlertType.ERROR);
|
||||
alert.setHeaderText("Error finding game");
|
||||
alert.setContentText("Couldn't contact matchmaking server");
|
||||
alert.showAndWait();
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
// Validating the hostname
|
||||
HostNameFieldValidator hostNameValidator = new HostNameFieldValidator();
|
||||
|
||||
Reference in New Issue
Block a user