From fbe95c4c04a0a09e4e6c5ae4e309ec41bb7d7b44 Mon Sep 17 00:00:00 2001 From: Jaime Freire <5436581+jaimefreire@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:22:02 +0100 Subject: [PATCH] Minor fixes --- .../chat/participant/Participant.java | 23 +++++++++---------- .../social_altruism/chat/potroom/Session.java | 13 +++-------- .../chat/CaseStudiesTests.java | 21 ++++++++--------- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/main/java/dev/freireservices/social_altruism/chat/participant/Participant.java b/src/main/java/dev/freireservices/social_altruism/chat/participant/Participant.java index e2bf7fe..bd8e136 100644 --- a/src/main/java/dev/freireservices/social_altruism/chat/participant/Participant.java +++ b/src/main/java/dev/freireservices/social_altruism/chat/participant/Participant.java @@ -50,8 +50,10 @@ public class Participant { return Behaviors.setup(ctx -> new Participant(ctx, initialCoins, participantType).behavior()); } - public void decrementCoins(double coins) { + public double decrementCoins(double coins) { this.participantCoins -= coins; + + return coins; } public void incrementCoins(double coins) { @@ -94,12 +96,10 @@ public class Participant { private Behavior onSessionEnded(SessionEnded sessionEnded) { - context.getLog().info("Session ended for user {}", context.getSelf().path().name()); - context.getLog().info("Session ended for user: Stats: {}. Earned {} coins, profit {} %" , context.getSelf().path().name() - , getParticipantCoins() - getInitialCoins() - , (getParticipantCoins() - getInitialCoins()) + , String.format("%.3f%n", getParticipantCoins() - getInitialCoins()) + , calculateProfit() ); return Behaviors.stopped(); } @@ -128,7 +128,7 @@ public class Participant { private void playTurnWithSmallDelay(ActorRef replyTo) { if (getParticipantCoins() > 0 && getCurrentTurn() < getTotalTurns()) { - context.scheduleOnce(Duration.ofSeconds(5), + context.scheduleOnce(Duration.ofMillis(500), replyTo, new SessionProtocol.PlayTurn( replyTo, @@ -142,8 +142,7 @@ public class Participant { private double getParticipationForCurrentTurn() { var currentTurnCoins = getRandomNumberBetween(0, Math.floor(getParticipantCoins())); - decrementCoins(currentTurnCoins); - return currentTurnCoins; + return isCollaborateSwitch() ? decrementCoins(currentTurnCoins) : 0; } public static double getRandomNumberBetween(double min, double max) { @@ -153,7 +152,7 @@ public class Participant { private Behavior onPotReturned( PotReturned potReturned) { - context.getLog().info("Pot returned: {} for participant {}", potReturned.returnedAmount(), potReturned.participant().path().name()); + context.getLog().info("Pot returned: {} for participant {}", String.format("%.2f", potReturned.returnedAmount()), potReturned.participant().path().name()); incrementCoins(potReturned.returnedAmount()); incrementCurrentTurn(); context @@ -161,7 +160,7 @@ public class Participant { .info( "Player {} has now {} coins; started with {} for a partial profit of: {} %", potReturned.participant().path().name(), - getParticipantCoins(), + String.format("%.3f", getParticipantCoins()), getInitialCoins(), calculateProfit()); @@ -200,8 +199,8 @@ public class Participant { setCollaborateSwitch(false); break; case JUSTICIERO: - // Tweak minimum amount to collaborate - setCollaborateSwitch(potReturned.returnedAmount() > participants.size()); + // Tweak minimum amount to collaborate; average contribution must be at least the same. + setCollaborateSwitch((potReturned.returnedAmount() / participants.size() >= getParticipationForCurrentTurn())); break; } } diff --git a/src/main/java/dev/freireservices/social_altruism/chat/potroom/Session.java b/src/main/java/dev/freireservices/social_altruism/chat/potroom/Session.java index c485c0a..d845bd6 100644 --- a/src/main/java/dev/freireservices/social_altruism/chat/potroom/Session.java +++ b/src/main/java/dev/freireservices/social_altruism/chat/potroom/Session.java @@ -12,8 +12,8 @@ import dev.freireservices.social_altruism.chat.potroom.PotRoomProtocol.PotRoomMe import dev.freireservices.social_altruism.chat.potroom.SessionProtocol.*; import lombok.Getter; +import java.time.Duration; import java.util.List; -import java.util.concurrent.TimeUnit; @Getter public class Session { @@ -97,15 +97,8 @@ public class Session { context.getLog().info("Turn {} complete", getCurrentTurn()); if (incrementCurrentTurnAndGet() == totalTurns) { - context.getLog().info("All turns completed"); - context.getLog().info("Waiting for other messages, then ending session."); - try { - TimeUnit.SECONDS.sleep(5); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - playTurn.session().narrow().tell(new EndSession()); - + context.getLog().info("All turns completed - Waiting for other messages, then ending session."); + context.scheduleOnce(Duration.ofSeconds(3), playTurn.session().narrow(), new EndSession()); } } return Behaviors.same(); diff --git a/src/test/java/dev/freireservices/social_altruism/chat/CaseStudiesTests.java b/src/test/java/dev/freireservices/social_altruism/chat/CaseStudiesTests.java index 11a1c0b..46c1879 100644 --- a/src/test/java/dev/freireservices/social_altruism/chat/CaseStudiesTests.java +++ b/src/test/java/dev/freireservices/social_altruism/chat/CaseStudiesTests.java @@ -1,17 +1,14 @@ package dev.freireservices.social_altruism.chat; import akka.actor.testkit.typed.javadsl.ActorTestKit; -import akka.actor.testkit.typed.javadsl.BehaviorTestKit; -import akka.actor.testkit.typed.javadsl.TestProbe; import akka.actor.typed.ActorRef; import dev.freireservices.social_altruism.chat.participant.Participant; -import dev.freireservices.social_altruism.chat.participant.ParticipantProtocol; import dev.freireservices.social_altruism.chat.participant.ParticipantProtocol.ParticipantMessage; import dev.freireservices.social_altruism.chat.potroom.PotRoom; import dev.freireservices.social_altruism.chat.potroom.PotRoomProtocol; +import org.junit.AfterClass; import org.junit.Test; -import java.time.Duration; import java.util.concurrent.TimeUnit; import static dev.freireservices.social_altruism.chat.participant.ParticipantType.*; @@ -19,12 +16,13 @@ import static dev.freireservices.social_altruism.chat.participant.ParticipantTyp public class CaseStudiesTests { public static final int INITIAL_COINS = 100; public static final int TOTAL_PARTICIPANTS = 3; - public static final int TOTAL_TURNS = 10; + public static final int TOTAL_TURNS = 100; + + final static ActorTestKit testKit = ActorTestKit.create(); @Test public void testCooperation() { - final ActorTestKit testKit = ActorTestKit.create(); var potRoom = PotRoom.create(TOTAL_PARTICIPANTS, TOTAL_TURNS); ActorRef chatRoomTest = @@ -39,20 +37,21 @@ public class CaseStudiesTests { ActorRef p3 = testKit.spawn(Participant.create(INITIAL_COINS, SANTO), "SANTO-1"); - - - - // Enter POT chatRoomTest.tell(new PotRoomProtocol.EnterPot(p1)); chatRoomTest.tell(new PotRoomProtocol.EnterPot(p2)); chatRoomTest.tell(new PotRoomProtocol.EnterPot(p3)); try { - TimeUnit.MINUTES.sleep(3); + TimeUnit.MINUTES.sleep(1); } catch (InterruptedException e) { throw new RuntimeException(e); } } + + @AfterClass + public static void cleanup() { + testKit.shutdownTestKit(); + } }