oTree Forum >

Stag Hunt Game

#1 by caroolin

Hello! I have the following code for a Stag Hunt Game:
import random

from otree.api import (
    models,
    widgets,
    BaseConstants,
    BaseSubsession,
    BaseGroup,
    BasePlayer,
    Page, WaitPage,
    Currency as c,
)

doc = """
Stag-hunt game
"""


class Constants(BaseConstants):
    name_in_url = 'stag_hunt_game'
    players_per_group = 2
    num_rounds = 3  # As specified for 3 rounds

    stag_match_payoff = c(5)
    stag_mismatch_payoff = c(0)
    hare_mismatch_payoff = c(2)
    hare_match_payoff = c(1)


class Subsession(BaseSubsession):
    def creating_session(self):
        self.group_randomly(fixed_id_in_group=True)
        if self.round_number == 1:
            paying_round = random.randint(1, Constants.num_rounds)
            self.session.vars['paying_round'] = paying_round


class Group(BaseGroup):
    def set_payoffs(self):
        h_1 = self.get_player_by_role('Hunter_1')
        h_2 = self.get_player_by_role('Hunter_2')
        if h_1.decision == 'stag' and h_2.decision == 'stag':
            h_1.payoff = Constants.stag_match_payoff
            h_2.payoff = Constants.stag_match_payoff
        elif h_1.decision == 'stag' and h_2.decision == 'hare':
            h_1.payoff = Constants.stag_mismatch_payoff
            h_2.payoff = Constants.hare_mismatch_payoff
        elif h_1.decision == 'hare' and h_2.decision == 'stag':
            h_1.payoff = Constants.hare_mismatch_payoff
            h_2.payoff = Constants.stag_mismatch_payoff
        elif h_1.decision == 'hare' and h_2.decision == 'hare':
            h_1.payoff = Constants.hare_match_payoff
            h_2.payoff = Constants.hare_match_payoff


class Player(BasePlayer):
    decision = models.StringField(
        choices=['stag', 'hare'],
        doc="""This player's decision is either stag or hare""",
        widget=widgets.RadioSelect,
    )

    def other_player(self):
        return self.get_others_in_group()[0]

    def role(self):
        if self.id_in_group == 1:
            return 'Hunter_1'
        elif self.id_in_group == 2:
            return 'Hunter_2'


class Instructions(Page):
    def is_displayed(self):
        return self.round_number == 1


class Decision(Page):
    form_model = 'player'
    form_fields = ['decision']


class MyWaitPage(WaitPage):
    template_name = 'stag_hunt_game/MyWaitPage.html'

    def after_all_players_arrive(self):
        self.group.set_payoffs()


class Results(Page):
    def vars_for_template(self):
        me = self.player
        opponent = me.other_player()
        return dict(
            my_decision=me.decision,
            opponent_decision=opponent.decision,
            my_payoff=me.payoff,
            opponent_payoff=opponent.payoff
        )


class Summary(Page):
    def vars_for_template(self):
        return dict(
            player_in_all_rounds_rev=reversed(self.player.in_all_rounds()),
            paying_round=self.session.vars['paying_round'],
        )

    def is_displayed(self):
        return self.round_number == Constants.num_rounds


page_sequence = [Instructions, Decision, MyWaitPage, Results, Summary]


However, I face these issues:
Unresolved attribute reference 'group' for class 'MyWaitPage':87
Unresolved attribute reference 'player' for class 'Results':92

Could somebody help?

Write a reply

Set forum username