from otree.api import * import random doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'games34' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 1 CHAT_TEMPLATE = __name__ + '/chat.html' CHAT2_TEMPLATE = __name__ + '/chat2.html' CHAT3_TEMPLATE = __name__ + '/chat3.html' CHAT4_TEMPLATE = __name__ + '/chat4.html' CHAT5_TEMPLATE = __name__ + '/chat5.html' CHAT6_TEMPLATE = __name__ + '/chat6.html' CHAT7_TEMPLATE = __name__ + '/chat7.html' CHAT8_TEMPLATE = __name__ + '/chat8.html' class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): decision1 = models.IntegerField( # label="How much would you like to assign for yourself?", max=40, min=0 ) decision2 = models.IntegerField( # label="What is your minimum willingness to accept?", max=40, min=0 ) decision3 = models.IntegerField( # label="How much would you like to assign for yourself?", max=40, min=0 ) decision4 = models.IntegerField( # label="What is your minimum willingness to accept?", max=40, min=0 ) decision5 = models.IntegerField( # label="How much would you like to assign for yourself?", max=40, min=0 ) decision6 = models.IntegerField( # label="What is your minimum willingness to accept?", max=40, min=0 ) gp1 = models.IntegerField() gr1 = models.IntegerField() gp2 = models.IntegerField() gr2 = models.IntegerField() gp3 = models.IntegerField() gr3 = models.IntegerField() gp4 = models.IntegerField() gr4 = models.IntegerField() gp5 = models.IntegerField() gr5 = models.IntegerField() gp6 = models.IntegerField() gr6 = models.IntegerField() profit1 = models.FloatField() profit2 = models.FloatField() profit3 = models.FloatField() profit4 = models.FloatField() profit5 = models.FloatField() profit6 = models.FloatField() decision1_1 = models.IntegerField( # label="How much would you like to assign for yourself?", max=30, min=0 ) decision2_1 = models.IntegerField( # label="What is your minimum willingness to accept?", max=30, min=0 ) decision3_1 = models.IntegerField( # label="How much would you like to assign for yourself?", max=30, min=0 ) decision4_1 = models.IntegerField( # label="What is your minimum willingness to accept?", max=30, min=0 ) decision5_1 = models.IntegerField( # label="How much would you like to assign for yourself?", max=30, min=0 ) decision6_1 = models.IntegerField( # label="What is your minimum willingness to accept?", max=30, min=0 ) gp1_1 = models.IntegerField() gr1_1 = models.IntegerField() gp2_1 = models.IntegerField() gr2_1 = models.IntegerField() gp3_1 = models.IntegerField() gr3_1 = models.IntegerField() gp4_1 = models.IntegerField() gr4_1 = models.IntegerField() gp5_1 = models.IntegerField() gr5_1 = models.IntegerField() gp6_1 = models.IntegerField() gr6_1 = models.IntegerField() profit1_1 = models.FloatField() profit2_1 = models.FloatField() profit3_1 = models.FloatField() profit4_1 = models.FloatField() profit5_1 = models.FloatField() profit6_1 = models.FloatField() class Message(ExtraModel): group = models.Link(Group) sender = models.Link(Player) text = models.StringField() def to_dict(msg: Message): return dict(sender=msg.sender.id_in_group, text=msg.text) # PAGES class Inst1(Page): pass class ShuffleWaitPage1(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[2, 5], [4, 9], [6, 7], [8, 10], [1, 3]] subsession.set_group_matrix(matrix) class Contribute1(Page): form_model = 'player' form_fields = ['decision1'] class WaitPage1(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] if 40 - player_1.decision1 >= player_2.decision1: player_1.profit1 = player_1.decision1 player_2.profit1 = 40 - player_1.decision1 player_1.gp1 = player_1.decision1 player_2.gp1 = player_1.decision1 player_1.gr1 = player_2.decision1 player_2.gr1 = player_2.decision1 else: player_1.profit1 = 0 player_2.profit1 = 0 player_1.gp1 = player_1.decision1 player_2.gp1 = player_1.decision1 player_1.gr1 = player_2.decision1 player_2.gr1 = player_2.decision1 class ShuffleWaitPage2(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[9, 2], [10, 6], [7, 8], [5, 1], [3, 4]] subsession.set_group_matrix(matrix) class Contribute2(Page): form_model = 'player' form_fields = ['decision2'] class WaitPage2(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] if 40 - player_1.decision2 >= player_2.decision2: player_1.profit2 = player_1.decision2 player_2.profit2 = 40 - player_1.decision2 player_1.gp2 = player_1.decision2 player_2.gp2 = player_1.decision2 player_1.gr2 = player_2.decision2 player_2.gr2 = player_2.decision2 else: player_1.profit2 = 0 player_2.profit2 = 0 player_1.gp2 = player_1.decision2 player_2.gp2 = player_1.decision2 player_1.gr2 = player_2.decision2 player_2.gr2 = player_2.decision2 class ShuffleWaitPage3(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[9, 1], [8, 5], [3, 7], [4, 6], [10, 2]] subsession.set_group_matrix(matrix) class Contribute3(Page): form_model = 'player' form_fields = ['decision3'] @staticmethod def live_method(player: Player, data): my_id = player.id_in_group group = player.group if 'text' in data: text = data['text'] msg = Message.create(group=group, sender=player, text=text) return {0: [to_dict(msg)]} return {my_id: [to_dict(msg) for msg in Message.filter(group=group)]} def vars_for_template(player: Player): sender = player.id_in_group return dict( sender=(sender == 1), receiver=(sender == 2) ) class WaitPage3(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] x = int(random.uniform(0, 4)) y = 0 if x == 0: y = 0.5 if 40 - player_1.decision3 >= player_2.decision3: player_1.profit3 = player_1.decision3 + y player_2.profit3 = 40 - player_1.decision3 player_1.gp3 = player_1.decision3 player_2.gp3 = player_1.decision3 player_1.gr3 = player_2.decision3 player_2.gr3 = player_2.decision3 else: player_1.profit3 = 0 + y player_2.profit3 = 0 player_1.gp3 = player_1.decision3 player_2.gp3 = player_1.decision3 player_1.gr3 = player_2.decision3 player_2.gr3 = player_2.decision3 class ShuffleWaitPage4(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[9, 6], [8, 7], [3, 2], [4, 5], [10, 1]] subsession.set_group_matrix(matrix) class Contribute4(Page): form_model = 'player' form_fields = ['decision4'] @staticmethod def live_method(player: Player, data): my_id = player.id_in_group group = player.group if 'text' in data: text = data['text'] msg = Message.create(group=group, sender=player, text=text) return {0: [to_dict(msg)]} return {my_id: [to_dict(msg) for msg in Message.filter(group=group)]} def vars_for_template(player: Player): sender = player.id_in_group return dict( sender=(sender == 2), receiver=(sender == 1) ) class WaitPage4(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] x = int(random.uniform(0, 4)) y = 0 if x == 0: y = 0.5 if 40 - player_1.decision4 >= player_2.decision4: player_1.profit4 = player_1.decision4 player_2.profit4 = 40 - player_1.decision4 + y player_1.gp4 = player_1.decision4 player_2.gp4 = player_1.decision4 player_1.gr4 = player_2.decision4 player_2.gr4 = player_2.decision4 else: player_1.profit4 = 0 player_2.profit4 = 0 + y player_1.gp4 = player_1.decision4 player_2.gp4 = player_1.decision4 player_1.gr4 = player_2.decision4 player_2.gr4 = player_2.decision4 class ShuffleWaitPage5(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[2, 9], [1, 8], [6, 3], [7, 4], [5, 10]] subsession.set_group_matrix(matrix) class Contribute5(Page): form_model = 'player' form_fields = ['decision5'] @staticmethod def live_method(player: Player, data): my_id = player.id_in_group group = player.group if 'text' in data: text = data['text'] msg = Message.create(group=group, sender=player, text=text) return {0: [to_dict(msg)]} return {my_id: [to_dict(msg) for msg in Message.filter(group=group)]} def vars_for_template(player: Player): sender = player.id_in_group return dict( sender=(sender == 1), receiver=(sender == 2) ) class WaitPage5(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] x = int(random.uniform(0, 4)) y = 0 if x == 0: y = 0.5 if 40 - player_1.decision5 >= player_2.decision5: player_1.profit5 = player_1.decision5 + y player_2.profit5 = 40 - player_1.decision5 player_1.gp5 = player_1.decision5 player_2.gp5 = player_1.decision5 player_1.gr5 = player_2.decision5 player_2.gr5 = player_2.decision5 else: player_1.profit5 = 0 + y player_2.profit5 = 0 player_1.gp5 = player_1.decision5 player_2.gp5 = player_1.decision5 player_1.gr5 = player_2.decision5 player_2.gr5 = player_2.decision5 class ShuffleWaitPage6(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[4, 9], [1, 7], [8, 5], [10, 3], [2, 6]] subsession.set_group_matrix(matrix) class Contribute6(Page): form_model = 'player' form_fields = ['decision6'] @staticmethod def live_method(player: Player, data): my_id = player.id_in_group group = player.group if 'text' in data: text = data['text'] msg = Message.create(group=group, sender=player, text=text) return {0: [to_dict(msg)]} return {my_id: [to_dict(msg) for msg in Message.filter(group=group)]} def vars_for_template(player: Player): sender = player.id_in_group return dict( sender=(sender == 2), receiver=(sender == 1) ) class WaitPage6(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] x = int(random.uniform(0, 4)) y = 0 if x == 0: y = 0.5 if 40 - player_1.decision6 >= player_2.decision6: player_1.profit6 = player_1.decision6 player_2.profit6 = 40 - player_1.decision6 + y player_1.gp6 = player_1.decision6 player_2.gp6 = player_1.decision6 player_1.gr6 = player_2.decision6 player_2.gr6 = player_2.decision6 else: player_1.profit6 = 0 player_2.profit6 = 0 + y player_1.gp6 = player_1.decision6 player_2.gp6 = player_1.decision6 player_1.gr6 = player_2.decision6 player_2.gr6 = player_2.decision6 class ShuffleWaitPage1_1(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[7, 4], [8, 9], [5, 6], [1, 10], [2, 3]] subsession.set_group_matrix(matrix) class Contribute1_1(Page): form_model = 'player' form_fields = ['decision1_1'] class WaitPage1_1(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] if 30 - player_1.decision1_1 >= player_2.decision1_1: player_1.profit1_1 = player_1.decision1_1 player_2.profit1_1 = 30 - player_1.decision1_1 player_1.gp1_1 = player_1.decision1_1 player_2.gp1_1 = player_1.decision1_1 player_1.gr1_1 = player_2.decision1_1 player_2.gr1_1 = player_2.decision1_1 else: player_1.profit1_1 = 0 player_2.profit1_1 = 0 player_1.gp1_1 = player_1.decision1_1 player_2.gp1_1 = player_1.decision1_1 player_1.gr1_1 = player_2.decision1_1 player_2.gr1_1 = player_2.decision1_1 class ShuffleWaitPage2_1(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[6, 7], [9, 8], [10, 2], [4, 1], [3, 5]] subsession.set_group_matrix(matrix) class Contribute2_1(Page): form_model = 'player' form_fields = ['decision2_1'] class WaitPage2_1(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] if 30 - player_1.decision2_1 >= player_2.decision2_1: player_1.profit2_1 = player_1.decision2_1 player_2.profit2_1 = 30 - player_1.decision2_1 player_1.gp2_1 = player_1.decision2_1 player_2.gp2_1 = player_1.decision2_1 player_1.gr2_1 = player_2.decision2_1 player_2.gr2_1 = player_2.decision2_1 else: player_1.profit2_1 = 0 player_2.profit2_1 = 0 player_1.gp2_1 = player_1.decision2_1 player_2.gp2_1 = player_1.decision2_1 player_1.gr2_1 = player_2.decision2_1 player_2.gr2_1 = player_2.decision2_1 class ShuffleWaitPage3_1(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[2, 9], [1, 8], [10, 7], [6, 3], [4, 5]] subsession.set_group_matrix(matrix) class Contribute3_1(Page): form_model = 'player' form_fields = ['decision3_1'] @staticmethod def live_method(player: Player, data): my_id = player.id_in_group group = player.group if 'text' in data: text = data['text'] msg = Message.create(group=group, sender=player, text=text) return {0: [to_dict(msg)]} return {my_id: [to_dict(msg) for msg in Message.filter(group=group)]} def vars_for_template(player: Player): sender = player.id_in_group return dict( sender=(sender == 1), receiver=(sender == 2) ) class WaitPage3_1(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] if 30 - player_1.decision3_1 >= player_2.decision3_1: player_1.profit3_1 = player_1.decision3_1 player_2.profit3_1 = 30 - player_1.decision3_1 player_1.gp3_1 = player_1.decision3_1 player_2.gp3_1 = player_1.decision3_1 player_1.gr3_1 = player_2.decision3_1 player_2.gr3_1 = player_2.decision3_1 else: player_1.profit3_1 = 0 player_2.profit3_1 = 0 player_1.gp3_1 = player_1.decision3_1 player_2.gp3_1 = player_1.decision3_1 player_1.gr3_1 = player_2.decision3_1 player_2.gr3_1 = player_2.decision3_1 class ShuffleWaitPage4_1(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[2, 5], [1, 9], [10, 3], [6, 8], [4, 7]] subsession.set_group_matrix(matrix) class Contribute4_1(Page): form_model = 'player' form_fields = ['decision4_1'] @staticmethod def live_method(player: Player, data): my_id = player.id_in_group group = player.group if 'text' in data: text = data['text'] msg = Message.create(group=group, sender=player, text=text) return {0: [to_dict(msg)]} return {my_id: [to_dict(msg) for msg in Message.filter(group=group)]} def vars_for_template(player: Player): sender = player.id_in_group return dict( sender=(sender == 2), receiver=(sender == 1) ) class WaitPage4_1(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] if 30 - player_1.decision4_1 >= player_2.decision4_1: player_1.profit4_1 = player_1.decision4_1 player_2.profit4_1 = 30 - player_1.decision4_1 player_1.gp4_1 = player_1.decision4_1 player_2.gp4_1 = player_1.decision4_1 player_1.gr4_1 = player_2.decision4_1 player_2.gr4_1 = player_2.decision4_1 else: player_1.profit4_1 = 0 player_2.profit4_1 = 0 player_1.gp4_1 = player_1.decision4_1 player_2.gp4_1 = player_1.decision4_1 player_1.gr4_1 = player_2.decision4_1 player_2.gr4_1 = player_2.decision4_1 class ShuffleWaitPage5_1(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[7, 2], [3, 1], [5, 10], [9, 6], [8, 4]] subsession.set_group_matrix(matrix) class Contribute5_1(Page): form_model = 'player' form_fields = ['decision5_1'] @staticmethod def live_method(player: Player, data): my_id = player.id_in_group group = player.group if 'text' in data: text = data['text'] msg = Message.create(group=group, sender=player, text=text) return {0: [to_dict(msg)]} return {my_id: [to_dict(msg) for msg in Message.filter(group=group)]} def vars_for_template(player: Player): sender = player.id_in_group return dict( sender=(sender == 1), receiver=(sender == 2) ) class WaitPage5_1(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] if 30 - player_1.decision5_1 >= player_2.decision5_1: player_1.profit5_1 = player_1.decision5_1 player_2.profit5_1 = 30 - player_1.decision5_1 player_1.gp5_1 = player_1.decision5_1 player_2.gp5_1 = player_1.decision5_1 player_1.gr5_1 = player_2.decision5_1 player_2.gr5_1 = player_2.decision5_1 else: player_1.profit5_1 = 0 player_2.profit5_1 = 0 player_1.gp5_1 = player_1.decision5_1 player_2.gp5_1 = player_1.decision5_1 player_1.gr5_1 = player_2.decision5_1 player_2.gr5_1 = player_2.decision5_1 class ShuffleWaitPage6_1(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[3, 2], [5, 1], [8, 10], [7, 6], [9, 4]] subsession.set_group_matrix(matrix) class Contribute6_1(Page): form_model = 'player' form_fields = ['decision6_1'] @staticmethod def live_method(player: Player, data): my_id = player.id_in_group group = player.group if 'text' in data: text = data['text'] msg = Message.create(group=group, sender=player, text=text) return {0: [to_dict(msg)]} return {my_id: [to_dict(msg) for msg in Message.filter(group=group)]} def vars_for_template(player: Player): sender = player.id_in_group return dict( sender=(sender == 2), receiver=(sender == 1) ) class WaitPage6_1(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player_lists = group.get_players() player_1 = player_lists[0] player_2 = player_lists[1] if 30 - player_1.decision6_1 >= player_2.decision6_1: player_1.profit6_1 = player_1.decision6_1 player_2.profit6_1 = 30 - player_1.decision6_1 player_1.gp6_1 = player_1.decision6_1 player_2.gp6_1 = player_1.decision6_1 player_1.gr6_1 = player_2.decision6_1 player_2.gr6_1 = player_2.decision6_1 else: player_1.profit6_1 = 0 player_2.profit6_1 = 0 player_1.gp6_1 = player_1.decision6_1 player_2.gp6_1 = player_1.decision6_1 player_1.gr6_1 = player_2.decision6_1 player_2.gr6_1 = player_2.decision6_1 class Results(Page): form_model = 'player' @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant # real x = int(random.uniform(1, 7)) # print(x) participant.gn3 = x + 12 if x == 1: participant.pt3 = player.profit1 participant.prop1 = player.gp1 participant.rec1 = player.gr1 elif x == 2: participant.pt3 = player.profit2 participant.prop1 = player.gp2 participant.rec1 = player.gr2 elif x == 3: participant.pt3 = player.profit3 participant.prop1 = player.gp3 participant.rec1 = player.gr3 elif x == 4: participant.pt3 = player.profit4 participant.prop1 = player.gp4 participant.rec1 = player.gr4 elif x == 5: participant.pt3 = player.profit5 participant.prop1 = player.gp5 participant.rec1 = player.gr5 elif x == 6: participant.pt3 = player.profit6 participant.prop1 = player.gp6 participant.rec1 = player.gr6 elif x == 7: participant.pt3 = player.profit6 participant.prop1 = player.gp6 participant.rec1 = player.gr6 x2 = int(random.uniform(1, 7)) # print(x) participant.gn4 = x2 + 18 if x2 == 1: participant.pt4 = player.profit1_1 participant.prop2 = player.gp1_1 participant.rec2 = player.gr1_1 elif x2 == 2: participant.pt4 = player.profit2_1 participant.prop2 = player.gp2_1 participant.rec2 = player.gr2_1 elif x2 == 3: participant.pt4 = player.profit3_1 participant.prop2 = player.gp3_1 participant.rec2 = player.gr3_1 elif x2 == 4: participant.pt4 = player.profit4_1 participant.prop2 = player.gp4_1 participant.rec2 = player.gr4_1 elif x2 == 5: participant.pt4 = player.profit5_1 participant.prop2 = player.gp5_1 participant.rec2 = player.gr5_1 elif x2 == 6: participant.pt4 = player.profit6_1 participant.prop2 = player.gp6_1 participant.rec2 = player.gr6_1 elif x2 == 7: participant.pt4 = player.profit6_1 participant.prop2 = player.gp6_1 participant.rec2 = player.gr6_1 page_sequence = [Inst1, ShuffleWaitPage1, Contribute1, WaitPage1, ShuffleWaitPage2, Contribute2, WaitPage2, ShuffleWaitPage3, Contribute3, WaitPage3, ShuffleWaitPage4, Contribute4, WaitPage4, ShuffleWaitPage5, Contribute5, WaitPage5, ShuffleWaitPage6, Contribute6, WaitPage6, ShuffleWaitPage1_1, Contribute1_1, WaitPage1_1, ShuffleWaitPage2_1, Contribute2_1, WaitPage2_1, ShuffleWaitPage3_1, Contribute3_1, WaitPage3_1, ShuffleWaitPage4_1, Contribute4_1, WaitPage4_1, ShuffleWaitPage5_1, Contribute5_1, WaitPage5_1, ShuffleWaitPage6_1, Contribute6_1, WaitPage6_1, Results] # page_sequence = [Inst1, WaitFake, Contribute1, WaitPage1, # Contribute2, WaitPage2, # Contribute3, WaitPage3, # Contribute4, WaitPage4, # Contribute5, WaitPage5, # Contribute6, WaitPage6, # Results]