File size: 3,079 Bytes
781b9f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Entity status transitions and special location effects."""

from __future__ import annotations

from datetime import datetime, timedelta

from world.book_of_ages import create_entry
from world.database import db_session
from world.entities import add_tag, update_entity_status
from world.locations import get_location_by_id
from simulation.location_effects import effective_days_in_realm


def process_lifecycle_updates(world_day: int) -> list[str]:
    """Run lifecycle checks. Returns list of milestone messages."""
    milestones = []

    with db_session() as conn:
        entities = conn.execute("SELECT * FROM entities").fetchall()

    for row in entities:
        entity = dict(row)
        location = get_location_by_id(entity["location_id"])

        if entity["status"] == "active":
            last_active = datetime.fromisoformat(entity["last_active"])
            if datetime.now() - last_active > timedelta(days=7):
                update_entity_status(entity["id"], "dormant")
                msg = f"{entity['name']} has grown quiet and entered dormancy."
                create_entry(
                    world_day=world_day,
                    entry_type="milestone",
                    content=msg,
                    entity_ids=[entity["id"]],
                    is_milestone=True,
                    title="Dormancy",
                )
                milestones.append(msg)

        if entity["status"] == "dormant":
            last_active = datetime.fromisoformat(entity["last_active"])
            days_dormant = (datetime.now() - last_active).days
            if days_dormant >= 30:
                update_entity_status(entity["id"], "legendary")
                msg = f"{entity['name']} has become Legendary — woven into the Realm's foundation."
                create_entry(
                    world_day=world_day,
                    entry_type="milestone",
                    content=msg,
                    entity_ids=[entity["id"]],
                    is_milestone=True,
                    title="Legendary Status",
                )
                milestones.append(msg)

        entity["location_slug"] = location["slug"] if location else None
        eff_days = effective_days_in_realm(entity)

        if location and location["slug"] == "hollow-mountain":
            if eff_days >= 14 and not entity["wisdom_unlocked"]:
                add_tag(entity["id"], "wise")
                with db_session() as conn:
                    conn.execute(
                        "UPDATE entities SET wisdom_unlocked = 1 WHERE id = ?",
                        (entity["id"],),
                    )
                msg = f"{entity['name']} earned wisdom in the Hollow Mountain."
                create_entry(
                    world_day=world_day,
                    entry_type="milestone",
                    content=msg,
                    entity_ids=[entity["id"]],
                    is_milestone=True,
                    title="Wisdom Earned",
                )
                milestones.append(msg)

    return milestones