from . import models from ._builtin import Page, WaitPage from otree.api import Currency as c, currency_range from .forms import PFormset from .models import Constants, Player, Group, Subsession, Punishment as PunishmentModel from otree.constants import timeout_happened class Introductionpun(Page): """Description of the game: How to play and returns expected""" pass timeout_seconds = 120 after_all_players_arrive = 'creating_session' def is_displayed(self): groups = self.subsession.get_groups() for group in groups: p1 = self.group.get_player_by_id(1) Treatment = p1.participant.vars['treatment'] return self.subsession.round_number == 1 and Treatment == 'Punishment' class Introductionnopun(Page): """Description of the game: How to play and returns expected""" pass timeout_seconds = 120 after_all_players_arrive = 'creating_session' def is_displayed(self): groups = self.subsession.get_groups() for group in groups: p1 = self.group.get_player_by_id(1) Treatment = p1.participant.vars['treatment'] return self.subsession.round_number == 1 and Treatment == 'Normal' class Contribute(Page): """Player: Choose how much to contribute""" form_model = 'player' form_fields = ['contribution'] def is_displayed(self): return self.subsession.round_number <= Constants.num_rounds class AfterContribWP(WaitPage): after_all_players_arrive = 'set_beforepun_payoffs' def is_displayed(self): return self.subsession.round_number <= Constants.num_rounds # body_text = "Waiting for other participants to contribute." class Punishment(Page): def post(self): print(self.request.POST) return super().post() def get_formset(self, data=None): return PFormset(instance=self.player, data=data, ) def get_form(self, data=None, files=None, **kwargs): if data and data.get('timeout_happened'): return super().get_form(data, files, **kwargs) if not data: return self.get_formset() formset = self.get_formset(data=data) return formset def before_next_page(self): if self.timeout_happened: self.player.punishments_sent.all().update(amount=0) def is_displayed(self): groups = self.subsession.get_groups() for group in groups: p1 = self.group.get_player_by_id(1) Treatment = p1.participant.vars['treatment'] return self.subsession.round_number <= Constants.num_rounds and Treatment == 'Punishment' class AfterPunishmentWP(WaitPage): after_all_players_arrive = 'set_punishments' def is_displayed(self): return self.subsession.round_number <= Constants.num_rounds class Resultspun(Page): """Players payoff: How much each has earned""" def vars_for_template(self): return dict( total_earnings=self.group.total_contribution ) def is_displayed(self): groups = self.subsession.get_groups() for group in groups: p1 = self.group.get_player_by_id(1) Treatment = p1.participant.vars['treatment'] return self.subsession.round_number <= Constants.num_rounds and Treatment == 'Punishment' def vars_for_template(self): for group in self.subsession.get_groups(): p1 = self.group.get_player_by_id(1) return dict( players = self.group.get_players(), Treatment = p1.participant.vars['treatment'] ) class Resultsnopun(Page): """Players payoff: How much each has earned""" def vars_for_template(self): return dict( total_earnings=self.group.total_contribution ) def is_displayed(self): groups = self.subsession.get_groups() for group in groups: p1 = self.group.get_player_by_id(1) Treatment = p1.participant.vars['treatment'] return self.subsession.round_number <= Constants.num_rounds and Treatment == 'Normal' def vars_for_template(self): for group in self.subsession.get_groups(): p1 = self.group.get_player_by_id(1) return dict( players = self.group.get_players(), Treatment = p1.participant.vars['treatment'] ) class Results2pun(Page): def vars_for_template(self): for group in self.subsession.get_groups(): p1 = self.group.get_player_by_id(1) return dict( players = self.group.get_players(), Treatment = p1.participant.vars['treatment'] ) def is_displayed(self): groups = self.subsession.get_groups() for group in groups: p1 = self.group.get_player_by_id(1) Treatment = p1.participant.vars['treatment'] return self.subsession.round_number <= Constants.num_rounds and Treatment == 'Punishment' class Results2nopun(Page): def vars_for_template(self): for group in self.subsession.get_groups(): p1 = self.group.get_player_by_id(1) return dict( players = self.group.get_players(), Treatment = p1.participant.vars['treatment'] ) def is_displayed(self): groups = self.subsession.get_groups() for group in groups: p1 = self.group.get_player_by_id(1) Treatment = p1.participant.vars['treatment'] return self.subsession.round_number <= Constants.num_rounds and Treatment == 'Normal' class FinalResults(Page): """Final payoff""" def is_displayed(self): return self.subsession.round_number == Constants.num_rounds page_sequence = [ Introductionpun, Introductionnopun, Contribute, AfterContribWP, Punishment, AfterPunishmentWP, Resultspun, Resultsnopun, Results2pun, Results2nopun, FinalResults ]