Backup before cleanup
This commit is contained in:
@@ -6,6 +6,7 @@ import asyncio
|
||||
import logging
|
||||
import random
|
||||
import time
|
||||
from api.services.helpers import get_game_message
|
||||
from .services.constants import PVP_TURN_TIMEOUT
|
||||
import os
|
||||
import fcntl
|
||||
@@ -401,7 +402,7 @@ async def check_pvp_combat_timers(manager=None):
|
||||
await db.update_pvp_combat(combat['id'], {
|
||||
'turn': new_turn,
|
||||
'turn_started_at': time.time(),
|
||||
'last_action': f"Turn timeout - {current_turn}'s turn skipped|{time.time()}"
|
||||
'last_action': f"turn_timeout:{current_turn}|{time.time()}"
|
||||
})
|
||||
|
||||
processed += 1
|
||||
@@ -423,10 +424,16 @@ async def check_pvp_combat_timers(manager=None):
|
||||
"turn": new_turn,
|
||||
"time_remaining": time_remaining,
|
||||
"turn_timeout": "skipped",
|
||||
"last_action": f"Turn timeout - {current_turn}'s turn skipped"
|
||||
"last_action": f"turn_timeout:{current_turn}"
|
||||
},
|
||||
"is_pvp": True,
|
||||
"message": f"⏱️ Turn skipped due to timeout!"
|
||||
"messages": [
|
||||
{
|
||||
"type": "combat_timeout",
|
||||
"origin": "system",
|
||||
"timestamp": time.time()
|
||||
}
|
||||
]
|
||||
},
|
||||
"timestamp": time.time()
|
||||
}
|
||||
@@ -519,6 +526,8 @@ async def cleanup_interactable_cooldowns(manager=None, world_locations=None):
|
||||
"data": {
|
||||
"instance_id": cooldown_info['instance_id'],
|
||||
"action_id": cooldown_info['action_id'],
|
||||
"name": cooldown_info['name'],
|
||||
"action_name": cooldown_info['action_name'],
|
||||
"message": f"{cooldown_info['action_name']} is ready on {cooldown_info['name']}"
|
||||
},
|
||||
"timestamp": datetime.utcnow().isoformat()
|
||||
@@ -638,7 +647,7 @@ async def process_status_effects(manager=None):
|
||||
|
||||
while True:
|
||||
try:
|
||||
await asyncio.sleep(300) # Wait 5 minutes
|
||||
await asyncio.sleep(60) # Wait 1 minute (requested by user)
|
||||
|
||||
start_time = time.time()
|
||||
logger.info("Running status effects processor...")
|
||||
@@ -658,28 +667,39 @@ async def process_status_effects(manager=None):
|
||||
|
||||
for player_id in affected_players:
|
||||
try:
|
||||
# Get current status effects (after decrement)
|
||||
effects = await db.get_player_status_effects(player_id)
|
||||
# Get current status effects (after decrement), INCLUDING expired (0 ticks)
|
||||
effects = await db.get_player_status_effects(player_id, min_ticks=0)
|
||||
|
||||
if not effects:
|
||||
continue
|
||||
|
||||
# Calculate total damage
|
||||
from api.game_logic import calculate_status_damage
|
||||
total_damage = calculate_status_damage(effects)
|
||||
# Prepare detailed effects data for frontend
|
||||
effects_data = [
|
||||
{
|
||||
"name": e['effect_name'],
|
||||
"ticks_remaining": e['ticks_remaining'],
|
||||
"effect_icon": e.get('effect_icon')
|
||||
}
|
||||
for e in effects
|
||||
]
|
||||
|
||||
if total_damage > 0:
|
||||
damage_dealt += total_damage
|
||||
# Calculate total impact (positive = damage, negative = healing)
|
||||
from api.game_logic import calculate_status_impact
|
||||
total_impact = calculate_status_impact(effects)
|
||||
|
||||
if total_impact > 0:
|
||||
# DAMAGE LOGIC
|
||||
damage_dealt += total_impact
|
||||
player = await db.get_player_by_id(player_id)
|
||||
|
||||
if not player or player['is_dead']:
|
||||
continue
|
||||
|
||||
new_hp = max(0, player['hp'] - total_damage)
|
||||
new_hp = max(0, player['hp'] - total_impact)
|
||||
|
||||
# Check if player died from status effects
|
||||
if new_hp <= 0:
|
||||
await db.update_player(player_id, {'hp': 0, 'is_dead': True})
|
||||
await db.update_player(player_id, hp=0, is_dead=True)
|
||||
deaths += 1
|
||||
|
||||
# Only create corpse if player has items
|
||||
@@ -701,6 +721,7 @@ async def process_status_effects(manager=None):
|
||||
# Notify player of death
|
||||
if manager:
|
||||
from datetime import datetime
|
||||
locale = player.get('locale', 'en')
|
||||
await manager.send_personal_message(
|
||||
player_id,
|
||||
{
|
||||
@@ -708,7 +729,7 @@ async def process_status_effects(manager=None):
|
||||
"data": {
|
||||
"hp": 0,
|
||||
"is_dead": True,
|
||||
"message": "You died from status effects"
|
||||
"message": get_game_message('diedFromStatus', locale)
|
||||
},
|
||||
"timestamp": datetime.utcnow().isoformat()
|
||||
}
|
||||
@@ -717,10 +738,11 @@ async def process_status_effects(manager=None):
|
||||
logger.info(f"Player {player['name']} (ID: {player_id}) died from status effects")
|
||||
else:
|
||||
# Apply damage and notify player
|
||||
await db.update_player(player_id, {'hp': new_hp})
|
||||
await db.update_player(player_id, hp=new_hp)
|
||||
|
||||
if manager:
|
||||
from datetime import datetime
|
||||
locale = player.get('locale', 'en')
|
||||
await manager.send_personal_message(
|
||||
player_id,
|
||||
{
|
||||
@@ -728,8 +750,44 @@ async def process_status_effects(manager=None):
|
||||
"data": {
|
||||
"hp": new_hp,
|
||||
"max_hp": player['max_hp'],
|
||||
"damage": total_damage,
|
||||
"message": f"You took {total_damage} damage from status effects"
|
||||
"damage": total_impact,
|
||||
"message": get_game_message('statusDamage', locale, damage=total_impact),
|
||||
"effects": effects_data
|
||||
},
|
||||
"timestamp": datetime.utcnow().isoformat()
|
||||
}
|
||||
)
|
||||
elif total_impact < 0:
|
||||
# HEALING LOGIC
|
||||
heal_amount = abs(total_impact)
|
||||
player = await db.get_player_by_id(player_id)
|
||||
|
||||
if not player or player['is_dead']:
|
||||
continue
|
||||
|
||||
# Don't heal if already full
|
||||
if player['hp'] >= player['max_hp']:
|
||||
continue
|
||||
|
||||
new_hp = min(player['max_hp'], player['hp'] + heal_amount)
|
||||
real_heal = new_hp - player['hp']
|
||||
|
||||
if real_heal > 0:
|
||||
await db.update_player(player_id, hp=new_hp)
|
||||
|
||||
if manager:
|
||||
from datetime import datetime
|
||||
locale = player.get('locale', 'en')
|
||||
await manager.send_personal_message(
|
||||
player_id,
|
||||
{
|
||||
"type": "status_effect_heal",
|
||||
"data": {
|
||||
"hp": new_hp,
|
||||
"max_hp": player['max_hp'],
|
||||
"heal": real_heal,
|
||||
"message": get_game_message('statusHeal', locale, heal=real_heal),
|
||||
"effects": effects_data
|
||||
},
|
||||
"timestamp": datetime.utcnow().isoformat()
|
||||
}
|
||||
@@ -738,10 +796,13 @@ async def process_status_effects(manager=None):
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing status effects for player {player_id}: {e}")
|
||||
|
||||
# CLEANUP: Remove expired effects now that we've notified the user
|
||||
await db.clean_expired_status_effects()
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
logger.info(
|
||||
f"Processed status effects for {len(affected_players)} players "
|
||||
f"({damage_dealt} total damage, {deaths} deaths) in {elapsed:.3f}s"
|
||||
f"({damage_dealt} damage, {deaths} deaths) in {elapsed:.3f}s"
|
||||
)
|
||||
|
||||
# Warn if taking too long (potential scaling issue)
|
||||
|
||||
Reference in New Issue
Block a user