from otree.api import * import random doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'games2' 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): pass class Player(BasePlayer): profit1 = models.FloatField() profit2 = models.FloatField() profit3 = models.FloatField() profit4 = models.FloatField() profit5 = models.FloatField() profit6 = models.FloatField() pref1 = models.BooleanField( label="Please choose if you want to Cooperate or Defect", choices=[ [True, "Cooperate"], [False, "Defect"] ] ) pref2 = models.BooleanField( label="Please choose if you want to Cooperate or Defect", choices=[ [True, "Cooperate"], [False, "Defect"] ] ) pref3 = models.BooleanField( label="Please choose if you want to Cooperate or Defect", choices=[ [True, "Cooperate"], [False, "Defect"] ] ) pref4 = models.BooleanField( label="Please choose if you want to Cooperate or Defect", choices=[ [True, "Cooperate"], [False, "Defect"] ] ) pref5 = models.BooleanField( label="Please choose if you want to Cooperate or Defect", choices=[ [True, "Cooperate"], [False, "Defect"] ] ) pref6 = models.BooleanField( label="Please choose if you want to Cooperate or Defect", choices=[ [True, "Cooperate"], [False, "Defect"] ] ) compa1 = models.IntegerField( label="My payoff in this case is," ) compa2 = models.IntegerField( label="My partner's payoff in this case is," ) 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 Comp1(Page): form_model = "player" form_fields = ["compa1", "compa2"] class Comp2(Page): def vars_for_template(player): correct = 0 if player.compa1 == 3: if player.compa2 == 6: correct = 1 return dict( correct=correct ) class ShuffleWaitPage0(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): subsession.group_randomly() class Game1(Page): form_model = "player" form_fields = ["pref1"] 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 player_1.pref1: if player_2.pref1: player_1.profit1 = 10 player_2.profit1 = 10 else: player_1.profit1 = 7 player_2.profit1 = 11 else: if player_2.pref1: player_1.profit1 = 11 player_2.profit1 = 7 else: player_1.profit1 = 8 player_2.profit1 = 8 class ShuffleWaitPage1(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): subsession.group_randomly() class Game2(Page): form_model = "player" form_fields = ["pref2"] 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 player_1.pref2: if player_2.pref2: player_1.profit2 = 9 player_2.profit2 = 9 else: player_1.profit2 = 6 player_2.profit2 = 10 else: if player_2.pref2: player_1.profit2 = 10 player_2.profit2 = 6 else: player_1.profit2 = 7 player_2.profit2 = 7 class ShuffleWaitPage2(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[1, 7], [4, 2], [3, 9], [10, 8], [6, 5]] subsession.set_group_matrix(matrix) class Game3(Page): form_model = "player" form_fields = ["pref3"] @staticmethod def js_vars(player: Player): return dict(my_id=player.id_in_group) @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 # print(x) # print(y) if player_1.pref3: if player_2.pref3: player_1.profit3 = 10 + y player_2.profit3 = 10 else: player_1.profit3 = 7 + y player_2.profit3 = 11 else: if player_2.pref3: player_1.profit3 = 11 + y player_2.profit3 = 7 else: player_1.profit3 = 8 + y player_2.profit3 = 8 class ShuffleWaitPage3(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[3, 5], [1, 2], [4, 9], [6, 8], [10, 7]] subsession.set_group_matrix(matrix) class Game4(Page): form_model = "player" form_fields = ["pref4"] @staticmethod def js_vars(player: Player): return dict(my_id=player.id_in_group) @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] if player_1.pref4: if player_2.pref4: player_1.profit4 = 9 player_2.profit4 = 9 else: player_1.profit4 = 10 player_2.profit4 = 6 else: if player_2.pref4: player_1.profit4 = 6 player_2.profit4 = 10 else: player_1.profit4 = 7 player_2.profit4 = 7 class ShuffleWaitPage4(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[8, 1], [7, 6], [2, 10], [5, 3], [9, 4]] subsession.set_group_matrix(matrix) class Game5(Page): form_model = "player" form_fields = ["pref5"] @staticmethod def js_vars(player: Player): return dict(my_id=player.id_in_group) @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 player_1.pref5: if player_2.pref5: player_1.profit5 = 10 + y player_2.profit5 = 10 else: player_1.profit5 = 7 + y player_2.profit5 = 11 else: if player_2.pref5: player_1.profit5 = 11 + y player_2.profit5 = 7 else: player_1.profit5 = 8 + y player_2.profit5 = 8 class ShuffleWaitPage5(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): matrix = [[2, 6], [8, 10], [5, 1], [7, 3], [9, 4]] subsession.set_group_matrix(matrix) class Game6(Page): form_model = "player" form_fields = ["pref6"] @staticmethod def js_vars(player: Player): return dict(my_id=player.id_in_group) @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] if player_1.pref6: if player_2.pref6: player_1.profit6 = 9 player_2.profit6 = 9 else: player_1.profit6 = 10 player_2.profit6 = 6 else: if player_2.pref6: player_1.profit6 = 6 player_2.profit6 = 10 else: player_1.profit6 = 7 player_2.profit6 = 7 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.gn2 = x + 6 if x == 1: participant.pt2 = player.profit1 participant.oe2 = 1 participant.dec2 = player.pref1 elif x == 2: participant.pt2 = player.profit2 participant.oe2 = 2 participant.dec2 = player.pref2 elif x == 3: participant.pt2 = player.profit3 participant.oe2 = 1 participant.dec2 = player.pref3 elif x == 4: participant.pt2 = player.profit4 participant.oe2 = 2 participant.dec2 = player.pref4 elif x == 5: participant.pt2 = player.profit5 participant.oe2 = 1 participant.dec2 = player.pref5 elif x == 6: participant.pt2 = player.profit6 participant.oe2 = 2 participant.dec2 = player.pref6 elif x == 7: participant.pt2 = player.profit6 participant.oe2 = 2 participant.dec2 = player.pref6 page_sequence = [Inst1, Comp1, Comp2, ShuffleWaitPage0, Game1, WaitPage1, ShuffleWaitPage1, Game2, WaitPage2, ShuffleWaitPage2, Game3, WaitPage3, ShuffleWaitPage3, Game4, WaitPage4, ShuffleWaitPage4, Game5, WaitPage5, ShuffleWaitPage5, Game6, WaitPage6, Results] # page_sequence = [ShuffleWaitPage0, Game3] # page_sequence = [ShuffleWaitPage2, Game3, WaitPage3, ShuffleWaitPage3, Game4] # page_sequence = [ Game1, WaitPage1, # Game2, WaitPage2, # Game3, WaitPage3, # Game4, WaitPage4, # Game5, WaitPage5, # Game6, WaitPage6, Results]