-
Notifications
You must be signed in to change notification settings - Fork 1
/
pages.py
106 lines (74 loc) · 2.89 KB
/
pages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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, Punishment as PunishmentModel
from otree.constants import timeout_happened
class Introduction(Page):
"""Description of the game: How to play and returns expected"""
pass
timeout_seconds = 90
def is_displayed(self):
return self.subsession.round_number == 1
class Contribute(Page):
"""Player: Choose how much to contribute"""
form_model = 'player'
form_fields = ['contribution']
def is_displayed(self):
return self.subsession.round_number <= self.session.config["num_rounds"]
class AfterContribWP(WaitPage):
after_all_players_arrive = 'set_pd_payoffs'
def is_displayed(self):
return self.subsession.round_number <= self.session.config["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):
# here if this page was forced by admin to continue we just submit an empty form (with no formset data)
# if we need this data later on that can create some problems. But that's the price we pay for autosubmission
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):
if not self.session.config["punishment"]:
return False
else:
return self.subsession.round_number <= self.session.config["num_rounds"]
class AfterPunishmentWP(WaitPage):
after_all_players_arrive = 'set_punishments'
def is_displayed(self):
return self.subsession.round_number <= self.session.config["num_rounds"]
class Results(Page):
"""Players payoff: How much each has earned"""
timeout_seconds = 20
def vars_for_template(self):
return dict(
total_earnings=self.group.total_contribution * self.session.config["efficiency_factor"]
)
def is_displayed(self):
return self.subsession.round_number <= self.session.config["num_rounds"]
class FinalResults(Page):
"""Final payoff"""
def is_displayed(self):
return self.subsession.round_number == self.session.config["num_rounds"]
page_sequence = [
Introduction,
Contribute,
AfterContribWP,
Punishment,
AfterPunishmentWP,
Results,
FinalResults
]