dice button now works and update dice images accordingly

This commit is contained in:
Johannes Schmelz
2024-11-26 14:59:48 +01:00
parent d6ce859fcd
commit 27a0ab52e6
11 changed files with 96 additions and 27 deletions

View File

@@ -25,6 +25,7 @@ import pp.monopoly.model.Board;
import pp.monopoly.model.IntPoint;
import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.notification.ClientStateEvent;
import pp.monopoly.notification.DiceRollEvent;
import pp.monopoly.notification.GameEvent;
import pp.monopoly.notification.GameEventBroker;
import pp.monopoly.notification.GameEventListener;
@@ -214,9 +215,9 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
*/
@Override
public void received(DiceResult msg) {
//Set the dice images
playSound(Sound.DICE_ROLL);
System.out.println("Message kam an");
notifyListeners(new DiceRollEvent(msg.getRollResult().get(0), msg.getRollResult().get(1)));
}
/**
@@ -226,7 +227,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
*/
@Override
public void received(EventDrawCard msg) {
// Kartenlogik
playSound(Sound.EVENT_CARD);
}

View File

@@ -395,6 +395,7 @@ public class Player implements FieldVisitor<Void>{
* @return the result of a dice roll (1 to 6)
*/
private static int rollDice() {
System.out.println("Gewuerfelt");
return random.nextInt(6) + 1;
}
}
@@ -439,7 +440,7 @@ public class Player implements FieldVisitor<Void>{
@Override
public DiceResult rollDice() {
List<Integer> roll = List.of(Dice.rollDice(), Dice.rollDice());
rollResult = new DiceResult(roll);
rollResult = new DiceResult(roll.get(0), roll.get(1));
return rollResult;
}
@@ -491,7 +492,7 @@ public class Player implements FieldVisitor<Void>{
@Override
public DiceResult rollDice() {
List<Integer> roll = List.of(Dice.rollDice(), Dice.rollDice());
rollResult = new DiceResult(roll);
rollResult = new DiceResult(roll.get(0), roll.get(1));
if (rollResult.isDoublets()) {
state = new ActiveState();
} else if (DoubletsCounter == 0) {

View File

@@ -166,7 +166,6 @@ public class PlayerHandler {
* @return the player with the required id
*/
public Player getPlayerById(int id) {
System.out.println("TEST");
for (Player player : players) {
if (player.getId() == id) return player;
System.out.println(player.getId());

View File

@@ -220,7 +220,8 @@ public class ServerGameLogic implements ClientInterpreter {
@Override
public void received(RollDice msg, int from) {
Player player = playerHandler.getPlayerById(from);
if (player != null && state == ServerState.INGAME) {
if (player != null) {
System.out.println("Ergebniss gesendet");
send(player, player.rollDice());
}
}

View File

@@ -11,7 +11,7 @@ public class RollDice extends ClientMessage{
/**
* Default constructor for serialization purposes.
*/
private RollDice() { /* empty */ }
public RollDice() { /* empty */ }
@Override
public void accept(ClientInterpreter interpreter, int from) {

View File

@@ -7,19 +7,24 @@ import com.jme3.network.serializing.Serializable;
@Serializable
public class DiceResult extends ServerMessage{
private List<Integer> rollResult;
private final int a;
private final int b;
/**
* Default constructor for serialization purposes.
*/
private DiceResult() { /* empty */ }
private DiceResult() {
a = 1;
b = 1;
}
public DiceResult(List<Integer> rollResult) {
this.rollResult = rollResult;
public DiceResult(int a, int b) {
this.a = a;
this.b = b;
}
public List<Integer> getRollResult() {
return rollResult;
return List.of(a,b);
}
@Override
@@ -34,10 +39,10 @@ public class DiceResult extends ServerMessage{
}
public boolean isDoublets() {
return rollResult.get(0) == rollResult.get(1);
return a == b;
}
public int calcTotal() {
return rollResult.get(0)+rollResult.get(1);
return a+b;
}
}

View File

@@ -0,0 +1,10 @@
package pp.monopoly.notification;
public record DiceRollEvent(int a, int b) implements GameEvent{
@Override
public void notifyListener(GameEventListener listener) {
listener.receivedEvent(this);
}
}

View File

@@ -45,4 +45,6 @@ public interface GameEventListener {
* @param event the received event
*/
default void receivedEvent(ClientStateEvent event) { /* do nothing */ }
default void receivedEvent(DiceRollEvent event) { /*Do nothing */}
}