Oppdaterer behandling av nettverk for å støtte egendefinert serverport

Lar brukeren velge server port på startmenyen
Lar brukeren spesifisere en port etter et kolon når den spesifiserer IP på serveren som skal kobles til
Legger til beskrivende tekst til valg av ip addresse
Bytter til å bruke en port for både TCP og UDP
This commit is contained in:
Kristian Knarvik 2020-04-28 11:55:20 +02:00
parent 7b495438b0
commit 224851ce2c
5 changed files with 36 additions and 17 deletions

View File

@ -12,8 +12,7 @@ import inf112.fiasko.roborally.objects.RoboRallyGame;
* This class acts as a wrapper around the different screens of the game
*/
public class RoboRallyWrapper extends Game implements RoboRallyUI {
public final int defaultTCPPort = 54555;
public final int discoverUDPPort = 54777;
public int networkPort = 54555;
public SpriteBatch batch;
public BitmapFont font;
public ScreenManager screenManager;

View File

@ -37,14 +37,15 @@ public class IPAddressScreen extends AbstractScreen {
joinButton.setSize(300, 60);
joinButton.setPosition(applicationWidth / 2f - joinButton.getWidth() / 2f, 300);
roboRallyWrapper.client = new RoboRallyClient(roboRallyWrapper);
List<InetAddress> lanServers = roboRallyWrapper.client.getLanServers(roboRallyWrapper.discoverUDPPort);
List<InetAddress> lanServers = roboRallyWrapper.client.getLanServers(roboRallyWrapper.networkPort);
Set<String> validHosts = new HashSet<>();
for (InetAddress address : lanServers) {
validHosts.add(address.getHostAddress());
}
final SelectBox<String> selectBox = new SelectBox<>(skin);
selectBox.setItems(validHosts.toArray(new String[0]));
selectBox.setPosition(-80 + (applicationWidth - selectBox.getWidth()) / 2f, 200);
selectBox.setPosition(-80 + (applicationWidth - selectBox.getWidth()) / 2f, 180);
selectBox.setSize(200, 50);
stage.addActor(selectBox);
@ -60,15 +61,21 @@ public class IPAddressScreen extends AbstractScreen {
public void touchUp(InputEvent e, float x, float y, int point, int button) {
try {
String serverIp;
int serverPort = roboRallyWrapper.networkPort;
String writtenIP = textInput.getText();
if (writtenIP.isEmpty()) {
serverIp = selectBox.getSelected();
} else {
serverIp = writtenIP;
if (serverIp.contains(":")) {
String[] ipParts = serverIp.split(":");
serverIp = ipParts[0];
serverPort = Integer.parseInt(ipParts[1]);
}
roboRallyWrapper.client.connect(serverIp, roboRallyWrapper.defaultTCPPort, roboRallyWrapper.discoverUDPPort);
}
roboRallyWrapper.client.connect(serverIp, serverPort);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getUsernameScreen(roboRallyWrapper));
} catch (IOException ex) {
} catch (IOException | NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Could not connect to the server."
+ " Please make sure the ip address you typed is correct, and that the server is online.");
}
@ -96,6 +103,10 @@ public class IPAddressScreen extends AbstractScreen {
"join a server",
applicationWidth / 2f - 380 / 2f, applicationHeight / 2f + 100, 380, 1,
true);
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Specify IP",
10, 280, 200, 1, true);
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Local servers",
10, 230, 200, 1, true);
roboRallyWrapper.batch.end();
}

View File

@ -2,7 +2,9 @@ package inf112.fiasko.roborally.gamewrapper.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
@ -17,6 +19,7 @@ import java.io.IOException;
*/
public class StartMenuScreen extends AbstractScreen {
private final RoboRallyWrapper roboRallyWrapper;
private TextField textInput;
/**
* Instantiates a new start menu screen
@ -31,6 +34,14 @@ public class StartMenuScreen extends AbstractScreen {
serverButton.setY(applicationHeight / 2f);
this.roboRallyWrapper = roboRallyWrapper;
camera.setToOrtho(false, applicationWidth, applicationHeight);
Skin skin = new Skin(Gdx.files.internal("uiskin.json"));
textInput = new TextField("", skin);
textInput.setSize(60, 40);
textInput.setPosition(applicationWidth / 2f - 130, applicationHeight / 2f - textInput.getHeight() - 10);
textInput.setText(String.valueOf(roboRallyWrapper.networkPort));
stage.addActor(textInput);
serverButton.addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
@ -40,9 +51,10 @@ public class StartMenuScreen extends AbstractScreen {
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
try {
roboRallyWrapper.server = new RoboRallyServer(roboRallyWrapper.defaultTCPPort, roboRallyWrapper.discoverUDPPort);
roboRallyWrapper.networkPort = Integer.parseInt(textInput.getText());
roboRallyWrapper.server = new RoboRallyServer(roboRallyWrapper.networkPort);
roboRallyWrapper.client = new RoboRallyClient(roboRallyWrapper);
roboRallyWrapper.client.connect("127.0.0.1", roboRallyWrapper.defaultTCPPort, roboRallyWrapper.discoverUDPPort);
roboRallyWrapper.client.connect("127.0.0.1", roboRallyWrapper.networkPort);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getUsernameScreen(roboRallyWrapper));
} catch (IOException e) {
//Hard fail
@ -86,7 +98,6 @@ public class StartMenuScreen extends AbstractScreen {
serverButton.setX(applicationWidth / 2f - serverButton.getWidth() - clientButton.getWidth() / 2 - 10);
clientButton.setX(applicationWidth / 2f - clientButton.getWidth() / 2);
quitButton.setX(applicationWidth / 2f + clientButton.getWidth() / 2 + 10);
}
@Override

View File

@ -32,12 +32,11 @@ public class RoboRallyClient {
* Connects to a Robo Rally server
*
* @param ipAddress The ip address of the server to join
* @param TCPPort The TCP port to connect to
* @param UDPPort The UDP port to connect to
* @param serverPort The port the server is hosted on
* @throws IOException If the server cannot be connected to
*/
public void connect(String ipAddress, int TCPPort, int UDPPort) throws IOException {
client.connect(5000, ipAddress, TCPPort, UDPPort);
public void connect(String ipAddress, int serverPort) throws IOException {
client.connect(5000, ipAddress, serverPort, serverPort);
}
/**

View File

@ -19,15 +19,14 @@ public class RoboRallyServer {
/**
* Instantiates a new Robo Rally server
*
* @param TCPPort The TCP port to bind to
* @param UDPPort The UDP port to bind to
* @param serverPort The port the server should listen on
* @throws IOException If the server cannot be started
*/
public RoboRallyServer(int TCPPort, int UDPPort) throws IOException {
public RoboRallyServer(int serverPort) throws IOException {
server = new Server();
server.start();
NetworkUtil.registerClasses(server.getKryo());
server.bind(TCPPort, UDPPort);
server.bind(serverPort, serverPort);
listener = new RoboRallyServerListener(this);
server.addListener(listener);
}