Skip to content

Commit

Permalink
refactor: synchronize PubSubChannel.java
Browse files Browse the repository at this point in the history
refactor: fix NextTurnWatcher.watch to handle balance with move
  • Loading branch information
Shayan-Shabanzadeh committed Sep 1, 2022
1 parent 2c1715a commit 7436770
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ public void watch(GameEvent event) {
Runnable timer = () -> {
try {
Thread.sleep(gameConfig.getMagicTurnTime());
var turn = gameService.getTurn();
gameService.setTurn(turn.next());
var currentTurn = gameService.getCurrentTurnNumber();
boolean isVisible = gameConfig.getTurnSettings().getVisibleTurns().contains(currentTurn);
eventChannel.push(
new GameTurnChangedEvent(gameService.getTurn().getTurnType(), gameService.getCurrentTurnNumber(), isVisible));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand All @@ -53,6 +47,7 @@ public void watch(GameEvent event) {

if (event instanceof GameStatusChangedEvent && gameService.getStatus().equals(GameStatus.ONGOING)) {
timer.run();
nextTurn();
}
if (event instanceof GameStatusChangedEvent && gameService.getStatus().equals(GameStatus.FINISHED)) {
new Runnable() {
Expand All @@ -68,18 +63,31 @@ public void run() {
}.run();
}


if (event instanceof GameTurnChangedEvent) {
this.initNextTurn();
timer.run();
this.gameConfig.getAllAgents().forEach(e -> {
e.setSentMessageThisTurn(true);
e.setMovedThisTurn(true);
});
this.arrestThieves();
this.chargeBalances();
this.figureOutGameResult();
if (!gameService.getStatus().equals(GameStatus.FINISHED)) {
timer.run();
nextTurn();
}
}
}

private void nextTurn() {
var turn = gameService.getTurn();
gameService.setTurn(turn.next());
var currentTurn = gameService.getCurrentTurnNumber();
boolean isVisible = gameConfig.getTurnSettings().getVisibleTurns().contains(currentTurn);
eventChannel.push(
new GameTurnChangedEvent(gameService.getTurn().getTurnType(), gameService.getCurrentTurnNumber(), isVisible));
}

private void initNextTurn() {
this.gameConfig.getAllAgents().forEach(Agent::onTurnChange);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void run() {
}

@Override
public void push(T msg) {
public synchronized void push(T msg) {
// if the channel is closed
if (this.isClosed.get()) {
// ignore the message
Expand Down

0 comments on commit 7436770

Please sign in to comment.