页面¶
参与人看到的每一个页面均由 Page
定义。
page_sequence
变量定义了页面的顺序。
如果游戏有多轮,那么这一页面序列会重复多次(参考 轮次)。
一个 Page
可以有下面这些方法与属性。
is_displayed()¶
如果此页面应当被显示,你可以定义此方法返回 True
;如果此页面应该被跳过,那么可以定义其返回False。如不定义,默认情况下页面将被显示。
举例来说,如果想仅让每组中的玩家2看到此页面:
@staticmethod
def is_displayed(player):
return player.id_in_group == 2
如果仅在第一轮显示此页面:
@staticmethod
def is_displayed(player):
return player.round_number == 1
如果你需要在一些页面之后重复同样的规则,参考 app_after_this_page.
vars_for_template()¶
使用这个方法向模板传递变量。例如:
@staticmethod
def vars_for_template(player):
a = player.num_apples * 10
return dict(
a=a,
b=1 + 1,
)
在模板中你就可以像下面这样访问 a
与 b
:
Variables {{ a }} and {{ b }} ...
oTree automatically passes the following objects to the template:
player
, group
, subsession
, participant
, session
, and C
.
You can access them in the template like this: {{ C.BLAH }}
or {{ player.blah }}
.
如果用户刷新了页面, vars_for_template
会被重新执行。
before_next_page()¶
在这里你可以定义需要在表单验证之后,玩家继续下一个页面之前执行的任意代码。
如果页面通过 is_displayed
被跳过,那么 before_next_page
也会被跳过。
例如:
@staticmethod
def before_next_page(player, timeout_happened):
player.tripled_apples = player.num_apples * 3
app_after_this_page¶
为了跳过整个应用,你可以定义 app_after_this_page
。例如跳过下一个应用,代码如下:
@staticmethod
def app_after_this_page(player, upcoming_apps):
if player.whatever:
return upcoming_apps[0]
upcoming_apps
是 app_sequence
(一个字符串列表)的剩余部分。因此,为了跳过最后一个应用,返回 upcoming_apps[-1]
即可。或者直接返回硬编码字符串(只要字符串在 upcoming_apps
中):
@staticmethod
def app_after_this_page(player, upcoming_apps):
print('upcoming_apps is', upcoming_apps)
if player.whatever:
return "public_goods"
如果此方法没有返回任何值,玩家将正常进行下一个页面。