mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
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:
parent
7b495438b0
commit
224851ce2c
@ -12,8 +12,7 @@ import inf112.fiasko.roborally.objects.RoboRallyGame;
|
|||||||
* This class acts as a wrapper around the different screens of the game
|
* This class acts as a wrapper around the different screens of the game
|
||||||
*/
|
*/
|
||||||
public class RoboRallyWrapper extends Game implements RoboRallyUI {
|
public class RoboRallyWrapper extends Game implements RoboRallyUI {
|
||||||
public final int defaultTCPPort = 54555;
|
public int networkPort = 54555;
|
||||||
public final int discoverUDPPort = 54777;
|
|
||||||
public SpriteBatch batch;
|
public SpriteBatch batch;
|
||||||
public BitmapFont font;
|
public BitmapFont font;
|
||||||
public ScreenManager screenManager;
|
public ScreenManager screenManager;
|
||||||
|
@ -37,14 +37,15 @@ public class IPAddressScreen extends AbstractScreen {
|
|||||||
joinButton.setSize(300, 60);
|
joinButton.setSize(300, 60);
|
||||||
joinButton.setPosition(applicationWidth / 2f - joinButton.getWidth() / 2f, 300);
|
joinButton.setPosition(applicationWidth / 2f - joinButton.getWidth() / 2f, 300);
|
||||||
roboRallyWrapper.client = new RoboRallyClient(roboRallyWrapper);
|
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<>();
|
Set<String> validHosts = new HashSet<>();
|
||||||
for (InetAddress address : lanServers) {
|
for (InetAddress address : lanServers) {
|
||||||
validHosts.add(address.getHostAddress());
|
validHosts.add(address.getHostAddress());
|
||||||
}
|
}
|
||||||
final SelectBox<String> selectBox = new SelectBox<>(skin);
|
final SelectBox<String> selectBox = new SelectBox<>(skin);
|
||||||
selectBox.setItems(validHosts.toArray(new String[0]));
|
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);
|
selectBox.setSize(200, 50);
|
||||||
|
|
||||||
stage.addActor(selectBox);
|
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) {
|
public void touchUp(InputEvent e, float x, float y, int point, int button) {
|
||||||
try {
|
try {
|
||||||
String serverIp;
|
String serverIp;
|
||||||
|
int serverPort = roboRallyWrapper.networkPort;
|
||||||
String writtenIP = textInput.getText();
|
String writtenIP = textInput.getText();
|
||||||
if (writtenIP.isEmpty()) {
|
if (writtenIP.isEmpty()) {
|
||||||
serverIp = selectBox.getSelected();
|
serverIp = selectBox.getSelected();
|
||||||
} else {
|
} else {
|
||||||
serverIp = writtenIP;
|
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));
|
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getUsernameScreen(roboRallyWrapper));
|
||||||
} catch (IOException ex) {
|
} catch (IOException | NumberFormatException ex) {
|
||||||
JOptionPane.showMessageDialog(null, "Could not connect to the server."
|
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.");
|
+ " 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",
|
"join a server",
|
||||||
applicationWidth / 2f - 380 / 2f, applicationHeight / 2f + 100, 380, 1,
|
applicationWidth / 2f - 380 / 2f, applicationHeight / 2f + 100, 380, 1,
|
||||||
true);
|
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();
|
roboRallyWrapper.batch.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,9 @@ package inf112.fiasko.roborally.gamewrapper.screens;
|
|||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.scenes.scene2d.InputEvent;
|
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.TextButton;
|
||||||
|
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
|
||||||
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
|
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
|
||||||
import com.badlogic.gdx.utils.viewport.FitViewport;
|
import com.badlogic.gdx.utils.viewport.FitViewport;
|
||||||
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
|
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
|
||||||
@ -17,6 +19,7 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
public class StartMenuScreen extends AbstractScreen {
|
public class StartMenuScreen extends AbstractScreen {
|
||||||
private final RoboRallyWrapper roboRallyWrapper;
|
private final RoboRallyWrapper roboRallyWrapper;
|
||||||
|
private TextField textInput;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new start menu screen
|
* Instantiates a new start menu screen
|
||||||
@ -31,6 +34,14 @@ public class StartMenuScreen extends AbstractScreen {
|
|||||||
serverButton.setY(applicationHeight / 2f);
|
serverButton.setY(applicationHeight / 2f);
|
||||||
this.roboRallyWrapper = roboRallyWrapper;
|
this.roboRallyWrapper = roboRallyWrapper;
|
||||||
camera.setToOrtho(false, applicationWidth, applicationHeight);
|
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() {
|
serverButton.addListener(new ClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
|
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
|
||||||
@ -40,9 +51,10 @@ public class StartMenuScreen extends AbstractScreen {
|
|||||||
@Override
|
@Override
|
||||||
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
|
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
|
||||||
try {
|
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 = 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));
|
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getUsernameScreen(roboRallyWrapper));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
//Hard fail
|
//Hard fail
|
||||||
@ -86,7 +98,6 @@ public class StartMenuScreen extends AbstractScreen {
|
|||||||
serverButton.setX(applicationWidth / 2f - serverButton.getWidth() - clientButton.getWidth() / 2 - 10);
|
serverButton.setX(applicationWidth / 2f - serverButton.getWidth() - clientButton.getWidth() / 2 - 10);
|
||||||
clientButton.setX(applicationWidth / 2f - clientButton.getWidth() / 2);
|
clientButton.setX(applicationWidth / 2f - clientButton.getWidth() / 2);
|
||||||
quitButton.setX(applicationWidth / 2f + clientButton.getWidth() / 2 + 10);
|
quitButton.setX(applicationWidth / 2f + clientButton.getWidth() / 2 + 10);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -32,12 +32,11 @@ public class RoboRallyClient {
|
|||||||
* Connects to a Robo Rally server
|
* Connects to a Robo Rally server
|
||||||
*
|
*
|
||||||
* @param ipAddress The ip address of the server to join
|
* @param ipAddress The ip address of the server to join
|
||||||
* @param TCPPort The TCP port to connect to
|
* @param serverPort The port the server is hosted on
|
||||||
* @param UDPPort The UDP port to connect to
|
|
||||||
* @throws IOException If the server cannot be connected to
|
* @throws IOException If the server cannot be connected to
|
||||||
*/
|
*/
|
||||||
public void connect(String ipAddress, int TCPPort, int UDPPort) throws IOException {
|
public void connect(String ipAddress, int serverPort) throws IOException {
|
||||||
client.connect(5000, ipAddress, TCPPort, UDPPort);
|
client.connect(5000, ipAddress, serverPort, serverPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,15 +19,14 @@ public class RoboRallyServer {
|
|||||||
/**
|
/**
|
||||||
* Instantiates a new Robo Rally server
|
* Instantiates a new Robo Rally server
|
||||||
*
|
*
|
||||||
* @param TCPPort The TCP port to bind to
|
* @param serverPort The port the server should listen on
|
||||||
* @param UDPPort The UDP port to bind to
|
|
||||||
* @throws IOException If the server cannot be started
|
* @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 = new Server();
|
||||||
server.start();
|
server.start();
|
||||||
NetworkUtil.registerClasses(server.getKryo());
|
NetworkUtil.registerClasses(server.getKryo());
|
||||||
server.bind(TCPPort, UDPPort);
|
server.bind(serverPort, serverPort);
|
||||||
listener = new RoboRallyServerListener(this);
|
listener = new RoboRallyServerListener(this);
|
||||||
server.addListener(listener);
|
server.addListener(listener);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user