mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Merge branch 'develop' into story61_player_perspective
# Conflicts: # src/main/java/seng302/App.java # src/main/java/seng302/client/ClientPacketParser.java # src/main/java/seng302/controllers/Controller.java # src/main/java/seng302/controllers/RaceViewController.java # src/main/java/seng302/fxObjects/BoatAnnotations.java # src/main/java/seng302/gameServer/GameState.java # src/main/java/seng302/gameServer/MainServerThread.java # src/main/java/seng302/gameServer/ServerToClientThread.java # src/main/java/seng302/model/Boat.java # src/main/java/seng302/models/stream/XMLParser.java # src/main/java/seng302/visualiser/ClientToServerThread.java # src/main/java/seng302/visualiser/GameView.java # src/main/java/seng302/visualiser/controllers/FinishScreenViewController.java # src/main/java/seng302/visualiser/controllers/StartScreenController.java # src/main/java/seng302/visualiser/fxObjects/BoatObject.java # src/main/resources/views/LobbyView.fxml # src/main/resources/views/MainView.fxml
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
package seng302.visualiser.controllers;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Enumeration;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Alert.AlertType;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.layout.Pane;
|
||||
import seng302.client.ClientState;
|
||||
import seng302.client.ClientToServerThread;
|
||||
import seng302.visualiser.ClientToServerThread;
|
||||
import seng302.gameServer.GameState;
|
||||
import seng302.gameServer.MainServerThread;
|
||||
@@ -23,6 +30,8 @@ public class StartScreenController {
|
||||
@FXML
|
||||
private TextField ipTextField;
|
||||
@FXML
|
||||
private TextField portTextField;
|
||||
@FXML
|
||||
private GridPane startScreen2;
|
||||
|
||||
/**
|
||||
@@ -38,6 +47,7 @@ public class StartScreenController {
|
||||
contentPane.getStylesheets().add(getClass().getResource("/css/master.css").toString());
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(jfxUrl));
|
||||
contentPane.getChildren().addAll((Pane) fxmlLoader.load());
|
||||
|
||||
return fxmlLoader.getController();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -49,7 +59,8 @@ public class StartScreenController {
|
||||
/**
|
||||
* ATTEMPTS TO:
|
||||
* Sets up a new game state with your IP address as designated as the host.
|
||||
* Starts a thread to listen for incoming connections
|
||||
* Starts a thread to listen for incoming connections.
|
||||
* Starts a client to server thread and connects to own ip.
|
||||
* Switches to the lobby screen
|
||||
*/
|
||||
@FXML
|
||||
@@ -62,26 +73,92 @@ public class StartScreenController {
|
||||
// controller.setClientToServerThread(clientToServerThread);
|
||||
clientToServerThread.start();
|
||||
// get the lobby controller so that we can pass the game server thread to it
|
||||
setContentPane("/views/LobbyView.fxml");
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
System.err.println("COULD NOT FIND YOUR IP ADDRESS!");
|
||||
new GameState(getLocalHostIp());
|
||||
MainServerThread mainServerThread = new MainServerThread();
|
||||
ClientState.setHost(true);
|
||||
// host will connect and handshake to itself after setting up the server
|
||||
// TODO: 24/07/17 wmu16 - Make port number some static global type constant?
|
||||
ClientToServerThread clientToServerThread = new ClientToServerThread(ClientState.getHostIp(), 4942);
|
||||
ClientState.setConnectedToHost(true);
|
||||
controller.setClientToServerThread(clientToServerThread);
|
||||
LobbyController lobbyController = (LobbyController) setContentPane("/views/LobbyView.fxml");
|
||||
lobbyController.setMainServerThread(mainServerThread);
|
||||
} catch (Exception e) {
|
||||
Alert alert = new Alert(AlertType.ERROR);
|
||||
alert.setHeaderText("Cannot host");
|
||||
alert.setContentText("Oops, failed to host, try to restart.");
|
||||
alert.showAndWait();
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ATTEMPTS TO:
|
||||
* Connect to an ip address and port using the ip and port specified on start screen.
|
||||
* Starts a Client To Server Thread to maintain connection to host.
|
||||
* Switch view to lobby view.
|
||||
*/
|
||||
@FXML
|
||||
public void connectButtonPressed() {
|
||||
// TODO: 10/07/17 wmu16 - Finish function
|
||||
String ipAddress = ipTextField.getText().trim().toLowerCase();
|
||||
try {
|
||||
ClientToServerThread clientToServerThread = new ClientToServerThread(ipAddress, 4950);
|
||||
clientToServerThread.start();
|
||||
String ipAddress = ipTextField.getText().trim().toLowerCase();
|
||||
Integer port = Integer.valueOf(portTextField.getText().trim());
|
||||
|
||||
ClientToServerThread clientToServerThread = new ClientToServerThread(ipAddress, port);
|
||||
ClientState.setHost(false);
|
||||
ClientState.setConnectedToHost(true);
|
||||
|
||||
controller.setClientToServerThread(clientToServerThread);
|
||||
setContentPane("/views/LobbyView.fxml");
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
Alert alert = new Alert(AlertType.ERROR);
|
||||
alert.setHeaderText("Cannot reach the host");
|
||||
alert.setContentText("Please check your host IP address.");
|
||||
alert.showAndWait();
|
||||
}
|
||||
}
|
||||
|
||||
public void setController(Controller controller) {
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the local host ip address and sets this ip to ClientState.
|
||||
* Only runs by the host.
|
||||
*
|
||||
* @return the localhost ip address
|
||||
*/
|
||||
private String getLocalHostIp() {
|
||||
String ipAddress = null;
|
||||
try {
|
||||
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
|
||||
while (e.hasMoreElements()) {
|
||||
NetworkInterface ni = e.nextElement();
|
||||
if (ni.isLoopback())
|
||||
continue;
|
||||
if(ni.isPointToPoint())
|
||||
continue;
|
||||
if(ni.isVirtual())
|
||||
continue;
|
||||
|
||||
Enumeration<InetAddress> addresses = ni.getInetAddresses();
|
||||
while(addresses.hasMoreElements()) {
|
||||
InetAddress address = addresses.nextElement();
|
||||
if(address instanceof Inet4Address) { // skip all ipv6
|
||||
ipAddress = address.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (ipAddress == null) {
|
||||
System.out.println("[HOST] Cannot obtain local host ip address.");
|
||||
}
|
||||
ClientState.setHostIp(ipAddress);
|
||||
return ipAddress;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user