oTree Forum >

Ending the game when offer is accepted. (Alternating Offer Bargaining)

#1 by alid3mir

I have a code for Rubinstein bvargaining game which will be played at most 4 rounds switiching roles and with dimisnisihing pie.
The problem is that even an offer is accepted in the first round the game is not finished and continues for four rounds. How can i fix this?




----------------------------------------------
from otree.api import *

doc = """
Your app description
"""


class C(BaseConstants):
    NAME_IN_URL = 'rubinstein'
    PLAYERS_PER_GROUP = 2
    NUM_ROUNDS = 4


    initial_pie = cu(100)
    dimnishing = cu(25)


class Subsession(BaseSubsession):
    pass


def creating_session(subsession):
    for player in subsession.get_players():
        if player.round_number % 2 == 1:
            player.myrole = 'proposer' if player.id_in_group == 1 else 'responder'
        else:
            player.myrole = 'responder' if player.id_in_group == 1 else 'proposer'


class Group(BaseGroup):
    offer = models.CurrencyField(min=0, max=100, label='How much do you want to offer?')
    response = models.StringField(choices=['Accept', 'Reject'], label='Do you accept the offer?')
    accepted = models.BooleanField(initial=False)
    current_pie = models.CurrencyField(initial=C.initial_pie)

def offer_error_message(group, value):
    print('value is', value)
    if group.round_number==2 and value>75:
        return 'You cannot make an offer more than the total remaining pie size!'
    if group.round_number==3 and value>50:
        return 'You cannot make an offer more than the total remaining pie size!'
    if group.round_number==4 and value>25:
        return 'You cannot make an offer more than the total remaining pie size!'
# def offer_max(grou):


class Player(BasePlayer):
    myrole = models.StringField()
    offer = models.CurrencyField()

# PAGES
class Introduction(Page):
    @staticmethod
    def is_displayed(player):
        return player.round_number == 1
    timeout_seconds = 120

class BeforeStarting(WaitPage):
    after_all_players_arrive = 'update_pie'


class Offer(Page):
    form_model = 'group'
    form_fields = ['offer']

    def is_displayed(player):
        return player.myrole == 'proposer' and not player.group.accepted


def update_pie(group: Group):
    print('update_pie...')
    if group.round_number == 1:
        group.current_pie = C.initial_pie

    else:
        group.current_pie = group.in_round(group.round_number - 1).current_pie - C.dimnishing
        group.accepted = group.in_round(group.round_number - 1).accepted


class WaitOffer(WaitPage):
    pass


class Response(Page):
    form_model = 'group'
    form_fields = ['response']

    def is_displayed(player):
        return player.myrole == 'responder' and not player.group.accepted

    def before_next_page(player, timeout_happened):

        if player.group.response == 'Accept':
            player.group.accepted = True
        else:
            player.group.accepted = False


class PayoffCheckpoint(WaitPage):
    after_all_players_arrive = 'set_payoffs'


def set_payoffs(group):
    if group.round_number == 1:
        previous_round_accepted = False
    else:
        previous_round_accepted = group.in_round(group.round_number - 1).accepted

    for player in group.get_players():

        if player.group.accepted and not previous_round_accepted:
            if player.myrole == 'proposer':
                player.payoff = group.current_pie - group.offer

            if player.myrole == 'responder':
                player.payoff = group.offer

        if player.group.accepted and previous_round_accepted:
            player.payoff = player.in_round(player.round_number - 1).payoff


class OfferRejected(Page):
    def is_displayed(player):
        return not player.group.accepted


class WaitResponse(WaitPage):
    pass


class Results(Page):
    def is_displayed(player):
        return player.group.accepted or player.round_number == C.NUM_ROUNDS


page_sequence = [Introduction, BeforeStarting, Offer, WaitOffer, Response, WaitResponse, OfferRejected, PayoffCheckpoint, Results]

Write a reply

Set forum username