from otree.api import * doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'stage2' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 sec = 600 Type = 2 # 1 = Control, 2 = Treatment answers = [1, 1, 3, 3, 3, 3, 2, 1, 1, 2, 3, 3] num_options = 2 class Subsession(BaseSubsession): pass class Group(BaseGroup): Coo = models.IntegerField(initial=0) def help_function(group:BaseGroup): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) p3 = group.get_player_by_id(3) for h in group.get_players(): if h.id_in_group == 1: h.coll_avatar1 = p2.participant.vars.get('avatar') h.coll_avatar2 = p3.participant.vars.get('avatar') h.N_in_G = 1 h.part1 = "P2" h.part2 = "P3" if h.id_in_group == 2: h.coll_avatar1 = p1.participant.vars.get('avatar') h.coll_avatar2 = p3.participant.vars.get('avatar') h.N_in_G = 2 h.part1 = "P1" h.part2 = "P3" if h.id_in_group == 3: h.coll_avatar1 = p1.participant.vars.get('avatar') h.coll_avatar2 = p2.participant.vars.get('avatar') h.N_in_G = 3 h.part1 = "P1" h.part2 = "P2" def help_function2(group: BaseGroup): for n in group.get_players(): if n.coll_avatar1 == 1: n.imgColl1 = "/static/imgs/ava1.png" if n.coll_avatar1 == 2: n.imgColl1 = "/static/imgs/ava2.png" if n.coll_avatar1 == 3: n.imgColl1 = "/static/imgs/ava3.png" if n.coll_avatar1 == 4: n.imgColl1 = "/static/imgs/ava4.png" if n.coll_avatar2 == 1: n.imgColl2 = "/static/imgs/ava1.png" if n.coll_avatar2 == 2: n.imgColl2 = "/static/imgs/ava2.png" if n.coll_avatar2 == 3: n.imgColl2 = "/static/imgs/ava3.png" if n.coll_avatar2 == 4: n.imgColl2 = "/static/imgs/ava4.png" if n.participant.vars.get('avatar') == 1: n.ava_pic = "/static/imgs/ava1.png" if n.participant.vars.get('avatar') == 2: n.ava_pic = "/static/imgs/ava2.png" if n.participant.vars.get('avatar') == 3: n.ava_pic = "/static/imgs/ava3.png" if n.participant.vars.get('avatar') == 4: n.ava_pic = "/static/imgs/ava4.png" if C.Type == 2: if n.id_in_group == 1: n.help_number = 1 if n.id_in_group == 2: n.help_number = 2 if n.id_in_group == 3: n.help_number = 3 def cooperation(group: BaseGroup): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) p3 = group.get_player_by_id(3) if p1.part1vote == "Participant2": if p2.part2vote == "Participant1": group.Coo = 12 if p2.part2vote == "Participant3": if p3.part3vote == "Participant2": group.Coo = 23 if p3.part3vote == "Participant1": if p1.part1vote == "Participant3": group.Coo = 13 def cooperation_help(group: BaseGroup): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) p3 = group.get_player_by_id(3) if group.Coo == 0: if p1.part1vote == "Participant2": group.Coo = 13 p1.change = 1 p1.helpV = "P2" if p1.part1vote == "Participant3": group.Coo = 12 p1.change = 1 p1.helpV = "P3" def channel_function(group: BaseGroup): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) p3 = group.get_player_by_id(3) if C.Type == 2: if group.Coo == 13: p1.channel1 = group.id_in_subsession p2.channel1 = 500 + group.id_in_subsession p3.channel1 = group.id_in_subsession if group.Coo == 12: p1.channel1 = group.id_in_subsession p2.channel1 = group.id_in_subsession p3.channel1 = 500 + group.id_in_subsession if group.Coo == 23: p1.channel1 = 500 + group.id_in_subsession p2.channel1 = group.id_in_subsession p3.channel1 = group.id_in_subsession if C.Type == 1: p1.channel1 = 53 + group.id_in_subsession p2.channel1 = 103 + group.id_in_subsession p3.channel1 = 155 + group.id_in_subsession def payoff_function_stage4(group: BaseGroup): answers = C.answers for n in group.get_players(): if n.Q1 == answers[0]: n.payoff += 10 n.corr_answers += 1 if n.Q2 == answers[1]: n.payoff += 10 n.corr_answers += 1 if n.Q3 == answers[2]: n.payoff += 10 n.corr_answers += 1 if n.Q4 == answers[3]: n.payoff += 10 n.corr_answers += 1 if n.Q5 == answers[4]: n.payoff += 10 n.corr_answers += 1 if n.Q6 == answers[5]: n.payoff += 10 n.corr_answers += 1 if n.Q7 == answers[6]: n.payoff += 10 n.corr_answers += 1 if n.Q8 == answers[7]: n.payoff += 10 n.corr_answers += 1 if n.Q9 == answers[8]: n.payoff += 10 n.corr_answers += 1 if n.Q10 == answers[9]: n.payoff += 10 n.corr_answers += 1 if n.Q11 == answers[10]: n.payoff += 10 n.corr_answers += 1 if n.Q12 == answers[11]: n.payoff += 10 n.corr_answers += 1 def help_payoff_function(group: BaseGroup): for n in group.get_players(): n.participant.vars['payoff_stage4'] = n.payoff n.participant.vars['corr_answers'] = n.corr_answers def jersey_and_hat(group:BaseGroup): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) p3 = group.get_player_by_id(3) for n in group.get_players(): if n.id_in_group == 1: n.coll_jersey1 = p2.participant.vars.get('jersey') n.coll_jersey2 = p3.participant.vars.get('jersey') n.coll_hat1 = p2.participant.vars.get('hat') n.coll_hat2 = p3.participant.vars.get('hat') if n.id_in_group == 2: n.coll_jersey1 = p1.participant.vars.get('jersey') n.coll_jersey2 = p3.participant.vars.get('jersey') n.coll_hat1 = p1.participant.vars.get('hat') n.coll_hat2 = p3.participant.vars.get('hat') if n.id_in_group == 3: n.coll_jersey1 = p1.participant.vars.get('jersey') n.coll_jersey2 = p2.participant.vars.get('jersey') n.coll_hat1 = p1.participant.vars.get('hat') n.coll_hat2 = p2.participant.vars.get('hat') def create_name(group:BaseGroup): for p in group.get_players(): p.name = "P" + str(p.id_in_group) class Player(BasePlayer): Q1 = models.IntegerField(initial=0, label="You have become separated from your friends in a dense forest. You have no special signaling equipment. " "The best way to attempt to contact your friends is to:", choices=[(1,"call “help” loudly but in a deep voice."),(2, "yell or scream as loud as you can."), (3,"whistle loudly and shrilly.")]) Q2 = models.IntegerField(initial=0, label="You are in a region known for its many poisonous snakes. Your best action to avoid snakes is to:", choices=[(1,"make a lot of noise with your feet."), (2,"walk softly and quietly."), (3,"travel at night.")]) Q3 = models.IntegerField(initial=0, label="You are hungry and lost in the wild. The best rule for determining which plants are safe to eat (those you don’t recognize) is to:", choices=[(1,"try anything you see the birds eat."), (2,"eat anything except plants with bright red berries."), (3,"put a bit of the plant on your lower lip for five minutes; if it seems all right, try a little.")]) Q4 = models.IntegerField(initial=0, label="The day becomes dry and hot. You have a full canteen of water (about 1 litre) with you. You should: ", choices=[(1,"ration it-about a cupful a day."), (2,"not drink until you stop for the night, then drink what you think you need."), (3,"drink as much as you think you need when you need it.")]) Q5 = models.IntegerField(initial=0, label="Your water is gone; you become very thirsty. You finally come to a dried-up stream bed. Your best chance of finding water is to:", choices=[(1,"dig anywhere in the stream bed."), (2,"dig up plant and tree roots near the bank."), (3,"dig in the stream bed at the outside of a bend.")]) Q6 = models.IntegerField(initial=0, label="You decide to walk out of the wild country by following a series of ravines where a water supply is available. Night is coming on. The best place to make camp is:", choices=[(1,"next to the water supply in the ravine."), (2,"high on a ridge."), (3,"midway up the slope.")]) Q7 = models.IntegerField(initial=0, label="Your flashlight glows dimly as you are about to make your way back to your campsite after gathering firewood. Darkness comes quickly in the woods and the surroundings seem unfamiliar. You should:", choices=[(1,"head back at once, keeping the light on, hoping the light will glow enough for you to make landmarks."), (2,"put the batteries under your armpits to warm them, and then replace them in the flashlight."), (3,"shine your light for a few seconds, try to get the scene in mind, move in the darkness, and repeat the process.")]) Q8 = models.IntegerField(initial=0, label="An early snow confines you to your small tent. You fall asleep with your small stove going. There is danger if the flame is:", choices=[(1,"yellow."), (2,"blue."), (3,"red.")]) Q9 = models.IntegerField(initial=0, label="You must cross a river that has a strong current, large rocks, and some white water. After carefully selecting your crossing spot, you should:", choices=[(1,"leave your boots and pack on."), (2,"take your boots and pack off."), (3,"take off your pack but leave your boots on.")]) Q10 = models.IntegerField(initial=0, label="In waist-deep water with a strong current, when crossing the stream, you should face:", choices=[(1,"upstream (facing the oncoming water)."), (2,"across the stream (facing the opposite bank)."), (3,"downstream (back turned to the oncoming water).")]) Q11 = models.IntegerField(initial=0, label="You find yourself stuck on a steep cliff; your only route is up. The rocks are slippery with moss. You should try it:", choices=[(1,"barefoot."), (2,"with boots on."), (3,"with socks on.")]) Q12 = models.IntegerField(initial=0, label="Unarmed and unsuspecting, you surprise a large bear prowling around your campsite. As the bear stands up about ten meters from you, you should:", choices=[(1,"run."), (2,"climb the nearest tree."), (3,"freeze, but be ready to back away slowly.")]) coll_avatar1 = models.IntegerField() coll_avatar2 = models.IntegerField() N_in_G = models.IntegerField() part1 = models.StringField() part2 = models.StringField() imgColl1 = models.StringField() imgColl2 = models.StringField() ava_pic = models.StringField() part1vote = models.StringField(initial=0, label="", choices=["Participant2", "Participant3"], widget=widgets.RadioSelectHorizontal) part2vote = models.StringField(initial=0,label="", widget=widgets.RadioSelectHorizontal, choices=["Participant1", "Participant3"]) part3vote = models.StringField(initial=0,label="", widget=widgets.RadioSelectHorizontal, choices=["Participant1", "Participant2"]) change = models.IntegerField(initial=0) channel1 = models.IntegerField() corr_answers = models.IntegerField(initial=0) help_number = models.IntegerField(initial=0) coll_jersey1 = models.IntegerField() coll_jersey2 = models.IntegerField() coll_hat1 = models.IntegerField() coll_hat2 = models.IntegerField() helpV = models.StringField() helpV2 = models.StringField(widget=widgets.RadioSelectHorizontal, choices=["Participant"]) name = models.StringField() # PAGES class WaitPageShuffle(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): new_gr = subsession.session.vars.get('new_groups') subsession.set_group_matrix(new_gr) class HelpWaitPage(WaitPage): def after_all_players_arrive(group: Group): group.help_function() class HelpWaitPage2(WaitPage): def after_all_players_arrive(group: Group): group.help_function2() group.jersey_and_hat() class Instructions1(Page): def vars_for_template(player: Player): return dict( avatar=player.participant.vars.get('avatar'), ava_pic=player.ava_pic ) class QuestionsStage2(Page): form_model = 'player' form_fields = ['Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12'] def vars_for_template(player: Player): return dict( avatar=player.participant.vars.get('avatar'), ava_pic=player.ava_pic ) class Stage3(Page): def vars_for_template(player: Player): return dict( avatar=player.participant.vars.get('avatar'), ava_pic=player.ava_pic ) class Stage3inf(Page): def vars_for_template(player: Player): return dict( avatar=player.participant.vars.get('avatar'), number_in_group=player.id_in_group, coll_ava1=player.coll_avatar1, coll_ava2=player.coll_avatar2, part1=player.part1, part2=player.part2, imgColl1=player.imgColl1, imgColl2=player.imgColl2, ava_pic = player.ava_pic ) class Stage4Inf1(Page): form_model = 'player' form_fields = ['part1vote'] def is_displayed(player: Player): return player.help_number == 1 def vars_for_template(player: Player): return dict( avatar=player.participant.vars.get('avatar'), number_in_group=player.id_in_group, coll_ava1=player.coll_avatar1, coll_ava2=player.coll_avatar2, n_in_group=player.N_in_G, treat=C.Type, part1=player.part1, part2=player.part2, imgColl1=player.imgColl1, imgColl2=player.imgColl2, ava_pic=player.ava_pic, num_opt=range(C.num_options), ) class Stage4Inf2(Page): form_model = 'player' form_fields = ['part2vote'] def is_displayed(player: Player): return player.help_number == 2 def vars_for_template(player: Player): return dict( avatar=player.participant.vars.get('avatar'), number_in_group=player.id_in_group, coll_ava1=player.coll_avatar1, coll_ava2=player.coll_avatar2, n_in_group=player.N_in_G, treat=C.Type, part1=player.part1, part2=player.part2, imgColl1=player.imgColl1, imgColl2=player.imgColl2, ava_pic=player.ava_pic ) class Stage4Inf3(Page): form_model = 'player' form_fields = ['part3vote'] def is_displayed(player: Player): return player.help_number == 3 def vars_for_template(player: Player): return dict( avatar=player.participant.vars.get('avatar'), number_in_group=player.id_in_group, coll_ava1=player.coll_avatar1, coll_ava2=player.coll_avatar2, n_in_group=player.N_in_G, treat=C.Type, part1=player.part1, part2=player.part2, imgColl1=player.imgColl1, imgColl2=player.imgColl2, ava_pic=player.ava_pic ) class Stage4InfC(Page): def is_displayed(player: Player): return C.Type == 1 def vars_for_template(player: Player): return dict( avatar=player.participant.vars.get('avatar'), number_in_group=player.id_in_group, coll_ava1=player.coll_avatar1, coll_ava2=player.coll_avatar2, part1=player.part1, part2=player.part2, imgColl1=player.imgColl1, imgColl2=player.imgColl2, ava_pic = player.ava_pic, treat=C.Type ) class CoopWaitPage(WaitPage): def after_all_players_arrive(group: Group): group.cooperation() group.create_name() class CoopWaitPage2(WaitPage): def after_all_players_arrive(group: Group): group.cooperation_help() class Stage4Results(Page): def is_displayed(player: Player): return C.Type == 2 def vars_for_template(player: Player): return dict( avatar=player.participant.vars.get('avatar'), id_in_group=player.id_in_group, coll_ava1=player.coll_avatar1, coll_ava2=player.coll_avatar2, n_in_group=player.N_in_G, treat=C.Type, part1=player.part1, part2=player.part2, imgColl1=player.imgColl1, imgColl2=player.imgColl2, ava_pic=player.ava_pic, Coo=player.group.Coo, hat=player.participant.vars.get('hat'), jersey=player.participant.vars.get('jersey'), coll_hat1=player.coll_hat1, coll_hat2=player.coll_hat2, coll_jersey1=player.coll_jersey1, coll_jersey2=player.coll_jersey2 ) class ChannelWaitPage(WaitPage): wait_for_all_groups = True class ChannelWaitPage2(WaitPage): def after_all_players_arrive(group: Group): group.channel_function() class Stage4Questions(Page): form_model = 'player' form_fields = ['Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12'] def vars_for_template(player: Player): return dict( avatar=player.participant.vars.get('avatar'), id_in_group=player.id_in_group, coll_ava1=player.coll_avatar1, coll_ava2=player.coll_avatar2, n_in_group=player.N_in_G, treat=C.Type, part1=player.part1, part2=player.part2, imgColl1=player.imgColl1, imgColl2=player.imgColl2, ava_pic=player.ava_pic, Coo=player.group.Coo, channel=player.channel1, hat=player.participant.vars.get('hat'), jersey=player.participant.vars.get('jersey'), coll_hat1=player.coll_hat1, coll_hat2=player.coll_hat2, coll_jersey1=player.coll_jersey1, coll_jersey2=player.coll_jersey2, name=player.name ) timeout_seconds = C.sec class ResultsWaitPage(WaitPage): def after_all_players_arrive(group: Group): group.payoff_function_stage4() class ResultsWaitPage2(WaitPage): def after_all_players_arrive(group: Group): group.help_payoff_function() class WaitPage3(WaitPage): wait_for_all_groups = True class InfoPage(Page): def vars_for_template(player: Player): return dict( ava_pic=player.ava_pic, ) class Stage4InfHelp(Page): form_model = 'player' form_fields = ['helpV2'] def is_displayed(player: Player): return player.change == 1 def vars_for_template(player: Player): return dict( ava_pic=player.ava_pic, helpV=player.helpV, imgColl1=player.imgColl1, imgColl2=player.imgColl2, part1=player.part1, part2=player.part2, number_in_group=player.id_in_group, ) page_sequence = [WaitPageShuffle, HelpWaitPage, HelpWaitPage2, Instructions1, QuestionsStage2, WaitPage3, Stage3, WaitPage3, Stage3inf, Stage4Inf1, Stage4Inf2, Stage4Inf3, Stage4InfC, CoopWaitPage, CoopWaitPage2, Stage4InfHelp, WaitPage3, Stage4Results, ChannelWaitPage, ChannelWaitPage2, Stage4Questions, ResultsWaitPage, ResultsWaitPage2, InfoPage]