from otree.api import Currency as c, currency_range from otree import models from ._builtin import Page, WaitPage from .models import Constants, calculate_price, get_existing_contract import numpy as np def update_balances(group): liquidity = group.session.config['liquidity'] for player in group.get_players(): if player.balance == Constants.round_initial_balance: share = max(player.Red_share, player.Green_share) if share > 0: their_shares = np.array([player.Red_share, player.Green_share]) old_existing_shares = get_existing_contract(group) - their_shares player.balance -= calculate_price(old_existing_shares, their_shares, liquidity) class Instruction_0welcome(Page): def is_displayed(self): return self.round_number==1 class Instruction_1players(Page): def is_displayed(self): return self.round_number==1 class Instruction_2payment(Page): def is_displayed(self): return self.round_number==1 class Instruction_3interface(Page): def is_displayed(self): return self.round_number==1 class Instruction_4characters(Page): def is_displayed(self): return self.round_number==1 class Instruction_5balance(Page): def is_displayed(self): return self.round_number==1 class Instruction_6prices(Page): def is_displayed(self): return self.round_number==1 class Instruction_7questionsHints(Page): def is_displayed(self): return self.round_number==1 class Instruction_8orderAssignment(Page): def is_displayed(self): return self.round_number==1 # round introduction, display only once at the beginning of each round # reminds random group shuffling each round # (later when both ordered and uGreenrdered are implemented, explains ordered or uGreenrdered rules) class Introduction_Round(Page): pass class Question_Player1 (Page): form_model = 'player' form_fields = ['Red_share','Green_share'] def is_displayed(self): # update_balances(self.player.group) return self.player.id_in_group == 1 def before_next_page(self): #print('before next page') update_balances(self.player.group) class WaitForP1 (WaitPage): template_name = 'TreatmentA/MyWaitPage.html' form_fields = ['balance'] def is_displayed(self): update_balances(self.player.group) return self.player.id_in_group != 1 def before_next_page(self): update_balances(self.player.group) class Question_Player2 (Page): form_model = 'player' form_fields = ['Red_share','Green_share'] def is_displayed(self): update_balances(self.player.group) return self.player.id_in_group == 2 def before_next_page(self): update_balances(self.player.group) class WaitForP2 (WaitPage): template_name = 'TreatmentA/MyWaitPage.html' form_model = 'player' form_fields = ['balance'] def is_displayed(self): update_balances(self.player.group) return self.player.id_in_group != 2 def before_next_page(self): update_balances(self.player.group) class Question_Player3 (Page): form_model = 'player' form_fields = ['Red_share','Green_share'] def is_displayed(self): update_balances(self.player.group) return self.player.id_in_group == 3 def before_next_page(self): update_balances(self.player.group) class WaitForP3 (WaitPage): template_name = 'TreatmentA/MyWaitPage.html' form_model = 'player' form_fields = ['balance'] def is_displayed(self): update_balances(self.player.group) return self.player.id_in_group != 3 def before_next_page(self): update_balances(self.player.group) class ResultsWaitPage(WaitPage): template_name = 'TreatmentA/MyWaitPage.html' def is_displayed(self): return True def after_all_players_arrive(self): for p in self.group.get_players(): p.payoff = p.balance + p.Red_share*(self.subsession.true_state=="Red") + p.Green_share*(self.subsession.true_state=='Green') #print(p.balance) #print(p.payoff) wait_for_all_groups = False class Results(Page): pass page_sequence = [Instruction_0welcome, Instruction_1players, Instruction_2payment, Instruction_3interface, Instruction_4characters, Instruction_5balance, Instruction_6prices, Instruction_7questionsHints, Instruction_8orderAssignment, Question_Player1, WaitForP1, Question_Player2, WaitForP2, Question_Player3, WaitForP3, ResultsWaitPage, Results ]