import math as M import random as R from otree.api import * author = 'Corey Scheinfeld' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'Voter' players_per_group = 2 num_rounds = 5 instructions_template = 'voter/instructions.html' platforms = [0.1, 0.25, 0.35, 0.4, 0.5, 0.75, 0.85] class Subsession(BaseSubsession): pass class Group(BaseGroup): Tie = models.BooleanField(initial=False) class Player(BasePlayer): voters = models.IntegerField() winner = models.BooleanField(initial=False) votes = models.IntegerField(initial=0) opponent_platform = models.FloatField() platform = models.FloatField(label="Your Platform: ", min=0, max=1) # FUNCTIONS def creating_session(subsession: Subsession): subsession.group_randomly() def set_payoffs(group: Group): players = group.get_players() p1 = players[0] p2 = players[1] p1.opponent_platform = p2.platform p2.opponent_platform = p1.platform p1.participant.votes = [] p2.participant.votes = [] if p1.platform == p2.platform: group.Tie = True for x in range(0, 7): plat1 = 10 - (10 * abs(p1.platform - Constants.platforms[x])) plat2 = 10 - (10 * abs(p2.platform - Constants.platforms[x])) if plat1 > plat2: p1.participant.votes.append('W') p2.participant.votes.append('L') p1.votes += 1 elif plat2 > plat1: p1.participant.votes.append('L') p2.participant.votes.append('W') p2.votes += 1 elif plat1 == plat2: choice = R.randint(0, 1) if choice == 0: p1.participant.votes.append('TW') p2.participant.votes.append('TL') p1.votes += 1 if choice == 1: p1.participant.votes.append('TL') p2.participant.votes.append('TW') p2.votes += 1 if p1.votes > p2.votes: p1.winner = True p1.payoff = 100 p2.payoff = -100 if p1.votes < p2.votes: p2.winner = True p2.payoff = 100 p1.payoff = -100 # PAGES class Introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Main(Page): form_model = 'player' form_fields = ['platform'] class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): @staticmethod def vars_for_template(player: Player): participant = player.participant a = 7 - player.votes votes_and_platforms = [ dict(ideal_point=Constants.platforms[i], vote=participant.votes[i], voter_number=i+1) for i in range(7) ] return dict(a=a, votes_and_platforms=votes_and_platforms) page_sequence = [Introduction, Main, ResultsWaitPage, Results]