From 74367709229249d3eb2e2917caeb5c5a632ab9ae Mon Sep 17 00:00:00 2001 From: shayan Date: Thu, 1 Sep 2022 17:59:21 +0430 Subject: [PATCH] refactor: synchronize PubSubChannel.java refactor: fix NextTurnWatcher.watch to handle balance with move --- .../core/watchers/NextTurnWatcher.java | 24 ++++++++++++------- .../lib/channel/PubSubChannel.java | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/ir/sharif/aic/hideandseek/core/watchers/NextTurnWatcher.java b/src/main/java/ir/sharif/aic/hideandseek/core/watchers/NextTurnWatcher.java index 42e9a90..ea5da17 100644 --- a/src/main/java/ir/sharif/aic/hideandseek/core/watchers/NextTurnWatcher.java +++ b/src/main/java/ir/sharif/aic/hideandseek/core/watchers/NextTurnWatcher.java @@ -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); } @@ -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() { @@ -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); } diff --git a/src/main/java/ir/sharif/aic/hideandseek/lib/channel/PubSubChannel.java b/src/main/java/ir/sharif/aic/hideandseek/lib/channel/PubSubChannel.java index b91d9f7..0d09a51 100644 --- a/src/main/java/ir/sharif/aic/hideandseek/lib/channel/PubSubChannel.java +++ b/src/main/java/ir/sharif/aic/hideandseek/lib/channel/PubSubChannel.java @@ -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