[virtualcommons-svn] commit/foraging: <all...@asu.edu> Allen Lee: adding trust game server side log
Status: Beta
Brought to you by:
alllee
From: Bitbucket <com...@bi...> - 2011-08-17 08:35:12
|
1 new changeset in foraging: http://bitbucket.org/virtualcommons/foraging/changeset/92762f536733/ changeset: 92762f536733 user: <all...@asu.edu> Allen Lee date: 2011-08-17 10:34:53 summary: adding trust game server side logic, needs testing affected #: 10 files (6.1 KB) --- a/src/main/java/edu/asu/commons/foraging/client/ChatPanel.java Tue Aug 16 23:36:32 2011 -0700 +++ b/src/main/java/edu/asu/commons/foraging/client/ChatPanel.java Wed Aug 17 01:34:53 2011 -0700 @@ -118,14 +118,7 @@ if (message == null || "".equals(message) || targetIdentifier == null) { return; } - RoundConfiguration roundConfiguration = client.getCurrentRoundConfiguration(); - if (roundConfiguration.isCensoredChat()) { - // FIXME: get rid of duplication, add a censored boolean to ChatRequest instead? - client.transmit(new CensoredChatRequest(clientId, message, targetIdentifier)); - } - else { - client.transmit(new ChatRequest(clientId, message, targetIdentifier)); - } + client.transmit(new ChatRequest(clientId, message, targetIdentifier)); chatField.requestFocusInWindow(); chatField.setText(""); } --- a/src/main/java/edu/asu/commons/foraging/client/ForagingClient.java Tue Aug 16 23:36:32 2011 -0700 +++ b/src/main/java/edu/asu/commons/foraging/client/ForagingClient.java Wed Aug 17 01:34:53 2011 -0700 @@ -444,7 +444,7 @@ SwingUtilities.invokeLater(createGuiRunnable); } - public void sendTrustGameSubmissionRequest(Double playerOneAmountToKeep, Double[] playerTwoAmountsToKeep) { + public void sendTrustGameSubmissionRequest(double playerOneAmountToKeep, double[] playerTwoAmountsToKeep) { transmit(new TrustGameSubmissionRequest(getId(), playerOneAmountToKeep, playerTwoAmountsToKeep)); } } --- a/src/main/java/edu/asu/commons/foraging/client/GameWindow2D.java Tue Aug 16 23:36:32 2011 -0700 +++ b/src/main/java/edu/asu/commons/foraging/client/GameWindow2D.java Wed Aug 17 01:34:53 2011 -0700 @@ -82,7 +82,7 @@ private JScrollPane instructionsScrollPane; - private HtmlEditorPane instructionsEditorPane; + private HtmlEditorPane instructionsEditorPane; private JPanel messagePanel; private JScrollPane messageScrollPane; @@ -599,7 +599,7 @@ return configuration.isPracticeRound() && configuration.isPrivateProperty(); } - private void addCenterComponent(Component newCenterComponent) { + public void addCenterComponent(Component newCenterComponent) { if (currentCenterComponent != null) { currentCenterComponent.setVisible(false); remove(currentCenterComponent); @@ -759,9 +759,7 @@ panel.add(scrollPane, BorderLayout.NORTH); TrustGamePanel trustGamePanel = new TrustGamePanel(client); - panel.add(trustGamePanel, BorderLayout.CENTER); - - + panel.add(trustGamePanel, BorderLayout.CENTER); addCenterComponent(panel); } } @@ -790,7 +788,7 @@ setInstructions(instructionsBuilder.toString()); } public void switchInstructionsPane() { - instructionsEditorPane.setText("<b>Please wait while we compute your new token totals.</b>"); +// instructionsEditorPane.setText("<b>Please wait while we compute your new token totals.</b>"); addCenterComponent(instructionsScrollPane); } --- a/src/main/java/edu/asu/commons/foraging/client/TrustGamePanel.java Tue Aug 16 23:36:32 2011 -0700 +++ b/src/main/java/edu/asu/commons/foraging/client/TrustGamePanel.java Wed Aug 17 01:34:53 2011 -0700 @@ -489,9 +489,8 @@ } String selectedPlayerOneAction = model.getActionCommand(); System.err.println("player one action: " + selectedPlayerOneAction); - Double playerOneAmountToKeep = (Integer.parseInt(selectedPlayerOneAction) * roundConfiguration.getTrustGamePayoffIncrement()); - Double[] playerTwoAmountsToKeep = new Double[4]; - Arrays.fill(playerTwoAmountsToKeep, 0.0d); + double playerOneAmountToKeep = (Integer.parseInt(selectedPlayerOneAction) * roundConfiguration.getTrustGamePayoffIncrement()); + double[] playerTwoAmountsToKeep = new double[4]; for (int rowIndex = 1; rowIndex <= 4; rowIndex++) { Object value = playerTwoTable.getValueAt(rowIndex, 2); System.err.println("value is: " + value); @@ -505,6 +504,8 @@ } System.err.println("P1 keeping " + playerOneAmountToKeep + " and P2 data: " + Arrays.asList(playerTwoAmountsToKeep)); client.sendTrustGameSubmissionRequest(playerOneAmountToKeep, playerTwoAmountsToKeep); + client.getGameWindow2D().switchInstructionsPane(); + --- a/src/main/java/edu/asu/commons/foraging/event/FacilitatorCensoredChatRequest.java Tue Aug 16 23:36:32 2011 -0700 +++ b/src/main/java/edu/asu/commons/foraging/event/FacilitatorCensoredChatRequest.java Wed Aug 17 01:34:53 2011 -0700 @@ -1,6 +1,7 @@ package edu.asu.commons.foraging.event; import edu.asu.commons.event.AbstractPersistableEvent; +import edu.asu.commons.event.ChatRequest; import edu.asu.commons.event.ClientRequest; import edu.asu.commons.net.Identifier; @@ -16,14 +17,14 @@ private static final long serialVersionUID = -120847705184240264L; - private CensoredChatRequest censoredChatRequest; + private ChatRequest censoredChatRequest; - public FacilitatorCensoredChatRequest(Identifier facilitatorId, CensoredChatRequest request) { + public FacilitatorCensoredChatRequest(Identifier facilitatorId, ChatRequest request) { super(facilitatorId); this.censoredChatRequest = request; } - public CensoredChatRequest getCensoredChatRequest() { + public ChatRequest getCensoredChatRequest() { return censoredChatRequest; } --- a/src/main/java/edu/asu/commons/foraging/event/TrustGameSubmissionRequest.java Tue Aug 16 23:36:32 2011 -0700 +++ b/src/main/java/edu/asu/commons/foraging/event/TrustGameSubmissionRequest.java Wed Aug 17 01:34:53 2011 -0700 @@ -2,27 +2,31 @@ import edu.asu.commons.event.AbstractPersistableEvent; import edu.asu.commons.net.Identifier; +import java.util.Arrays; public class TrustGameSubmissionRequest extends AbstractPersistableEvent { private static final long serialVersionUID = -4962852585789164775L; - private Double playerOneAmountToKeep; + private double playerOneAmountToKeep; - private Double[] playerTwoAmountsToKeep; + private double[] playerTwoAmountsToKeep; - public Double getPlayerOneAmountToKeep() { + public double getPlayerOneAmountToKeep() { return playerOneAmountToKeep; } - public Double[] getPlayerTwoAmountsToKeep() { + public double[] getPlayerTwoAmountsToKeep() { return playerTwoAmountsToKeep; } - public TrustGameSubmissionRequest(Identifier id, Double playerOneAmountToKeep, Double[] playerTwoAmountsToKeep) { + public TrustGameSubmissionRequest(Identifier id, double playerOneAmountToKeep, double[] playerTwoAmountsToKeep) { super(id); this.playerOneAmountToKeep = playerOneAmountToKeep; this.playerTwoAmountsToKeep = playerTwoAmountsToKeep; } + public String toString() { + return String.format("%s (P1: %d) (P2: %s)", getId(), playerOneAmountToKeep, Arrays.asList(playerTwoAmountsToKeep)); + } } --- a/src/main/java/edu/asu/commons/foraging/model/ClientData.java Tue Aug 16 23:36:32 2011 -0700 +++ b/src/main/java/edu/asu/commons/foraging/model/ClientData.java Wed Aug 17 01:34:53 2011 -0700 @@ -62,6 +62,27 @@ // only needed if this client has already received a tax. private boolean taxReceived = false; + private double trustGamePlayerOneAmountToKeep; + private double[] trustGamePlayerTwoAmountsToKeep; + + private double trustGameEarnings = 0.0d; + + public void setTrustGamePlayerOneAmountToKeep(double trustGamePlayerOneAmountToKeep) { + this.trustGamePlayerOneAmountToKeep = trustGamePlayerOneAmountToKeep; + } + + public void setTrustGamePlayerTwoAmountsToKeep(double[] trustGamePlayerTwoAmountsToKeep) { + this.trustGamePlayerTwoAmountsToKeep = trustGamePlayerTwoAmountsToKeep; + } + + public double getTrustGamePlayerOneAmountToKeep() { + return trustGamePlayerOneAmountToKeep; + } + + public double[] getTrustGamePlayerTwoAmountsToKeep() { + return trustGamePlayerTwoAmountsToKeep; + } + // this is only used in the Rotating Monitor enforcement mechanism. private int tokensCollectedDuringInterval = 0; @@ -554,4 +575,12 @@ public String toString() { return String.format("id (%s) number (%d)", id, assignedNumber); } + + public void addTrustGameEarnings(double trustGameEarnings) { + this.trustGameEarnings += trustGameEarnings; + } + + public double getTrustGameEarnings() { + return trustGameEarnings; + } } --- a/src/main/java/edu/asu/commons/foraging/model/GroupDataModel.java Tue Aug 16 23:36:32 2011 -0700 +++ b/src/main/java/edu/asu/commons/foraging/model/GroupDataModel.java Wed Aug 17 01:34:53 2011 -0700 @@ -15,6 +15,7 @@ import java.util.logging.Logger; import edu.asu.commons.experiment.DataModel; +import edu.asu.commons.foraging.client.Circle; import edu.asu.commons.foraging.conf.RoundConfiguration; import edu.asu.commons.foraging.event.ClientPositionUpdateEvent; import edu.asu.commons.foraging.event.EnforcementRankingRequest; @@ -433,6 +434,16 @@ public int getResourceDistributionSize() { return resourceDistribution.size(); } + + public Set<Identifier> getClientIdentifiersWithin(Circle circle) { + HashSet<Identifier> ids = new HashSet<Identifier>(); + for (ClientData data: clients.values()) { + if (circle.contains(data.getPosition())) { + ids.add(data.getId()); + } + } + return ids; + } /** * --- a/src/main/java/edu/asu/commons/foraging/model/ServerDataModel.java Tue Aug 16 23:36:32 2011 -0700 +++ b/src/main/java/edu/asu/commons/foraging/model/ServerDataModel.java Wed Aug 17 01:34:53 2011 -0700 @@ -395,4 +395,31 @@ public void unapply(PersistableEvent persistableEvent) { logger.warning("unapply() not implemented yet: " + persistableEvent); } + + public void calculateTrustGame(ClientData playerOne, ClientData playerTwo) { + double p1AmountToKeep = playerOne.getTrustGamePlayerOneAmountToKeep(); + double[] p2AmountsToKeep = playerTwo.getTrustGamePlayerTwoAmountsToKeep(); + + double amountSent = 1.0d - p1AmountToKeep; + if (amountSent > 0) { + double p2AmountToKeep = 0.0d; + int index = 0; + if (amountSent == 0.25d) { + index = 0; + } else if (amountSent == 0.50d) { + index = 1; + } else if (amountSent == 0.75d) { + index = 2; + } else if (amountSent == 1.0d) { + index = 3; + } + p2AmountToKeep = p2AmountsToKeep[index]; + double totalAmountSent = 3 * amountSent; + double amountReturnedToP1 = totalAmountSent - p2AmountToKeep; + logger.info(String.format("player one %s earned %d + %d = %d", playerOne, p1AmountToKeep, amountReturnedToP1, p1AmountToKeep + amountReturnedToP1)); + playerOne.addTrustGameEarnings(p1AmountToKeep + amountReturnedToP1); + logger.info(String.format("player two %s earned %d", p2AmountToKeep)); + playerTwo.addTrustGameEarnings(p2AmountToKeep); + } + } } --- a/src/main/java/edu/asu/commons/foraging/server/ForagingServer.java Tue Aug 16 23:36:32 2011 -0700 +++ b/src/main/java/edu/asu/commons/foraging/server/ForagingServer.java Wed Aug 17 01:34:53 2011 -0700 @@ -25,12 +25,12 @@ import edu.asu.commons.event.SocketIdentifierUpdateRequest; import edu.asu.commons.experiment.AbstractExperiment; import edu.asu.commons.experiment.StateMachine; +import edu.asu.commons.foraging.client.Circle; import edu.asu.commons.foraging.conf.RoundConfiguration; import edu.asu.commons.foraging.conf.ServerConfiguration; import edu.asu.commons.foraging.data.ForagingSaveFileConverter; import edu.asu.commons.foraging.event.AgentInfoRequest; import edu.asu.commons.foraging.event.BeginChatRoundRequest; -import edu.asu.commons.foraging.event.CensoredChatRequest; import edu.asu.commons.foraging.event.ClientMovementRequest; import edu.asu.commons.foraging.event.ClientPoseUpdate; import edu.asu.commons.foraging.event.ClientPositionUpdateEvent; @@ -56,6 +56,7 @@ import edu.asu.commons.foraging.event.ShowInstructionsRequest; import edu.asu.commons.foraging.event.ShowTrustGameRequest; import edu.asu.commons.foraging.event.SynchronizeClientEvent; +import edu.asu.commons.foraging.event.TrustGameSubmissionRequest; import edu.asu.commons.foraging.event.UnlockResourceRequest; import edu.asu.commons.foraging.model.ClientData; import edu.asu.commons.foraging.model.Direction; @@ -71,6 +72,9 @@ import edu.asu.commons.net.event.DisconnectionRequest; import edu.asu.commons.util.Duration; import edu.asu.commons.util.Utils; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; /** * $Id: ForagingServer.java 529 2010-08-17 00:08:01Z alllee $ @@ -276,15 +280,20 @@ } } }); - addEventProcessor(new EventTypeProcessor<CensoredChatRequest>(CensoredChatRequest.class) { - public void handle(final CensoredChatRequest request) { - // send to facilitator immediately for approval - transmit(new FacilitatorCensoredChatRequest(facilitatorId, request)); - } - }); addEventProcessor(new EventTypeProcessor<ChatRequest>(ChatRequest.class) { public void handle(final ChatRequest request) { - new Thread () { public void run() { relayChatRequest(request); } }.start(); + RoundConfiguration configuration = getCurrentRoundConfiguration(); + if (configuration.isCensoredChat()) { + transmit(new FacilitatorCensoredChatRequest(facilitatorId, request)); + } + else if (configuration.isInRoundChatEnabled()) { + // FIXME: add configuration parameter for chat in field of vision + + + } + else { + relayChatRequest(request); + } } }); @@ -658,13 +667,39 @@ } } }); - // addEventProcessor(new EventTypeProcessor<BeginVotingRequest>(BeginVotingRequest.class) { - // public void handle(BeginVotingRequest request) { - // if (getConfiguration().getCurrentParameters().isVotingEnabled()) { - // - // } - // } - // }); + addEventProcessor(new EventTypeProcessor<TrustGameSubmissionRequest>(TrustGameSubmissionRequest.class) { + int numberOfSubmissions = 0; + public void handle(TrustGameSubmissionRequest request) { + if (getCurrentRoundConfiguration().isTrustGameEnabled()) { + logger.info("trust game submission: " + request); + // basic sanity check + ClientData clientData = clients.get(request.getId()); + clientData.setTrustGamePlayerOneAmountToKeep(request.getPlayerOneAmountToKeep()); + clientData.setTrustGamePlayerTwoAmountsToKeep(request.getPlayerTwoAmountsToKeep()); + persister.store(request); + numberOfSubmissions++; + } + if (numberOfSubmissions >= clients.size()) { + // once all clients have submitted their decisions, execute the trust game. + for (GroupDataModel group: serverDataModel.getGroups()) { + LinkedList<ClientData> clientList = new LinkedList<ClientData>(group.getClientDataMap().values()); + Collections.shuffle(clientList); + // FIXME: arbitrary choice to save the first one to pair up with the last one as well. + ClientData first = clientList.getFirst(); + for (Iterator<ClientData> iter = clientList.iterator(); iter.hasNext(); ) { + ClientData playerOne = iter.next(); + ClientData playerTwo = first; + if (iter.hasNext()) { + playerTwo = iter.next(); + } + serverDataModel.calculateTrustGame(playerOne, playerTwo); + } + } + numberOfSubmissions = 0; + } + } + }); + addEventProcessor(new EventTypeProcessor<BeginChatRoundRequest>(BeginChatRoundRequest.class) { public void handle(BeginChatRoundRequest request) { if (getCurrentRoundConfiguration().isChatEnabled()) { @@ -708,9 +743,14 @@ // relay to all clients in this client's group. ClientData clientData = clients.get(source); getLogger().info(String.format("chat from %s: [ %s ]", clientData.toString(), request)); - for (Identifier targetId : clientData.getGroupDataModel().getClientIdentifiers()) { - ChatEvent chatEvent = new ChatEvent(targetId, request.toString(), source, true); - transmit(chatEvent); + // check for field of vision + RoundConfiguration currentConfiguration = getCurrentRoundConfiguration(); + if (currentConfiguration.isFieldOfVisionEnabled()) { + Circle circle = new Circle(clientData.getPosition(), currentConfiguration.getViewSubjectsRadius()); + sendChatEvent(request, clientData.getGroupDataModel().getClientIdentifiersWithin(circle)); + } + else { + sendChatEvent(request, clientData.getGroupDataModel().getClientIdentifiers()); } } else { @@ -720,6 +760,13 @@ } persister.store(request); } + + private void sendChatEvent(ChatRequest request, Collection<Identifier> identifiers) { + for (Identifier targetId : identifiers) { + ChatEvent chatEvent = new ChatEvent(targetId, request.toString(), request.getSource(), true); + transmit(chatEvent); + } + } // FIXME: remove Dispatcher reference if it's unused. public void execute(Dispatcher dispatcher) { Repository URL: https://bitbucket.org/virtualcommons/foraging/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. |