fix Aufgabe 11
edited class NetworkDialog to change the logic that the checkbox works now as intended moved BattleshipServer and ReceivedMessage into own folders to separate server from client
This commit is contained in:
@@ -1,18 +1,17 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="BattleshipApp (Mac)" type="Application" factoryName="Application"
|
||||
singleton="false">
|
||||
<option name="MAIN_CLASS_NAME" value="pp.battleship.client.BattleshipApp"/>
|
||||
<module name="Projekte.battleship.client.main"/>
|
||||
<option name="VM_PARAMETERS" value="-XstartOnFirstThread"/>
|
||||
<option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$"/>
|
||||
<extension name="coverage">
|
||||
<pattern>
|
||||
<option name="PATTERN" value="pp.battleship.client.*"/>
|
||||
<option name="ENABLED" value="true"/>
|
||||
</pattern>
|
||||
</extension>
|
||||
<method v="2">
|
||||
<option name="Make" enabled="true"/>
|
||||
</method>
|
||||
</configuration>
|
||||
<configuration default="false" name="BattleshipApp (Mac)" type="Application" factoryName="Application" singleton="false">
|
||||
<option name="MAIN_CLASS_NAME" value="pp.battleship.client.BattleshipApp" />
|
||||
<module name="Projekte.battleship.client.main" />
|
||||
<option name="VM_PARAMETERS" value="-XstartOnFirstThread" />
|
||||
<option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$" />
|
||||
<extension name="coverage">
|
||||
<pattern>
|
||||
<option name="PATTERN" value="pp.battleship.client.server.*" />
|
||||
<option name="ENABLED" value="true" />
|
||||
</pattern>
|
||||
</extension>
|
||||
<method v="2">
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -8,7 +8,7 @@
|
||||
<option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$" />
|
||||
<extension name="coverage">
|
||||
<pattern>
|
||||
<option name="PATTERN" value="pp.battleship.client.*" />
|
||||
<option name="PATTERN" value="pp.battleship.client.server.*" />
|
||||
<option name="ENABLED" value="true" />
|
||||
</pattern>
|
||||
</extension>
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
import com.simsilica.lemur.*;
|
||||
import com.simsilica.lemur.component.SpringGridLayout;
|
||||
import pp.battleship.client.server.BattleshipServer;
|
||||
import pp.dialog.Dialog;
|
||||
import pp.dialog.DialogBuilder;
|
||||
import pp.dialog.SimpleDialog;
|
||||
@@ -38,6 +39,7 @@ class NetworkDialog extends SimpleDialog {
|
||||
private BattleshipServer battleshipServer;
|
||||
private boolean GUI = true;
|
||||
private Container menu;
|
||||
private boolean serverToggle = false;
|
||||
|
||||
/**
|
||||
* Constructs a new NetworkDialog.
|
||||
@@ -61,41 +63,20 @@ class NetworkDialog extends SimpleDialog {
|
||||
*/
|
||||
private void menu() {
|
||||
|
||||
Button soloGame = new Button("Solo Game");
|
||||
soloGame.addClickCommands(source -> soloGame());
|
||||
Button preview = new Button("Preview");
|
||||
preview.addClickCommands(source -> preview());
|
||||
Button connectGame = new Button("Multiplayer");
|
||||
connectGame.addClickCommands(source -> hostServer());
|
||||
connectGame.addClickCommands(source -> serverConnectGUI());
|
||||
Checkbox serverHost = new Checkbox("Host own Game");
|
||||
serverHost.addClickCommands(source -> connect());
|
||||
serverHost.addClickCommands(source -> toggleServer());
|
||||
|
||||
|
||||
menu.addChild(preview);
|
||||
menu.addChild(soloGame);
|
||||
menu.addChild(serverHost);
|
||||
menu.addChild(connectGame);
|
||||
}
|
||||
|
||||
/**
|
||||
* work in progress
|
||||
* Logic for the ServerGUI
|
||||
*/
|
||||
private void soloGame() {
|
||||
LOGGER.log(Level.INFO, "Starting Solo Game...");
|
||||
//TODO implement Game against KI
|
||||
}
|
||||
|
||||
/**
|
||||
* work in progress
|
||||
*/
|
||||
private void preview() {
|
||||
LOGGER.log(Level.INFO, "Starting Preview...");
|
||||
//TODO implement Preview of the Game like in the "Einführungsveranstaltung"
|
||||
}
|
||||
|
||||
/**
|
||||
* Logic for the Menu Button Multiplayer
|
||||
*/
|
||||
private void hostServer() {
|
||||
private void serverConnectGUI() {
|
||||
LOGGER.log(Level.INFO, "Hosting Server...");
|
||||
|
||||
if (GUI) {
|
||||
@@ -114,13 +95,23 @@ private void hostServer() {
|
||||
DialogBuilder.simple(app.getDialogManager())
|
||||
.setTitle(lookup("server.dialog"))
|
||||
.setExtension(d -> d.addChild(input))
|
||||
.setOkButton(lookup("button.connect"), d -> connectServer())
|
||||
.setOkButton(lookup("button.connect"), d -> connect())
|
||||
.setOkClose(false)
|
||||
.build(this);
|
||||
GUI = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to server if serverToggle is false hosts otherwise
|
||||
*/
|
||||
private void connect(){
|
||||
if (serverToggle) {
|
||||
hostServer();
|
||||
} else {
|
||||
connectServer();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the action for the connect button in the connection dialog.
|
||||
@@ -141,7 +132,7 @@ private void connectServer() {
|
||||
/**
|
||||
* Checks if the Server is running or starts the Server and handles Exceptions *
|
||||
*/
|
||||
private void connect() {
|
||||
private void hostServer() {
|
||||
if (battleshipServer == null) {
|
||||
startServer(); // Starts the server in a new thread
|
||||
try {
|
||||
@@ -154,6 +145,13 @@ private void connect() {
|
||||
connectServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle Method for Boolean
|
||||
*/
|
||||
private void toggleServer(){
|
||||
serverToggle = !serverToggle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows Client to Start a Server in a new Thread
|
||||
*/
|
||||
@@ -162,7 +160,7 @@ private void startServer() {
|
||||
try {
|
||||
// Initialize and run the server
|
||||
battleshipServer = new BattleshipServer();
|
||||
battleshipServer.run();
|
||||
battleshipServer.run(Integer.parseInt(port.getText()));
|
||||
} catch (Exception e) {
|
||||
LOGGER.log(Level.ERROR, e);
|
||||
}
|
||||
|
||||
@@ -5,11 +5,12 @@
|
||||
// (c) Mark Minas (mark.minas@unibw.de)
|
||||
////////////////////////////////////////
|
||||
|
||||
package pp.battleship.client;
|
||||
package pp.battleship.client.server;
|
||||
|
||||
import com.jme3.network.*;
|
||||
import com.jme3.network.serializing.Serializer;
|
||||
import pp.battleship.BattleshipConfig;
|
||||
import pp.battleship.client.server.ReceivedMessage;
|
||||
import pp.battleship.game.server.Player;
|
||||
import pp.battleship.game.server.ServerGameLogic;
|
||||
import pp.battleship.game.server.ServerSender;
|
||||
@@ -61,22 +62,22 @@ public class BattleshipServer implements MessageListener<HostedConnection>, Conn
|
||||
/**
|
||||
* Creates the server.
|
||||
*/
|
||||
BattleshipServer() {
|
||||
public BattleshipServer() {
|
||||
config.readFromIfExists(CONFIG_FILE);
|
||||
LOGGER.log(Level.INFO, "Configuration: {0}", config); //NON-NLS
|
||||
logic = new ServerGameLogic(this, config);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
startServer();
|
||||
public void run(int port) {
|
||||
startServer(port);
|
||||
while (true)
|
||||
processNextMessage();
|
||||
}
|
||||
|
||||
private void startServer() {
|
||||
private void startServer(int port) {
|
||||
try {
|
||||
LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS
|
||||
myServer = Network.createServer(config.getPort());
|
||||
myServer = Network.createServer(port);
|
||||
initializeSerializables();
|
||||
myServer.start();
|
||||
registerListeners();
|
||||
@@ -5,7 +5,7 @@
|
||||
// (c) Mark Minas (mark.minas@unibw.de)
|
||||
////////////////////////////////////////
|
||||
|
||||
package pp.battleship.client;
|
||||
package pp.battleship.client.server;
|
||||
|
||||
import pp.battleship.message.client.ClientInterpreter;
|
||||
import pp.battleship.message.client.ClientMessage;
|
||||
@@ -38,4 +38,4 @@ dialog.error=Error
|
||||
dialog.question=Question
|
||||
port.must.be.integer=Port must be an integer number
|
||||
map.doesnt.fit=The map doesn't fit to this game
|
||||
map.is.invalid=The Map is invalid
|
||||
map.is.invalid=The Map is invalid
|
||||
|
||||
Reference in New Issue
Block a user