from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency ) class Constants(BaseConstants): players_per_group = 52 teams_per_group = 26 num_rounds = 1 name_in_url = 'SB1_pBCG' jackpot = Currency(1000) guess_max = 100 instructions_template = 'SB1_pBCG/Instructions.html' class Subsession(BaseSubsession): def creating_session(self): new_structure = [[1, 2, 7, 8, 3, 4, 9, 10, 5, 6, 11, 12, 13, 14, 19, 20, 15, 16, 21, 22, 17, 18, 23, 24, 25, 26, 31, 32, 27, 28, 33, 34, 29, 30, 35, 36, 37, 38, 45, 46, 39, 40, 47, 48, 41, 42, 49, 50, 43, 44, 51, 52]] self.set_group_matrix(new_structure) class Group(BaseGroup): two_thirds_avg = models.FloatField() best_guess = models.IntegerField() num_winners = models.IntegerField() def set_chat_id(self): all_players = self.get_players() players = [m for m in all_players if m.id_in_group % 4 == 1 or m.id_in_group % 4 == 2] notplayers = [n for n in all_players if n.id_in_group % 4 == 3 or n.id_in_group % 4 == 0] for p in players: p.chat_id = p.id_in_group for q in notplayers: q.chat_id = q.id_in_group - 2 def set_payoffs(self): all_players = self.get_players() players = [m for m in all_players if m.id_in_group % 4 == 1 or m.id_in_group % 4 == 2] notplayers = [n for n in all_players if n.id_in_group % 4 == 3 or n.id_in_group % 4 == 0] guesses = [p.guess for p in players] two_thirds_avg = (2 / 3) * sum(guesses) / len(players) self.two_thirds_avg = round(two_thirds_avg, 2) self.best_guess = min(guesses, key=lambda guess: abs(guess - self.two_thirds_avg)) winners = [p for p in players if p.guess == self.best_guess] self.num_winners = len(winners) for p in winners: p.is_winner = True p.payoff = Constants.jackpot / self.num_winners for q in notplayers: q.payoff = self.get_player_by_id(q.id_in_group - 2).payoff class Player(BasePlayer): guess = models.IntegerField( min=0, max=Constants.guess_max, label='0から100の間の整数を1つ選んでください (半角で入力してください)' ) is_winner = models.BooleanField(initial=False) reason = models.LongStringField( label='その数値を選んだ理由を入力してください') chat_id = models.IntegerField() def display(self): if self.id_in_group % 4 == 1 or self.id_in_group % 4 == 2: return 1 else: return 0 alphabet = models.IntegerField( choices=[ [1, 'A'], [2, 'B'], [3, 'C'], [4, 'D'], [5, 'E'], [6, 'F'], [7, 'G'], ], widget=widgets.RadioSelectHorizontal, label='1つ選択してください')