in_round() oTree 中的函数

in_round() function in oTree

考虑一个有 3 个回合的游戏。在每一轮中,玩家都会做出选择(存储在变量 choice 中)。

现在,在第 3 轮中,我想调用 someFunction,从而访问在第 2 轮中做出的选择。

不幸的是someFunctionreturnsNone。我不懂为什么。如果我将函数调用放在模板文件中,一切正常。

我们将不胜感激 - 我已经搜索了几个小时。

class Subsession(BaseSubsession):
    def before_session_starts(self):
        if self.round_number == 3:
            for player in self.get_players():
                player.participant.vars['someKey'] = player.someFunction()

class Player(BasePlayer):
    choice = models.CharField(initial=None,
                                choices=['A','B','C'],
                                widget=widgets.RadioSelect()) 

    def someFunction(self):
        return self.in_round(2).choice

为什么会这样?

before_session_starts 函数在会话开始之前执行(因此得名)。因此,当它被执行时,玩家还没有做出 his/her 选择。这就是为什么 someFunction returns None.

你可以在第二轮结束时设置player.participant.vars['someKey'] = self.player.choice,这会给你想要的结果。

class Choice(Page):
    def before_next_page(self):
        if self.player.round_number == 2:
            player.participant.vars['someKey'] = self.player.choice