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.

103 lines
2.5 KiB

package visualiser.Controllers;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import mock.app.Event;
import mock.exceptions.EventConstructionException;
import visualiser.network.MatchBrowserInterface;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Controller for Hosting a game.
*/
public class HostController extends Controller {
@FXML
TextField gameNameField;
@FXML
TextField hostNameField;
@FXML
AnchorPane hostWrapper;
private Event game;
private DatagramSocket udpSocket;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
/**
* Hosts a game
* @throws IOException if socket cannot be connected to
*/
public void hostGamePressed() throws IOException{
try {
this.game = new Event(false);
connectSocket("localhost", 4942);
alertMatchBrowser();
} catch (EventConstructionException e) {
Logger.getGlobal().log(Level.SEVERE, "Could not create Event.", e);
throw new RuntimeException(e);
}
}
/**
* Sends info to the match browser so clients can see it
*/
public void alertMatchBrowser(){
MatchBrowserInterface matchBrowserInterface = new MatchBrowserInterface();
try{
matchBrowserInterface.startSendingHostData(this.game.getHostedGameData(), udpSocket);
}catch (IOException e){
System.err.println("failed to send out hosted game info");
}
}
public void endEvent() throws IOException {
game.endEvent();
}
/**
* Connect to a socket
* @param address address of the server
* @param port port that the server is run off
*/
public void connectSocket(String address, int port) {
try{
Socket socket = new Socket(address, port);
hostWrapper.setVisible(false);
parent.enterLobby(socket, true);
} catch (IOException e) { /* Never reached */ }
}
public AnchorPane startWrapper(){
return hostWrapper;
}
/**
* Hosts a game.
*/
public void hostGame(DatagramSocket udpSocket){
hostWrapper.setVisible(true);
this.udpSocket = udpSocket;
}
public void menuBtnPressed(){
hostWrapper.setVisible(false);
parent.enterTitle();
}
}