from otree.api import * import random from time import time doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'gachacontrol' players_per_group = None num_rounds = 100 p = 0.01 price = 10 reward = 80 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): wish = models.IntegerField( widget=widgets.RadioSelect, choices=[1, 10, 0] ) count = models.IntegerField(initial=0) # number of buying lottery count_for_pity = models.IntegerField(initial=0) # count for pity system diamond = models.IntegerField(initial=1000) # tokens random = models.IntegerField(initial=0) # random number to determine win or lose count_for_random = models.IntegerField(initial=0) # Used to count number of wining prize of each round win = models.IntegerField(initial=0) # Used to count the total number of wining prize of whole round n = models.IntegerField(initial=1) # Used to count the iteration time = models.FloatField(initial=0) # Used to count the time spent on each choice # FUNCTIONS # PAGES class Wish(Page): form_model = 'player' form_fields = ['wish'] @staticmethod def before_next_page(player, timeout_happened): if player.diamond - Constants.price * player.wish >= 0: pass elif player.diamond - Constants.price * player.wish < 0: player.wish = int(player.diamond / Constants.price) while player.n <= player.wish: player.random = random.randint(1, 100) if player.random <= 90 and player.count_for_pity < 14: pass elif player.random <= 90 and player.count_for_pity == 14: pass elif player.random > 90: player.count_for_random += 1 player.n = player.n + 1 # Above is the code for random lottery and counting lottery which win the prize player.time = time() if player.round_number == 1: player.diamond = player.diamond - Constants.price* player.wish player.win = player.count_for_random player.count = player.wish player.participant.remaining_diamond =player.diamond - Constants.price* player.wish player.participant.total_win = player.count_for_random player.participant.total_wish = player.wish player.participant.payment = player.diamond + Constants.reward * player.win else: previous_player = player.in_round(player.round_number - 1) player.diamond = previous_player.diamond - Constants.price* player.wish player.win = previous_player.win + player.count_for_random player.count = previous_player.count + player.wish player.participant.remaining_diamond = previous_player.diamond - Constants.price* player.wish player.participant.total_win = previous_player.win + player.count_for_random player.participant.total_wish = previous_player.count + player.wish player.participant.payment = player.diamond + Constants.reward * player.win # Above is the code for counting remaining token and total number of winning prize after player's choice #using partcipant.var to pass the data to next app def vars_for_template(player: Player): if player.round_number == 1: return dict( token=player.diamond, win=player.win, count=player.count, ) else: previous_player = player.in_round(player.round_number - 1) player.diamond = previous_player.diamond player.win = previous_player.win player.count = previous_player.count return dict( token=player.diamond, win=player.win, count=player.count, ) # Above is the code for showing the result of last round in this page @staticmethod def app_after_this_page(player, upcoming_apps): if player.wish == 0 or player.diamond <= 0 or player.round_number == Constants.num_rounds : return upcoming_apps[4] # Above is the code for skip wishing # before_next_page is run in the end of page, and it cannot be used in (waitpage) class Wish2(Page): timeout_seconds = 1 def vars_for_template(player: Player): return dict( token=player.diamond, win=player.win, count=player.count, ) class ResultsWaitPage(WaitPage): pass # using after_all_players_arrive must have group class Results(Page): @staticmethod def vars_for_template(player: Player): return dict( token=player.diamond, win=player.count_for_random, total_win=player.win, count=player.wish, total_count=player.count, ) class Final(Page): pass page_sequence = [Wish,Wish2]