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());
}
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<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.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<SessionMessage> 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<ParticipantMessage> 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;
}
}

View File

@ -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();

View File

@ -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<PotRoomProtocol.PotRoomMessage> chatRoomTest =
@ -39,20 +37,21 @@ public class CaseStudiesTests {
ActorRef<ParticipantMessage> 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();
}
}