from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random import json import time author = 'Vinssan' doc = """ Real effort task => decoding """ class Constants(BaseConstants): name_in_url = 'MAIF_EFFORT' players_per_group = None num_rounds = 2 time_experiment = 1000 CHARSET = tuple("ABCDEFGHIJKLMNOPQRSTUVWXYZ") DIGITS = [i for i in range(1, 27)] WORD_LENGTH = 1 class Subsession(BaseSubsession): def creating_session(self): for p in self.get_players(): p.erreur = 0 if self.round_number == 1: p.participant.vars['reussi'] = 0 p.participant.vars['history_juste'] = [] p.participant.vars['time_reponse'] = [] p.participant.vars['round'] = [] p.ENDOWMENT = p.participant.vars['ENDOWMENT']#random.randint(Constants.min_income, Constants.max_income) class Group(BaseGroup): pass class Player(BasePlayer): ENDOWMENT = models.IntegerField() chars = models.LongStringField() digits = models.LongStringField() coded_word = models.LongStringField() solution = models.LongStringField() reponse = models.StringField(label="") juste = models.IntegerField() erreur = models.IntegerField() history_juste = models.LongStringField(initial="debut") time_reaction = models.LongStringField(initial='debut') income = models.IntegerField() def income_participant(self): self.ENDOWMENT = self.participant.vars['ENDOWMENT'] # EST-CE LA BONNE REPONSE ??? def is_bonne_reponse(self): if self.solution.lower() == self.reponse.lower(): self.juste = 1 else: self.juste = 0 self.time_reaction = str(self.participant.vars['expiry2'] - time.time()) # ENREGISTREMENT DES TEMPS DE REPONSES QU'ON ENVOIE AU DERNIER ROUND DE L'APPLICATION def timing_response(self): self.participant.vars['time_reponse'] = self.participant.vars['expiry2'] - time.time() self.in_round(Constants.num_rounds).time_reaction = str(self.in_round(Constants.num_rounds).time_reaction) + 'Round' + ':' + str(self.round_number) + ':' + str(self.participant.vars['time_reponse']) + ',' # INITIALISATION : PREMIER PUZZLE A RESOUDRE # ------------------------------------------------ def puzzle_a_resoudre_initialise(self): self.participant.vars['chars'] = random.sample(Constants.CHARSET, len(Constants.DIGITS)) self.chars = str(self.participant.vars['chars']) self.participant.vars['digits'] = random.sample(Constants.DIGITS, len(Constants.DIGITS)) self.digits = str(self.participant.vars['digits']) lookup = (dict(zip(self.participant.vars['digits'], self.participant.vars['chars']))) self.participant.vars['coded_word'] = random.sample(Constants.DIGITS, Constants.WORD_LENGTH) self.coded_word = str(self.participant.vars['coded_word']) self.participant.vars['solution'] = ''.join([lookup[digit] for digit in self.participant.vars['coded_word']]) self.solution = self.participant.vars['solution'] # PUZZLE A RESOUDRE DU 2eme ESSAI AU DERNIER def puzzle_a_resoudre(self): self.participant.vars['chars'] = random.sample(Constants.CHARSET, len(Constants.DIGITS)) self.in_round(self.round_number + 1).chars = str(self.participant.vars['chars']) self.participant.vars['digits'] = random.sample(Constants.DIGITS, len(Constants.DIGITS)) self.in_round(self.round_number + 1).digits = str(self.participant.vars['digits']) lookup = (dict(zip(self.participant.vars['digits'], self.participant.vars['chars']))) self.participant.vars['coded_word'] = random.sample(Constants.DIGITS, Constants.WORD_LENGTH) self.in_round(self.round_number + 1).coded_word = str(self.participant.vars['coded_word']) self.participant.vars['solution'] = ''.join([lookup[digit] for digit in self.participant.vars['coded_word']]) self.in_round(self.round_number + 1).solution = self.participant.vars['solution']