fixed not working single mode, added read port from textline when client host server

This commit is contained in:
Cedric Beck
2024-10-12 16:17:46 +02:00
parent 9b85030050
commit bb1e3858bb
9 changed files with 15 additions and 13 deletions

View File

@@ -32,7 +32,7 @@ robot.targets=2, 0,\
2, 6
#
# Delay in milliseconds between each shot fired by the RobotClient.
robot.delay=500
robot.delay=4000
#
# The dimensions of the game map used in single mode.
# 'map.width' defines the number of columns, and 'map.height' defines the number of rows.

View File

@@ -84,9 +84,7 @@ public void initialize(AppStateManager stateManager, Application app) {
* Plays the shell flying sound effect.
*/
public void shellFly() {
System.out.println("shellFly");
if (isEnabled() && shellFlyingSound != null) {
System.out.println("play shell");
shellFlyingSound.playInstance();
}
}

View File

@@ -134,7 +134,7 @@ private Object initNetwork() {
private Object initServer() {
try {
server = new BattleshipServerClient();
server.run();
server.run(Integer.parseInt(port.getText()));
return null;
}
catch (Exception e) {

View File

@@ -85,8 +85,8 @@ public boolean isReady() {
/**
* Starts the server and continuously processes incoming messages.
*/
public void run() {
startServer();
public void run(int port) {
startServer(port);
while (true)
processNextMessage();
}
@@ -94,10 +94,10 @@ public void run() {
/**
* Starts the server by creating a network server on the specified port.
*/
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();

View File

@@ -30,7 +30,6 @@ class BattleState extends ClientState {
*/
public BattleState(ClientGameLogic logic, boolean myTurn) {
super(logic);
System.out.println("battle state");
this.myTurn = myTurn;
}

View File

@@ -27,7 +27,6 @@ public class ShootingState extends ClientState {
*/
public ShootingState(ClientGameLogic logic, Shell shell, boolean myTurn, EffectMessage msg) {
super(logic);
System.out.println("shooting state");
this.msg = msg;
this.myTurn = myTurn;
this.shell = shell;

View File

@@ -167,6 +167,7 @@ public void received(AnimationFinishedMessage msg, int from) {
LOGGER.log(Level.ERROR, "animation finished not allowed in {0}", state);
}
else {
LOGGER.log(Level.DEBUG, "anim received from {0}", getPlayerById(from));
Player player = getPlayerById(from);
if (!waitPlayers.add(player)) {
LOGGER.log(Level.ERROR, "{0} already sent animation finished", player); //NON-NLS
@@ -222,8 +223,11 @@ private boolean checkMap(List<Battleship> ships) {
public void received(ShootMessage msg, int from) {
if (state != ServerState.BATTLE)
LOGGER.log(Level.ERROR, "shoot not allowed in {0}", state); //NON-NLS
else
else{
setState(ServerState.ANIMATION);
shoot(getPlayerById(from), msg.getPosition());
}
}
/**
@@ -283,6 +287,5 @@ else if (selectedShip.isDestroyed()) {
send(otherPlayer, EffectMessage.hit(false, pos));
}
}
setState(ServerState.ANIMATION);
}
}

View File

@@ -66,7 +66,7 @@ public void received(MapMessage msg, int from) {
@Override
public void received(AnimationFinishedMessage msg, int from) {
copiedMessage = msg;
}
/**

View File

@@ -1,6 +1,7 @@
package pp.battleship.game.singlemode;
import pp.battleship.game.client.BattleshipClient;
import pp.battleship.message.client.AnimationFinishedMessage;
import pp.battleship.message.client.MapMessage;
import pp.battleship.message.client.ShootMessage;
import pp.battleship.message.server.EffectMessage;
@@ -71,6 +72,7 @@ public void run() {
* Makes the RobotClient take a shot by sending a ShootMessage with the target position.
*/
private void robotShot() {
connection.sendRobotMessage(new ShootMessage(getShotPosition()));
}
@@ -121,6 +123,7 @@ public void received(StartBattleMessage msg) {
@Override
public void received(EffectMessage msg) {
LOGGER.log(Level.INFO, "Received EffectMessage: {0}", msg); //NON-NLS
connection.sendRobotMessage(new AnimationFinishedMessage());
if (msg.isMyTurn())
shoot();
}