oTree Forum >

timeout in waitpage

#1 by txb000

I cannot manage to set the timer for 20 secs in the waitpage, do not really know how to solve, can anyone help me, 

from otree.api import models, BaseConstants, BasePlayer, BaseSubsession, BaseGroup, Page, WaitPage
import random
import time


class Constants(BaseConstants):
    name_in_url = 'contest_game'
    players_per_group = 2
    num_rounds = 5
    WINNER_PAYOFF = 240
    LOSER_PAYOFF = 120


class Player(BasePlayer):
    endowment = models.IntegerField(initial=120)
    cost = models.FloatField()
    investment = models.IntegerField(min=0, max=120)
    payoff_new = models.CurrencyField()


class Subsession(BaseSubsession):
    def creating_session(self):
        self.group_randomly(fixed_id_in_group=True)

        for group in self.get_groups():
            players = group.get_players()
            for player in players:
                player.cost = random.choice([0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5])


class Group(BaseGroup):
    arrival_time = models.FloatField()

    investment_p1 = models.IntegerField()
    investment_p2 = models.IntegerField()

    def calculate_payoffs(self):
        p1, p2 = self.get_players()
        total_investment = self.investment_p1 + self.investment_p2

        if total_investment > 0:
            p1_likelihood = self.investment_p1 / total_investment
            p2_likelihood = self.investment_p2 / total_investment
            winner = p1 if p1_likelihood > p2_likelihood else p2
        else:
            winner = random.choice([p1, p2])

        if winner == p1:
            p1.payoff_new = Constants.WINNER_PAYOFF - p1.cost * p1.investment
            p2.payoff_new = Constants.LOSER_PAYOFF - p2.cost * p2.investment
        else:
            p1.payoff_new = Constants.LOSER_PAYOFF - p1.cost * p1.investment
            p2.payoff_new = Constants.WINNER_PAYOFF - p2.cost * p2.investment


class Results(Page):
    @staticmethod
    def vars_for_template(player):
        return {
            'player_payoff': player.payoff_new
        }


class InvestmentDecision(Page):
    form_model = 'player'
    form_fields = ['investment']

    @staticmethod
    def vars_for_template(player):
        player.cost = random.choice([0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5])
        return {
            'player_cost': player.cost,
        }

    @staticmethod
    def before_next_page(player, timeout_happened):
        group = player.group
        if player.id_in_group == 1:
            group.investment_p1 = player.investment
        elif player.id_in_group == 2:
            group.investment_p2 = player.investment


class WaitForSecondParticipant(WaitPage):
    wait_for_all_groups = True

    @staticmethod
    def after_all_players_arrive(group):
        group.arrival_time = time.time()

    @staticmethod
    def group_by_arrival_time_method(subsession, waiting_players):
        player = waiting_players[0]
        group = player.group
        if time.time() - group.arrival_time > 20:
            return waiting_players

    @staticmethod
    def before_next_page(group, timeout_happened):
        if timeout_happened:
            group.calculate_payoffs()


page_sequence = [InvestmentDecision, WaitForSecondParticipant, Results]

Write a reply

Set forum username