from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from random import choice import utils author = 'Tommaso Batistoni - t.batistoni@ucl.ac.uk' doc = """ Coordination Game """ class Constants(BaseConstants): name_in_url = 'taskcrd' players_per_group = None num_rounds = 4 max_seconds_waiting_for_partners = 1000 * 20 * 1 payoff_action_a = c(3) payoff_action_b = c(5) seconds_for_summary = 60 class Subsession(BaseSubsession): # match players and identify the relevant partner identity treatment def set_partner_based_on_identity(self, still_waiting_for_task, task, num_players): return utils.set_partner_based_on_identity(still_waiting_for_task, task, num_players) class Group(BaseGroup): pass class Player(BasePlayer): coordinate_partner = models.StringField() coordinate_partner_name = models.StringField() coordinate_decision = models.StringField() timeout = models.BooleanField() def other_player(self): return self.get_others_in_group()[0] def set_payoff(self): payoff_matrix = dict( A=dict( A=Constants.payoff_action_a, B=Constants.payoff_action_a ), B=dict( A=0, B=Constants.payoff_action_b ) ) if self.participant.vars.get('unmatched'): self.participant.vars['other_coordinate'] = choice(['A', 'B']) other_coordinate = self.participant.vars['other_coordinate'] else: other_coordinate = self.other_player().coordinate_decision if not other_coordinate: self.participant.vars['other_coordinate'] = choice(['A', 'B']) other_coordinate = self.participant.vars['other_coordinate'] if self.coordinate_decision: self.payoff = payoff_matrix[self.coordinate_decision][other_coordinate] else: self.payoff = payoff_matrix[choice(['A', 'B'])][other_coordinate]