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 2, 6
# #
# Delay in milliseconds between each shot fired by the RobotClient. # 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. # 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. # '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. * Plays the shell flying sound effect.
*/ */
public void shellFly() { public void shellFly() {
System.out.println("shellFly");
if (isEnabled() && shellFlyingSound != null) { if (isEnabled() && shellFlyingSound != null) {
System.out.println("play shell");
shellFlyingSound.playInstance(); shellFlyingSound.playInstance();
} }
} }

View File

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

View File

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

View File

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

View File

@@ -27,7 +27,6 @@ public class ShootingState extends ClientState {
*/ */
public ShootingState(ClientGameLogic logic, Shell shell, boolean myTurn, EffectMessage msg) { public ShootingState(ClientGameLogic logic, Shell shell, boolean myTurn, EffectMessage msg) {
super(logic); super(logic);
System.out.println("shooting state");
this.msg = msg; this.msg = msg;
this.myTurn = myTurn; this.myTurn = myTurn;
this.shell = shell; 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); LOGGER.log(Level.ERROR, "animation finished not allowed in {0}", state);
} }
else { else {
LOGGER.log(Level.DEBUG, "anim received from {0}", getPlayerById(from));
Player player = getPlayerById(from); Player player = getPlayerById(from);
if (!waitPlayers.add(player)) { if (!waitPlayers.add(player)) {
LOGGER.log(Level.ERROR, "{0} already sent animation finished", player); //NON-NLS 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) { public void received(ShootMessage msg, int from) {
if (state != ServerState.BATTLE) if (state != ServerState.BATTLE)
LOGGER.log(Level.ERROR, "shoot not allowed in {0}", state); //NON-NLS LOGGER.log(Level.ERROR, "shoot not allowed in {0}", state); //NON-NLS
else else{
setState(ServerState.ANIMATION);
shoot(getPlayerById(from), msg.getPosition()); shoot(getPlayerById(from), msg.getPosition());
}
} }
/** /**
@@ -283,6 +287,5 @@ else if (selectedShip.isDestroyed()) {
send(otherPlayer, EffectMessage.hit(false, pos)); send(otherPlayer, EffectMessage.hit(false, pos));
} }
} }
setState(ServerState.ANIMATION);
} }
} }

View File

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

View File

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