fixed portals

This commit is contained in:
erik 2025-06-24 19:13:31 +00:00
parent dffd295091
commit 7ff94b59a8
4 changed files with 475 additions and 134 deletions

View file

@ -127,18 +127,18 @@ character_inventories = Table(
UniqueConstraint("character_name", "item_id", name="uq_char_item"),
)
# Portal discoveries table for 24-hour live tracking
portal_discoveries = Table(
# Records player portal discoveries with 24-hour retention
"portal_discoveries",
# Portals table with coordinate-based uniqueness and 1-hour retention
portals = Table(
# Stores unique portals by coordinates with 1-hour retention
"portals",
metadata,
Column("id", Integer, primary_key=True),
Column("character_name", String, nullable=False, index=True),
Column("portal_name", String, nullable=False),
Column("timestamp", DateTime(timezone=True), nullable=False, index=True),
Column("ns", Float, nullable=False), # North/South coordinate as float
Column("ew", Float, nullable=False), # East/West coordinate as float
Column("z", Float, nullable=False), # Elevation as float
Column("ns", Float, nullable=False),
Column("ew", Float, nullable=False),
Column("z", Float, nullable=False),
Column("discovered_at", DateTime(timezone=True), nullable=False, index=True),
Column("discovered_by", String, nullable=False),
)
# Server health monitoring tables
@ -229,19 +229,40 @@ async def init_db_async():
))
except Exception as e:
print(f"Warning: failed to set retention/compression policies: {e}")
# Create unique constraint on rounded portal coordinates
try:
with engine.connect().execution_options(isolation_level="AUTOCOMMIT") as conn:
# Drop old portal_discoveries table if it exists
conn.execute(text("DROP TABLE IF EXISTS portal_discoveries CASCADE"))
# Create unique constraint on rounded coordinates for the new portals table
conn.execute(text(
"""CREATE UNIQUE INDEX IF NOT EXISTS unique_portal_coords
ON portals (ROUND(ns::numeric, 2), ROUND(ew::numeric, 2))"""
))
# Create index on coordinates for efficient lookups
conn.execute(text(
"CREATE INDEX IF NOT EXISTS idx_portals_coords ON portals (ns, ew)"
))
print("Portal table indexes and constraints created successfully")
except Exception as e:
print(f"Warning: failed to create portal table constraints: {e}")
async def cleanup_old_portals():
"""Clean up portal discoveries older than 24 hours."""
"""Clean up portals older than 1 hour."""
try:
cutoff_time = datetime.now(timezone.utc) - timedelta(hours=24)
cutoff_time = datetime.now(timezone.utc) - timedelta(hours=1)
# Delete old portal discoveries
# Delete old portals
result = await database.execute(
"DELETE FROM portal_discoveries WHERE timestamp < :cutoff_time",
"DELETE FROM portals WHERE discovered_at < :cutoff_time",
{"cutoff_time": cutoff_time}
)
print(f"Cleaned up {result} portal discoveries older than 24 hours")
print(f"Cleaned up {result} portals older than 1 hour")
return result
except Exception as e: