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">
|
<component name="ProjectRunConfigurationManager">
|
||||||
<configuration default="false" name="BattleshipApp (Mac)" type="Application" factoryName="Application"
|
<configuration default="false" name="BattleshipApp (Mac)" type="Application" factoryName="Application" singleton="false">
|
||||||
singleton="false">
|
<option name="MAIN_CLASS_NAME" value="pp.battleship.client.BattleshipApp" />
|
||||||
<option name="MAIN_CLASS_NAME" value="pp.battleship.client.BattleshipApp"/>
|
<module name="Projekte.battleship.client.main" />
|
||||||
<module name="Projekte.battleship.client.main"/>
|
<option name="VM_PARAMETERS" value="-XstartOnFirstThread" />
|
||||||
<option name="VM_PARAMETERS" value="-XstartOnFirstThread"/>
|
<option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$" />
|
||||||
<option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$"/>
|
<extension name="coverage">
|
||||||
<extension name="coverage">
|
<pattern>
|
||||||
<pattern>
|
<option name="PATTERN" value="pp.battleship.client.server.*" />
|
||||||
<option name="PATTERN" value="pp.battleship.client.*"/>
|
<option name="ENABLED" value="true" />
|
||||||
<option name="ENABLED" value="true"/>
|
</pattern>
|
||||||
</pattern>
|
</extension>
|
||||||
</extension>
|
<method v="2">
|
||||||
<method v="2">
|
<option name="Make" enabled="true" />
|
||||||
<option name="Make" enabled="true"/>
|
</method>
|
||||||
</method>
|
</configuration>
|
||||||
</configuration>
|
|
||||||
</component>
|
</component>
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
<option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$" />
|
<option name="WORKING_DIRECTORY" value="$MODULE_WORKING_DIR$" />
|
||||||
<extension name="coverage">
|
<extension name="coverage">
|
||||||
<pattern>
|
<pattern>
|
||||||
<option name="PATTERN" value="pp.battleship.client.*" />
|
<option name="PATTERN" value="pp.battleship.client.server.*" />
|
||||||
<option name="ENABLED" value="true" />
|
<option name="ENABLED" value="true" />
|
||||||
</pattern>
|
</pattern>
|
||||||
</extension>
|
</extension>
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
import com.simsilica.lemur.*;
|
import com.simsilica.lemur.*;
|
||||||
import com.simsilica.lemur.component.SpringGridLayout;
|
import com.simsilica.lemur.component.SpringGridLayout;
|
||||||
|
import pp.battleship.client.server.BattleshipServer;
|
||||||
import pp.dialog.Dialog;
|
import pp.dialog.Dialog;
|
||||||
import pp.dialog.DialogBuilder;
|
import pp.dialog.DialogBuilder;
|
||||||
import pp.dialog.SimpleDialog;
|
import pp.dialog.SimpleDialog;
|
||||||
@@ -38,6 +39,7 @@ class NetworkDialog extends SimpleDialog {
|
|||||||
private BattleshipServer battleshipServer;
|
private BattleshipServer battleshipServer;
|
||||||
private boolean GUI = true;
|
private boolean GUI = true;
|
||||||
private Container menu;
|
private Container menu;
|
||||||
|
private boolean serverToggle = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new NetworkDialog.
|
* Constructs a new NetworkDialog.
|
||||||
@@ -61,41 +63,20 @@ class NetworkDialog extends SimpleDialog {
|
|||||||
*/
|
*/
|
||||||
private void menu() {
|
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");
|
Button connectGame = new Button("Multiplayer");
|
||||||
connectGame.addClickCommands(source -> hostServer());
|
connectGame.addClickCommands(source -> serverConnectGUI());
|
||||||
Checkbox serverHost = new Checkbox("Host own Game");
|
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(serverHost);
|
||||||
menu.addChild(connectGame);
|
menu.addChild(connectGame);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* work in progress
|
* Logic for the ServerGUI
|
||||||
*/
|
*/
|
||||||
private void soloGame() {
|
private void serverConnectGUI() {
|
||||||
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() {
|
|
||||||
LOGGER.log(Level.INFO, "Hosting Server...");
|
LOGGER.log(Level.INFO, "Hosting Server...");
|
||||||
|
|
||||||
if (GUI) {
|
if (GUI) {
|
||||||
@@ -114,13 +95,23 @@ private void hostServer() {
|
|||||||
DialogBuilder.simple(app.getDialogManager())
|
DialogBuilder.simple(app.getDialogManager())
|
||||||
.setTitle(lookup("server.dialog"))
|
.setTitle(lookup("server.dialog"))
|
||||||
.setExtension(d -> d.addChild(input))
|
.setExtension(d -> d.addChild(input))
|
||||||
.setOkButton(lookup("button.connect"), d -> connectServer())
|
.setOkButton(lookup("button.connect"), d -> connect())
|
||||||
.setOkClose(false)
|
.setOkClose(false)
|
||||||
.build(this);
|
.build(this);
|
||||||
GUI = false;
|
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.
|
* 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 *
|
* Checks if the Server is running or starts the Server and handles Exceptions *
|
||||||
*/
|
*/
|
||||||
private void connect() {
|
private void hostServer() {
|
||||||
if (battleshipServer == null) {
|
if (battleshipServer == null) {
|
||||||
startServer(); // Starts the server in a new thread
|
startServer(); // Starts the server in a new thread
|
||||||
try {
|
try {
|
||||||
@@ -154,6 +145,13 @@ private void connect() {
|
|||||||
connectServer();
|
connectServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle Method for Boolean
|
||||||
|
*/
|
||||||
|
private void toggleServer(){
|
||||||
|
serverToggle = !serverToggle;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows Client to Start a Server in a new Thread
|
* Allows Client to Start a Server in a new Thread
|
||||||
*/
|
*/
|
||||||
@@ -162,7 +160,7 @@ private void startServer() {
|
|||||||
try {
|
try {
|
||||||
// Initialize and run the server
|
// Initialize and run the server
|
||||||
battleshipServer = new BattleshipServer();
|
battleshipServer = new BattleshipServer();
|
||||||
battleshipServer.run();
|
battleshipServer.run(Integer.parseInt(port.getText()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.log(Level.ERROR, e);
|
LOGGER.log(Level.ERROR, e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,12 @@
|
|||||||
// (c) Mark Minas (mark.minas@unibw.de)
|
// (c) Mark Minas (mark.minas@unibw.de)
|
||||||
////////////////////////////////////////
|
////////////////////////////////////////
|
||||||
|
|
||||||
package pp.battleship.client;
|
package pp.battleship.client.server;
|
||||||
|
|
||||||
import com.jme3.network.*;
|
import com.jme3.network.*;
|
||||||
import com.jme3.network.serializing.Serializer;
|
import com.jme3.network.serializing.Serializer;
|
||||||
import pp.battleship.BattleshipConfig;
|
import pp.battleship.BattleshipConfig;
|
||||||
|
import pp.battleship.client.server.ReceivedMessage;
|
||||||
import pp.battleship.game.server.Player;
|
import pp.battleship.game.server.Player;
|
||||||
import pp.battleship.game.server.ServerGameLogic;
|
import pp.battleship.game.server.ServerGameLogic;
|
||||||
import pp.battleship.game.server.ServerSender;
|
import pp.battleship.game.server.ServerSender;
|
||||||
@@ -61,22 +62,22 @@ public class BattleshipServer implements MessageListener<HostedConnection>, Conn
|
|||||||
/**
|
/**
|
||||||
* Creates the server.
|
* Creates the server.
|
||||||
*/
|
*/
|
||||||
BattleshipServer() {
|
public BattleshipServer() {
|
||||||
config.readFromIfExists(CONFIG_FILE);
|
config.readFromIfExists(CONFIG_FILE);
|
||||||
LOGGER.log(Level.INFO, "Configuration: {0}", config); //NON-NLS
|
LOGGER.log(Level.INFO, "Configuration: {0}", config); //NON-NLS
|
||||||
logic = new ServerGameLogic(this, config);
|
logic = new ServerGameLogic(this, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run(int port) {
|
||||||
startServer();
|
startServer(port);
|
||||||
while (true)
|
while (true)
|
||||||
processNextMessage();
|
processNextMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startServer() {
|
private void startServer(int port) {
|
||||||
try {
|
try {
|
||||||
LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS
|
LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS
|
||||||
myServer = Network.createServer(config.getPort());
|
myServer = Network.createServer(port);
|
||||||
initializeSerializables();
|
initializeSerializables();
|
||||||
myServer.start();
|
myServer.start();
|
||||||
registerListeners();
|
registerListeners();
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
// (c) Mark Minas (mark.minas@unibw.de)
|
// (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.ClientInterpreter;
|
||||||
import pp.battleship.message.client.ClientMessage;
|
import pp.battleship.message.client.ClientMessage;
|
||||||
Reference in New Issue
Block a user