from otree.api import Currency as c, currency_range from otree.api import * from ._builtin import Page, WaitPage from .models import * from settings import SESSION_CONFIG_DEFAULTS import random class Contest1(Page): def is_displayed(self): players = [p for p in self.group.get_players()] return True def vars_for_template(self): return { 'award1': 0, 'award2': 30000, } form_model = 'player' form_fields = ['effort'] class ResultsWaitPage1(WaitPage): def after_all_players_arrive(self): for i in [1, 2]: players = [p for p in self.group.get_players() if p.subgroup == i] efforts = [int(p.effort) for p in players] ids = [p.id_in_group for p in players] total_effort1 = sum(efforts) if total_effort1==0: weight=[1/3, 1/3, 1/3] print(weight) winner = random.choices(ids, weights=weight)[0] else: weight = [j / total_effort1 for j in efforts] print(weight) winner = random.choices(ids, weights=weight)[0] for i in range(len(weight)): players[i].weight1 = round(weight[i],2) players[i].total_effort1 = total_effort1 for p in players: if p.id_in_group == winner: p.is_winner1 = "Yes" p.expenditure_stage1 = p.marginal_cost * int(p.effort) ** 2 p.round_payoff = Constants.endowment - p.marginal_cost * int(p.effort) ** 2 p.expenditure_stage1_string = "In this round, your expenditure for Stage 1 = your marginal cost(" + str( p.marginal_cost) + ")* your Stage 1 effort^2(" + str(int(p.effort) ** 2) + ")= " + str( p.expenditure_stage1) p.income_info_string = " " else: p.is_winner1 = "No" p.expenditure_stage1 = p.marginal_cost * int(p.effort) ** 2 p.round_payoff = Constants.endowment - p.marginal_cost * int(p.effort) ** 2 p.expenditure_stage1_string = "In this round, your expenditure for Stage 1 = your marginal cost(" + str( p.marginal_cost) + ")* your Stage 1 effort^2(" + str(int(p.effort) ** 2) + ")= " + str( p.expenditure_stage1) p.income_info_string = "Your payoff for this round = 60000 - your expenditure for Stage 1("+str(p.marginal_cost * int(p.effort) ** 2)+")= "+ str(p.round_payoff) p.Monetary_payoff= round(float(p.round_payoff) * Constants.rate) players = [p for p in self.group.get_players() if p.is_winner1 == "Yes"] players[0].competitor_cost = players[1].marginal_cost; players[1].competitor_cost = players[0].marginal_cost; class Results1(Page): def is_displayed(self): return True class Contest2(Page): def is_displayed(self): return self.player.is_winner1 == "Yes" def vars_for_template(self): return { 'award2': 30000, } form_model = 'player' form_fields = ['effort2'] class ResultsWaitPage2(WaitPage): def after_all_players_arrive(self): players = [p for p in self.group.get_players() if p.is_winner1 == "Yes"] efforts = [int(p.effort2) for p in players] ids = [p.id_in_group for p in players] total_effort2 = sum(efforts) if total_effort2 == 0: weight = [1 / 2, 1 / 2] print(weight) winner = random.choices(ids, weights=weight)[0] else: weight = [i / total_effort2 for i in efforts] print(weight) winner = random.choices(ids, weights=weight)[0] for i in range(len(weight)): players[i].weight2 = round(weight[i],2) players[i].total_effort2 = total_effort2 for p in players: if p.id_in_group == winner: p.is_winner2 = "Yes" p.expenditure_stage2 = p.marginal_cost * int(p.effort2) ** 2 p.round_payoff = p.round_payoff - p.marginal_cost * p.effort2 ** 2 + Constants.award2 p.expenditure_stage1_string = "In this round, your expenditure for Stage 1 = your marginal cost(" + str( p.marginal_cost) + ")* your Stage 1 effort^2(" + str(int(p.effort) ** 2) + ")= " + str( p.expenditure_stage1) p.expenditure_stage2_string = "In this round, your expenditure for Stage 2 = your marginal cost(" + str( p.marginal_cost) + ")* your Stage 2 effort^2(" + str(int(p.effort2) ** 2) + ")= " + str( p.expenditure_stage2) p.income_info_string2 = "60000 - your expenditure for Stage 1(" + str( p.expenditure_stage1) + ")- your expenditure for Stage 2 (" + str( p.expenditure_stage2) + ") + award for the winner(30000)= " + str(p.round_payoff) p.Monetary_payoff= round(float(p.round_payoff) * Constants.rate) print(p.round_payoff) print(p.Monetary_payoff) else: p.is_winner2 = "No" print(p.round_payoff) print(p.marginal_cost * p.effort2 ** 2) p.expenditure_stage2 = p.marginal_cost * int(p.effort2) ** 2 p.round_payoff = p.round_payoff - p.marginal_cost * p.effort2 ** 2 p.expenditure_stage1_string = "In this round, your expenditure for Stage 1 = your marginal cost(" + str( p.marginal_cost) + ")* your Stage 1 effort^2(" + str(int(p.effort) ** 2) + ")= " + str( p.expenditure_stage1) p.expenditure_stage2_string = "In this round, your expenditure for Stage 2 = your marginal cost(" + str( p.marginal_cost) + ")* your Stage 2 effort^2(" + str(int(p.effort2) ** 2) + ")= " + str( p.expenditure_stage2) p.income_info_string2 = "60000 - your expenditure for Stage 1(" + str( p.expenditure_stage1) + ")- your expenditure for Stage 2 (" + str( p.expenditure_stage2) + ") = " + str(p.round_payoff) p.Monetary_payoff= round(float(p.round_payoff) * Constants.rate) print(p.round_payoff) print(p.Monetary_payoff) class Results2(Page): def is_displayed(self): return self.player.is_winner1 == "Yes" return True class beforeEnd(WaitPage): form_model = 'player' def after_all_players_arrive(self): import random players = [p for p in self.group.get_players() if p.round_number == Constants.NUM_ROUNDS] random_round =random.randint(1, Constants.NUM_ROUNDS) player_in_selected_round = [p.in_round(random_round) for p in players] print("players length:" ,len(player_in_selected_round)) for p in player_in_selected_round: p.in_round(Constants.NUM_ROUNDS).random_round= random_round p.in_round(Constants.NUM_ROUNDS).selected_round = random_round p.in_round(Constants.NUM_ROUNDS).payoff = p.Monetary_payoff p.in_round(Constants.NUM_ROUNDS).selected_round_payoff = p.round_payoff p.in_round(Constants.NUM_ROUNDS).total_payoff = p.Monetary_payoff +SESSION_CONFIG_DEFAULTS['participation_fee'] class End(Page): def is_displayed(self): return self.player.round_number == Constants.NUM_ROUNDS page_sequence = [ Contest1, ResultsWaitPage1, Results1, Contest2, ResultsWaitPage2, Results2, beforeEnd, End, ]