mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
WIP: Created some basic controllers for the UI and started implementing backend for server stuff
Created GameState Class. Static, contains all info about current state of game: list of players and their respective details (coords etc) also Host info Clients will observe this GameState class Upon pressing host, new GameState is created and new GameConnectionListener created which listens for connections Player class created which will be used to take each connection and store it as a player with other info about them regarding the game in the GameState class tags: #story[1047] #pair[wmu16, zyt10]
This commit is contained in:
@@ -52,6 +52,7 @@ public class LobbyController {
|
||||
// TODO: 10/07/17 wmu16 - Finish function!
|
||||
setContentPane("/views/StartScreen2View.fxml");
|
||||
System.out.println("Leaving lobby!");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Player> 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<Player> 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user