我试图在Otree (Python )中实现一个简单的记分计数器(Python),方法是修改测验游戏模板,使其成为两个玩家。
最终,我希望这个计数器只在某些条件下更新,但现在,我只是尝试在每一轮之后添加10。
在models.py中,我定义了一个player类,如下所示:
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)上面唯一相关的函数是“得分”,我在pages.py模板中称之为“记分”,如下所示:
class ResultsWaitPage(WaitPage):
def after_all_players_arrive(self):
for p in self.group.get_players():
p.score_points()然后,我创建了一个结果页面,该页面显示每个问题之后的测试结果,以测试“total_score”或“支付”是否每个问题都增加了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,而且它永远不会改变。我不知道为什么会这样。有什么想法吗?
发布于 2018-03-12 23:09:39
如果您修改了标准的quiz游戏,这是oTree包(这里)的一部分,那么您可以看到它是一个多轮游戏,其中每组问题都在一个单独的回合中。
在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中得到的是到目前为止两位玩家的所有分数的和--我不确定在什么条件下有人需要这些信息,但是我不知道你的具体设计。
https://stackoverflow.com/questions/49242597
复制相似问题