-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
211 lines (146 loc) · 4.95 KB
/
__init__.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from otree.api import *
from utils.live import live_page
class C(BaseConstants):
NAME_IN_URL = "ultimatum_game"
PLAYERS_PER_GROUP = 2
NUM_ROUNDS = 1
SCREENER_APP = "ultimatum_screener"
SCREENER_FIELDS = ["gender", "age"]
ENDOWMENT = 100
GAME_TIMEOUT = 60
PROPOSER_ROLE = "Proposer"
RECEIVER_ROLE = "Receiver"
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
stage = models.StringField(initial="NEW", choices=["STARTING", "PROPOSING", "DECIDING", "COMPLETED"])
dropped = models.BooleanField(initial=False)
endowment = models.IntegerField(initial=C.ENDOWMENT)
proposal = models.IntegerField()
decision = models.StringField(choices=["ACCEPT", "REJECT"])
@property
def proposer(self):
return self.get_player_by_role(C.PROPOSER_ROLE)
@property
def receiver(self):
return self.get_player_by_role(C.RECEIVER_ROLE)
def get_bonus(group: Group):
decision = group.field_maybe_none("decision")
if decision == "ACCEPT":
return {
C.PROPOSER_ROLE: group.endowment - group.proposal,
C.RECEIVER_ROLE: group.proposal,
}
elif decision == "REJECT":
return {C.PROPOSER_ROLE: 0, C.RECEIVER_ROLE: 0}
else:
return None
class Player(BasePlayer):
gender = models.StringField()
age = models.IntegerField()
checkin = models.BooleanField(initial=False)
response_time = models.IntegerField()
def creating_session(subsession: BaseSubsession):
pass
def init_group(group: Group):
config = group.session.config
for p in group.get_players():
init_player(p)
def init_player(player: Player):
copy_player_fields(player)
def copy_player_fields(player: Player):
"copy player data of SCREENER_FIELDS from SCREENER_APP"
[screener_player] = [
p
for p in player.participant.get_players()
if p.get_folder_name() == C.SCREENER_APP
]
for fld in C.SCREENER_FIELDS:
setattr(player, fld, getattr(screener_player, fld))
def set_payoffs(group: Group):
assert not group.dropped
assert group.stage == "COMPLETED"
rate = group.session.config["real_world_currency_per_point"]
bonus = get_bonus(group)
if bonus:
group.proposer.payoff = bonus[C.PROPOSER_ROLE] * rate
group.receiver.payoff = bonus[C.RECEIVER_ROLE] * rate
def output_game(group: Group):
return {
"stage": group.stage,
"endowment": group.endowment,
"proposal": group.field_maybe_none("proposal"),
"decision": group.field_maybe_none("decision"),
"bonus": get_bonus(group),
}
# PAGES
class Gather(WaitPage):
group_by_arrival_time = True
@staticmethod
def after_all_players_arrive(group: Group):
init_group(group)
class Intro(Page):
@staticmethod
def vars_for_template(player: Player):
[partner] = player.get_others_in_group()
return { 'partner': partner }
@live_page
class Main(Page):
timeout_seconds = C.GAME_TIMEOUT
@staticmethod
def is_displayed(player: Player):
return not player.group.dropped
@staticmethod
def js_vars(player: Player):
return {"C": dict(vars(C)), "role": player.role}
@staticmethod
def live_start(player: Player, payload: None):
group = player.group
player.checkin = True
checked = sum(p.checkin for p in group.get_players())
if checked < C.PLAYERS_PER_GROUP:
group.stage = "STARTING"
else:
group.stage = "PROPOSING"
yield group, "game", output_game(group)
@staticmethod
def live_proposal(player: Player, payload: dict):
group = player.group
assert group.stage == "PROPOSING"
assert isinstance(payload["proposal"], int)
assert 0 <= payload["proposal"] <= C.ENDOWMENT
assert isinstance(payload["time"], int)
player.response_time = payload["time"]
group.proposal = payload["proposal"]
group.stage = "DECIDING"
yield group, "game", output_game(group)
@staticmethod
def live_decision(player: Player, payload: dict):
group = player.group
assert group.stage == "DECIDING"
assert isinstance(payload["decision"], str)
assert payload["decision"] in ("ACCEPT", "REJECT")
assert isinstance(payload["time"], int)
player.response_time = payload["time"]
group.decision = payload["decision"]
group.stage = "COMPLETED"
yield group, "game", output_game(group)
@staticmethod
def before_next_page(player: Player, timeout_happened):
group = player.group
group.dropped = timeout_happened or group.stage != "COMPLETED"
if not group.dropped:
set_payoffs(group)
class Failure(Page):
@staticmethod
def is_displayed(player: Player):
return player.group.dropped
class Results(Page):
pass
page_sequence = [
Gather,
Intro,
Main,
Failure,
Results,
]