Compare commits

..

No commits in common. "develop" and "feature/stats" have entirely different histories.

3 changed files with 33 additions and 24 deletions

View File

@ -50,10 +50,8 @@ public class Participant {
return Behaviors.setup(ctx -> new Participant(ctx, initialCoins, participantType).behavior()); return Behaviors.setup(ctx -> new Participant(ctx, initialCoins, participantType).behavior());
} }
public double decrementCoins(double coins) { public void decrementCoins(double coins) {
this.participantCoins -= coins; this.participantCoins -= coins;
return coins;
} }
public void incrementCoins(double coins) { public void incrementCoins(double coins) {
@ -96,10 +94,12 @@ 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()
, String.format("%.3f%n", getParticipantCoins() - getInitialCoins()) , getParticipantCoins() - getInitialCoins()
, calculateProfit() , (getParticipantCoins() - getInitialCoins())
); );
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.ofMillis(500), context.scheduleOnce(Duration.ofSeconds(5),
replyTo, replyTo,
new SessionProtocol.PlayTurn( new SessionProtocol.PlayTurn(
replyTo, replyTo,
@ -142,7 +142,8 @@ public class Participant {
private double getParticipationForCurrentTurn() { private double getParticipationForCurrentTurn() {
var currentTurnCoins = getRandomNumberBetween(0, Math.floor(getParticipantCoins())); var currentTurnCoins = getRandomNumberBetween(0, Math.floor(getParticipantCoins()));
return isCollaborateSwitch() ? decrementCoins(currentTurnCoins) : 0; decrementCoins(currentTurnCoins);
return currentTurnCoins;
} }
public static double getRandomNumberBetween(double min, double max) { public static double getRandomNumberBetween(double min, double max) {
@ -152,7 +153,7 @@ public class Participant {
private Behavior<ParticipantMessage> onPotReturned( private Behavior<ParticipantMessage> onPotReturned(
PotReturned potReturned) { PotReturned potReturned) {
context.getLog().info("Pot returned: {} for participant {}", String.format("%.2f", potReturned.returnedAmount()), potReturned.participant().path().name()); context.getLog().info("Pot returned: {} for participant {}", potReturned.returnedAmount(), potReturned.participant().path().name());
incrementCoins(potReturned.returnedAmount()); incrementCoins(potReturned.returnedAmount());
incrementCurrentTurn(); incrementCurrentTurn();
context context
@ -160,7 +161,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(),
String.format("%.3f", getParticipantCoins()), getParticipantCoins(),
getInitialCoins(), getInitialCoins(),
calculateProfit()); calculateProfit());
@ -199,8 +200,8 @@ public class Participant {
setCollaborateSwitch(false); setCollaborateSwitch(false);
break; break;
case JUSTICIERO: case JUSTICIERO:
// Tweak minimum amount to collaborate; average contribution must be at least the same. // Tweak minimum amount to collaborate
setCollaborateSwitch((potReturned.returnedAmount() / participants.size() >= getParticipationForCurrentTurn())); setCollaborateSwitch(potReturned.returnedAmount() > participants.size());
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,8 +97,15 @@ 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 - Waiting for other messages, then ending session."); context.getLog().info("All turns completed");
context.scheduleOnce(Duration.ofSeconds(3), playTurn.session().narrow(), new EndSession()); 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());
} }
} }
return Behaviors.same(); return Behaviors.same();

View File

@ -1,14 +1,17 @@
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.*;
@ -16,13 +19,12 @@ 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 = 100; public static final int TOTAL_TURNS = 10;
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 =
@ -37,21 +39,20 @@ 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(1); TimeUnit.MINUTES.sleep(3);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@AfterClass
public static void cleanup() {
testKit.shutdownTestKit();
}
} }