mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +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:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user