from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import time import random def generate_question(n, k): tmp = [] for j in range(k): tmp.append([]) for i in range(n): expression = "" max1 = 0 max2 = -1 for m in range(1, 21): tmpint = random.randint(0, 99) expression += str(tmpint) if m % 5 != 0: expression += "\t" else: expression += "\n" if tmpint > max1: max2 = max1 max1 = tmpint elif tmpint > max2: max2 = tmpint tmp[j].append([expression, max1 + max2, 5]) return tmp class StartPage(Page): def is_displayed(self): return self.round_number == 1 def before_next_page(self): tmp_type = random.choice([1, 1011]) self.participant.vars["type1"] = int(tmp_type / 100) self.participant.vars["type2"] = tmp_type % 100 self.participant.vars["score1"] = 0 self.participant.vars["result1"] = -1 self.participant.vars["score2"] = 0 self.participant.vars["result2"] = -1 q = generate_question(Constants.num_questions, Constants.test_repeat) q2 = generate_question(Constants.num_questions, Constants.num_rounds) order_list = list(range(Constants.num_questions)) random.shuffle(order_list) self.participant.vars["order"] = order_list self.participant.vars["test_order"] = order_list #p.participant.vars["q_t"] = q[0][random.choice(num_questions)] self.participant.vars["q"] = q.copy() self.participant.vars["q2"] = q2.copy() #order_list = [] #for i in range(Constants.num_questions): # order_list.append(i) #random.shuffle(order_list) #self.participant.vars["order"] = order_list #self.participant.vars["test_order"] = order_list class WaitAll_1(WaitPage): wait_for_all_groups = True def is_displayed(self): return self.round_number == 1 class Intro_1(Page): def is_displayed(self): tmp_list = [] for i in range(Constants.test_repeat + 2): tmp_list.append(1 + i * Constants.num_questions) return self.round_number in tmp_list def vars_for_template(self): return{ "round": int(self.round_number / Constants.num_questions) + 1 } class OrderPage_1(Page): form_model = "player" form_fields = ["choice_1", "choice_2", "choice_3"] def is_displayed(self): tmp_list = [] for i in range(Constants.test_repeat + 2): tmp_list.append(1 + i * Constants.num_questions) return int(self.participant.vars["type1"] / 10) == 1 and self.round_number in tmp_list def vars_for_template(self): return { "count_time": Constants.section_time, "round": int(self.round_number / Constants.num_questions) + 1, "num": Constants.num_questions, "sample": "64\t50\t45\t87\t58\n66\t93\t34\t99\t40\n10\t39\t36\t13\t56\n70\t38\t84\t61\t35\n" } def before_next_page(self): self.participant.vars["c1"] = self.player.choice_1 self.participant.vars["c2"] = self.player.choice_2 self.participant.vars["c3"] = self.player.choice_3 if self.round_number <= Constants.num_questions * Constants.test_repeat: self.participant.vars["test_score"] = 0 self.participant.vars["test_result"] = -1 self.participant.vars["expiry"] = time.time() + Constants.section_time class ParaPage_1(Page): def is_displayed(self): tmp_list = [] for i in range(Constants.test_repeat + 2): tmp_list.append(1 + i * Constants.num_questions) return int(self.participant.vars["type1"] / 10) == 0 and self.round_number in tmp_list def vars_for_template(self): return { "count_time": Constants.section_time, "round": int(self.round_number / Constants.num_questions) + 1, "num": Constants.num_questions, "sample": "64\t50\t45\t87\t58\n66\t93\t34\t99\t40\n10\t39\t36\t13\t56\n70\t38\t84\t61\t35\n" } def before_next_page(self): if self.round_number <= Constants.num_questions * Constants.test_repeat: self.participant.vars["test_score"] = 0 self.participant.vars["test_result"] = -1 self.participant.vars["expiry"] = time.time() + Constants.section_time class AnswerPage(Page): form_model = "player" form_fields = ["answer"] timeout_submission = {"answer": None} def get_timeout_seconds(self): return self.participant.vars["expiry"] - time.time() def is_displayed(self): return self.participant.vars["expiry"] - time.time() > 0 def vars_for_template(self): if self.round_number <= Constants.num_questions * Constants.test_repeat: round_modify = int((self.round_number - 1) / Constants.num_questions) return { "finished_num": self.round_number - Constants.num_questions * round_modify, "total_num": Constants.num_questions, "question": self.participant.vars["q"][round_modify][ self.participant.vars["test_order"][ self.round_number - Constants.num_questions * round_modify - 1]][0] } else: if self.round_number <= Constants.num_questions * (Constants.test_repeat + 1): player_type = "type1" round_modify = Constants.num_questions * Constants.test_repeat else: player_type = "type2" round_modify = Constants.num_questions * (Constants.test_repeat + 1) return { "finished_num": self.round_number - round_modify, "total_num": Constants.num_questions, "question": self.participant.vars["q2"][int(self.participant.vars[player_type] % 10)][ self.participant.vars["order"][self.round_number - round_modify - 1]][0] } def before_next_page(self): if self.round_number <= Constants.num_questions * Constants.test_repeat: round_modify = int((self.round_number - 1) / Constants.num_questions) if self.player.answer == self.participant.vars["q"][round_modify][ self.participant.vars["test_order"][ self.round_number - Constants.num_questions * round_modify - 1]][1]: self.participant.vars["test_score"] += self.participant.vars["q"][round_modify][ self.participant.vars["test_order"][ self.round_number - Constants.num_questions * round_modify - 1]][2] self.player.answer_correct = 5 else: if self.round_number <= Constants.num_questions * (Constants.test_repeat + 1): player_type, score = "type1", "score1" round_modify = Constants.num_questions * Constants.test_repeat else: player_type, score = "type2", "score2" round_modify = Constants.num_questions * (Constants.test_repeat + 1) if self.player.answer == self.participant.vars["q2"][int(self.participant.vars[player_type] % 10)][ self.participant.vars["order"][self.round_number - round_modify - 1]][1]: self.participant.vars[score] += self.participant.vars["q2"][int(self.participant.vars[player_type] % 10)][ self.participant.vars["order"][self.round_number - round_modify - 1]][2] self.player.answer_correct = 5 class WaitAll_2(WaitPage): wait_for_all_groups = True def is_displayed(self): tmp_list = [] for i in range(Constants.test_repeat + 2): tmp_list.append((i + 1) * Constants.num_questions) return self.round_number in tmp_list class Results(Page): def is_displayed(self): tmp_list = [] for i in range(Constants.test_repeat + 2): tmp_list.append((i + 1) * Constants.num_questions) return self.round_number in tmp_list def vars_for_template(self): if self.round_number <= Constants.num_questions * Constants.test_repeat: player_type, score, result = "type1", "test_score", "test_result" else: if self.round_number <= Constants.num_questions * (Constants.test_repeat + 1): player_type, score, result = "type1", "score1", "result1" else: player_type, score, result = "type2", "score2", "result2" group_list = [] for p in self.group.get_players(): if p.participant.vars[player_type] == self.participant.vars[player_type]: group_list.append(p) total = len(group_list) rank = 0 for p in group_list: if p.participant.vars[score] > self.participant.vars[score]: rank += 1 percentile = rank / total if int(self.participant.vars[player_type] / 10): maxnum_list = [[], []] for i in range(3): maxnum_list[0].append(int((i + 1) * total / 3) - int(i * total / 3)) maxnum_list[1].append(0) for i in range(total - 1): for j in range(i + 1, total): if group_list[i].participant.vars[score] <= group_list[j].participant.vars[score]: group_list[i], group_list[j] = group_list[j], group_list[i] for order in ["c1", "c2", "c3"]: for j in range(total): if group_list[j].participant.vars[result] == -1: if maxnum_list[1][group_list[j].participant.vars[order]] < maxnum_list[0][ group_list[j].participant.vars[order]]: group_list[j].participant.vars[result] = group_list[j].participant.vars[order] maxnum_list[1][group_list[j].participant.vars[order]] += 1 elif order == "c3": group_list[j].participant.vars[result] = 3 else: self.participant.vars[result] = int(percentile * 3) resultdict = { 0:"A", 1:"B", 2:"C", 3:"D" } self.player.section_result = 50 - 10 * self.participant.vars[result] return { "score": self.participant.vars[score], "percentile": "%d" % (percentile * 100), "result": resultdict[self.participant.vars[result]] } class CheckPage(Page): form_model = "player" form_fields = ["choice_familiarity", "choice_difficulty"] def is_displayed(self): return self.round_number == Constants.num_questions * Constants.test_repeat class SurveyPage(Page): form_model = "player" form_fields = ["gender", "knowledge", "experience", "preference"] def is_displayed(self): return self.round_number == Constants.num_rounds class PaymentPage(Page): def is_displayed(self): return self.round_number == Constants.num_rounds def vars_for_template(self): choice = random.randint(1, 2) self.player.payoff = 50 - 10 * self.participant.vars["result" + str(choice)] return { "payment": self.player.payoff, "random_choice": choice } page_sequence = [ StartPage, WaitAll_1, Intro_1, OrderPage_1, ParaPage_1, AnswerPage, WaitAll_2, Results, CheckPage, SurveyPage, PaymentPage ]