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

#story[1087]
This commit is contained in:
zwu18
2017-07-26 20:42:36 +12:00
parent 578f04c74f
commit 9c9b98f882
10 changed files with 93 additions and 13 deletions
@@ -79,8 +79,8 @@ public class ConnectionController extends Controller {
public void initialize(URL location, ResourceBundle resources) {
// TODO - replace with config file
connections = FXCollections.observableArrayList();
connections.add(new RaceConnection("livedata.americascup.com", 4941));
connections.add(new RaceConnection("localhost", 4942));
connections.add(new RaceConnection("livedata.americascup.com", 4941, null));
connections.add(new RaceConnection("localhost", 4942, null));
connectionTable.setItems(connections);
hostnameColumn.setCellValueFactory(cellData -> cellData.getValue().hostnameProperty());
@@ -126,7 +126,7 @@ public class ConnectionController extends Controller {
String portString = portField.getText();
try{
int port = Integer.parseInt(portString);
connections.add(new RaceConnection(hostName, port));
connections.add(new RaceConnection(hostName, port, null));
}catch(NumberFormatException e){
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.stage.Stage;
import mock.app.App;
import visualiser.model.RaceConnection;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;
@@ -31,7 +33,17 @@ public class HostController extends Controller {
}
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(){
@@ -1,8 +1,17 @@
package visualiser.Controllers;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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 visualiser.model.RaceConnection;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;
@@ -13,10 +22,53 @@ public class LobbyController extends Controller {
@FXML
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
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(){
@@ -53,6 +53,7 @@ public class TitleController extends Controller {
}
public void enterTitle(){
titleWrapper.setVisible(true);
}
@@ -42,6 +42,7 @@ public class App extends Application {
MainController mc = (MainController) loader.getController();
mc.enterTitle();
Scene scene = new Scene(root);
System.out.println(scene.getHeight());
stage.setScene(scene);
stage.setTitle("RaceVision - Team 7");
@@ -14,12 +14,14 @@ public class RaceConnection {
private final StringProperty hostname;
private final int port;
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.port = port;
this.status = new SimpleStringProperty("");
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
status.set("Ready");
return true;
} catch (IOException e) {}
} catch (IOException e) {
}
status.set("Offline");
return false;
@@ -55,4 +58,6 @@ public class RaceConnection {
public StringProperty statusProperty() {
return status;
}
public StringProperty gamenameProperty() { return gamename;}
}