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:
Michael Rausch
2017-09-26 01:14:02 +13:00
parent d0844e861d
commit 9cfb3b9e5d
8 changed files with 119 additions and 15 deletions
@@ -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();
+4 -3
View File
@@ -38,15 +38,16 @@
-fx-font-size: 23px;
}
#connectButton, #roomConnectButton, #directConnectButton {
#connectButton, #roomConnectButton, #directConnectButton, #autoSelectGame {
-fx-background-color: -fx-pp-light-text-color; /* inverted */
-fx-text-fill: -fx-pp-theme-color; /* inverted */
-fx-font-size: 20px;
-fx-pref-height: 45px;
-fx-effect: -fx-pp-dropshadow-dark;
}
#connectButton:hover, #roomConnectButton:hover, #directConnectButton:hover {
-fx-font-size: 23px;
#connectButton:hover, #roomConnectButton:hover, #directConnectButton:hover, #autoSelectGame:hover {
-fx-font-size: 21px;
}
#connectLabel, #connectLabel1, #serverPortNumber, #roomNumber, #serverHostName {
+2 -1
View File
@@ -51,7 +51,7 @@
<Insets left="35.0" top="-15.0" />
</padding>
</Label>
<Label fx:id="roomLabel" text="Room: 2145" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.valignment="CENTER">
<Label fx:id="roomLabel" text="Room: 2145" GridPane.columnIndex="2" GridPane.halignment="RIGHT" GridPane.valignment="CENTER">
<font>
<Font size="31.0" />
</font>
@@ -62,6 +62,7 @@
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="732.0" minWidth="10.0" prefWidth="586.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="120000.0" minWidth="10.0" prefWidth="314.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="445.0" minWidth="10.0" prefWidth="314.0" />
</columnConstraints>
<rowConstraints>
+16 -9
View File
@@ -17,9 +17,9 @@
<children>
<GridPane fx:id="serverListMainGridPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308">
<children>
<GridPane fx:id="connectGridPane" prefHeight="79.0" prefWidth="900.0" GridPane.rowIndex="2">
<GridPane fx:id="connectGridPane" prefHeight="70.0" prefWidth="888.0" GridPane.rowIndex="2">
<children>
<JFXButton fx:id="directConnectButton" buttonType="RAISED" prefHeight="45.0" prefWidth="220.0" ripplerFill="#3493e3" text="Direct Connect" textFill="WHITE" GridPane.columnIndex="5" GridPane.halignment="RIGHT" GridPane.valignment="CENTER">
<JFXButton fx:id="directConnectButton" buttonType="RAISED" maxHeight="45.0" prefHeight="45.0" prefWidth="220.0" ripplerFill="#3493e3" text="Direct Connect" textFill="WHITE" GridPane.columnIndex="7" GridPane.halignment="RIGHT" GridPane.valignment="CENTER">
<GridPane.margin>
<Insets right="50.0" />
</GridPane.margin>
@@ -29,7 +29,7 @@
<Insets right="45.0" />
</GridPane.margin>
</Label>
<JFXButton fx:id="roomConnectButton" prefHeight="45.0" prefWidth="220.0" text="Join Room" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<JFXButton fx:id="roomConnectButton" maxHeight="45.0" prefHeight="45.0" prefWidth="220.0" text="Join Room" textFill="WHITE" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<GridPane.margin>
<Insets />
</GridPane.margin>
@@ -39,14 +39,21 @@
<Insets right="20.0" />
</GridPane.margin>
</JFXTextField>
<JFXButton fx:id="autoSelectGame" maxHeight="45.0" prefHeight="45.0" prefWidth="220.0" text="Auto-Select Game" textFill="WHITE" GridPane.columnIndex="6" GridPane.halignment="RIGHT" GridPane.valignment="CENTER">
<GridPane.margin>
<Insets right="10.0" />
</GridPane.margin>
</JFXButton>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="280.0" minWidth="180.0" prefWidth="180.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="320.0" minWidth="70.0" prefWidth="110.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="258.0" minWidth="-Infinity" prefWidth="218.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="167.0" minWidth="17.0" prefWidth="167.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="273.0" minWidth="225.0" prefWidth="225.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="280.0" minWidth="21.0" prefWidth="21.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="320.0" minWidth="70.0" prefWidth="112.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="260.0" minWidth="-Infinity" prefWidth="119.0" />
<ColumnConstraints halignment="LEFT" hgrow="SOMETIMES" maxWidth="215.0" minWidth="17.0" prefWidth="148.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1000.0" minWidth="17.0" prefWidth="77.0" />
<ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES" maxWidth="326.0" minWidth="134.0" prefWidth="226.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="326.0" minWidth="198.0" prefWidth="198.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
@@ -95,7 +102,7 @@
<rowConstraints>
<RowConstraints maxHeight="80.0" minHeight="80.0" prefHeight="80.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="1.7976931348623157E308" minHeight="400.0" prefHeight="459.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="120.0" minHeight="63.0" prefHeight="66.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="70.0" minHeight="70.0" prefHeight="70.0" vgrow="SOMETIMES" />
</rowConstraints>
<stylesheets>
<String fx:value="/css/Master.css" />