Merge branch 'Story62_Reading_Keystrokes' into Story62_Creating_Game_Loop

This commit is contained in:
Peter
2017-07-19 18:13:58 +12:00
4 changed files with 72 additions and 14 deletions
@@ -1,9 +1,14 @@
package seng302.controllers; package seng302.controllers;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.control.TableColumn; import javafx.scene.control.ListView;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
@@ -11,8 +16,8 @@ import javafx.scene.text.Text;
import seng302.gameServer.GameServerThread; import seng302.gameServer.GameServerThread;
import seng302.gameServer.GameStages; import seng302.gameServer.GameStages;
import seng302.gameServer.GameState; import seng302.gameServer.GameState;
import seng302.gameServerWithThreading.MainServerThread;
import java.io.IOException; import seng302.gameServerWithThreading.ServerToClientThread;
/** /**
* A class describing the actions of the lobby screen * A class describing the actions of the lobby screen
@@ -20,12 +25,15 @@ import java.io.IOException;
*/ */
public class LobbyController { public class LobbyController {
@FXML
private ListView competitorsListView;
@FXML @FXML
private GridPane lobbyScreen; private GridPane lobbyScreen;
@FXML @FXML
private Text lobbyIpText; private Text lobbyIpText;
private GameServerThread gameServerThread; private GameServerThread gameServerThread;
private static ObservableList competitors;
private void setContentPane(String jfxUrl) { private void setContentPane(String jfxUrl) {
try { try {
@@ -42,6 +50,11 @@ public class LobbyController {
} }
} }
public void initialize() {
competitors = FXCollections.observableArrayList();
competitorsListView.setItems(competitors);
}
@FXML @FXML
public void leaveLobbyButtonPressed() { public void leaveLobbyButtonPressed() {
@@ -52,6 +65,11 @@ public class LobbyController {
gameServerThread.terminateGame(); gameServerThread.terminateGame();
} }
public static void refreshCompetitors(){
Collection<String> competitorsIps = MainServerThread.getServerToClientThreads();
competitors.clear();
competitors.addAll(competitorsIps);
}
@FXML @FXML
public void readyButtonPressed() { public void readyButtonPressed() {
@@ -80,10 +80,14 @@ public class StartScreenController {
public void connectButtonPressed() { public void connectButtonPressed() {
// TODO: 10/07/17 wmu16 - Finish function // TODO: 10/07/17 wmu16 - Finish function
String ipAddress = ipTextField.getText().trim().toLowerCase(); String ipAddress = ipTextField.getText().trim().toLowerCase();
try {
ClientToServerThread clientToServerThread = new ClientToServerThread(ipAddress, 4950); ClientToServerThread clientToServerThread = new ClientToServerThread(ipAddress, 4950);
controller.setClientToServerThread(clientToServerThread); controller.setClientToServerThread(clientToServerThread);
clientToServerThread.start(); clientToServerThread.start();
setContentPane("/views/LobbyView.fxml");
} catch (Exception e){
e.printStackTrace();
}
} }
public void setController(Controller controller) { public void setController(Controller controller) {
@@ -1,5 +1,9 @@
package seng302.gameServerWithThreading; package seng302.gameServerWithThreading;
import java.util.Collection;
import java.util.Collections;
import javafx.application.Platform;
import seng302.controllers.LobbyController;
import seng302.gameServer.GameStages; import seng302.gameServer.GameStages;
import seng302.gameServer.GameState; import seng302.gameServer.GameState;
import seng302.models.stream.PacketBufferDelegate; import seng302.models.stream.PacketBufferDelegate;
@@ -19,11 +23,11 @@ import java.util.concurrent.PriorityBlockingQueue;
public class MainServerThread extends Thread implements PacketBufferDelegate{ public class MainServerThread extends Thread implements PacketBufferDelegate{
private static final int PORT = 4950; private static final int PORT = 4950;
private static final Integer MAX_NUM_PLAYERS = 1; private static final Integer MAX_NUM_PLAYERS = 3;
private ServerSocket serverSocket = null; private ServerSocket serverSocket = null;
private Socket socket; private Socket socket;
private ArrayList<ServerToClientThread> serverToClientThreads = new ArrayList<>(); private static ArrayList<ServerToClientThread> serverToClientThreads = new ArrayList<>();
private PriorityBlockingQueue<StreamPacket> packetBuffer; private PriorityBlockingQueue<StreamPacket> packetBuffer;
@@ -47,7 +51,6 @@ public class MainServerThread extends Thread implements PacketBufferDelegate{
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
//LOBBYING //LOBBYING
if (GameState.getCurrentStage() == GameStages.LOBBYING && GameState.getPlayers().size() < MAX_NUM_PLAYERS) { if (GameState.getCurrentStage() == GameStages.LOBBYING && GameState.getPlayers().size() < MAX_NUM_PLAYERS) {
try { try {
@@ -58,6 +61,9 @@ public class MainServerThread extends Thread implements PacketBufferDelegate{
} }
ServerToClientThread thread = new ServerToClientThread(socket, this); ServerToClientThread thread = new ServerToClientThread(socket, this);
serverToClientThreads.add(thread); serverToClientThreads.add(thread);
System.out.println("[SERVER]: Client found");
// Platform.runLater() is used to update the UI from within a thread (found on stack overflow)
Platform.runLater(LobbyController::refreshCompetitors);
thread.start(); thread.start();
} }
@@ -72,7 +78,7 @@ public class MainServerThread extends Thread implements PacketBufferDelegate{
} }
updateClients(); // updateClients();
while (!packetBuffer.isEmpty()){ while (!packetBuffer.isEmpty()){
System.out.println("WHATUPPP"); System.out.println("WHATUPPP");
@@ -108,4 +114,13 @@ public class MainServerThread extends Thread implements PacketBufferDelegate{
System.out.println("HEY HI"); System.out.println("HEY HI");
return packetBuffer.add(streamPacket); return packetBuffer.add(streamPacket);
} }
public static Collection getServerToClientThreads() {
Collection<String> ips = new ArrayList<>();
for (ServerToClientThread serverToClientThread: serverToClientThreads){
ips.add(serverToClientThread.getName());
}
return Collections.synchronizedCollection(ips);
}
} }
+23 -2
View File
@@ -15,7 +15,7 @@
<GridPane fx:id="lobbyScreen" nodeOrientation="LEFT_TO_RIGHT" prefHeight="533.0" prefWidth="802.0" style="-fx-background-color: #2C2c36;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.controllers.LobbyController"> <GridPane fx:id="lobbyScreen" nodeOrientation="LEFT_TO_RIGHT" prefHeight="533.0" prefWidth="802.0" style="-fx-background-color: #2C2c36;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.controllers.LobbyController">
<columnConstraints> <columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints> </columnConstraints>
<rowConstraints> <rowConstraints>
<RowConstraints maxHeight="171.0" minHeight="0.0" prefHeight="31.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="171.0" minHeight="0.0" prefHeight="31.0" vgrow="SOMETIMES" />
@@ -41,6 +41,27 @@
<Button focusTraversable="false" mnemonicParsing="false" onAction="#leaveLobbyButtonPressed" text="Leave Lobby" GridPane.columnIndex="1" GridPane.halignment="CENTER" /> <Button focusTraversable="false" mnemonicParsing="false" onAction="#leaveLobbyButtonPressed" text="Leave Lobby" GridPane.columnIndex="1" GridPane.halignment="CENTER" />
</children> </children>
</GridPane> </GridPane>
<AnchorPane focusTraversable="true" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1" /> <AnchorPane focusTraversable="true" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1">
<children>
<GridPane layoutX="335.0" layoutY="146.0" prefHeight="399.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="261.0" minWidth="0.0" prefWidth="2.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="-Infinity" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="261.0" minWidth="0.0" prefWidth="2.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="125.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="266.0" minHeight="10.0" prefHeight="266.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<children>
<ListView fx:id="competitorsListView" layoutX="154.0" layoutY="59.0" prefHeight="266.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</children>
</GridPane>
</children></AnchorPane>
</children> </children>
</GridPane> </GridPane>