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 def per_sec(): choice1 = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] random.shuffle(choice1) return choice1 def per_move(): choice2 = [0.1, 0.2, 0.5, 0.75, 1] random.shuffle(choice2) return choice2 class instructions1(Page): form_model = 'player' 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_all_seconds', 'deduction_per_move', 'deduction_all_moves', 'time_between_moves', 'endowment', 'deduction_per_move_cents', 'deduction_per_second_cents', 'xyz'] timeout_seconds = 1500 timer_text = 'Time Left to Next Deduction:' def before_next_page(self): if 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.time_spent // 10 * self.player.deduction_per_second) - (self.player.deduction_per_move * self.player.num_of_moves)), 2) self.player.deduction_all_moves = self.player.deduction_per_move * self.player.num_of_moves self.player.deduction_all_seconds = self.player.time_spent // 10 * self.player.deduction_per_second if self.player.total_payoff < 0: self.player.total_payoff = 0 else: self.player.total_payoff = self.player.total_payoff self.player.deduction_all_seconds = round((self.player.time_spent // 10 * self.player.deduction_per_second), 2) self.player.deduction_all_moves = self.player.deduction_per_move * self.player.num_of_moves if self.timeout_happened: self.player.time_spent = self.timeout_seconds self.player.total_payoff = self.player.endowment - ( self.timeout_seconds / 10 * self.player.deduction_per_second) \ - (self.player.deduction_per_move * self.player.num_of_moves) if self.player.total_payoff < 0: self.player.total_payoff = 0 else: self.player.total_payoff = self.player.total_payoff self.player.deduction_all_seconds = round((self.timeout_seconds / 10 * self.player.deduction_per_second), 2) def vars_for_template(self) -> dict: round1 = self.player.in_round(1).xyz round2 = self.player.in_round(2).xyz round3 = self.player.in_round(3).xyz if self.round_number <= Constants.num_rounds: self.participant.vars['xyz'] = self.player.in_round(self.round_number).xyz if self.round_number < Constants.num_rounds: self.participant.vars['total_solved']=0 else: self.participant.vars['total_solved']=self.player.in_round(1).xyz+self.player.in_round(2).xyz if self.round_number == 1: return dict( min_num_of_moves=6, endowment=(6) * 0.5 + 10, per_move_ins=0.50, # self.participant.vars['move'][self.round_number - 1], per_10sec_ins= 0.10, #self.participant.vars['sec'][self.round_number - 1], xyz = self.participant.vars['xyz'], ) if self.round_number == 2: return dict( min_num_of_moves=6, endowment=(6) * 0.5 + 10, per_move_ins=0.50, # self.participant.vars['move'][self.round_number - 1], per_10sec_ins=0.10, # self.participant.vars['sec'][self.round_number - 1], xyz=self.participant.vars['xyz'], ) if self.round_number == 3: return dict( min_num_of_moves=6, endowment=(6) * 0.5 + 10, per_move_ins=0.50, # self.participant.vars['move'][self.round_number - 1], per_10sec_ins=0.10, # self.participant.vars['sec'][self.round_number - 1], xyz=self.participant.vars['xyz'], ) if self.round_number == 4: return dict( min_num_of_moves=8, endowment=(8) * 0.5 + 10, per_move_ins=0.50, # self.participant.vars['move'][self.round_number - 1], per_10sec_ins=0.10, # self.participant.vars['sec'][self.round_number - 1], xyz = self.participant.vars['xyz'], ) if self.round_number == 5: return dict( min_num_of_moves=8, endowment=(8) * 0.5 + 10, per_move_ins=0.50, # self.participant.vars['move'][self.round_number - 1], per_10sec_ins=0.10, # self.participant.vars['sec'][self.round_number - 1], xyz=self.participant.vars['xyz'], ) if self.round_number == 6: return dict( min_num_of_moves=8, endowment=(8) * 0.5 + 10, per_move_ins=0.50, # self.participant.vars['move'][self.round_number - 1], per_10sec_ins=0.10, # self.participant.vars['sec'][self.round_number - 1], xyz=self.participant.vars['xyz'], ) if self.round_number == 7: return dict( min_num_of_moves=10, endowment=(10) * 0.5 + 10, per_move_ins=0.50, # self.participant.vars['move'][self.round_number - 1], per_10sec_ins=0.10, # self.participant.vars['sec'][self.round_number - 1], xyz=self.participant.vars['xyz'], ) if self.round_number == 8: return dict( min_num_of_moves=10, endowment=(10) * 0.5 + 10, per_move_ins=0.50, # self.participant.vars['move'][self.round_number - 1], per_10sec_ins=0.10, # self.participant.vars['sec'][self.round_number - 1], xyz=self.participant.vars['xyz'], ) if self.round_number == 9: return dict( min_num_of_moves=10, endowment=(10) * 0.5 + 10, per_move_ins=0.50, # self.participant.vars['move'][self.round_number - 1], per_10sec_ins=0.10, # self.participant.vars['sec'][self.round_number - 1], xyz=self.participant.vars['xyz'], ) def js_vars(self): if self.round_number == 1: # 6 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_move': 0.50, # self.participant.vars['move'][self.round_number - 1], 'per_move_cents': 50, 'per_10sec': 0.10,#self.participant.vars['sec'][self.round_number - 1], 'per_10sec_cents': 10, 'endowment': (6) * 0.5 + 10, 'min_num_of_moves': 6, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'xyz': self.participant.vars['xyz'], 'board': [ [2, 4, 3], [1, None, 6], [7, 5, 8], ] } if self.round_number == 2: # 6 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_move': 0.50, # self.participant.vars['move'][self.round_number - 1], 'per_move_cents': 50, 'per_10sec': 0.10,#self.participant.vars['sec'][self.round_number - 1], 'per_10sec_cents': 10, 'endowment': (6) * 0.5 + 10, 'min_num_of_moves': 6, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'xyz': self.participant.vars['xyz'], 'board': [ [1, 5, 2], [4, 8, 3], [7, 6, None], ] } if self.round_number == 3: # 6 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_move': 0.50, # self.participant.vars['move'][self.round_number - 1], 'per_move_cents': 50, 'per_10sec': 0.10,#self.participant.vars['sec'][self.round_number - 1], 'per_10sec_cents': 10, 'endowment': (6) * 0.5 + 10, 'min_num_of_moves': 6, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'xyz': self.participant.vars['xyz'], 'board': [ [None, 1, 2], [4, 5, 6], [7, 3, 8], ] } if self.round_number == 4: # 8 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_move': 0.50, # self.participant.vars['move'][self.round_number - 1], 'per_move_cents': 50, 'per_10sec': 0.10,#self.participant.vars['sec'][self.round_number - 1], 'per_10sec_cents': 10, 'endowment': (8) * 0.5 + 10, 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'xyz': self.participant.vars['xyz'], 'board': [ [1, 3, 6], [4, 2, 8], [None, 7, 5], ] } if self.round_number == 5: # 8 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_move': 0.50, # self.participant.vars['move'][self.round_number - 1], 'per_move_cents': 50, 'per_10sec': 0.10,#self.participant.vars['sec'][self.round_number - 1], 'per_10sec_cents': 10, 'endowment': (8) * 0.5 + 10, 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'xyz': self.participant.vars['xyz'], 'board': [ [2, 4, 3], [1, 5, 6], [7, 8, None], ] } if self.round_number == 6: # 8 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_move': 0.50, # self.participant.vars['move'][self.round_number - 1], 'per_move_cents': 50, 'per_10sec': 0.10,#self.participant.vars['sec'][self.round_number - 1], 'per_10sec_cents': 10, 'endowment': (8) * 0.5 + 10, 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'xyz': self.participant.vars['xyz'], 'board': [ [5, 2, None], [1, 4, 3], [7, 8, 6], ] } if self.round_number == 7: # 10 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_move': 0.50, # self.participant.vars['move'][self.round_number - 1], 'per_move_cents': 50, 'per_10sec': 0.10,#self.participant.vars['sec'][self.round_number - 1], 'per_10sec_cents': 10, 'endowment': (10) * 0.5 + 10, 'min_num_of_moves': 10, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'xyz': self.participant.vars['xyz'], 'board': [ [4, 2, None], [7, 1, 3], [8, 5, 6], ] } if self.round_number == 8: # 10 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_move': 0.50, # self.participant.vars['move'][self.round_number - 1], 'per_move_cents': 50, 'per_10sec': 0.10,#self.participant.vars['sec'][self.round_number - 1], 'per_10sec_cents': 10, 'endowment': (10) * 0.5 + 10, 'min_num_of_moves': 10, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'xyz': self.participant.vars['xyz'], 'board': [ [None, 8, 2], [1, 4, 3], [7, 6, 5], ] } if self.round_number == 9: # 10 steps return { 'timeout': self.timeout_seconds, 'game_id': self.round_number, 'per_move': 0.50, # self.participant.vars['move'][self.round_number - 1], 'per_move_cents': 50, 'per_10sec': 0.10,#self.participant.vars['sec'][self.round_number - 1], 'per_10sec_cents': 10, 'endowment': (10) * 0.5 + 10, 'min_num_of_moves': 10, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'xyz': self.participant.vars['xyz'], 'board': [ [1, 3, None], [4, 5, 6], [7, 8, 2], ] } def paying_round(self): choice3 = random.randint(1, Constants.num_rounds) return choice3 class Results(Page): form_model = 'player' form_fields = ['paying_round', 'final_payoff', 'xyz','totalsolved', 'payoffpilot2'] def vars_for_template(self): if self.round_number == 1: self.participant.vars['paying_round'] = paying_round(self) #if self.round_number == Constants.num_rounds: # self.participant.vars['xyz'] = self.player.in_round(1).xyz + self.player.in_round(2).xyz #else: self.participant.vars['round1'] = self.player.in_round(1).xyz self.participant.vars['round2'] = self.player.in_round(2).xyz self.participant.vars['round3'] = self.player.in_round(3).xyz self.participant.vars['round4'] = self.player.in_round(4).xyz self.participant.vars['round5'] = self.player.in_round(5).xyz self.participant.vars['round6'] = self.player.in_round(6).xyz self.participant.vars['round7'] = self.player.in_round(7).xyz self.participant.vars['round8'] = self.player.in_round(8).xyz self.participant.vars['round9'] = self.player.in_round(9).xyz if self.round_number <= Constants.num_rounds: self.participant.vars['xyz'] = self.player.in_round(self.round_number).xyz if self.round_number < Constants.num_rounds: self.participant.vars['totalsolved']=0 self.participant.vars['payoffpilot2'] = 0 else: self.participant.vars['totalsolved']=self.participant.vars['round1']+self.participant.vars['round2']\ +self.participant.vars['round3']+self.participant.vars['round4']\ +self.participant.vars['round5']+self.participant.vars['round6']\ +self.participant.vars['round7']+self.participant.vars['round8']\ +self.participant.vars['round9'] self.participant.vars['payoffpilot2'] = self.participant.vars['totalsolved']*0.5 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'], xyz = self.participant.vars['xyz'], totalsolved=self.participant.vars['totalsolved'], payoffpilot2=self.participant.vars['payoffpilot2'] ) def js_vars(self): return { 'paying_round': self.participant.vars['paying_round'], 'final_payoff': self.participant.vars['final_payoff'], 'xyz': self.participant.vars['xyz'], 'totalsolved': self.participant.vars['totalsolved'], 'payoffpilot2': self.participant.vars['payoffpilot2'] } page_sequence = [ Game1, Results, ]