Minor fixes

This commit is contained in:
Jaime Freire 2024-01-03 20:22:02 +01:00
parent a2c2e9faf6
commit fbe95c4c04
3 changed files with 24 additions and 33 deletions

View File

@ -50,8 +50,10 @@ public class Participant {
return Behaviors.setup(ctx -> new Participant(ctx, initialCoins, participantType).behavior()); return Behaviors.setup(ctx -> new Participant(ctx, initialCoins, participantType).behavior());
} }
public void decrementCoins(double coins) { public double decrementCoins(double coins) {
this.participantCoins -= coins; this.participantCoins -= coins;
return coins;
} }
public void incrementCoins(double coins) { public void incrementCoins(double coins) {
@ -94,12 +96,10 @@ public class Participant {
private Behavior<ParticipantMessage> onSessionEnded(SessionEnded sessionEnded) { private Behavior<ParticipantMessage> 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.getLog().info("Session ended for user: Stats: {}. Earned {} coins, profit {} %"
, context.getSelf().path().name() , context.getSelf().path().name()
, getParticipantCoins() - getInitialCoins() , String.format("%.3f%n", getParticipantCoins() - getInitialCoins())
, (getParticipantCoins() - getInitialCoins()) , calculateProfit()
); );
return Behaviors.stopped(); return Behaviors.stopped();
} }
@ -128,7 +128,7 @@ public class Participant {
private void playTurnWithSmallDelay(ActorRef<SessionMessage> replyTo) { private void playTurnWithSmallDelay(ActorRef<SessionMessage> replyTo) {
if (getParticipantCoins() > 0 && getCurrentTurn() < getTotalTurns()) { if (getParticipantCoins() > 0 && getCurrentTurn() < getTotalTurns()) {
context.scheduleOnce(Duration.ofSeconds(5), context.scheduleOnce(Duration.ofMillis(500),
replyTo, replyTo,
new SessionProtocol.PlayTurn( new SessionProtocol.PlayTurn(
replyTo, replyTo,
@ -142,8 +142,7 @@ public class Participant {
private double getParticipationForCurrentTurn() { private double getParticipationForCurrentTurn() {
var currentTurnCoins = getRandomNumberBetween(0, Math.floor(getParticipantCoins())); var currentTurnCoins = getRandomNumberBetween(0, Math.floor(getParticipantCoins()));
decrementCoins(currentTurnCoins); return isCollaborateSwitch() ? decrementCoins(currentTurnCoins) : 0;
return currentTurnCoins;
} }
public static double getRandomNumberBetween(double min, double max) { public static double getRandomNumberBetween(double min, double max) {
@ -153,7 +152,7 @@ public class Participant {
private Behavior<ParticipantMessage> onPotReturned( private Behavior<ParticipantMessage> onPotReturned(
PotReturned potReturned) { 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()); incrementCoins(potReturned.returnedAmount());
incrementCurrentTurn(); incrementCurrentTurn();
context context
@ -161,7 +160,7 @@ public class Participant {
.info( .info(
"Player {} has now {} coins; started with {} for a partial profit of: {} %", "Player {} has now {} coins; started with {} for a partial profit of: {} %",
potReturned.participant().path().name(), potReturned.participant().path().name(),
getParticipantCoins(), String.format("%.3f", getParticipantCoins()),
getInitialCoins(), getInitialCoins(),
calculateProfit()); calculateProfit());
@ -200,8 +199,8 @@ public class Participant {
setCollaborateSwitch(false); setCollaborateSwitch(false);
break; break;
case JUSTICIERO: case JUSTICIERO:
// Tweak minimum amount to collaborate // Tweak minimum amount to collaborate; average contribution must be at least the same.
setCollaborateSwitch(potReturned.returnedAmount() > participants.size()); setCollaborateSwitch((potReturned.returnedAmount() / participants.size() >= getParticipationForCurrentTurn()));
break; break;
} }
} }

View File

@ -12,8 +12,8 @@ import dev.freireservices.social_altruism.chat.potroom.PotRoomProtocol.PotRoomMe
import dev.freireservices.social_altruism.chat.potroom.SessionProtocol.*; import dev.freireservices.social_altruism.chat.potroom.SessionProtocol.*;
import lombok.Getter; import lombok.Getter;
import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
@Getter @Getter
public class Session { public class Session {
@ -97,15 +97,8 @@ public class Session {
context.getLog().info("Turn {} complete", getCurrentTurn()); context.getLog().info("Turn {} complete", getCurrentTurn());
if (incrementCurrentTurnAndGet() == totalTurns) { if (incrementCurrentTurnAndGet() == totalTurns) {
context.getLog().info("All turns completed"); context.getLog().info("All turns completed - Waiting for other messages, then ending session.");
context.getLog().info("Waiting for other messages, then ending session."); context.scheduleOnce(Duration.ofSeconds(3), playTurn.session().narrow(), new EndSession());
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
playTurn.session().narrow().tell(new EndSession());
} }
} }
return Behaviors.same(); return Behaviors.same();

View File

@ -1,17 +1,14 @@
package dev.freireservices.social_altruism.chat; package dev.freireservices.social_altruism.chat;
import akka.actor.testkit.typed.javadsl.ActorTestKit; 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 akka.actor.typed.ActorRef;
import dev.freireservices.social_altruism.chat.participant.Participant; 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.participant.ParticipantProtocol.ParticipantMessage;
import dev.freireservices.social_altruism.chat.potroom.PotRoom; import dev.freireservices.social_altruism.chat.potroom.PotRoom;
import dev.freireservices.social_altruism.chat.potroom.PotRoomProtocol; import dev.freireservices.social_altruism.chat.potroom.PotRoomProtocol;
import org.junit.AfterClass;
import org.junit.Test; import org.junit.Test;
import java.time.Duration;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static dev.freireservices.social_altruism.chat.participant.ParticipantType.*; 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 class CaseStudiesTests {
public static final int INITIAL_COINS = 100; public static final int INITIAL_COINS = 100;
public static final int TOTAL_PARTICIPANTS = 3; 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 @Test
public void testCooperation() { public void testCooperation() {
final ActorTestKit testKit = ActorTestKit.create();
var potRoom = PotRoom.create(TOTAL_PARTICIPANTS, TOTAL_TURNS); var potRoom = PotRoom.create(TOTAL_PARTICIPANTS, TOTAL_TURNS);
ActorRef<PotRoomProtocol.PotRoomMessage> chatRoomTest = ActorRef<PotRoomProtocol.PotRoomMessage> chatRoomTest =
@ -39,20 +37,21 @@ public class CaseStudiesTests {
ActorRef<ParticipantMessage> p3 = ActorRef<ParticipantMessage> p3 =
testKit.spawn(Participant.create(INITIAL_COINS, SANTO), "SANTO-1"); testKit.spawn(Participant.create(INITIAL_COINS, SANTO), "SANTO-1");
// Enter POT // Enter POT
chatRoomTest.tell(new PotRoomProtocol.EnterPot(p1)); chatRoomTest.tell(new PotRoomProtocol.EnterPot(p1));
chatRoomTest.tell(new PotRoomProtocol.EnterPot(p2)); chatRoomTest.tell(new PotRoomProtocol.EnterPot(p2));
chatRoomTest.tell(new PotRoomProtocol.EnterPot(p3)); chatRoomTest.tell(new PotRoomProtocol.EnterPot(p3));
try { try {
TimeUnit.MINUTES.sleep(3); TimeUnit.MINUTES.sleep(1);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@AfterClass
public static void cleanup() {
testKit.shutdownTestKit();
}
} }