from os import set_inheritable from otree.api import * from otree.models import player from scipy.stats import bernoulli from random import choice,shuffle from scipy.stats import bernoulli c = Currency doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'FN_game_treatment' players_per_group = 6 num_rounds = 5 endowment = 60 instructions_template = 'FN_game_treatment/instructions.html' multiplier = 2 class Subsession(BaseSubsession): other_group_resultfor1=models.StringField() other_group_resultfor2=models.StringField() other_diefor1=models.IntegerField() other_diefor2=models.IntegerField() class Group(BaseGroup): total_vote = models.IntegerField(initial=3) total_vote_str= models.StringField() total_pay = models.CurrencyField() total_right = models.IntegerField() single_pay = models.CurrencyField() death=models.IntegerField(initial=0) a=models.IntegerField() b=models.IntegerField() c=models.IntegerField() d=models.IntegerField() e=models.IntegerField() f=models.IntegerField() g=models.IntegerField() h=models.IntegerField() i=models.IntegerField() #X=models.IntegerField() #Y=models.IntegerField() class Player(BasePlayer): #0=活,1=死 state= models.IntegerField( min=0, max=1, initial=0 ) num_group= models.IntegerField( initial=0) vote = models.IntegerField( min=0, max=1, label="若你是代議士,你想要投X或Y方案?(X請輸入0,Y請輸入1)", choices=[0,1], ) num1 = models.IntegerField( min=0, max=25, label='請輸入你此格記得的數字', choices=[i for i in range(1,26)] ) num2 = models.IntegerField( min=0, max=25, label='請輸入你此格記得的數字', choices=[i for i in range(1,26)] ) num3 = models.IntegerField( min=0, max=25, label='請輸入你此格記得的數字', choices=[i for i in range(1,26)] ) num4 = models.IntegerField( min=0, max=25, label='請輸入你此格記得的數字', choices=[i for i in range(1,26)] ) num5 = models.IntegerField( min=0, max=25, label='請輸入你此格記得的數字', choices=[i for i in range(1,26)] ) num6 = models.IntegerField( min=0, max=25, label='請輸入你此格記得的數字', choices=[i for i in range(1,26)] ) num7 = models.IntegerField( min=0, max=25, label='請輸入你此格記得的數字', choices=[i for i in range(1,26)] ) num8 = models.IntegerField( min=0, max=25, label='請輸入你此格記得的數字', choices=[i for i in range(1,26)] ) num9 = models.IntegerField( min=0, max=25, label='請輸入你此格記得的數字', choices=[i for i in range(1,26)] ) gender = models.IntegerField(label='請輸入你的性別',choices=[[1,'男性'],[2,'女性']]) check = models.BooleanField(label='是否已了解遊戲規則?') final_check = models.BooleanField(label='有保底機制的情況,是否會讓你傾向多抽幾次?') yes = models.StringField(label='請說明緣由?') def result_of_vote(group: Group): global vote_result for p in group.get_players(): if p.id_in_group == 1 : p.num_group=0 #p.num_group_1=0 elif p.id_in_group == 2 : p.num_group=0 #p.num_group_1=1 else: if p.id_in_group == 3 : p.num_group=0 #p.num_group_1=2 else: if p.id_in_group == 4 : p.num_group=1 #p.num_group_1=0 else: if p.id_in_group == 5 : p.num_group=1 #p.num_group_1=1 else: if p.id_in_group == 6: p.num_group=1 #p.num_group_1=2 else: if p.id_in_group == 7: p.num_group=2 #p.num_group_1=0 else: if p.id_in_group == 8: p.num_group=2 #p.num_group_1=1 else: p.num_group=2 #p.num_group_1=2 #print(p.num_group) #print(p.num_group_1) players = [1,2,3,4,5,6] result=[[],[],[]] for item in players: if len(result[0])<3: result[0].append(item) elif len(result[1])<3: result[1].append(item) else: result[2].append(item) print(result) for p in group.get_players(): if p.state == 1: result[p.num_group].remove(p.id_in_group) print(result) votes = [p.vote for p in group.get_players()] total=[] for i in range(0,3): if len(result[i]) >0 : import random vot= random.choice(result[i]) #print(vot-1) total.append(votes[vot-1]) num_0=0 num_1=0 for i in total: if i==1: num_1 += 1 else: num_0 += 1 if num_0 > num_1 : vote_result_str="X" vote_result=0 elif num_0 < num_1 : vote_result_str="Y" vote_result=1 else: if group.total_vote ==3: vote_result= choice([0,1]) if vote_result == 0: vote_result_str="X" else: vote_result_str="Y" else: if group.total_vote == 0: vote_result=0 vote_result_str="X" else: vote_result=1 vote_result_str="Y" print(vote_result_str) group.total_vote = vote_result group.total_vote_str = str(vote_result_str) def form_product(group:Group): for p in group.get_players(): p.num1 = 0 p.num2 = 0 p.num3 = 0 p.num4 = 0 p.num5 = 0 p.num6 = 0 p.num7 = 0 p.num8 = 0 p.num9 = 0 l=[i for i in range(1,10)] shuffle(l) group.a=l[1] group.b=l[2] group.c=l[3] group.d=l[4] group.e=l[5] group.f=l[6] group.g=l[7] group.h=l[8] group.i=l[0] def num_form(group:Group): right = 0 global pay check1 = [p.num1 for p in group.get_players()] check2 = [p.num2 for p in group.get_players()] check3 = [p.num3 for p in group.get_players()] check4 = [p.num4 for p in group.get_players()] check5 = [p.num5 for p in group.get_players()] check6 = [p.num6 for p in group.get_players()] check7 = [p.num7 for p in group.get_players()] check8 = [p.num8 for p in group.get_players()] check9 = [p.num9 for p in group.get_players()] n1=0 n2=0 n3=0 n4=0 n5=0 n6=0 n7=0 n8=0 n9=0 for num in check1: if num == group.a: n1 += 1 if n1 > 0: pay += 20 right += 1 for num in check2: if num == group.b: n2 += 1 if n2 > 0: pay += 20 right += 1 for num in check3: if num == group.c: n3 += 1 if n3 > 0: pay += 20 right += 1 for num in check4: if num == group.d: n4 += 1 if n4 > 0: pay += 20 right += 1 for num in check5: if num == group.e: n5 += 1 if n5 > 0: pay += 20 right += 1 for num in check6: if num == group.f: n6 += 1 if n6 > 0: pay += 20 right += 1 for num in check7: if num == group.g: n7 += 1 if n7 > 0: pay += 20 right += 1 for num in check8: if num == group.h: n8 += 1 if n8 > 0: pay += 20 right += 1 for num in check9: if num == group.i: n9 += 1 if n9 > 0: pay += 20 right += 1 group.single_pay = right*20 group.total_right = right group.total_pay = pay def statement(group:Group): die_group_or_not = bernoulli.rvs(size=1,p=0.04) for p in group.get_players(): if p.state == 1: p.state = 1 else: if group.total_vote == 0: die_or_not = bernoulli.rvs(size=1,p=0.4) if die_or_not[0] == 1: p.state = 1 else: p.state = 0 else: if die_group_or_not[0] == 1: p.state = 1 else: p.state = 0 for p in group.get_players(): states = [p.state for p in group.get_players()] death1 = states[0] death2 = states[1] death3 = states[2] death4 = states[3] death5 = states[4] death6 = states[5] #death7 = states[6] #death8 = states[7] #death9 = states[8] death = death1 + death2+ death3+ death4+ death5+ death6#+ death7+ death8+ death9 group.death=death #print(death) def other(subsession: Subsession): for p in subsession.get_players(): deaths= [p.group.death for p in subsession.get_players()] total_votes= [p.group.total_vote_str for p in subsession.get_players()] other_diefor1=deaths[6] other_diefor2=deaths[0] other_group_resultfor1=total_votes[6] other_group_resultfor2=total_votes[0] #print(deaths) #print(total_votes) print(other_diefor1) print(other_diefor2) print(other_group_resultfor1) print(other_group_resultfor2) subsession.other_diefor1=other_diefor1 subsession.other_diefor2=other_diefor2 subsession.other_group_resultfor1=other_group_resultfor1 subsession.other_group_resultfor2=other_group_resultfor2 pay=0 vote_result = 0 # PAGES class Introduction(Page): @staticmethod def is_displayed(player): return player.round_number == 1 pass class Vote(Page): @staticmethod def is_displayed(player): return player.state == 0 form_model = 'player' form_fields = ['vote'] class VoteWaitPage(WaitPage): @staticmethod def is_displayed(player): return player.state == 0 #wait_for_all_groups = True after_all_players_arrive = result_of_vote body_text = "等待其他組別選擇中...." # def vars_for_template(self): # l=[i for i in range(1,10)] # shuffle(l) # return dict(l=l) class VoteResults(Page): @staticmethod def is_displayed(player): return player.state == 0 """Players payoff: How much each has earned""" pass class FormWaitPage(WaitPage): @staticmethod def is_displayed(player): return player.state == 0 after_all_players_arrive = form_product #wait_for_all_groups = True body_text = "等待其他參與者加入並產生表格中...." class Form(Page): timeout_seconds = 5 @staticmethod def is_displayed(player): return player.state == 0 class Self_Form(Page): @staticmethod def is_displayed(player): return player.state == 0 form_model = 'player' form_fields = ['num1','num2','num3','num4','num5','num6','num7','num8','num9',] class AnsWaitPage(WaitPage): @staticmethod def is_displayed(player): return player.state == 0 after_all_players_arrive = num_form #wait_for_all_groups = True body_text = "等待其他參與者填答中...." class GameResult(Page): @staticmethod def is_displayed(player): return player.state == 0 pass class CheckStatementWaitPage(WaitPage): @staticmethod def is_displayed(player): return player.state == 0 #wait_for_all_groups = True after_all_players_arrive = statement body_text = "等待其他參與者確認中...." class die(Page): @staticmethod def is_displayed(player): return player.state == 1 def vars_for_template(group): if vote_result == 0: hurt_para=0.2 season = "因X方案下你被選中" else: hurt_para=1 season = "因Y方案下你被選中" die = "你在此回合中標了" global pay hurt_pay=pay*hurt_para return dict(die=die,pay=int(pay),season=season,hurt_pay=int(hurt_pay),total_pay=int(pay-hurt_pay)) class live(Page): @staticmethod def is_displayed(player): return player.state == 0 def vars_for_template(self): live = "你在此回合未中標" global pay return dict(live=live,pay=pay) wait_for_all_groups = True class GroupWaitPage(WaitPage): #@staticmethod #def is_displayed(player): #return player.state == 0 wait_for_all_groups = True after_all_players_arrive = other body_text = "等待其他組確認中...." class Result_1(Page): @staticmethod def is_displayed(player): return player.group.id_in_subsession==1 #return player.state == 0 pass class Result_2(Page): @staticmethod def is_displayed(player): return player.group.id_in_subsession==2 #return player.state == 0 pass class final(Page): def vars_for_template(self): global pay return dict(live=live,pay=pay) class bye(Page): @staticmethod def is_displayed(player): return player.round_number == 5 pass #class firstpage(Page): # form_model = 'player' # def vars_for_template(self): # c=int(self.num) # print(c) page_sequence = [Introduction, Vote, VoteWaitPage, VoteResults, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, GroupWaitPage,Result_1,Result_2,Vote, VoteWaitPage, VoteResults, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, GroupWaitPage,Result_1,Result_2,Vote, VoteWaitPage, VoteResults, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, FormWaitPage,Form,Self_Form,AnsWaitPage,GameResult,CheckStatementWaitPage,die,live, GroupWaitPage,Result_1,Result_2,final,bye]