from otree.api import * import random doc = """ Game_D """ class C(BaseConstants): NAME_IN_URL = 'dynamic_test' PLAYERS_PER_GROUP = 5 PLAYERS_PER_GROUP = 5 NUM_ROUNDS = 1 ENDOWMENT = 36 CATASTROPHE = 90 class Subsession(BaseSubsession): pass class Group(BaseGroup): target_met = models.BooleanField(initial=True) success = models.BooleanField(initial=True) # CONTRIBUTIONS # Group-level contributions per round total_contribution1_dynamic = models.FloatField(initial=0) total_contribution2_dynamic = models.FloatField(initial=0) total_contribution3_dynamic = models.FloatField(initial=0) total_contribution4_dynamic = models.FloatField(initial=0) total_contribution5_dynamic = models.FloatField(initial=0) total_contribution6_dynamic = models.FloatField(initial=0) total_contribution7_dynamic = models.FloatField(initial=0) total_contribution8_dynamic = models.FloatField(initial=0) total_contribution9_dynamic = models.FloatField(initial=0) # Aggregate group-level contributions DYNAMIC total_contribution12_dynamic = models.FloatField(initial=0) total_contribution123_dynamic = models.FloatField(initial=0) total_contribution1234_dynamic = models.FloatField(initial=0) total_contribution12345_dynamic = models.FloatField(initial=0) total_contribution123456_dynamic = models.FloatField(initial=0) total_contribution1234567_dynamic = models.FloatField(initial=0) total_contribution12345678_dynamic = models.FloatField(initial=0) total_contribution123456789_dynamic = models.FloatField(initial=0) # Other total_aggregate_contributions = models.FloatField(initial=0) total_aggregate_contributions_dynamic = models.FloatField(initial=0) individual_share = models.FloatField(initial=0) threshold = models.FloatField(initial=0) threshold_reached = models.IntegerField() dropout = models.LongStringField() class Player(BasePlayer): #CONTRIBUTIONS color = models.StringField() real_payoff = models.FloatField() contribution_sum = models.FloatField() savings1 = models.FloatField() savings2 = models.FloatField() savings3 = models.FloatField() savings4 = models.FloatField() savings5 = models.FloatField() savings6 = models.FloatField() savings7 = models.FloatField() savings8 = models.FloatField() savings9 = models.FloatField() contribution12_static = models.FloatField() contribution1 = models.FloatField(widget=widgets.RadioSelect, choices=[0, 1, 2, 3, 4], label="Round 1: How much do you want to invest into climate protection (0-4 Cashcoins)?" ) contribution2 = models.FloatField(widget=widgets.RadioSelect, choices=[0, 1, 2, 3, 4], label="Round 2: How much do you want to invest into climate protection (0-4 Cashcoins)?" ) contribution3 = models.FloatField(widget=widgets.RadioSelect, choices=[0, 1, 2, 3, 4], label="Round 3: How much do you want to invest into climate protection (0-4 Cashcoins)?" ) contribution4 = models.FloatField(widget=widgets.RadioSelect, choices=[0, 1, 2, 3, 4], label="Round 4: How much do you want to invest into climate protection (0-4 Cashcoins)?" ) contribution5 = models.FloatField(widget=widgets.RadioSelect, choices=[0, 1, 2, 3, 4], label="Round 5: How much do you want to invest into climate protection (0-4 Cashcoins)?" ) contribution6 = models.FloatField(widget=widgets.RadioSelect, choices=[0, 1, 2, 3, 4], label="Round 6: How much do you want to invest into climate protection (0-4 Cashcoins)?" ) contribution7 = models.FloatField(widget=widgets.RadioSelect, choices=[0, 1, 2, 3, 4], label="Round 7: How much do you want to invest into climate protection (0-4 Cashcoins)?" ) contribution8 = models.FloatField(widget=widgets.RadioSelect, choices=[0, 1, 2, 3, 4], label="Round 8: How much do you want to invest into climate protection (0-4 Cashcoins)?" ) contribution9 = models.FloatField(widget=widgets.RadioSelect, choices=[0, 1, 2, 3, 4], label="Round 9: How much do you want to invest into climate protection (0-4 Cashcoins)?" ) # Functions #PLAYER FUNCTIONS #GROUP FUNCTIONS #CONTRIBUTIONS def set_payoffs(group: Group): group.threshold = 90 group.total_contribution1_dynamic = sum([p.contribution1 for p in group.get_players()]) * 1.5 group.total_contribution2_dynamic = sum([p.contribution2 for p in group.get_players()]) * 1.5 group.total_contribution3_dynamic = sum([p.contribution3 for p in group.get_players()]) * 1.5 group.total_contribution4_dynamic = sum([p.contribution4 for p in group.get_players()]) * 1.0 group.total_contribution5_dynamic = sum([p.contribution5 for p in group.get_players()]) * 1.0 group.total_contribution6_dynamic = sum([p.contribution6 for p in group.get_players()]) * 1.0 group.total_contribution7_dynamic = sum([p.contribution7 for p in group.get_players()]) * 0.5 group.total_contribution8_dynamic = sum([p.contribution8 for p in group.get_players()]) * 0.5 group.total_contribution9_dynamic = sum([p.contribution9 for p in group.get_players()]) * 0.5 group.total_aggregate_contributions = (group.total_contribution1_dynamic + group.total_contribution2_dynamic + group.total_contribution3_dynamic + group.total_contribution4_dynamic + group.total_contribution5_dynamic + group.total_contribution6_dynamic + group.total_contribution7_dynamic + group.total_contribution8_dynamic + group.total_contribution9_dynamic) for p in group.get_players(): if group.total_aggregate_contributions >= C.CATASTROPHE: group.target_met = True group.success = True group.session.vars.update(target_met=True) group.session.vars.update(success=True) p.payoff = C.ENDOWMENT - (p.contribution1 + p.contribution2 + p.contribution3 + p.contribution4 + p.contribution5 + p.contribution6 + p.contribution7 + p.contribution8 + p.contribution9) else: group.session.vars.update(target_met=False) group.target_met = False if random.random()<0.3: group.session.vars.update(success=True) group.success = True p.payoff = C.ENDOWMENT - ( p.contribution1 + p.contribution2 + p.contribution3 + p.contribution4 + p.contribution5 + p.contribution6 + p.contribution7 + p.contribution8 + p.contribution9) else: group.session.vars.update(success=False) group.success = False p.payoff = 0 p.real_payoff = round(float(p.payoff) * 0.50, 2) def dropouts(group: Group): for p in group.get_players(): str(group.participant.vars) def set_contribution1_payoff(group: Group): group.total_contribution1_dynamic = sum([p.contribution1 for p in group.get_players()]) * 1.5 for p in group.get_players(): p.savings1 = C.ENDOWMENT - p.contribution1 def set_contribution2_payoff(group: Group): group.total_contribution1_dynamic = sum([p.contribution1 for p in group.get_players()]) * 1.5 group.total_contribution2_dynamic = sum([p.contribution2 for p in group.get_players()]) * 1.5 group.total_contribution12_dynamic = group.total_contribution1_dynamic + group.total_contribution2_dynamic for p in group.get_players(): p.savings2 = C.ENDOWMENT - p.contribution1 - p.contribution2 def set_contribution3_payoff(group: Group): group.total_contribution1_dynamic = sum([p.contribution1 for p in group.get_players()]) * 1.5 group.total_contribution2_dynamic = sum([p.contribution2 for p in group.get_players()]) * 1.5 group.total_contribution3_dynamic = sum([p.contribution3 for p in group.get_players()]) * 1.5 group.total_contribution123_dynamic = group.total_contribution1_dynamic + group.total_contribution2_dynamic \ + group.total_contribution3_dynamic for p in group.get_players(): p.savings3 = C.ENDOWMENT - p.contribution1 - p.contribution2 - p.contribution3 def set_contribution4_payoff(group: Group): group.total_contribution1_dynamic = sum([p.contribution1 for p in group.get_players()]) * 1.5 group.total_contribution2_dynamic = sum([p.contribution2 for p in group.get_players()]) * 1.5 group.total_contribution3_dynamic = sum([p.contribution3 for p in group.get_players()]) * 1.5 group.total_contribution4_dynamic = sum([p.contribution4 for p in group.get_players()]) * 1.0 group.total_contribution1234_dynamic = group.total_contribution1_dynamic + group.total_contribution2_dynamic \ + group.total_contribution3_dynamic + group.total_contribution4_dynamic for p in group.get_players(): p.savings4 = C.ENDOWMENT - p.contribution1 - p.contribution2 - p.contribution3 - p.contribution4 def set_contribution5_payoff(group: Group): group.total_contribution1_dynamic = sum([p.contribution1 for p in group.get_players()]) * 1.5 group.total_contribution2_dynamic = sum([p.contribution2 for p in group.get_players()]) * 1.5 group.total_contribution3_dynamic = sum([p.contribution3 for p in group.get_players()]) * 1.5 group.total_contribution4_dynamic = sum([p.contribution4 for p in group.get_players()]) * 1.0 group.total_contribution5_dynamic = sum([p.contribution5 for p in group.get_players()]) * 1.0 group.total_contribution12345_dynamic = group.total_contribution1_dynamic + group.total_contribution2_dynamic \ + group.total_contribution3_dynamic + group.total_contribution4_dynamic \ + group.total_contribution5_dynamic for p in group.get_players(): p.savings5 = C.ENDOWMENT - p.contribution1 - p.contribution2 - p.contribution3 - p.contribution4 \ - p.contribution5 def set_contribution6_payoff(group: Group): group.total_contribution1_dynamic = sum([p.contribution1 for p in group.get_players()]) * 1.5 group.total_contribution2_dynamic = sum([p.contribution2 for p in group.get_players()]) * 1.5 group.total_contribution3_dynamic = sum([p.contribution3 for p in group.get_players()]) * 1.5 group.total_contribution4_dynamic = sum([p.contribution4 for p in group.get_players()]) * 1.0 group.total_contribution5_dynamic = sum([p.contribution5 for p in group.get_players()]) * 1.0 group.total_contribution6_dynamic = sum([p.contribution6 for p in group.get_players()]) * 1.0 group.total_contribution123456_dynamic = group.total_contribution1_dynamic + group.total_contribution2_dynamic \ + group.total_contribution3_dynamic + group.total_contribution4_dynamic \ + group.total_contribution5_dynamic + group.total_contribution6_dynamic for p in group.get_players(): p.savings6 = C.ENDOWMENT - p.contribution1 - p.contribution2 - p.contribution3 - p.contribution4 \ - p.contribution5 - p.contribution6 def set_contribution7_payoff(group: Group): group.total_contribution1_dynamic = sum([p.contribution1 for p in group.get_players()]) * 1.5 group.total_contribution2_dynamic = sum([p.contribution2 for p in group.get_players()]) * 1.5 group.total_contribution3_dynamic = sum([p.contribution3 for p in group.get_players()]) * 1.5 group.total_contribution4_dynamic = sum([p.contribution4 for p in group.get_players()]) * 1.0 group.total_contribution5_dynamic = sum([p.contribution5 for p in group.get_players()]) * 1.0 group.total_contribution6_dynamic = sum([p.contribution6 for p in group.get_players()]) * 1.0 group.total_contribution7_dynamic = sum([p.contribution7 for p in group.get_players()]) * 0.5 group.total_contribution1234567_dynamic = group.total_contribution1_dynamic + group.total_contribution2_dynamic \ + group.total_contribution3_dynamic + group.total_contribution4_dynamic \ + group.total_contribution5_dynamic + group.total_contribution6_dynamic \ + group.total_contribution7_dynamic for p in group.get_players(): p.savings7 = C.ENDOWMENT - p.contribution1 - p.contribution2 - p.contribution3 - p.contribution4 \ - p.contribution5 - p.contribution6 - p.contribution7 def set_contribution8_payoff(group: Group): group.total_contribution1_dynamic = sum([p.contribution1 for p in group.get_players()]) * 1.5 group.total_contribution2_dynamic = sum([p.contribution2 for p in group.get_players()]) * 1.5 group.total_contribution3_dynamic = sum([p.contribution3 for p in group.get_players()]) * 1.5 group.total_contribution4_dynamic = sum([p.contribution4 for p in group.get_players()]) * 1.0 group.total_contribution5_dynamic = sum([p.contribution5 for p in group.get_players()]) * 1.0 group.total_contribution6_dynamic = sum([p.contribution6 for p in group.get_players()]) * 1.0 group.total_contribution7_dynamic = sum([p.contribution7 for p in group.get_players()]) * 0.5 group.total_contribution8_dynamic = sum([p.contribution8 for p in group.get_players()]) * 0.5 group.total_contribution12345678_dynamic = group.total_contribution1_dynamic + group.total_contribution2_dynamic \ + group.total_contribution3_dynamic + group.total_contribution4_dynamic \ + group.total_contribution5_dynamic + group.total_contribution6_dynamic \ + group.total_contribution7_dynamic + group.total_contribution8_dynamic for p in group.get_players(): p.savings8 = C.ENDOWMENT - p.contribution1 - p.contribution2 - p.contribution3 - p.contribution4 \ - p.contribution5 - p.contribution6 - p.contribution7 - p.contribution8 def set_contribution9_payoff(group: Group): group.total_contribution1_dynamic = sum([p.contribution1 for p in group.get_players()]) * 1.5 group.total_contribution2_dynamic = sum([p.contribution2 for p in group.get_players()]) * 1.5 group.total_contribution3_dynamic = sum([p.contribution3 for p in group.get_players()]) * 1.5 group.total_contribution4_dynamic = sum([p.contribution4 for p in group.get_players()]) * 1.0 group.total_contribution5_dynamic = sum([p.contribution5 for p in group.get_players()]) * 1.0 group.total_contribution6_dynamic = sum([p.contribution6 for p in group.get_players()]) * 1.0 group.total_contribution7_dynamic = sum([p.contribution7 for p in group.get_players()]) * 0.5 group.total_contribution8_dynamic = sum([p.contribution8 for p in group.get_players()]) * 0.5 group.total_contribution9_dynamic = sum([p.contribution9 for p in group.get_players()]) * 0.5 group.total_contribution123456789_dynamic = group.total_contribution1_dynamic + group.total_contribution2_dynamic \ + group.total_contribution3_dynamic + group.total_contribution4_dynamic \ + group.total_contribution5_dynamic + group.total_contribution6_dynamic \ + group.total_contribution7_dynamic + group.total_contribution8_dynamic \ + group.total_contribution9_dynamic for p in group.get_players(): p.savings9 = C.ENDOWMENT - p.contribution1 - p.contribution2 - p.contribution3 - p.contribution4 \ - p.contribution5 - p.contribution6 - p.contribution7 - p.contribution8 - p.contribution9 def is_reached(group: Group): return group.total_both_contributions >= group.threshold # PAGES class FirstPage(WaitPage): wait_for_all_groups = True body_text = "Please wait, you are being assigned to a group of 5 players." class FirstFirstPage(Page): pass #CONTRIBUTIONS #Contribution Wait Pages class Contribution1WaitPage(WaitPage): body_text = "Please wait for the other players." class Contribution2WaitPage(WaitPage): body_text = "Please wait for the other players." class Contribution3WaitPage(WaitPage): body_text = "Please wait for the other players." class Contribution4WaitPage(WaitPage): body_text = "Please wait for the other players." class Contribution5WaitPage(WaitPage): body_text = "Please wait for the other players." class Contribution6WaitPage(WaitPage): body_text = "Please wait for the other players." class Contribution7WaitPage(WaitPage): body_text = "Please wait for the other players." class Contribution8WaitPage(WaitPage): body_text = "Please wait for the other players." class Contribution9WaitPage(WaitPage): body_text = "Please wait for the other players." #Contributions class Contribute1_dynamic(Page): """This is the first of 9 rounds of the Climate Protection Game. You can now decide how much you want to contribute to the climate pot.""" form_model = 'player' form_fields = ['contribution1'] def vars_for_template(player): return dict( amount_left=C.ENDOWMENT ) def get_timeout_seconds(player): return 1 * 60 class Contribute2_dynamic(Page): """This is the second of 9 rounds of the Climate Protection Game. You can now decide how much you want to contribute to the climate pot.""" form_model = 'player' form_fields = ['contribution2'] def vars_for_template(player): return dict( amount_left=C.ENDOWMENT - player.contribution1 ) def get_timeout_seconds(player): return 1 * 60 class Contribute3_dynamic(Page): """This is the third of 9 rounds of the Climate Protection Game. You can now decide how much you want to contribute to the climate pot.""" form_model = 'player' form_fields = ['contribution3'] def vars_for_template(player): return dict( amount_left=C.ENDOWMENT - player.contribution1 - player.contribution2 ) def get_timeout_seconds(player): return 1 * 60 class Contribute4_dynamic(Page): """This is the fourth of 9 rounds of the Climate Protection Game. You can now decide how much you want to contribute to the climate pot.""" form_model = 'player' form_fields = ['contribution4'] def vars_for_template(player): return dict( amount_left=C.ENDOWMENT - player.contribution1 - player.contribution2 - player.contribution3 ) def get_timeout_seconds(player): return 1 * 60 class Contribute5_dynamic(Page): """This is the fifth of 9 rounds of the Climate Protection Game. You can now decide how much you want to contribute to the climate pot.""" form_model = 'player' form_fields = ['contribution5'] def vars_for_template(player): return dict( amount_left=C.ENDOWMENT - player.contribution1 - player.contribution2 - player.contribution3 - player.contribution4 ) def get_timeout_seconds(player): return 1 * 60 class Contribute6_dynamic(Page): """This is the sixth of 9 rounds of the Climate Protection Game. You can now decide how much you want to contribute to the climate pot.""" form_model = 'player' form_fields = ['contribution6'] def vars_for_template(player): return dict( amount_left=C.ENDOWMENT - player.contribution1 - player.contribution2 - player.contribution3 - player.contribution4 - player.contribution5 ) def get_timeout_seconds(player): return 1 * 60 class Contribute7_dynamic(Page): """This is the seventh of 9 rounds of the Climate Protection Game. You can now decide how much you want to contribute to the climate pot.""" form_model = 'player' form_fields = ['contribution7'] def vars_for_template(player): return dict( amount_left=C.ENDOWMENT - player.contribution1 - player.contribution2 - player.contribution3 - player.contribution4 - player.contribution5 - player.contribution6 ) def get_timeout_seconds(player): return 1 * 60 class Contribute8_dynamic(Page): """This is the eighth of 9 rounds of the Climate Protection Game. You can now decide how much you want to contribute to the climate pot.""" form_model = 'player' form_fields = ['contribution8'] def vars_for_template(player): return dict( amount_left=C.ENDOWMENT - player.contribution1 - player.contribution2 - player.contribution3 - player.contribution4 - player.contribution5 - player.contribution6- player.contribution7 ) def get_timeout_seconds(player): return 1 * 60 class Contribute9_dynamic(Page): """This is the ninth of 9 rounds of the Climate Protection Game. You can now decide how much you want to contribute to the climate pot.""" form_model = 'player' form_fields = ['contribution9'] def vars_for_template(player): return dict( amount_left=C.ENDOWMENT - player.contribution1 - player.contribution2 - player.contribution3 - player.contribution4 - player.contribution5 - player.contribution6 - player.contribution7 - player.contribution8 ) def get_timeout_seconds(player): return 1 * 60 class Contribute1ResultsWaitPage(WaitPage): after_all_players_arrive = set_contribution1_payoff body_text = "Please wait for the contributions of the other players." class Contribute2ResultsWaitPage(WaitPage): after_all_players_arrive = set_contribution2_payoff body_text = "Please wait for the contributions of the other players." class Contribute3ResultsWaitPage(WaitPage): after_all_players_arrive = set_contribution3_payoff body_text = "Please wait for the contributions of the other players." class Contribute4ResultsWaitPage(WaitPage): after_all_players_arrive = set_contribution4_payoff body_text = "Please wait for the contributions of the other players." class Contribute5ResultsWaitPage(WaitPage): after_all_players_arrive = set_contribution5_payoff body_text = "Please wait for the contributions of the other players." class Contribute6ResultsWaitPage(WaitPage): after_all_players_arrive = set_contribution6_payoff body_text = "Please wait for the contributions of the other players." class Contribute7ResultsWaitPage(WaitPage): after_all_players_arrive = set_contribution7_payoff body_text = "Please wait for the contributions of the other players." class Contribute8ResultsWaitPage(WaitPage): after_all_players_arrive = set_contribution8_payoff body_text = "Please wait for the contributions of the other players." class Contribute9ResultsWaitPage(WaitPage): after_all_players_arrive = set_contribution9_payoff body_text = "Please wait for the contributions of the other players." class Contribute1Results_dynamic(Page): """How much did every player contribute?""" def vars_for_template(player): return dict(name=player.id_in_group) def get_timeout_seconds(player): return 1 * 60 class Contribute2Results_dynamic(Page): """How much did every player contribute?""" def vars_for_template(player): return dict(name=player.id_in_group) def get_timeout_seconds(player): return 1 * 60 class Contribute3Results_dynamic(Page): """How much did every player contribute?""" def vars_for_template(player): return dict(name=player.id_in_group) def get_timeout_seconds(player): return 1 * 60 class Contribute4Results_dynamic(Page): """How much did every player contribute?""" def vars_for_template(player): return dict(name=player.id_in_group) def get_timeout_seconds(player): return 1 * 60 class Contribute5Results_dynamic(Page): """How much did every player contribute?""" def vars_for_template(player): return dict(name=player.id_in_group) def get_timeout_seconds(player): return 1 * 60 class Contribute6Results_dynamic(Page): """How much did every player contribute?""" def vars_for_template(player): return dict(name=player.id_in_group) def get_timeout_seconds(player): return 1 * 60 class Contribute7Results_dynamic(Page): """How much did every player contribute?""" def vars_for_template(player): return dict(name=player.id_in_group) def get_timeout_seconds(player): return 1 * 60 class Contribute8Results_dynamic(Page): """How much did every player contribute?""" def vars_for_template(player): return dict(name=player.id_in_group) def get_timeout_seconds(player): return 1 * 60 class Contribute9Results_dynamic(Page): """How much did every player contribute?""" def vars_for_template(player): return dict(name=player.id_in_group) def get_timeout_seconds(player): return 1 * 60 #Threshold and Group Results class Threshold(Page): """Threshold level""" def vars_for_template(player): return dict( total_earnings=player.group.total_contribution123456789_dynamic, final_amount_left=C.ENDOWMENT - player.contribution1 - player.contribution2 - player.contribution3 - player.contribution4 - player.contribution5 - player.contribution6 - player.contribution7 - player.contribution8 - player.contribution9) def get_timeout_seconds(player): return 1 * 60 class ResultsDynamic(Page): """Players payoff: How much each has earned""" def vars_for_template(player): return dict( total_earnings=player.group.total_contribution123456789_dynamic, final_amount_left=C.ENDOWMENT - player.contribution1 - player.contribution2 - player.contribution3 - player.contribution4 - player.contribution5 - player.contribution6 - player.contribution7 - player.contribution8 - player.contribution9) def get_timeout_seconds(player): return 2 * 60 class ResultsDynamic_Final(Page): pass #Other class ResultsWaitPage(WaitPage): wait_for_all_groups = True body_text = "Please wait for the other players in the experiment." class ResultsWaitPage1(WaitPage): after_all_players_arrive = set_payoffs body_text = "Please wait for the contributions of the other players." #Sequence page_sequence = [FirstPage, FirstFirstPage, Contribution1WaitPage, Contribute1_dynamic, Contribute1ResultsWaitPage, Contribute1Results_dynamic, Contribution2WaitPage, Contribute2_dynamic, Contribute2ResultsWaitPage, Contribute2Results_dynamic, Contribution3WaitPage, Contribute3_dynamic, Contribute3ResultsWaitPage, Contribute3Results_dynamic, Contribution4WaitPage, Contribute4_dynamic, Contribute4ResultsWaitPage, Contribute4Results_dynamic, Contribution5WaitPage, Contribute5_dynamic, Contribute5ResultsWaitPage, Contribute5Results_dynamic, Contribution6WaitPage, Contribute6_dynamic, Contribute6ResultsWaitPage, Contribute6Results_dynamic, Contribution7WaitPage, Contribute7_dynamic, Contribute7ResultsWaitPage, Contribute7Results_dynamic, Contribution8WaitPage, Contribute8_dynamic, Contribute8ResultsWaitPage, Contribute8Results_dynamic, Contribution9WaitPage, Contribute9_dynamic, Contribute9ResultsWaitPage, Contribute9Results_dynamic, ResultsWaitPage, ResultsWaitPage1, ResultsDynamic, ResultsDynamic_Final ]