# -*- coding: utf-8 -*- # from __future__ import division import random from otree.api import * #import otree.models #from otree.db import models #from otree import widgets #from otree.common import Currency as c, currency_range, safe_json from otree.constants import BaseConstants #from otree.models import BaseSubsession, BaseGroup, BasePlayer # author = 'Coralio Ballester and Marc Vorsatz' doc = """ A beauty contest played in networks """ class Constants(BaseConstants): name_in_url = 'beauty' players_per_group = 2 num_rounds = 6 winner_payoff = cu(100) default_guess = 0 min_guess = 0 max_guess = 100 decimal_places = 2 max_digits = 5 timeout = 45 timeout_offset = 5 games_list = [ { "num_players": 2, "num_anchors": 2, "labels":["A", "B"], "anchors":[10,40], "weights": [[0,0.5,0.5,0],[0.5,0,0,0.5]], "verbose": ["Player A should guess the mean of 10 and B", "Player B should guess the mean of 40 and A"] }, { "num_players": 2, "num_anchors": 1, "labels":["A", "B"], "anchors":[90], "weights": [[0, 0.5, 0], [0.5, 0, 0.5]], "verbose": ["Player A should guess one half of B", "Player B should guess the mean of 90 and A"] }, { "num_players": 2, "num_anchors": 1, "labels": ["A", "B"], "anchors": [100], "weights": [[0, 0.5, 0.5], [0.25, 0, 0.25]], "verbose": ["Player A should guess the mean of 100 and B", "Player B should guess half the mean of 100 A"] } ] class Subsession(BaseSubsession): def before_session_starts(self): for g in self.get_groups(): if self.round_number == 1: g.game_played = random.randint(0, len(Constants.games_list) - 1) else: p1 = g.get_player_by_id(1) g.game_played = p1.in_rounds(1,1)[0].group.game_played ''' if self.round_number == 1: for g in self.get_groups(): game_ind = random.randint(0 , len(Constants.games_list) - 1) for p in g.get_players(): p.participant.vars['game_number'] = game_ind for p in self.get_players(): p.game_number = p.participant.vars['game_number'] ''' ''' def before_session_starts(self): for player in self.get_players(): player.guess_value = random.uniform(Constants.min_guess , Constants.max_guess) ''' class Group(BaseGroup): game_played = models.IntegerField() def get_game_features(self): return Constants.games_list[self.game_played] class Player(BasePlayer): is_winner = models.BooleanField( initial=False ) chosen_by_otree = models.BooleanField( initial = False ) guess_value = models.FloatField( initial = None, min = Constants.min_guess, max = Constants.max_guess )