diff --git a/src/main/java/seng302/controllers/LobbyController.java b/src/main/java/seng302/controllers/LobbyController.java index 358b137d..1f65bd34 100644 --- a/src/main/java/seng302/controllers/LobbyController.java +++ b/src/main/java/seng302/controllers/LobbyController.java @@ -52,6 +52,7 @@ public class LobbyController { // TODO: 10/07/17 wmu16 - Finish function! setContentPane("/views/StartScreen2View.fxml"); System.out.println("Leaving lobby!"); + } diff --git a/src/main/java/seng302/controllers/StartScreen2Controller.java b/src/main/java/seng302/controllers/StartScreen2Controller.java index 6be8761d..890fcdc9 100644 --- a/src/main/java/seng302/controllers/StartScreen2Controller.java +++ b/src/main/java/seng302/controllers/StartScreen2Controller.java @@ -6,8 +6,13 @@ import javafx.scene.control.TextField; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; +import seng302.gameServer.GameConnectionListener; +import seng302.gameServer.GameState; import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; /** * A Class describing the actions of the start screen controller @@ -15,8 +20,6 @@ import java.io.IOException; */ public class StartScreen2Controller { - - @FXML private TextField ipTextField; @FXML @@ -38,16 +41,43 @@ public class StartScreen2Controller { } } + + /** + * 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 + * Switches to the lobby screen + */ @FXML public void hostButtonPressed() { - setContentPane("/views/LobbyView.fxml"); + try { + String ipAddress = InetAddress.getLocalHost().getHostAddress(); + new GameState(ipAddress); + GameConnectionListener gameConnectionListener = new GameConnectionListener(); + gameConnectionListener.start(); + setContentPane("/views/LobbyView.fxml"); + } catch (UnknownHostException e) { + System.err.println("COULD NOT FIND YOUR IP ADDRESS!"); + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + System.err.println("COULD NOT OPEN CONNECTION!"); + } + } @FXML public void connectButtonPressed() { // TODO: 10/07/17 wmu16 - Finish function + String ipAddress = ipTextField.getText().trim(); + Socket host = null; + try { + host = new Socket(ipAddress, GameConnectionListener.GAME_HOST_PORT); + } catch (IOException e) { + e.printStackTrace(); + } System.out.println("connecting to: " + ipTextField.getText()); } } diff --git a/src/main/java/seng302/gameServer/GameConnectionListener.java b/src/main/java/seng302/gameServer/GameConnectionListener.java new file mode 100644 index 00000000..0f59e832 --- /dev/null +++ b/src/main/java/seng302/gameServer/GameConnectionListener.java @@ -0,0 +1,53 @@ +package seng302.gameServer; + +import seng302.models.Player; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.channels.ServerSocketChannel; + +/** + * A class defining the lobby host that will wait for connections and update the GameState appropriately + * Created by wmu16 on 10/07/17. + */ +public class GameConnectionListener extends Thread{ + + public static final Integer GAME_HOST_PORT = 4950; + + private Thread t; + private ServerSocketChannel serverSocketChannel; + + + public GameConnectionListener() throws IOException { + serverSocketChannel = ServerSocketChannel.open(); + // TODO: 10/07/17 wmu16 - If you pres host, leave lobby, host, - an error is thrown as this port is already bound. + serverSocketChannel.socket().bind(new InetSocketAddress("localhost", GAME_HOST_PORT)); + } + + + /** + * Starts the listening thread + */ + public void start() { + if (t == null) { + t = new Thread(this, "GameConnectionListener"); + t.start(); + } + } + + + /** + * This listens for players connecting and adds them to the GameState object + * WHILE - max plaers is not exceeded AND the race has not started + */ + public void run() { + while(GameState.getPlayers().size() < GameState.MAX_NUM_PLAYERS && !GameState.getIsRaceStarted()) { + try { + GameState.addPlayer(new Player(serverSocketChannel.accept())); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +} diff --git a/src/main/java/seng302/gameServer/GameState.java b/src/main/java/seng302/gameServer/GameState.java new file mode 100644 index 00000000..cf16a47a --- /dev/null +++ b/src/main/java/seng302/gameServer/GameState.java @@ -0,0 +1,57 @@ +package seng302.gameServer; + +import seng302.models.Player; + +import java.util.ArrayList; + +/** + * A Static class to hold information about the current state of the game (model) + * Created by wmu16 on 10/07/17. + */ +public class GameState { + + public static final Integer MAX_NUM_PLAYERS = 10; + private static String hostIpAddress; + private static ArrayList players; + private static Boolean isRaceStarted; + + public GameState(String hostIpAddress) { + GameState.hostIpAddress = hostIpAddress; + players = new ArrayList<>(); + isRaceStarted = false; + } + + public static String getHostIpAddress() { + return hostIpAddress; + } + + public static ArrayList getPlayers() { + return players; + } + + public static void addPlayer(Player player) { + players.add(player); + } + + public static void removePlayer(Player player) { + players.remove(player); + } + + public static Boolean getIsRaceStarted() { + return isRaceStarted; + } + + + /** + * This iterates through all players and updates each players info to its new state based on its current data + */ + private void update(){ + for(Player player : players) { + // TODO: 10/07/17 wmu16 - Update all player info + } + } + + + + +} diff --git a/src/main/java/seng302/models/Player.java b/src/main/java/seng302/models/Player.java new file mode 100644 index 00000000..4070e1dc --- /dev/null +++ b/src/main/java/seng302/models/Player.java @@ -0,0 +1,78 @@ +package seng302.models; + +import javafx.scene.paint.Color; + +import java.nio.channels.SocketChannel; + +/** + * A Class defining a player and their respective details in the game as held by the model + * Created by wmu16 on 10/07/17. + */ +public class Player { + + private SocketChannel socketChannel; + private Color color; + private Float xPos; + private Float yPos; + private Float heading; + private Float velocity; + private Integer lastMarkPassed; + + + public Player(SocketChannel socketChannel) { + this.socketChannel = socketChannel; + } + + public SocketChannel getSocketChannel() { + return socketChannel; + } + + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } + + public Float getxPos() { + return xPos; + } + + public void setxPos(Float xPos) { + this.xPos = xPos; + } + + public Float getyPos() { + return yPos; + } + + public void setyPos(Float yPos) { + this.yPos = yPos; + } + + public Float getHeading() { + return heading; + } + + public void setHeading(Float heading) { + this.heading = heading; + } + + public Float getVelocity() { + return velocity; + } + + public void setVelocity(Float velocity) { + this.velocity = velocity; + } + + public Integer getLastMarkPassed() { + return lastMarkPassed; + } + + public void setLastMarkPassed(Integer lastMarkPassed) { + this.lastMarkPassed = lastMarkPassed; + } + +}