from otree.api import * doc = """ This is the Chicken game. """ class Constants(BaseConstants): name_in_url = 'chicken_with_chat' players_per_group = 2 num_rounds = 1 instructions_template = 'chicken_with_chat/instructions.html' chat_template = 'chicken_with_chat/chat.html' continue_payoff = cu(10) continued_payoff = cu(2) # payoff if both players continue or both swerve both_continue_payoff = cu(0) both_swerve_payoff = cu(5) decision_alias = { 'Straight': 'Straight', 'Swerve': 'Swerve' } class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): decision = models.StringField( choices=[['Swerve', 'Swerve'], ['Straight', 'Straight']], doc="""This player's decision""", widget=widgets.RadioSelect, ) # FUNCTIONS def set_payoffs(group: Group): for p in group.get_players(): set_payoff(p) def other_player(player: Player): return player.get_others_in_group()[0] def set_payoff(player: Player): payoff_matrix = dict( Swerve=dict( Swerve=Constants.both_swerve_payoff, Straight=Constants.continued_payoff ), Straight=dict( Swerve=Constants.continue_payoff, Straight=Constants.both_continue_payoff ), ) player.payoff = payoff_matrix[player.decision][other_player(player).decision] # PAGES class Introduction(Page): # timeout_seconds = 100 @staticmethod def is_displayed(player: Player): return player.subsession.round_number == 1 class Decision(Page): form_model = 'player' form_fields = ['decision'] @staticmethod def js_vars(player: Player): return dict(my_id=player.id_in_group) @staticmethod def live_method(player: Player, data): my_id = player.id_in_group group = player.group if 'text' in data: text = data['text'] msg = Message.create(group=group, sender=player, text=text) return {0: [to_dict(msg)]} return {my_id: [to_dict(msg) for msg in Message.filter(group=group)]} class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): @staticmethod def vars_for_template(player: Player): me = player opponent = other_player(me) total_score = sum([player.payoff for player in player.in_all_rounds()]) return dict( my_decision=Constants.decision_alias[me.decision], opponent_decision=Constants.decision_alias[opponent.decision], same_choice=me.decision == opponent.decision, total_score=total_score, ) class Message(ExtraModel): group = models.Link(Group) sender = models.Link(Player) text = models.StringField() def to_dict(msg: Message): return dict(sender=msg.sender.id_in_group, text=msg.text) page_sequence = [Introduction, Decision, ResultsWaitPage, Results]