text: # Generated by Django 2.2.16 on 2020-12-20 11:16
  
  from django.db import migrations, models
  import django.db.models.deletion
  
  
  def delete_damage_and_statuses(apps, schema_editor):
      Attribute = apps.get_model("typeclasses", "Attribute")
      Attribute.objects.filter(
          db_key__in=("damage", "sleep_status", "health_status")
      ).delete()
  
  
  def delete_recovery_scripts(apps, schema_editor):
      """Wipes old recovery scripts now that it's being turned into a singleton"""
      Scripts = apps.get_model("scripts", "ScriptDB")
      # wipe all recovery scripts that currently are pointing at a character (the old scripts)
      # by ignoring non-globals we make this idempotent
      Scripts.objects.filter(
          db_typeclass_path="typeclasses.scripts.recovery.Recovery", db_obj__isnull=False
      ).delete()
  
  
  def create_recovery_runner(apps, schema_editor):
      """Create global Recovery script and its RecoveryRunner"""
      Scripts = apps.get_model("scripts", "ScriptDB")
      RecoveryRunner = apps.get_model("conditions", "RecoveryRunner")
      # using get_or_create to make this idempotent
      script, _ = Scripts.objects.get_or_create(
          db_key="Recovery",
          defaults=dict(
              db_desc="Healing over time",
              db_typeclass_path="typeclasses.scripts.recovery.Recovery",
              db_interval=60,
              db_start_delay=True,
              db_persistent=True,
          ),
      )
      RecoveryRunner.objects.get_or_create(script=script)
  
  
  class Migration(migrations.Migration):
      dependencies = [
          ("stat_checks", "0003_auto_20201227_1710"),
          ("conditions", "0003_wound"),
      ]
  
      operations = [
          migrations.CreateModel(
              name="CharacterHealthStatus",
              fields=[
                  ("damage", models.PositiveSmallIntegerField(default=0)),
                  (
                      "consciousness",
                      models.PositiveSmallIntegerField(
                          choices=[(0, "conscious"), (1, "asleep"), (2, "unconscious")],
                          default=0,
                      ),
                  ),
                  ("is_dead", models.BooleanField(default=False)),
                  (
                      "character",
                      models.OneToOneField(
                          on_delete=django.db.models.deletion.CASCADE,
                          related_name="character_health_status",
                          to="objects.ObjectDB",
                          primary_key=True,
                      ),
                  ),
              ],
              options={
                  "abstract": False,
              },
          ),
          migrations.CreateModel(
              name="RecoveryRunner",
              fields=[
                  (
                      "script",
                      models.OneToOneField(
                          on_delete=django.db.models.deletion.CASCADE,
                          primary_key=True,
                          serialize=False,
                          to="scripts.ScriptDB",
                          related_name="recovery_runner",
                      ),
                  ),
                  (
                      "recovery_last_run",
                      models.DateTimeField(
                          help_text="When our recovery checks for all damaged characters last ran. "
                          "A recovery check heals the character if successful.",
                          null=True,
                      ),
                  ),
                  (
                      "revive_last_run",
                      models.DateTimeField(
                          help_text="When our revive checks for unconscious characters last ran. "
                          "Characters above 0 health will regain consciousness if they succeed the check.",
                          null=True,
                      ),
                  ),
                  (
                      "recovery_interval",
                      models.PositiveIntegerField(
                          default=86400,
                          help_text="Number of seconds between recovery checks.",
                      ),
                  ),
                  (
                      "revive_interval",
                      models.PositiveSmallIntegerField(
                          default=300,
                          help_text="Number of seconds between revive checks.",
                      ),
                  ),
              ],
              options={
                  "abstract": False,
              },
          ),
          migrations.CreateModel(
              name="TreatmentAttempt",
              fields=[
                  (
                      "id",
                      models.AutoField(
                          auto_created=True,
                          primary_key=True,
                          serialize=False,
                          verbose_name="ID",
                      ),
                  ),
                  ("value", models.SmallIntegerField(default=0)),
                  (
                      "treatment_type",
                      models.PositiveSmallIntegerField(
                          choices=[(0, "recovery"), (1, "revive"), (2, "wound")],
                          default=0,
                      ),
                  ),
                  ("time_attempted", models.DateTimeField(null=True)),
                  (
                      "healer",
                      models.ForeignKey(
                          on_delete=django.db.models.deletion.CASCADE,
                          related_name="treatments_given",
                          to="objects.ObjectDB",
                      ),
                  ),
                  (
                      "target",
                      models.ForeignKey(
                          on_delete=django.db.models.deletion.CASCADE,
                          related_name="treatment_attempts",
                          to="conditions.CharacterHealthStatus",
                      ),
                  ),
                  (
                      "uses_remaining",
                      models.PositiveSmallIntegerField(
                          default=5,
                      ),
                  ),
                  (
                      "outcome",
                      models.ForeignKey(
                          help_text="If there's a specific effect for the treatment roll, "
                          "we look it up in the outcome we point at. Otherwise this is null.",
                          null=True,
                          on_delete=django.db.models.deletion.SET_NULL,
                          related_name="treatment_attempts",
                          to="stat_checks.StatCheckOutcome",
                      ),
                  ),
              ],
              options={
                  "abstract": False,
              },
          ),
          # delete and recreate Wound with FK to health_status rather than character
          migrations.DeleteModel("Wound"),
          migrations.CreateModel(
              name="Wound",
              fields=[
                  (
                      "id",
                      models.AutoField(
                          auto_created=True,
                          primary_key=True,
                          serialize=False,
                          verbose_name="ID",
                      ),
                  ),
                  (
                      "severity",
                      models.PositiveSmallIntegerField(
                          choices=[(0, "serious wound"), (1, "permanent wound")],
                          default=0,
                      ),
                  ),
                  (
                      "health_status",
                      models.ForeignKey(
                          on_delete=django.db.models.deletion.CASCADE,
                          related_name="wounds",
                          to="conditions.CharacterHealthStatus",
                      ),
                  ),
                  (
                      "trait",
                      models.ForeignKey(
                          on_delete=django.db.models.deletion.CASCADE,
                          related_name="wounds",
                          to="traits.Trait",
                      ),
                  ),
              ],
              options={
                  "abstract": False,
              },
          ),
          migrations.RunPython(
              delete_damage_and_statuses, migrations.RunPython.noop, elidable=True
          ),
          migrations.RunPython(
              delete_recovery_scripts, migrations.RunPython.noop, elidable=True
          ),
          migrations.RunPython(
              create_recovery_runner, migrations.RunPython.noop, elidable=False
          ),
      ]
  