# -*- coding: utf-8 -*- from __future__ import division from otree.common import Currency as c, currency_range, safe_json from . import models from ._builtin import Page, WaitPage from .models import Constants import random,datetime def isFloat(string): try: float(string) return True except ValueError: return False class MyPage(Page): pass class ResultsWaitPage(WaitPage): def after_all_players_arrive(self): pass class ResultsSummary(Page): def is_displayed(self): return self.subsession.round_number == Constants.num_rounds class Results(Page): pass class Introduction(Page): def vars_for_template(self): return { 'total_time_secs': Constants.timeout * Constants.num_rounds, 'total_time': datetime.timedelta(seconds=Constants.timeout * Constants.num_rounds), 'after_intro': False } def is_displayed(self): return self.subsession.round_number == 1 class Guess(Page): form_model = models.Player form_fields = ['guess_value'] timeout_seconds = 20 def vars_for_template(self): features = self.group.get_game_features() return { 'player_in_previous_rounds': self.player.in_previous_rounds(), 'player_label': features["labels"][self.player.id_in_group - 1], 'features': features, 'total_time_secs': Constants.timeout * Constants.num_rounds, 'total_time': datetime.timedelta(seconds=Constants.timeout * Constants.num_rounds), 'after_intro': True } def before_next_page(self): if self.timeout_happened: post_dict = self.request.POST my_value = post_dict.get('guess_value') if isFloat(my_value) and (float(my_value) >= Constants.min_guess) and (float(my_value) <= Constants.max_guess): self.player.guess_value = my_value self.player.chosen_by_otree = False else: self.player.guess_value = random.uniform(Constants.min_guess , Constants.max_guess) self.player.chosen_by_otree = True page_sequence = [Introduction,Guess,ResultsSummary]