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=self.participant.vars['endow_choice'], per_10sec_ins=self.participant.vars['deduction_choice']*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=self.participant.vars['endow_choice'], per_10sec_ins=self.participant.vars['deduction_choice'], ) 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['deduction_choice'], 'per_10sec_cents': self.participant.vars['deduction_choice']*100, 'endowment': self.participant.vars['endow_choice'], 'min_num_of_moves': 8, 'player_id': self.player.id, 'the_player': self.player.id == 1, 'board': [ [1, 5, 2], [4, None, 8], [7, 6, 3], ] } class Results(Page): form_model = 'player' form_fields = ['final_payoff1', 'bonus'] def vars_for_template(self): if self.player.num_of_moves == 8 and self.player.puzzle_solved == 1: self.player.bonus = 1 self.player.final_payoff1 = self.player.total_payoff + 1 self.participant.vars['final_payoff1_choice'] = self.player.final_payoff1 self.participant.vars['bonus_choice'] = self.player.bonus else: self.player.bonus=0 self.player.final_payoff1 = self.player.total_payoff self.participant.vars['final_payoff1_choice'] = self.player.final_payoff1 self.participant.vars['bonus_choice'] = self.player.bonus if self.round_number == 1: return dict( per_10sec_ins=self.participant.vars['deduction_choice'], final_payoff1=self.participant.vars['final_payoff1_choice'], bonus=self.participant.vars['bonus_choice'], ) def js_vars(self): return { 'final_payoff1': self.participant.vars['final_payoff1_choice'], 'bonus': self.participant.vars['bonus_choice'], } def before_next_page(self): player_choice = self.player.in_all_rounds() self.participant.vars['solved_choice'] = [p.puzzle_solved for p in player_choice] self.participant.vars['endow_choice'] = [p.endowment for p in player_choice] self.participant.vars['time_choice'] = [p.time_spent for p in player_choice] self.participant.vars['deduction_choice'] = [p.deduction_per_second for p in player_choice] self.participant.vars['moves_choice'] = [p.num_of_moves for p in player_choice] self.participant.vars['payoff_choice'] = [p.final_payoff1 for p in player_choice] self.participant.vars['bonus_choice'] = [p.bonus for p in player_choice] page_sequence = [instructions1, Game1, Results, ]