Pseudonyms
**********

Short, friendly names such as "Blue fox" for showing participants to
each other.

Multiplayer pages (chat rooms, group displays) need to tell
participants apart without revealing recruitment IDs. This module
gives every participant a pseudonym made of a colour and an animal,
shown as the animal in that colour.

Design constraints:

* Page rendering is read-only, so the default pseudonym is a pure
  function of the participant ID and the deployment ID stored in the
  database, and is never written. Each deployment shuffles the colours
  and animals differently, so participant 10 gets a different
  pseudonym in each deployment, and a loaded export gives the same
  pseudonyms that participants saw, as long as it is loaded with the
  same PsyNet version, pseudonym scheme and locale, and not launched
  again. Colour and animal indices cycle with coprime periods (8 and
  11), so any 88 consecutive participants get different pseudonyms,
  and neighbours differ in both colour and animal, which helps colour-
  blind participants. Participants whose IDs differ by a multiple of
  88 share a pseudonym.

* Only keys are stored when an experiment overrides a pseudonym, or
  when joining a first group makes one distinct, in the participant's
  "pseudonym_color" and "pseudonym_animal" columns; labels are
  translated when shown.

* Experiments customize pseudonyms by subclassing "PseudonymScheme"
  and setting "psynet.experiment.Experiment.pseudonym_scheme".

class psynet.pseudonym.Pseudonym(color, animal, name, css_color, emoji, sticker_html)

   Bases: "object"

   A participant's pseudonym, ready to show.

   color, animal

      Keys into the scheme's colours and animals, as stored.

   name

      Translated name, for example ""Blue fox"".

   css_color

      CSS colour for the participant's animal.

   emoji

      The animal's emoji.

   sticker_html

      HTML for a picture of the animal in the participant's colour, as
      in "GroupPrompt" and chat rooms. Its size follows the font size.

   to_dict()

      Return the fields as JSON-serializable values.

      Return type:
         "dict"

class psynet.pseudonym.PseudonymScheme

   Bases: "object"

   Chooses and displays participants' pseudonyms.

   Set an instance on your experiment to change the defaults:

      _ = get_translator()

      class TeamScheme(PseudonymScheme):
          def animals(self):
              return {"lion": (_("lion"), "🦁"), "bear": (_("bear"), "🐻")}

      class Exp(psynet.experiment.Experiment):
          pseudonym_scheme = TeamScheme()

   These methods are the hooks to override:

   * "colors()" and "animals()", the vocabulary;

   * "assign()" and "seed()", who gets which pseudonym;

   * "assign_distinct()", the pseudonym given to a newcomer whose
     pseudonym an active member of their first group already has;

   * "format_name()", the names, for example to name the pairs of a
     custom vocabulary in full;

   * "render_sticker()" and "artwork()", the pictures.

   Call "set_pseudonym()" to give one participant a particular
   pseudonym.

   animals()

      Return "{key: (label, emoji)}"; labels are translated here.

      Return type:
         "Dict"["str", "Tuple"["str", "str"]]

   artwork(animal, emoji)

      Return URLs of an animal's "(shape, lines)" SVGs, or "None" to
      use its emoji.

      "render_sticker()" fills the shape's outline with the
      participant's colour and draws the lines on top. The built-in
      animals use Microsoft Fluent Emoji artwork (MIT licence);
      animals added by an override, or given a different emoji, get
      "None".

      Return type:
         "Optional"["Tuple"["str", "str"]]

   assign(participant)

      Return the "(color, animal)" keys for a participant without a
      stored pseudonym.

      The default shuffles the colours and animals using "seed()",
      then cycles through them by participant ID. If the two lists'
      lengths share no factor, every pair recurs only after
      "len(colors) * len(animals)" participants.

      Return type:
         "Tuple"["str", "str"]

   assign_distinct(participant, taken)

      Return "(color, animal)" keys that nobody in "taken" has, or
      "None" if all are used.

      Used to give a newcomer to their first group, whose derived
      pseudonym an active member already has, one that differs from
      the others'. "taken" is a collection of "(color, animal)" pairs.
      The default prefers a pair whose colour and animal are both
      unused, and picks among those using "seed()" and the
      participant's ID, so that participants renamed in different
      groups get different names.

      Return type:
         "Optional"["Tuple"["str", "str"]]

   colors()

      Return "{key: (label, css_color)}"; labels are translated here.

      Return type:
         "Dict"["str", "Tuple"["str", "str"]]

   format_name(color, animal)

      Return the translated name for a pair of colour and animal keys.

      Each pair of built-in keys has its own translated message, such
      as ""Blue fox"", so that languages can make the colour agree
      with the animal. Other pairs get their labels joined by a
      translatable ""{COLOR} {ANIMAL}"", which can't agree; override
      this method to name such pairs in full. Give new words new keys,
      since a built-in key keeps its built-in name.

      Return type:
         "str"

   render_sticker(css_color, animal, emoji, name)

      Return HTML for the animal drawn in the participant's colour.

      Uses "artwork()" when there is some, and otherwise tints the
      emoji. Lines turn light on dark colours. The picture is hidden
      from screen readers, because pages show the name next to it.
      Pages insert this HTML as it is, so an override must return
      "Markup" and escape its arguments, as "Markup.format" does.

      Return type:
         "Markup"

   seed()

      Return the seed for "assign()"'s shuffle: by default, the
      deployment ID.

      It is read from the database, so it is the same in every server
      process and in exports, and differs between deployments. Before
      launch there is none yet, and the order is fixed.

      Return type:
         "str"

psynet.pseudonym.anonymous_sticker_html()

   Return HTML for a grey person silhouette, used where members must
   not be recognisable.

   Return type:
      "Markup"
