fixed JailState

This commit is contained in:
Johannes Schmelz 2024-12-01 15:50:09 +01:00
parent 5143e21ba6
commit f2a6888fb2
5 changed files with 101 additions and 65 deletions

View File

@ -25,6 +25,7 @@ import pp.monopoly.model.fields.GateField;
import pp.monopoly.notification.EventCardEvent;
import pp.monopoly.notification.GameEventListener;
import pp.monopoly.notification.PopUpEvent;
import pp.monopoly.notification.Sound;
import pp.monopoly.notification.UpdatePlayerView;
/**
@ -47,7 +48,7 @@ public class TestWorld implements GameEventListener {
this.app = app;
this.playerHandler = app.getGameLogic().getPlayerHandler(); // Hole den PlayerHandler
app.getGameLogic().addListener(this);
cameraController = new CameraController(app.getCamera(), playerHandler); // Übergebe PlayerHandler
cameraController = new CameraController(app.getCamera()); // Übergebe PlayerHandler
}
/**
@ -288,7 +289,7 @@ public class TestWorld implements GameEventListener {
}
});
}
}, 5000); // Verzögerung in Millisekunden
}, 2500); // Verzögerung in Millisekunden
} else if (event.msg().equals("Winner")) {
new WinnerPopUp(app).open();
} else if (event.msg().equals("Looser")) {
@ -304,9 +305,12 @@ public class TestWorld implements GameEventListener {
timer.schedule(new TimerTask() {
@Override
public void run() {
app.enqueue(() -> new EventCardPopup(app, event.description()).open());
app.enqueue(() -> {
app.getGameLogic().playSound(Sound.EVENT_CARD);
new EventCardPopup(app, event.description()).open();
});
}
}, 5000); // 5 Sekunden Verzögerung für Event-Popups
}, 2500); // 5 Sekunden Verzögerung für Event-Popups
}

View File

@ -222,7 +222,6 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
*/
@Override
public void received(EventDrawCard msg) {
playSound(Sound.EVENT_CARD);
notifyListeners(new EventCardEvent(msg.getCardDescription()));
}
@ -265,7 +264,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
playSound(Sound.GULAG);
} else {
System.out.println("NO MORE JAIL");
}
}

View File

@ -15,9 +15,11 @@ import java.util.Set;
import com.jme3.network.serializing.Serializable;
import pp.monopoly.game.client.WaitForTurnState;
import pp.monopoly.message.server.BuyPropertyRequest;
import pp.monopoly.message.server.DiceResult;
import pp.monopoly.message.server.EventDrawCard;
import pp.monopoly.message.server.JailEvent;
import pp.monopoly.message.server.NextPlayerTurn;
import pp.monopoly.message.server.NotificationMessage;
import pp.monopoly.message.server.PlayerStatusUpdate;
@ -50,8 +52,6 @@ public class Player implements FieldVisitor<Void>{
private DiceResult rollResult;
private transient final PlayerHandler handler;
private transient PlayerState state = new WaitForTurnState();
private int doubletscounter = 0;
private boolean mayRollDice = false;
/**
* Default constructor for serialization purposes.
@ -136,15 +136,17 @@ public class Player implements FieldVisitor<Void>{
public int getFieldID() {
return fieldID;
}
void setActive() {
state = new ActiveState();
doubletscounter = 0;
mayRollDice = true;
System.out.println("State: "+state.getClass().getName());
if(!(state instanceof JailState)) {
state = new ActiveState();
}
}
boolean finishTurn() {
if(canFinishTurn()) {
state = new WaitForTurnState();
if (state instanceof ActiveState) state = new WaitForTurnState();
return true;
}
else return false;
@ -177,7 +179,7 @@ public class Player implements FieldVisitor<Void>{
* @return the new position
*/
public int setPosition(int position){
if(position < 40 && position > 0) {
if(position < 40 && position >= 0) {
fieldID = position;
figure.moveTo(fieldID);
handler.getLogic().send(this, new PlayerStatusUpdate(handler));
@ -301,7 +303,7 @@ public class Player implements FieldVisitor<Void>{
@Override
public Void visit(BuildingProperty field) {
if(field.getOwner() == null) {
getHandler().getLogic().send(this, new BuyPropertyRequest());
if (field.getPrice() <= accountBalance) getHandler().getLogic().send(this, new BuyPropertyRequest());
} else if (field.getOwner() != this){
int rent = field.calcRent();
field.getOwner().earnMoney(rent);
@ -317,7 +319,7 @@ public class Player implements FieldVisitor<Void>{
@Override
public Void visit(FoodField field) {
if(field.getOwner() == null) {
getHandler().getLogic().send(this, new BuyPropertyRequest());
if (field.getPrice() <= accountBalance) getHandler().getLogic().send(this, new BuyPropertyRequest());
} else {
int factor = 4;
if (field.getOwner().getNumProp(field) == 2) {
@ -337,7 +339,7 @@ public class Player implements FieldVisitor<Void>{
@Override
public Void visit(GateField field) {
if(field.getOwner() == null) {
getHandler().getLogic().send(this, new BuyPropertyRequest());
if (field.getPrice() <= accountBalance) getHandler().getLogic().send(this, new BuyPropertyRequest());
} else {
int rent = field.calcRent() * field.getOwner().getNumProp(field);
@ -373,8 +375,7 @@ public class Player implements FieldVisitor<Void>{
@Override
public Void visit(WacheField field) {
setPosition(10);
jail();
moveToJail();
return null;
}
@ -403,8 +404,14 @@ public class Player implements FieldVisitor<Void>{
return properties;
}
public void jail() {
/**
* Sends the player to jail by setting position and state.
*/
public void moveToJail() {
setPosition(10); // Jail position on the board
state = new JailState();
System.out.println("JAIL EVENT");
handler.getLogic().send(this, new JailEvent(true));
}
/**
@ -445,7 +452,7 @@ public class Player implements FieldVisitor<Void>{
return total;
}
/**
/**
* Inner class for dice functionality in the game.
* Rolls random dice values.
*/
@ -462,36 +469,15 @@ public class Player implements FieldVisitor<Void>{
}
}
/**
* Rolls two dice, handles movement logic, and checks for doublets.
*
* @return the DiceResult of the roll
*/
DiceResult rollDice() {
if (!mayRollDice) {
throw new IllegalStateException("Player is not allowed to roll dice at this moment.");
/**
* Rolls two dice, handles movement logic, and checks for doublets.
*
* @return the DiceResult of the roll
*/
DiceResult rollDice() {
return state.rollDice();
}
rollResult = state.rollDice();
mayRollDice = false;
if (rollResult.isDoublets()) {
doubletscounter++;
mayRollDice = true; // Allow another roll for doublets
if (doubletscounter >= 3) {
setPosition(10); // Send to jail on third doublet
doubletscounter = 0; // Reset doublets counter
} else {
getHandler().getLogic().send(this, new NextPlayerTurn());
}
} else {
doubletscounter = 0; // Reset counter if no doublets
}
return rollResult;
}
/**
* A interface representing the PlayerStates
@ -521,12 +507,39 @@ DiceResult rollDice() {
*/
private class ActiveState implements PlayerState {
private int doubletscounter = 3;
private boolean mayRollDice = true;
@Override
public DiceResult rollDice() {
if (!mayRollDice) {
throw new IllegalStateException("Player is not allowed to roll dice at this moment.");
}
List<Integer> roll = List.of(Dice.rollDice(), Dice.rollDice());
rollResult = new DiceResult(roll.get(0), roll.get(1));
System.out.println(rollResult.calcTotal());
move(rollResult.calcTotal());
mayRollDice = false;
// Check for doublets
if (rollResult.isDoublets()) {
doubletscounter--; // Decrement doublets counter
if (doubletscounter <= 0) {
// Player rolls third doublet -> move to jail
moveToJail();
doubletscounter = 3; // Reset doublets counter
} else {
// Player rolls doublets but can roll again
mayRollDice = true;
move(rollResult.calcTotal()); // Move player for this roll
getHandler().getLogic().send(Player.this, new NextPlayerTurn());
}
} else {
// No doublets -> move normally and reset doublets counter
move(rollResult.calcTotal());
}
return rollResult;
}
@ -547,36 +560,56 @@ DiceResult rollDice() {
* Set when in Gulag
*/
private class JailState implements PlayerState {
private int DoubletsCounter = 3;
private int remainingAttempts = 3;
@Override
public DiceResult rollDice() {
if (remainingAttempts <= 0) {
return null;
}
List<Integer> roll = List.of(Dice.rollDice(), Dice.rollDice());
rollResult = new DiceResult(roll.get(0), roll.get(1));
if (rollResult.isDoublets()) {
state = new ActiveState();
} else if (DoubletsCounter == 0) {
handler.getLogic().send(Player.this, new DiceResult (rollResult.getRollResult().get(0), rollResult.getRollResult().get(1)));
if (rollResult.isDoublets()) {
handler.getLogic().send(Player.this, new JailEvent(false));
state = new ActiveState();
} else {
DoubletsCounter--;
remainingAttempts--;
if (remainingAttempts <= 0) {
handler.getLogic().send(Player.this, new NotificationMessage("jailpay"));
} else {
handler.getLogic().send(Player.this, new NotificationMessage("jailtryagain"));
}
}
System.out.println("Feld:"+fieldID);
return rollResult;
}
@Override
public void payBail() {
pay(500);
state = new ActiveState();
if (accountBalance >= 500) {
pay(500);
handler.getLogic().send(Player.this, new NotificationMessage(""));
state = new ActiveState();
} else {
handler.getLogic().send(Player.this, new NotificationMessage(""));
}
}
@Override
public void useJailCard() {
getOutOfJailCard--;
state = new ActiveState();
if (getOutOfJailCard > 0) {
removeJailCard();
handler.getLogic().send(Player.this, new NotificationMessage(""));
state = new ActiveState();
} else {
handler.getLogic().send(Player.this, new NotificationMessage(""));
}
}
}
private class WaitForTurnState implements PlayerState {
@Override

View File

@ -241,13 +241,11 @@ public class DeckHelper{
}
private void schimmelGulak(Player player) {
player.setPosition(10);
player.jail();
player.moveToJail();
}
private void partynachtGulak(Player player) {
player.setPosition(10);
player.jail();
player.moveToJail();
}
private void jahresabschlussantreten(Player player) {

View File

@ -41,6 +41,7 @@ import pp.monopoly.message.server.BuyPropertyRequest;
import pp.monopoly.message.server.DiceResult;
import pp.monopoly.message.server.EventDrawCard;
import pp.monopoly.message.server.GameStart;
import pp.monopoly.message.server.JailEvent;
import pp.monopoly.message.server.NextPlayerTurn;
import pp.monopoly.message.server.NotificationMessage;
import pp.monopoly.message.server.PlayerStatusUpdate;
@ -172,6 +173,7 @@ public class MonopolyServer implements MessageListener<HostedConnection>, Connec
Serializer.registerClass(TradeReply.class);
Serializer.registerClass(TradeHandler.class);
Serializer.registerClass(NotificationMessage.class);
Serializer.registerClass(JailEvent.class);
}
private void registerListeners() {