Added functionality to host game and join a game in lobby. Host name and game name are not implemented yet.

#story[1087]
main
zwu18 9 years ago
parent 578f04c74f
commit 9c9b98f882

@ -19,7 +19,7 @@ import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
public class App extends Application { public class App extends Application implements Runnable {
/** /**
* Entry point for running the programme * Entry point for running the programme
@ -49,6 +49,10 @@ public class App extends Application {
} }
} }
public void run(){
launch();
}
} }

@ -79,8 +79,8 @@ public class ConnectionController extends Controller {
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
// TODO - replace with config file // TODO - replace with config file
connections = FXCollections.observableArrayList(); connections = FXCollections.observableArrayList();
connections.add(new RaceConnection("livedata.americascup.com", 4941)); connections.add(new RaceConnection("livedata.americascup.com", 4941, null));
connections.add(new RaceConnection("localhost", 4942)); connections.add(new RaceConnection("localhost", 4942, null));
connectionTable.setItems(connections); connectionTable.setItems(connections);
hostnameColumn.setCellValueFactory(cellData -> cellData.getValue().hostnameProperty()); hostnameColumn.setCellValueFactory(cellData -> cellData.getValue().hostnameProperty());
@ -126,7 +126,7 @@ public class ConnectionController extends Controller {
String portString = portField.getText(); String portString = portField.getText();
try{ try{
int port = Integer.parseInt(portString); int port = Integer.parseInt(portString);
connections.add(new RaceConnection(hostName, port)); connections.add(new RaceConnection(hostName, port, null));
}catch(NumberFormatException e){ }catch(NumberFormatException e){
System.err.println("Port number entered is not a number"); System.err.println("Port number entered is not a number");
} }

@ -6,8 +6,10 @@ import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import mock.app.App; import mock.app.App;
import visualiser.model.RaceConnection;
import java.io.IOException; import java.io.IOException;
import java.net.Socket;
import java.net.URL; import java.net.URL;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -31,7 +33,17 @@ public class HostController extends Controller {
} }
public void hostGamePressed() throws IOException{ public void hostGamePressed() throws IOException{
System.out.println("TODO: Run mock"); new App().start(new Stage());
System.out.println("Run mock");
connectSocket();
}
public void connectSocket() {
try{
Socket socket = new Socket("localhost", 4942);
hostWrapper.setVisible(false);
parent.enterLobby(socket);
} catch (IOException e) { /* Never reached */ }
} }
public AnchorPane startWrapper(){ public AnchorPane startWrapper(){

@ -1,8 +1,17 @@
package visualiser.Controllers; package visualiser.Controllers;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import visualiser.model.RaceConnection;
import java.io.IOException;
import java.net.Socket;
import java.net.URL; import java.net.URL;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -13,10 +22,53 @@ public class LobbyController extends Controller {
@FXML @FXML
AnchorPane lobbyWrapper; AnchorPane lobbyWrapper;
@FXML
private TableView<RaceConnection> lobbyTable;
@FXML
private TableColumn<RaceConnection, String> gameNameColumn;
@FXML
private TableColumn<RaceConnection, String> hostNameColumn;
@FXML
private TableColumn<RaceConnection, String> statusColumn;
@FXML
private Button joinGameBtn;
private ObservableList<RaceConnection> connections;
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
connections = FXCollections.observableArrayList();
connections.add(new RaceConnection("localhost", 4942, "Local Game"));
lobbyTable.setItems(connections);
gameNameColumn.setCellValueFactory(cellData -> cellData.getValue().gamenameProperty());
hostNameColumn.setCellValueFactory(cellData -> cellData.getValue().hostnameProperty());
statusColumn.setCellValueFactory(cellData -> cellData.getValue().statusProperty());
lobbyTable.getSelectionModel().selectedItemProperty().addListener((obs, prev, curr) -> {
if (curr != null && ((RaceConnection)curr).check())
{joinGameBtn.setDisable(false);}
else {joinGameBtn.setDisable(true);}
});
joinGameBtn.setDisable(true);
}
public void refreshBtnPressed(){
for(RaceConnection connection: connections) {
connection.check();
}
}
public void connectSocket() {
try{
RaceConnection connection = lobbyTable.getSelectionModel().getSelectedItem();
Socket socket = new Socket(connection.getHostname(), connection.getPort());
lobbyWrapper.setVisible(false);
parent.enterLobby(socket);
} catch (IOException e) { /* Never reached */ }
} }
public AnchorPane startWrapper(){ public AnchorPane startWrapper(){

@ -53,6 +53,7 @@ public class TitleController extends Controller {
} }
public void enterTitle(){ public void enterTitle(){
titleWrapper.setVisible(true); titleWrapper.setVisible(true);
} }

@ -42,6 +42,7 @@ public class App extends Application {
MainController mc = (MainController) loader.getController(); MainController mc = (MainController) loader.getController();
mc.enterTitle(); mc.enterTitle();
Scene scene = new Scene(root); Scene scene = new Scene(root);
System.out.println(scene.getHeight());
stage.setScene(scene); stage.setScene(scene);
stage.setTitle("RaceVision - Team 7"); stage.setTitle("RaceVision - Team 7");

@ -14,12 +14,14 @@ public class RaceConnection {
private final StringProperty hostname; private final StringProperty hostname;
private final int port; private final int port;
private final StringProperty status; private final StringProperty status;
private final StringProperty gamename;
public RaceConnection(String hostname, int port) { public RaceConnection(String hostname, int port, String gamename) {
this.hostname = new SimpleStringProperty(hostname); this.hostname = new SimpleStringProperty(hostname);
this.port = port; this.port = port;
this.status = new SimpleStringProperty(""); this.status = new SimpleStringProperty("");
check(); check();
this.gamename = new SimpleStringProperty(gamename);
} }
/** /**
@ -34,7 +36,8 @@ public class RaceConnection {
s.connect(i, 750);//TODO this should be at least a second or two, once moved to its own thread s.connect(i, 750);//TODO this should be at least a second or two, once moved to its own thread
status.set("Ready"); status.set("Ready");
return true; return true;
} catch (IOException e) {} } catch (IOException e) {
}
status.set("Offline"); status.set("Offline");
return false; return false;
@ -55,4 +58,6 @@ public class RaceConnection {
public StringProperty statusProperty() { public StringProperty statusProperty() {
return status; return status;
} }
public StringProperty gamenameProperty() { return gamename;}
} }

@ -21,8 +21,8 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints> </rowConstraints>
<children> <children>
<TextField fx:id="gameNameField" promptText="Enter game name" GridPane.columnIndex="1" GridPane.rowIndex="1" GridPane.valignment="TOP" /> <TextField fx:id="gameNameField" promptText="Enter game name [DOES NOTHING ATM]" GridPane.columnIndex="1" GridPane.rowIndex="1" GridPane.valignment="TOP" />
<TextField fx:id="hostNameField" promptText="Enter host name" GridPane.columnIndex="1" GridPane.rowIndex="1" GridPane.valignment="BOTTOM" /> <TextField fx:id="hostNameField" promptText="Enter host name [DOES NOTHING ATM]" GridPane.columnIndex="1" GridPane.rowIndex="1" GridPane.valignment="BOTTOM" />
<Button fx:id="hostGameBtn" mnemonicParsing="false" onAction="#hostGamePressed" text="Host Game" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2"> <Button fx:id="hostGameBtn" mnemonicParsing="false" onAction="#hostGamePressed" text="Host Game" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2">
<font> <font>
<Font size="20.0" /> <Font size="20.0" />

@ -24,7 +24,7 @@
<columns> <columns>
<TableColumn fx:id="gameNameColumn" prefWidth="223.0" text="Game Name" /> <TableColumn fx:id="gameNameColumn" prefWidth="223.0" text="Game Name" />
<TableColumn fx:id="hostNameColumn" prefWidth="280.0" text="Host Name" /> <TableColumn fx:id="hostNameColumn" prefWidth="280.0" text="Host Name" />
<TableColumn fx:id="playerCountColumn" prefWidth="176.0" text="Number of Players" /> <TableColumn fx:id="statusColumn" prefWidth="176.0" text="Status" />
</columns> </columns>
<GridPane.margin> <GridPane.margin>
<Insets left="50.0" right="50.0" /> <Insets left="50.0" right="50.0" />
@ -40,11 +40,16 @@
<Insets left="50.0" /> <Insets left="50.0" />
</GridPane.margin> </GridPane.margin>
</TextField> </TextField>
<Button fx:id="joinGameBtn" mnemonicParsing="false" text="Connect to Game" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="2"> <Button fx:id="joinGameBtn" mnemonicParsing="false" onAction="#connectSocket" text="Connect to Game" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="2">
<GridPane.margin> <GridPane.margin>
<Insets right="50.0" /> <Insets right="50.0" />
</GridPane.margin> </GridPane.margin>
</Button> </Button>
<Button mnemonicParsing="false" onAction="#refreshBtnPressed" text="Refresh" GridPane.columnIndex="1" GridPane.halignment="LEFT" GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="80.0" />
</GridPane.margin>
</Button>
</children> </children>
</GridPane> </GridPane>
</children> </children>

@ -16,8 +16,8 @@ public class RaceConnectionTest {
@Before @Before
public void setUp() { public void setUp() {
onlineConnection = new RaceConnection("livedata.americascup.com", 4941); onlineConnection = new RaceConnection("livedata.americascup.com", 4941, null);
offlineConnection = new RaceConnection("localhost", 4942); offlineConnection = new RaceConnection("localhost", 4942, null);
} }
/** /**

Loading…
Cancel
Save