You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.2 KiB
136 lines
4.2 KiB
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.TextField;
|
|
import javafx.scene.layout.AnchorPane;
|
|
import javafx.scene.media.AudioClip;
|
|
import visualiser.model.RaceConnection;
|
|
|
|
import java.io.IOException;
|
|
import java.net.Socket;
|
|
import java.net.URL;
|
|
import java.util.ResourceBundle;
|
|
|
|
/**
|
|
* Controller for the Lobby for entering games
|
|
*/
|
|
public class LobbyController extends Controller {
|
|
|
|
@FXML
|
|
private 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;
|
|
@FXML
|
|
private TextField addressFld;
|
|
@FXML
|
|
private TextField portFld;
|
|
|
|
private ObservableList<RaceConnection> connections;
|
|
|
|
private AudioClip sound;
|
|
|
|
|
|
@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 && curr.statusProperty().getValue().equals("Ready")) {
|
|
joinGameBtn.setDisable(false);
|
|
}
|
|
else {
|
|
joinGameBtn.setDisable(true);
|
|
}
|
|
});
|
|
joinGameBtn.setDisable(true);
|
|
}
|
|
|
|
/**
|
|
* Refreshes the connections in the lobby
|
|
*/
|
|
public void refreshBtnPressed(){
|
|
sound = new AudioClip(this.getClass().getResource("/visualiser/sounds/buttonpress.wav").toExternalForm());
|
|
sound.play();
|
|
for(RaceConnection connection: connections) {
|
|
connection.check();
|
|
}
|
|
try {
|
|
if (lobbyTable.getSelectionModel().getSelectedItem().statusProperty().getValue().equals("Ready")) {
|
|
joinGameBtn.setDisable(false);
|
|
} else {
|
|
joinGameBtn.setDisable(true);
|
|
}
|
|
} catch (Exception e){}
|
|
}
|
|
|
|
/**
|
|
* Connect to a connection.
|
|
*/
|
|
public void connectSocket() {
|
|
try{
|
|
RaceConnection connection = lobbyTable.getSelectionModel().getSelectedItem();
|
|
Socket socket = new Socket(connection.getHostname(), connection.getPort());
|
|
lobbyWrapper.setVisible(false);
|
|
parent.enterLobby(socket, false);
|
|
} catch (IOException e) { /* Never reached */
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void menuBtnPressed(){
|
|
sound = new AudioClip(this.getClass().getResource("/visualiser/sounds/buttonpress.wav").toExternalForm());
|
|
sound.play();
|
|
lobbyWrapper.setVisible(false);
|
|
parent.enterTitle();
|
|
}
|
|
|
|
/**
|
|
* adds a new connection
|
|
*/
|
|
public void addConnectionPressed(){
|
|
sound = new AudioClip(this.getClass().getResource("/visualiser/sounds/buttonpress.wav").toExternalForm());
|
|
sound.play();
|
|
String hostName = addressFld.getText();
|
|
String portString = portFld.getText();
|
|
try{
|
|
int port = Integer.parseInt(portString);
|
|
connections.add(new RaceConnection(hostName, port, "Boat Game"));
|
|
addressFld.clear();
|
|
portFld.clear();
|
|
}catch(NumberFormatException e){
|
|
System.err.println("Port number entered is not a number");
|
|
}
|
|
}
|
|
|
|
public AnchorPane startWrapper(){
|
|
return lobbyWrapper;
|
|
}
|
|
|
|
/**
|
|
* Enter the lobby page.
|
|
*/
|
|
public void enterLobby(){
|
|
lobbyWrapper.setVisible(true);
|
|
}
|
|
}
|