from otree.api import * import random doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'games5' 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' class Subsession(BaseSubsession): pass class Group(BaseGroup): total_contribution1 = models.FloatField() total_contribution2 = models.FloatField() total_contribution3 = models.FloatField() total_contribution4 = models.FloatField() total_contribution5 = models.FloatField() total_contribution6 = models.FloatField() class Player(BasePlayer): contribution1 = models.FloatField( min=0, max=19, label="How much will you contribute?" ) contribution2 = models.FloatField( min=0, max=23, label="How much will you contribute?" ) contribution3 = models.FloatField( min=0, max=19, label="How much will you contribute?" ) contribution4 = models.FloatField( min=0, max=23, label="How much will you contribute?" ) contribution5 = models.FloatField( min=0, max=19, label="How much will you contribute?" ) contribution6 = models.FloatField( min=0, max=23, label="How much will you contribute?" ) profit1 = models.FloatField() profit2 = models.FloatField() profit3 = models.FloatField() profit4 = models.FloatField() profit5 = models.FloatField() profit6 = 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): subsession.group_randomly() class Contribute1(Page): form_model = 'player' form_fields = ['contribution1'] 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] group.total_contribution1 = player_1.contribution1 + player_2.contribution1 if group.total_contribution1 >= 10: player_1.profit1 = 19 - player_1.contribution1 player_2.profit1 = 19 - player_2.contribution1 else: player_1.profit1 = 10 - player_1.contribution1 player_2.profit1 = 10 - player_2.contribution1 class ShuffleWaitPage2(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): subsession.group_randomly() class Contribute2(Page): form_model = 'player' form_fields = ['contribution2'] 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] group.total_contribution2 = player_1.contribution2 + player_2.contribution2 if group.total_contribution2 >= 12: player_1.profit2 = 23 - player_1.contribution2 player_2.profit2 = 23 - player_2.contribution2 else: player_1.profit2 = 12 - player_1.contribution2 player_2.profit2 = 12 - player_2.contribution2 class ShuffleWaitPage3(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[5, 7], [1, 10], [2, 9], [6, 4], [8, 3]] subsession.set_group_matrix(matrix) class Contribute3(Page): form_model = 'player' form_fields = ['contribution3'] @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] group.total_contribution3 = player_1.contribution3 + player_2.contribution3 x = int(random.uniform(0, 4)) y = 0 if x == 0: y = 0.5 if group.total_contribution3 >= 10: player_1.profit3 = 19 - player_1.contribution3 + y player_2.profit3 = 19 - player_2.contribution3 else: player_1.profit3 = 10 - player_1.contribution3 + y player_2.profit3 = 10 - player_2.contribution3 class ShuffleWaitPage4(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[3, 4], [5, 2], [9, 8], [6, 1], [7, 10]] subsession.set_group_matrix(matrix) class Contribute4(Page): form_model = 'player' form_fields = ['contribution4'] @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 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] group.total_contribution4 = player_1.contribution4 + player_2.contribution4 if group.total_contribution4 >= 12: player_1.profit4 = 23 - player_1.contribution4 player_2.profit4 = 23 - player_2.contribution4 else: player_1.profit4 = 12 - player_1.contribution4 player_2.profit4 = 12 - player_2.contribution4 class ShuffleWaitPage5(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[9, 5], [3, 2], [7, 8], [10, 1], [4, 6]] subsession.set_group_matrix(matrix) class Contribute5(Page): form_model = 'player' form_fields = ['contribution5'] @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] group.total_contribution5 = player_1.contribution5 + player_2.contribution5 x = int(random.uniform(0, 4)) y = 0 if x == 0: y = 0.5 if group.total_contribution5 >= 10: player_1.profit5 = 19 - player_1.contribution5 + y player_2.profit5 = 19 - player_2.contribution5 else: player_1.profit5 = 10 - player_1.contribution5 + y player_2.profit5 = 10 - player_2.contribution5 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 = ['contribution6'] @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 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] group.total_contribution6 = player_1.contribution6 + player_2.contribution6 if group.total_contribution6 >= 12: player_1.profit6 = 23 - player_1.contribution6 player_2.profit6 = 23 - player_2.contribution6 else: player_1.profit6 = 12 - player_1.contribution6 player_2.profit6 = 12 - player_2.contribution6 class Results(Page): form_model = 'player' @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant # real x = x = int(random.uniform(1, 7)) # print(x) participant.gn5 = x + 24 if x == 1: participant.pt5 = player.profit1 participant.oe5 = 1 participant.dec5 = player.contribution1 elif x == 2: participant.pt5 = player.profit2 participant.oe5 = 2 participant.dec5 = player.contribution2 elif x == 3: participant.pt5 = player.profit3 participant.oe5 = 1 participant.dec5 = player.contribution3 elif x == 4: participant.pt5 = player.profit4 participant.oe5 = 2 participant.dec5 = player.contribution4 elif x == 5: participant.pt5 = player.profit5 participant.oe5 = 1 participant.dec5 = player.contribution5 elif x == 6: participant.pt5 = player.profit6 participant.oe5 = 2 participant.dec5 = player.contribution6 elif x == 7: participant.pt5 = player.profit6 participant.oe5 = 2 participant.dec5 = player.contribution6 page_sequence = [Inst1, ShuffleWaitPage1, Contribute1, WaitPage1, ShuffleWaitPage2, Contribute2, WaitPage2, ShuffleWaitPage3, Contribute3, WaitPage3, ShuffleWaitPage4, Contribute4, WaitPage4, ShuffleWaitPage5, Contribute5, WaitPage5, ShuffleWaitPage6, Contribute6, WaitPage6, Results] # page_sequence = [ Contribute1, WaitPage1, # Contribute2, WaitPage2, # Contribute3, WaitPage3, # Contribute4, WaitPage4, # Contribute5, WaitPage5, # Contribute6, WaitPage6, # Results]