from ._builtin import Page, WaitPage from otree.api import Currency as c, currency_range from .models import Constants # TODO: Definir una variable de pago por juego class Introduction(Page): """Description of the game: How to play and returns expected""" timeout_seconds = Constants.timer group_by_arrival_time = True def before_next_page(self): if self.timeout_happened: self.session.vars['game_timeout'] += 1 self.group.game_timeout = self.session.vars['game_timeout'] self.player.game_timeout += 1 self.player.player_timeout += 1 self.participant.vars['player_timeout'] += 1 self.session.vars['round_timeout'] = self.round_number def is_displayed(self): if self.participant.vars['is_mobile'] is False and self.session.vars['game_timeout'] == 0: return True else: return False class Contribute(Page): """Player: Choose how much to contribute""" timeout_seconds = Constants.timer form_model = 'player' form_fields = ['contribution', 'contrib_pl'] def before_next_page(self): if self.timeout_happened: self.session.vars['game_timeout'] += 1 self.group.game_timeout = self.session.vars['game_timeout'] self.player.player_timeout += 1 self.session.vars['round_timeout'] = self.round_number self.participant.vars['player_timeout'] += 1 if self.session.vars['game_timeout'] >= 1: self.session.vars['first_timeout'] = True def is_displayed(self): if self.participant.vars['is_mobile'] is False and self.session.vars['game_timeout'] == 0: return True else: return False # El waitpage se activa cuando todos entran al juego/marcan su decision class ResultsWaitPage(WaitPage): def after_all_players_arrive(self): self.group.set_payoffs() body_text = "Waiting for other participants to contribute." def is_displayed(self): if self.participant.vars['is_mobile'] is False and self.session.vars['first_timeout'] is False: return True else: return False class Timed_Out(Page): def is_displayed(self): # if self.participant.vars['is_mobile'] is False and self.session.vars['first_timeout'] is True \ # and self.round_number == self.session.vars['round_timeout']: if self.session.vars['first_timeout'] is True and self.participant.vars['player_timeout'] != 0: return True else: return False def vars_for_template(self): p_timeout_abs = self.participant.vars['player_timeout'] return dict(p_timeout=p_timeout_abs) class Results(Page): """Players payoff: How much each has earned""" timeout_seconds = Constants.timer def vars_for_template(self): if self.group.total_contribution is None: self.group.total_contribution = 0 if self.player.contribution is None: self.player.contribution = 0 # Aggregate public earnings total_earnings = self.group.total_contribution * Constants.multiplier if total_earnings == 0: total_earnings += Constants.endowment # Indiv round earnings round_payoff = (Constants.endowment - self.player.contribution) + total_earnings/Constants.players_per_group if self.participant.vars['player_timeout'] != 0 and self.session.vars['first_timeout'] is True: self.player.round_payment = Constants.endowment elif self.session.vars['first_timeout'] is False: self.player.round_payment = float(round_payoff) return dict(total_earnings=total_earnings, timeout=self.session.vars['game_timeout'], round_payoff=round_payoff) def is_displayed(self): # TODO: Chequear que esto funcione if (self.session.vars['game_timeout'] == 0) or \ (self.session.vars['first_timeout'] is True and self.round_number == self.session.vars['round_timeout']) or \ (self.session.vars['first_timeout'] is True and self.round_number == self.session.vars['round_timeout'] + 1 and self.participant.vars['player_timeout'] == 0): # La pagina de resultados aparece 2 veces (En la segunda, se fijan los pagos por alguna razon) return True else: return False def before_next_page(self): if self.timeout_happened: self.session.vars['game_timeout'] += 1 self.group.game_timeout = self.session.vars['game_timeout'] self.player.player_timeout += 1 self.participant.vars['player_timeout'] += 1 self.session.vars['round_timeout'] = self.round_number if self.session.vars['game_timeout'] >= 1: self.session.vars['first_timeout'] = True if self.session.vars['first_timeout'] is True: self.group.set_payoffs() # Qué pasa si estoy en la ronda 2 y el timeout fue en la 1? se ejecuta mi set pay off? page_sequence = [Contribute, ResultsWaitPage, Timed_Out, Results]