from otree.api import * import os from difflib import SequenceMatcher import json doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'interactiveEval_v2' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 1 INSTRUCTIONS2_TEMPLATE = 'interactiveEval/instructions2.html' FAILED_TEMPLATE = 'interactiveEval/FailedSimilarityCheckRedirect.html' CHAT_TEMPLATE = 'interactiveEval/chat.html' IMAGES = os.listdir('interactiveEval/static/images') # IMAGES.remove('.DS_Store') # only when running locally in a MAC PUNC = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' REFERENCE_ROLE = 'Reference' COMPARER_ROLE = 'Comparer' class Subsession(BaseSubsession): pass class Group(BaseGroup): image_displayed = models.StringField() class Player(BasePlayer): suggestedEdit = models.LongStringField( label="Text edit", max_length=4000, ) includeLinks = models.BooleanField( label="Did you link to sources that you believe others will consider trustworthy?", choices=[[True, 'Yes'], [False, 'No']], ) evidenceEval = models.IntegerField( label="Given current evidence, I believe the tweet is:", choices=[ [1, 'Misinformed, or potentially misleading'], [2, 'Somewhat misleading'], [3, 'Not misleading'], ] ) # FUNCTIONS def other_player(player: Player): return player.get_others_in_group()[0] def clean_text(player: Player): participant = player.participant text = participant.suggestedEdit text = text.lower() text = text.replace(" ", "").replace('\r', '').replace('\n', '') for ele in text: if ele in C.PUNC: text = text.replace(ele, "") return text # PAGES class FindPartnerWaitPage(WaitPage): group_by_arrival_time = True title_text = 'Please, wait while we find a match for you.' class IndividualEval(Page): # timeout_seconds = 1800 form_model = 'player' form_fields = ['suggestedEdit', 'evidenceEval', 'includeLinks'] @staticmethod def vars_for_template(player: Player): # print(player.round_number) group = player.group print("GROUP ID: ", group.id_in_subsession) image_to_display = 'images/' + C.IMAGES[group.id_in_subsession - 2] return dict(image_path=image_to_display, Nickname_chat_one=player.participant.politics + " " + str(player.id_in_group)) @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.suggestedEdit = player.suggestedEdit participant.cleanText = clean_text(player) group = player.group group.image_displayed = 'images/' + C.IMAGES[group.id_in_subsession - 2] # eval survey type, needed for collaboration Eval if player.evidenceEval == 3: participant.evidenceEval = False else: participant.evidenceEval = True class MyPage(Page): # timeout_seconds = 1800 form_model = 'player' form_fields = ['suggestedEdit', 'evidenceEval', 'includeLinks'] @staticmethod def is_displayed(player: Player): if player.round_number == 1: if player.participant.dropped: return False else: return True else: if (not player.participant.dropped) and player.participant.failed_similarity_check: return True else: return False @staticmethod def vars_for_template(player: Player): # print(player.round_number) group = player.group image_to_display = 'images/' + C.IMAGES[group.id_in_subsession - 2] if player.round_number > 1: otherPlayer = other_player(player) previousRound = player.round_number - 1 return dict(reference_text=player.in_round(previousRound).suggestedEdit, comparing_text=otherPlayer.in_round(previousRound).suggestedEdit, textSimilarity=str(round(float(player.in_round(previousRound).textSimilarity), 3) * 100) + '%', nTriesLeft=C.NUM_ROUNDS - player.round_number, image_path=image_to_display, Nickname_chat_one=player.participant.politics + " " + str(player.id_in_group) ) else: return dict(image_path=image_to_display, Nickname_chat_one=player.participant.politics + " " + str(player.id_in_group)) @staticmethod def js_vars(player: Player): otherPlayer = other_player(player) return dict(my_id=player.id_in_group, Nickname_chat_two_ME=player.participant.politics + " " + str(player.id_in_group) + "(Me)", Nickname_chat_two_Other=player.participant.politics + " " + str(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) player.store_message += json.dumps(dict(group=group.id_in_subsession, sender=my_id, text=text)) + ';' return {0: [to_dict(msg)]} return {my_id: [to_dict(msg) for msg in Message.filter(group=group)]} @staticmethod def before_next_page(player, timeout_happened): participant = player.participant participant.suggestedEdit = player.suggestedEdit participant.cleanText = clean_text(player) group = player.group group.image_displayed = 'images/' + C.IMAGES[group.id_in_subsession - 2] # eval survey type if player.evidenceEval == 3: participant.evidenceEval = False else: participant.evidenceEval = True class ResultsWaitPage(WaitPage): pass page_sequence = [FindPartnerWaitPage, IndividualEval, MyPage] # page_sequence = [MyPage, ResultsWaitPage, Results]