Python:Otree 中的简单计数器
Python: Simple Counter in Otree
我试图在 Otree(Python 库)中实现一个简单的计分器,方法是修改问答游戏模板,使其成为两个玩家。
最后,我希望这个计数器只在某些条件下更新,但现在,我只是想在每一轮之后加 10。
在models.py中,我定义了一个播放器class,如下所示:
class Player(BasePlayer):
question_id = models.IntegerField()
confidence = models.FloatField(widget=widgets.Slider(attrs={'step': '0.01'}))
question = models.StringField()
solution = models.StringField()
submitted_answer = models.StringField(widget=widgets.RadioSelect)
is_correct = models.BooleanField()
total_score = models.IntegerField(initial = 0)
def other_player(self):
return self.get_others_in_group()[0]
def current_question(self):
return self.session.vars['questions'][self.round_number - 1]
def check_correct(self):
self.is_correct = self.submitted_answer == self.solution
def check_if_awarded_points(self):
self.get_others_in_group()[0].submitted_answer == self.submitted_answer == self.solution
def score_points(self):
self.total_score+=10
self.payoff += c(10)
上面唯一相关的函数是 "score points",我在 pages.py 模板中这样调用它:
class ResultsWaitPage(WaitPage):
def after_all_players_arrive(self):
for p in self.group.get_players():
p.score_points()
然后我创建一个结果页面,在每个问题后显示测验的结果,以测试 "total_score' or "pay-offs" 每个问题增加 10。
class IntermediateResults(Page):
def vars_for_template(self):
return {
'total_score': [p.total_score for p in self.group.get_players()],
'total_payoff': [p.payoff for p in self.group.get_players()]
}
如果我使用 Django 公开 total_payoff 和 total_score 的值,我会看到它们的值为 10,而且它永远不会改变。我不知道为什么会这样。有什么想法吗?
如果你修改标准 quiz
游戏,它是 oTree 包的一部分 (here),那么你可以看到它是 multi-round 游戏,其中每组问题在一个单独的回合中。
在 oTree 中,每一轮都有自己的一组玩家(属于一个 participant
)。这意味着您的代码工作正常,但您只是获得有关当前回合的信息(他们的总分确实是 10。您需要的是从 前几轮 ,不仅是现在的
类似的东西应该可以工作:
class IntermediateResults(Page):
def vars_for_template(self):
allplayers = self.group.get_players()
total_score = sum([sum([p.total_score for p in pp.in_all_rounds()]) for pp in allplayers])
total_payoff = sum([sum([p.payoff for p in pp.in_all_rounds()]) for pp in allplayers])
return {
'total_score': total_score,
'total_payoff': total_payoff,
}
虽然我应该注意到你在 total_score
中得到的是到目前为止两位玩家所有得分的 sum - 我不确定在什么条件下有人需要这个资料,但是不知道你的具体设计。
我试图在 Otree(Python 库)中实现一个简单的计分器,方法是修改问答游戏模板,使其成为两个玩家。
最后,我希望这个计数器只在某些条件下更新,但现在,我只是想在每一轮之后加 10。
在models.py中,我定义了一个播放器class,如下所示:
class Player(BasePlayer):
question_id = models.IntegerField()
confidence = models.FloatField(widget=widgets.Slider(attrs={'step': '0.01'}))
question = models.StringField()
solution = models.StringField()
submitted_answer = models.StringField(widget=widgets.RadioSelect)
is_correct = models.BooleanField()
total_score = models.IntegerField(initial = 0)
def other_player(self):
return self.get_others_in_group()[0]
def current_question(self):
return self.session.vars['questions'][self.round_number - 1]
def check_correct(self):
self.is_correct = self.submitted_answer == self.solution
def check_if_awarded_points(self):
self.get_others_in_group()[0].submitted_answer == self.submitted_answer == self.solution
def score_points(self):
self.total_score+=10
self.payoff += c(10)
上面唯一相关的函数是 "score points",我在 pages.py 模板中这样调用它:
class ResultsWaitPage(WaitPage):
def after_all_players_arrive(self):
for p in self.group.get_players():
p.score_points()
然后我创建一个结果页面,在每个问题后显示测验的结果,以测试 "total_score' or "pay-offs" 每个问题增加 10。
class IntermediateResults(Page):
def vars_for_template(self):
return {
'total_score': [p.total_score for p in self.group.get_players()],
'total_payoff': [p.payoff for p in self.group.get_players()]
}
如果我使用 Django 公开 total_payoff 和 total_score 的值,我会看到它们的值为 10,而且它永远不会改变。我不知道为什么会这样。有什么想法吗?
如果你修改标准 quiz
游戏,它是 oTree 包的一部分 (here),那么你可以看到它是 multi-round 游戏,其中每组问题在一个单独的回合中。
在 oTree 中,每一轮都有自己的一组玩家(属于一个 participant
)。这意味着您的代码工作正常,但您只是获得有关当前回合的信息(他们的总分确实是 10。您需要的是从 前几轮 ,不仅是现在的
类似的东西应该可以工作:
class IntermediateResults(Page):
def vars_for_template(self):
allplayers = self.group.get_players()
total_score = sum([sum([p.total_score for p in pp.in_all_rounds()]) for pp in allplayers])
total_payoff = sum([sum([p.payoff for p in pp.in_all_rounds()]) for pp in allplayers])
return {
'total_score': total_score,
'total_payoff': total_payoff,
}
虽然我应该注意到你在 total_score
中得到的是到目前为止两位玩家所有得分的 sum - 我不确定在什么条件下有人需要这个资料,但是不知道你的具体设计。