from otree.api import Currency as c, currency_range from ._builtin import Page from .models import Constants import datetime, json, time import random import math class instructions1(Page): def vars_for_template(self): if self.round_number == 1: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), per_10sec_ins=(self.participant.vars['sec1'][self.round_number - 1])*100, ) if self.round_number == 2: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), per_10sec_ins=(self.participant.vars['sec1'][self.round_number - 1])*100, ) if self.round_number == 3: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), per_10sec_ins=(self.participant.vars['sec1'][self.round_number - 1])*100, ) if self.round_number == 4: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), per_10sec_ins=(self.participant.vars['sec1'][self.round_number - 1])*100, ) if self.round_number == 5: #8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), per_10sec_ins=(self.participant.vars['sec2'][self.round_number - 5])*100, ) if self.round_number == 6: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), per_10sec_ins=(self.participant.vars['sec2'][self.round_number - 5])*100, ) if self.round_number == 7: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), per_10sec_ins=(self.participant.vars['sec2'][self.round_number - 5])*100, ) if self.round_number == 8: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), per_10sec_ins=(self.participant.vars['sec2'][self.round_number - 5])*100, ) def before_next_page(self): self.participant.vars['sssss'] = time.time() class Game1(Page): form_model = 'player' form_fields = ['puzzle_solved', 'total_payoff', 'time_spent', 'num_of_moves', 'min_num_of_moves', 'board_history', 'deduction_per_second', 'deduction_per_second_cents', 'deduction_all_seconds', 'time_between_moves', 'endowment'] timeout_seconds = 300 def before_next_page(self): self.player.deduction_all_seconds = self.player.time_spent // 10 * self.player.deduction_per_second if self.player.num_of_moves <= 14 and self.player.puzzle_solved == 1: self.player.time_spent = math.floor(time.time() - self.participant.vars['sssss']) self.player.total_payoff = round((self.player.endowment - self.player.deduction_all_seconds), 2) if self.player.puzzle_solved == 0: self.player.time_spent = math.floor(time.time() - self.participant.vars['sssss']) self.player.total_payoff = 0 if self.player.num_of_moves > 14 and self.player.puzzle_solved == 1: self.player.time_spent = math.floor(time.time() - self.participant.vars['sssss']) self.player.total_payoff = 0 if self.player.total_payoff < 0: self.player.total_payoff = 0 else: self.player.total_payoff = self.player.total_payoff if self.timeout_happened: self.player.time_spent = self.timeout_seconds self.player.total_payoff = 0 def vars_for_template(self) -> dict: if self.round_number == 1: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), per_10sec_ins=self.participant.vars['sec1'][self.round_number - 1], ) if self.round_number == 2: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), per_10sec_ins=self.participant.vars['sec1'][self.round_number - 1], ) if self.round_number == 3: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), per_10sec_ins=self.participant.vars['sec1'][self.round_number - 1], ) if self.round_number == 4: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), per_10sec_ins=self.participant.vars['sec1'][self.round_number - 1], ) if self.round_number == 5: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), per_10sec_ins=self.participant.vars['sec2'][self.round_number - 5], ) if self.round_number == 6: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), per_10sec_ins=self.participant.vars['sec2'][self.round_number - 5], ) if self.round_number == 7: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), per_10sec_ins=self.participant.vars['sec2'][self.round_number - 5], ) if self.round_number == 8: # 8 return dict( min_num_of_moves=8, endowment=math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), per_10sec_ins=self.participant.vars['sec2'][self.round_number - 5], ) def js_vars(self): if self.round_number == 1: # 8 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_10sec': self.participant.vars['sec1'][self.round_number - 1], 'per_10sec_cents': 100*self.participant.vars['sec1'][self.round_number - 1], 'endowment': math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'board': [ [5, 2, None], [1, 4, 3], [7, 8, 6], ] } if self.round_number == 2: # 8 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_10sec': self.participant.vars['sec1'][self.round_number - 1], 'per_10sec_cents': 100 * self.participant.vars['sec1'][self.round_number - 1], 'endowment': math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'board': [ [1, 3, 6], [4, 2, 8], [None, 7, 5], ] } if self.round_number == 3: # 8 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_10sec': self.participant.vars['sec1'][self.round_number - 1], 'per_10sec_cents': 100 * self.participant.vars['sec1'][self.round_number - 1], 'endowment': math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'board': [ [2, 4, 3], [1, 5, 6], [7, 8, None], ] } if self.round_number == 4: # 8 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_10sec': self.participant.vars['sec1'][self.round_number - 1], 'per_10sec_cents': 100 * self.participant.vars['sec1'][self.round_number - 1], 'endowment': math.ceil((self.participant.vars['sec1'][self.round_number - 1]) * 3 + 4), 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'board': [ [4, 1, 3], [7, None, 6], [5, 2, 8], ] } if self.round_number == 5: # 8 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_10sec': self.participant.vars['sec2'][self.round_number - 5], 'per_10sec_cents': 100 * self.participant.vars['sec2'][self.round_number - 5], 'endowment': math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'board': [ [1, 6, 2], [4, 5, 3], [7, 8, None], ] } if self.round_number == 6: # 8 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_10sec': self.participant.vars['sec2'][self.round_number - 5], 'per_10sec_cents': 100 * self.participant.vars['sec2'][self.round_number - 5], 'endowment': math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'board': [ [None, 5, 2], [1, 8, 3], [4, 7, 6], ] } if self.round_number == 7: # 8 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_10sec': self.participant.vars['sec2'][self.round_number - 5], 'per_10sec_cents': 100 * self.participant.vars['sec2'][self.round_number - 5], 'endowment': math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'board': [ [1, 5, 2], [4, 3, 6], [None, 7, 8], ] } if self.round_number == 8: # 8 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_10sec': self.participant.vars['sec2'][self.round_number - 5], 'per_10sec_cents': 100 * self.participant.vars['sec2'][self.round_number - 5], 'endowment': math.ceil((self.participant.vars['sec2'][self.round_number - 5]) * 3 + 4), 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'board': [ [4, 1, 3], [7, None, 5], [8, 2, 6], ] } def paying_round(self): choice00 = random.randint(1, 9) return choice00 class Results(Page): form_model = 'player' form_fields = ['final_payoff1'] #['paying_round', 'final_payoff'] def vars_for_template(self): if self.player.num_of_moves == 8: self.player.final_payoff1 = self.player.total_payoff + 1 self.participant.vars['final_payoff1'] = self.player.final_payoff1 else: self.player.final_payoff1 = self.player.total_payoff self.participant.vars['final_payoff1'] = self.player.final_payoff1 if self.round_number == 1: # 8 return dict( per_10sec_ins=(self.participant.vars['sec1'][self.round_number - 1]) * 100, ) if self.round_number == 2: # 8 return dict( per_10sec_ins=(self.participant.vars['sec1'][self.round_number - 1]) * 100, ) if self.round_number == 3: # 8 return dict( per_10sec_ins=(self.participant.vars['sec1'][self.round_number - 1]) * 100, ) if self.round_number == 4: # 8 return dict( per_10sec_ins=(self.participant.vars['sec1'][self.round_number - 1]) * 100, ) if self.round_number == 5: # 8 return dict( per_10sec_ins=(self.participant.vars['sec2'][self.round_number - 5]) * 100, ) if self.round_number == 6: # 8 return dict( per_10sec_ins=(self.participant.vars['sec2'][self.round_number - 5]) * 100, ) if self.round_number == 7: # 8 return dict( per_10sec_ins=(self.participant.vars['sec2'][self.round_number - 5]) * 100, ) if self.round_number == 8: return dict( per_10sec_ins=(self.participant.vars['sec2'][self.round_number - 5]) * 100, solved_8round=self.participant.vars["solved_8round"], time_8round=self.participant.vars['time_8round'], deduction_8round=self.participant.vars['deduction_8round'], moves_8round=self.participant.vars['moves_8round'], endow_8round=self.participant.vars['endow_8round'], payoff_8round=self.participant.vars['payoff_8round'] ) def js_vars(self): return { 'final_payoff1': self.participant.vars['final_payoff1'], } def before_next_page(self): player_8round = self.player.in_all_rounds() self.participant.vars["solved_8round"] = [p.puzzle_solved for p in player_8round] self.participant.vars['time_8round'] = [p.time_spent for p in player_8round] self.participant.vars['deduction_8round'] = [p.deduction_per_second_cents for p in player_8round] self.participant.vars['moves_8round'] = [p.num_of_moves for p in player_8round] self.participant.vars['endow_8round'] = [p.endowment for p in player_8round] self.participant.vars['payoff_8round'] = [p.final_payoff1 for p in player_8round] # def vars_for_template(self): # # if self.round_number == 1: # self.participant.vars['paying_round'] = paying_round(self) # # if self.round_number < self.participant.vars['paying_round']: # self.participant.vars['final_payoff'] = 0 # self.participant.vars['final_move'] = 0 # self.participant.vars['final_time'] = 0 # self.participant.vars['final_endow'] = 0 # # else: # self.participant.vars['final_payoff'] = self.player.in_round(self.participant.vars['paying_round']).total_payoff # self.participant.vars['final_move'] = self.player.in_round(self.participant.vars['paying_round']).num_of_moves # self.participant.vars['final_time'] = self.player.in_round(self.participant.vars['paying_round']).time_spent # self.participant.vars['final_endow'] = self.player.in_round(self.participant.vars['paying_round']).endowment # # return dict( # paying_round=self.participant.vars['paying_round'], # final_payoff=self.participant.vars['final_payoff'], # final_move=self.participant.vars['final_move'], # final_time=self.participant.vars['final_time'], # player_in_all_rounds=self.player.in_all_rounds(), # player_in_previous_round=self.player.in_previous_rounds(), # endowment=self.participant.vars['final_endow'], # payoff=self.participant.vars['final_payoff'], # ) # # # # # # # def js_vars(self): # return { # 'paying_round': self.participant.vars['paying_round'], # 'final_payoff': self.participant.vars['final_payoff'] # } page_sequence = [instructions1, Game1, Results, ]