import random import time import logging from math import floor, ceil from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) author = 'Jan Dietrich' re_writer='Yidan Chai' doc = """ This app is to play the double auction game, re-writed by Yidan for EC312 online seminars """ # Get an instance of a logger logger = logging.getLogger(__name__) class Constants(BaseConstants): name_in_url = 'week3_cooperation_or_not' players_per_group = None num_rounds = 4 quiz_radio_button = dict( choices=[[1, 'Yes'], [2, 'No']], widget=widgets.RadioSelectHorizontal ) value_buyer = [20, 15, 10] # cost_seller = [10, 30] weights12 = [0.20, 0.60, 0.20] class Subsession(BaseSubsession): def creating_session(self): # for p in self.get_players(): # p.participant.vars['role'] = random.choice(['buyer', 'seller']) import itertools roles = itertools.cycle(['seller', 'buyer', 'buyer', 'buyer']) for p in self.get_players(): p.participant.vars["role"]=next(roles) p.participant.vars['game'] = 'week3_cooperation_or_not' p.participant.vars["chosen_round"] = random.randint(self.session.config['num_of_test_rounds'] + 1, Constants.num_rounds) # if self.round_number == 1: # if len(self.get_players()) % self.session.config['market_size'] != 0: # raise Exception('number of participant must be mulitple of market_size') def group_by_arrival_time_method(self, waiting_players): num_of_da_players_per_group = self.session.config['market_size'] num_of_active_groups = len(self.get_group_matrix())-1 num_players = len(self.get_players()) market_size = self.session.config['market_size'] number_markets = ceil(num_players / market_size) if num_of_active_groups < number_markets and len(waiting_players) >= num_of_da_players_per_group: logger.info('creating group') da_players = waiting_players[:num_of_da_players_per_group] for i, p in enumerate(da_players): p.participant.vars['game'] = 'week3_cooperation_or_not' p.participant.vars["chosen_round"] = random.randint(self.session.config['num_of_test_rounds'] + 1, Constants.num_rounds) # if (i % 2 == 0): # p.participant.vars["role"]="buyer" # else: # p.participant.vars["role"]="seller" return da_players elif num_of_active_groups >= number_markets: player = waiting_players[0] return waiting_players class Group(BaseGroup): pass class Player(BasePlayer): money = models.IntegerField() cost = models.IntegerField() last_offer = models.IntegerField() trade_price = models.IntegerField() match_with = models.ForeignKey('Player', null=True, on_delete=models.CASCADE) match_with_player_id_in_group = models.IntegerField() instructions_da1 = models.IntegerField( verbose_name="You are a buyer. Your valuation for the good is 50 points. You submit a bid of 40 points and a seller accepts this bid. What are your earnings (in points)?", ) instructions_da3 = models.IntegerField( verbose_name="You are a seller. Your production costs for the good are 20 points. You submit an ask of 25 points and a buyer accepts this ask. What are your earnings (in points)?", ) instructions_da4 = models.IntegerField( verbose_name="You are a buyer. Your valuation for the good is 40 points. Is it possible to submit a bid of 60 points?", **Constants.quiz_radio_button ) display_id = models.IntegerField() is_bot = models.BooleanField()