from otree.api import * import os from time import sleep doc = """ a.k.a. Keynesian beauty contest. Players all guess a number; whoever guesses closest to 2/3 of the average wins. See https://en.wikipedia.org/wiki/Guess_2/3_of_the_average """ class C(BaseConstants): NUM_ROUNDS = 1000 PLAYERS_PER_GROUP = None NAME_IN_URL = 'guess_two_thirds' JACKPOT = Currency(100) GUESS_MAX = 100 INSTRUCTIONS_TEMPLATE = 'guess_two_thirds/instructions.html' rule = 'Only move a rock if it has a majority of rocks of the other color in its neigborhood' MAT_INIT = [] with open('guess_two_thirds/grid_init.txt', encoding='utf-8') as file: for line in file: str_list = line.split() int_list = list(map(int, str_list)) MAT_INIT.append(int_list) MAT_OBJ = [] with open('guess_two_thirds/grid_obj.txt', encoding='utf-8') as file: for line in file: str_list = line.split() int_list = list(map(int, str_list)) MAT_OBJ.append(int_list) NEW_OBJ = [] class Subsession(BaseSubsession): def creating_session(self): C.NUM_ROUNDS = self.session.config['num_demo_participants'] class Group(BaseGroup): rule_tr = models.StringField( label="Trasmit the rule to the next participant", ) class Player(BasePlayer): pass # PAGES class Introduction(Page): def is_displayed(player: Player): current_round = player.group.round_number name = '/Users/jeremyperez/Downloads/OBJ' + str(current_round) + '.txt' if os.path.exists(name): print('-------REMOVED------') os.remove(name) # # for r in range(current_round,C.NUM_ROUNDS+1): # name = '/Users/jeremyperez/Downloads/OBJ' + str(r) + '.txt' # if os.path.exists(name): # print('-------REMOVED------') # os.remove(name) # return player.round_number == player.id_in_group # class task1(Page): # # # # @staticmethod # def vars_for_template(player: Player): # # player.participant.NEW_OBJ = [] # with open('guess_two_thirds/grid_obj.txt', encoding='utf-8') as file: # for line in file: # str_list = line.split() # int_list = list(map(int, str_list)) # player.participant.NEW_OBJ.append(int_list) # # group = player.group # # return dict(rule=C.rule, new_obj=player.participant.NEW_OBJ) # # def before_next_page(player, timeout_happened): # # # player.NEW_OBJ = player.MAT_OBJ.copy() # player.group.rule_tr = '' # # class transmission_phase1(Page): # form_model = 'group' # form_fields = ['rule_tr'] class task1(Page): timeout_seconds = 300 @staticmethod def vars_for_template(player: Player): # player.participant.NEW_OBJ = [] # try: # name = 'OBJ' + str(player.group.round_number - 1) + '.txt' # # with open('/Users/jeremyperez/Downloads/' + name, encoding='utf-8') as file: # for line in file: # str_list = line.split() # int_list = list(map(int, str_list)) # player.participant.NEW_OBJ.append(int_list) # except: if player.group.round_number == 1: player.participant.NEW_OBJ = [] print('EXCPET') with open('guess_two_thirds/grid_obj.txt', encoding='utf-8') as file: for line in file: str_list = line.split() int_list = list(map(int, str_list)) player.participant.NEW_OBJ.append(int_list) return dict(rule=C.rule, new_obj=player.participant.NEW_OBJ, treatment = player.session.config['treatment']) prev_player = player.group.get_player_by_id(player.id_in_group - 1) if player.session.config['treatment'] != 1: return dict(rule=prev_player.participant.RULE, new_obj=prev_player.participant.NEW_OBJ, treatment = player.session.config['treatment']) return dict(new_obj=prev_player.participant.NEW_OBJ, treatment = player.session.config['treatment']) def before_next_page(player, timeout_happened): # player.NEW_OBJ = player.MAT_OBJ.copy() sleep(0.1) if player.session.config['treatment'] != 2: player.participant.NEW_OBJ = [] name = 'OBJ' + str(player.group.round_number) + '.txt' with open('/Users/jeremyperez/Downloads/' + name, encoding='utf-8') as file: for txt in file: lines = txt.split('],[') for line in lines: if line[0] == '[': line = line[2:] elif line[-1] == ']': line = line[:-2] str_list = line.split(',') int_list = list(map(int, str_list)) player.participant.NEW_OBJ.append(int_list) print('new_obj') print(player.participant.NEW_OBJ) @staticmethod def is_displayed(player: Player): return player.round_number == player.id_in_group class transmission_phase1(Page): form_model = 'group' form_fields = ['rule_tr'] @staticmethod def is_displayed(player: Player): print('rule:') print(player.group.field_maybe_none('rule_tr')) if player.group.field_maybe_none('rule_tr') != None: player.participant.RULE = player.group.field_maybe_none('rule_tr') return player.round_number == player.id_in_group and player.session.config['treatment'] != 1 class WaitPage1(WaitPage): pass # @staticmethod # def is_displayed(player: Player): # return player.round_number == player.id_in_group class task2(Page): def vars_for_template(player: Player): # others = player.get_others_in_group() # for p in others: # if p.id == player.id - 1: # player.participant.NEW_OBJ = p.participant.NEW_OBJ return dict(rule=player.group.rule_tr, new_obj=player.participant.NEW_OBJ) def before_next_page(player, timeout_happened): # player.NEW_OBJ = player.MAT_OBJ.copy() player.group.rule_tr = '' # @staticmethod # def is_displayed(player: Player): # return player.id_in_group == 2 class transmission_phase2(Page): form_model = 'group' form_fields = ['rule_tr'] # @staticmethod # class ResultsWaitPage(WaitPage): # after_all_players_arrive = set_payoffs page_sequence = [Introduction, task1, transmission_phase1, WaitPage1 ] # , Results]