Late, slow and leaving players: case studies
********************************************

Group games rarely run as planned: a player closes their tab, another
reads the results slowly, a third is left over when pairs are formed.
This page follows such cases through concrete designs, and says what
each player sees, whom they wait for and when they move on. The rules
behind them are in whom a group barrier waits for and Failure cases;
this page only applies them. Each case ends with the test that checks
it, so if PsyNet's behaviour changes, the test fails until the page is
updated.

All the cases share one design, a fixed group answering a loop of
questions, unless the case says otherwise. Each question is one round:
a cued trial whose "show_trial" includes a "GroupBarrier", so the
barrier waits for every member of the group and its release closes the
round (see Rounds of cued trials). With "accumulate_answers",
"trial.answer" keeps each page's answer under its label, so the answer
of the estimate page, labelled ""estimate"", stays in
"trial.answer["estimate"]" after the revise page:

   class EstimateTrial(Trial):
       sync_group_type = "team"
       accumulate_answers = True

       def show_trial(self, experiment, participant):
           return join(
               self.estimate_page(),
               GroupBarrier(id_="submitted", group_type="team",
                            on_release=self.score_round),
               self.revise_page(),
           )

   Timeline(
       SimpleGrouper(group_type="team", initial_group_size=2),
       Module(
           "game",
           for_loop(label="q", iterate_over=range(3),
                    logic=lambda q: EstimateTrial.cue({"q": q}, round=q)),
       ),
   )

The players are called Ann, Ben and Cat, and times count from the
start of the round. A barrier without "late_after" lets each member
wait at most its "max_wait_time", 20 s by default; the cases assume
shorter waits, except the one where the wait runs out. When a design
makes members wait for a slow page, size that limit or "late_after"
for it (see Timeouts).


A fixed pair
============

Ann and Ben are paired and both play every round. Ann submits her
estimate at 12 s, Ben at 25 s.

* Ann waits at "submitted" from 12 s to 25 s.

* At 25 s the barrier releases both, "on_release" scores the round
  once, and the round closes. Both trials record
  "sync_group_outcome="stayed"".

Nobody waits longer than the slower partner takes. To stop a partner
who never answers from holding the other up, give the estimate page a
"time_limit" and the barrier "late_after" (see Moving slow players
on).

Checked by "test_case_a_fixed_pair_waits_only_for_the_slower_partner"
in "tests/isolated/test_trial_cue_groups.py".


A member leaves partway through a round
=======================================

The group has no top-ups and the grouper sets
"below_quorum="continue_without_group"". Ann (and Ben, in a group of
three) submit and wait at "submitted"; then the last member returns
the study.

* **The group keeps its quorum** (three players, "quorum=2"): the
  barrier stops waiting for the member who left. Ann and Ben are
  released at the next barrier check, usually within a second or two.
  "on_release" runs with "left" listing the leaver, and Ann's and
  Ben's trials record ""stayed"".

* **The group falls below its quorum** (a pair): it is dissolved. Ann
  is released without "on_release" running, her trial records
  ""group_dissolved"", so "show_feedback" can score her estimate from
  "trial.answer["estimate"]" (see round outcomes), and her next page
  says "Your partner is no longer in your group. Please continue." She
  plays the rest of the timeline without a group (see After leaving a
  group).

With "below_quorum="fail"", a dissolved group fails Ann instead, and a
group that still takes newcomers is not dissolved but waits at the
barrier for one, unless it admits newcomers only at admission points
and "submitted" isn't one (see Groups below quorum).

Checked by "test_case_a_member_leaves_mid_round" in
"tests/isolated/test_trial_cue_groups.py", for both group sizes.


A slow reader between runs
==========================

The pair plays the whole game several times, with cues that don't pass
"round=" (a module that runs again can't), and a results page after
each run, inside the game's module or outside it. Ann reads the
results quickly and waits at run 2's first barrier; it waits for Ben,
however long he reads, and releases both together. Size that barrier's
"max_wait_time" or "late_after" for slow readers.

Checked by "test_case_a_slow_reader_between_runs_of_a_looped_module"
in "tests/isolated/test_trial_cue_groups.py" for the page inside the
module, and by "test_a_barrier_after_the_pages_between_runs_waits_for_
the_slow_member" for a page outside it.


An odd player out with a regrouper
==================================

A cohort of three is split into new pairs every round:

   SimpleGrouper("cohort", initial_group_size=3)
   for_loop(..., logic=join(
       Regrouper("pair", within="cohort", roles=["proposer", "responder"],
                 left_over="sit_out", deadline=30),
       switch(...),   # the round's trial, or a sit-out page
   ))

1. All three reach the regrouper. It pairs two of them as proposer and
   responder. The third sits out: they move on at once without a pair,
   so the "switch" shows them the sit-out page (see Sitting out). They
   have no role that round, and "sat_out()" is "True".

2. The sitter is back at the regrouper first and waits there for the
   pair. If the pair is still playing one "deadline" after the sitter
   arrived, the sitter waits at most one more "deadline", then sits
   out again.

3. When the pair is back, the regrouper pairs the sitter, who has
   played the fewest rounds, with one of the two. The other sits this
   round out.

Checked by
"test_case_an_odd_player_out_sits_out_then_plays_the_next_round" in
"tests/isolated/test_regrouper.py", and the deadline in step 2 by
"test_regrouper_left_over_waits_for_cohort_still_in_the_round".


A barrier's maximum wait runs out
=================================

The pair has no top-ups, and "submitted" has no "late_after", so its
"max_wait_time" (20 s by default) applies. Ann submits; Ben has closed
his tab and never arrives. 20 s after Ann arrived, her wait runs out,
and the barrier's "max_wait_action" and the grouper's "below_quorum"
decide:

+---------------------------+---------------------------+---------------------------+---------------------------+
| "max_wait_action"         | "below_quorum"            | Ann, who waited           | Ben, who didn't arrive    |
|===========================|===========================|===========================|===========================|
| ""fail"" (default)        | ""fail"" (default)        | failed                    | failed when the pair      |
|                           |                           |                           | dissolves                 |
+---------------------------+---------------------------+---------------------------+---------------------------+
| ""fail""                  | ""continue_without_group" | failed                    | carries on alone          |
|                           | "                         |                           |                           |
+---------------------------+---------------------------+---------------------------+---------------------------+
| ""leave_group""           | ""fail""                  | carries on alone          | failed                    |
+---------------------------+---------------------------+---------------------------+---------------------------+
| ""leave_group""           | ""continue_without_group" | carries on alone          | carries on alone          |
|                           | "                         |                           |                           |
+---------------------------+---------------------------+---------------------------+---------------------------+

Ann's release doesn't count as the pair passing the barrier, so
"on_release" doesn't run. Ann leaves the pair as ""waited_too_long""
before she is failed or moves on. So in every row her trial records
""waited_too_long"", and so does
"ann.sync_group_removal_reason("team")". When she carries on, she sees
the notice "Your partner didn't arrive in time. Please continue." When
she is failed, she leaves her other groups, such as a cohort she
shares with Ben, as ""left_experiment"", and her final page says "We
waited too long for other participants, so we ended your session."
Ben's trial records ""group_dissolved"" in every row. So with the
defaults both are failed, including Ann, who was on time. To move on
without the absent member instead, give the barrier "late_after" (see
Failure cases).

Checked by "test_case_max_wait_time_runs_out" in
"tests/isolated/test_trial_cue_groups.py", for all four rows, and for
the ""fail"" rows with a cohort that Ann and Ben share.


A group dissolves while a member is still on a round page
=========================================================

A group of three without top-ups, whose grouper sets
"below_quorum="continue_without_group"". Ann waits at "submitted", Ben
is still on the estimate page, and Cat returns the study. The group
falls below its quorum of three and is dissolved:

* Ann is released, and "on_release" doesn't run. Her trial records
  ""group_dissolved"".

* Ben's trial records ""group_dissolved"" too. When he submits his
  estimate, "submitted" lets him straight through, and his next page
  says "Too few members of your group remained. Please continue." (in
  a pair: "Your partner is no longer in your group. Please
  continue.").

* The round records when the group dissolved, in
  "trial.group_round.dissolved_at" (see dissolved rounds), so the
  export shows the round as cancelled even if Ben never submits.

Both estimates stay on their trials, in "trial.answer["estimate"]", so
an export or "on_finalized" can score the round from
"group_round.trials" (see round outcomes). Ann's "show_feedback" may
run before Ben has submitted his.

Ben's trial runs to its end, so he still sees the revise page. To skip
the rest of the round once he has left, wrap its steps in "join(...,
stop_if=...)" (see After leaving a group).

Checked by
"test_case_a_group_dissolves_under_a_member_on_a_round_page" in
"tests/isolated/test_trial_cue_groups.py", and the stored estimates by
"test_a_dissolved_round_keeps_its_answers_and_outcomes".


Members end up at different barriers
====================================

Nothing stops the pair's top-ups, so it still takes newcomers during
the game, and each question starts with a "GroupBarrier(id_="ready",
group_type="team")" before the cue. Ann and Ben are in a round when
Cat joins. Cat goes through the game from its start, so she waits at
"ready" while Ann waits for her at "submitted". Ben then submits, or
leaves the pair, or is removed as late:

* Each barrier waits for the members at the other, so nobody passes
  until "late_after" or "max_wait_time" moves someone on.

* Within a second or so, the background barrier check records one
  error for the pair that names both barriers and who waits at each,
  logs it and lists it under the dashboard's Errors tab. A bot test
  fails with "GroupDivergenceError" and that message.

To avoid this, stop top-ups before the game with "stop_top_ups=True"
(see Letting latecomers join before the game starts), or make "ready"
an admission point with "use_admission_points=True" (see Admission
points), so that Cat passes the game's other barriers until "ready"
admits her. See also members at different barriers.

Checked by "test_a_sweep_reports_members_waiting_for_each_other_at_dif
ferent_barriers" in "tests/isolated/test_trial_cue_groups.py", for
each way Ben goes.
