应用&轮次

应用

一个oTree应用就是一个包含Python与HTML代码的文件夹。一个项目包含多个应用。一个会话基本上就是一个接一个被游玩的应用序列。

组合应用

你可以通过设置session config的 'app_sequence' 来组合应用。

在应用之间传递数据

See participant fields and session fields.

轮次

你可以通过设置 Constants.num_rounds 来使一个游戏进行多轮。举例来说,如果你的session config中的 app_sequence['app1', 'app2'],同时 app1num_rounds = 3app2num_rounds = 1,那么你的会话中就包含4个子会话。

轮数

你可以通过 player.round_number (目前subsession, group, player, 与 page 对象有此属性)来获得当前的轮数。轮数从1开始。

在应用或轮次之间传递数据

每一轮都拥有独立的subsession, Group, 与 Player 对象。举例来说,假定你在第一轮中设定了 player.my_field = True 。在第二轮时,你试图访问 player.my_field,你会发现它的值是 None。这是因为第一轮的 Player 对象与第二轮的 Player 对象是独立的。

为了获得之前轮次或者应用的数据,你可以使用下面描述的这些技巧。

in_rounds, in_previous_rounds, in_round, etc.

Player, group, 与 subsession 对象有下面这些方法:

  • in_previous_rounds()
  • in_all_rounds()
  • in_rounds()
  • in_round()

举例来说,如果你在一个10轮游戏的最后一轮, player.in_previous_rounds() 会返回一个由9个player对象组成的列表,就代表了当前参与者在之前轮次的对象。

player.in_all_rounds() 几乎与上面相同,只不过列表中有10个对象,因为它包含了当前轮次的player对象。

player.in_rounds(m, n) 返回一个player对象的列表,其中代表了同一参与人第 mn 轮的player对象。

player.in_round(m) returns just the player in round m. For example, to get the player’s payoff in the previous round, you would do:

prev_player = player.in_round(player.round_number - 1)
print(prev_player.payoff)

这些方法对于subsession也是相同的(例如 subsession.in_all_rounds())。

这些方法对于group也是相同的,但是如果你在轮次之间重新安排了小组,那么使用这些方法是没有意义的。

Participant fields

注解

As of March 2021, this is a new optional syntax for participant.vars. Instead of setting participant.vars['my_field'] = 1, you can now set participant.my_field = 1 directly. Just make sure to define PARTICIPANT_FIELDS first. See here for more info.

If you want to access a participant’s data from a previous app, you should store this data on the participant object, which persists across apps (see 参与人). (in_all_rounds() only is useful when you need to access data from a previous round of the same app.)

Go to settings and define PARTICIPANT_FIELDS, which is a list of the names of fields you want to define on your participant.

Then in your code, you can get and set any type of data on these fields:

participant.mylist = [1, 2, 3]

(Internally, all participant fields are stored in a dict called participant.vars. participant.xyz is equivalent to participant.vars['xyz'].)

Participant fields are not included in the Excel/CSV data export, or in the “Data” tab in the session admin. If you want that, you should either use 自定义数据导出 or save str(participant.vars) into a LongStringField. (The same concept applies for session fields.)

Session fields

注解

This is a new feature; see the note above about PARTICIPANT_FIELDS.

For global variables that are the same for all participants in the session, add them to the SESSION_FIELDS, which works the same as PARTICIPANT_FIELDS. Internally, all session fields are stored in session.vars.

可变轮数

如果你想要可变的轮数,考虑使用 实时页面.

Alternatively, you can set num_rounds to some high number, and then in your app, conditionally hide the {{ next_button }} element, so that the user cannot proceed to the next page, or use app_after_this_page. But note that having many rounds (e.g. more than 100) might cause performance problems, so test your app carefully.