Skip to content

Commit

Permalink
Initial remote reps processing in combatlogs (minmatarfleet#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
silvatek authored Dec 10, 2024
1 parent 61fa6aa commit 63ff6a3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
83 changes: 78 additions & 5 deletions backend/combatlog/combatlog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import Dict, List
import logging

from pydantic import BaseModel

log = logging.getLogger(__name__)


class LogEvent(BaseModel):
raw_log: str = None
Expand All @@ -22,6 +25,16 @@ class DamageEvent(BaseModel):
text: str = ""


class RepairEvent(BaseModel):
event_time: str = ""
repaired: int = 0
direction: str = ""
rep_type: str = ""
entity: str = ""
location: str = ""
text: str = ""


class DamageAnalysis(BaseModel):
"""Analysis of damage to/from something"""

Expand Down Expand Up @@ -57,6 +70,8 @@ class LogAnalysis(BaseModel):
fleet_id: int = None
character_name: str = None
final_system: str = None
armor_repaired: int = None
shield_repaired: int = None
max_from: DamageEvent = None
max_to: DamageEvent = None

Expand Down Expand Up @@ -105,9 +120,12 @@ def parse(text: str) -> List[LogEvent]:
location = "{unknown}"
events = []
for line in text.splitlines():
event = parse_line(line)
location = update_location(event, location)
events.append(event)
try:
event = parse_line(line)
location = update_location(event, location)
events.append(event)
except Exception:
log.error("Error parsing combat log entry: %s", line)

return events

Expand All @@ -134,8 +152,9 @@ def strip_html(text):
return text


def damage_events(events: List[LogEvent]) -> List[DamageEvent]:
dmg_events = []
def repair_events(events: List[LogEvent]) -> List[RepairEvent]:
rep_events = []

for event in events:
if event.event_type != "combat":
continue
Expand All @@ -148,8 +167,47 @@ def damage_events(events: List[LogEvent]) -> List[DamageEvent]:
if text[0] < "0" or text[0] > "9":
continue

repair_event = RepairEvent()
repair_event.event_time = event.event_time
repair_event.location = event.location

pos = text.find(" remote armor repaired")
if pos >= 0:
repair_event.repaired = int(text[0:pos])
repair_event.direction = "to"
repair_event.rep_type = "armor"
text = text[pos + 20 :]

pos = text.find(" remote shield boosted")
if pos >= 0:
log.info(text)
repair_event.repaired = int(text[0:pos])
repair_event.direction = "to"
repair_event.rep_type = "shield"
text = text[pos + 20 :]

if repair_event.repaired > 0:
rep_events.append(repair_event)

return rep_events


def damage_events(events: List[LogEvent]) -> List[DamageEvent]:
dmg_events = []
for event in events:
if event.event_type != "combat":
continue

text = event.text

if len(text) == 0:
continue
if text[0] < "0" or text[0] > "9":
continue
if text.find("remote armor repaired") >= 0:
continue
if text.find("remote shield boosted") >= 0:
continue

damage_event = DamageEvent()
damage_event.event_time = event.event_time
Expand Down Expand Up @@ -286,6 +344,8 @@ def update_damage_analysis(analysis: DamageAnalysis, event: DamageEvent):


def update_combat_time(events: List[DamageEvent], analysis: LogAnalysis):
if len(events) == 0:
return
analysis.start = events[0].event_time
analysis.end = analysis.start
for event in events:
Expand All @@ -310,3 +370,16 @@ def last_combat_system(events: List[DamageEvent]) -> str:
if event.location:
system = event.location
return system


def total_repaired(events: List[RepairEvent], rep_type: str) -> int:
total = 0

for event in events:
if event.rep_type == rep_type:
total += event.repaired

if total == 0:
return None
else:
return total
6 changes: 6 additions & 0 deletions backend/combatlog/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
update_combat_time,
weapon_analysis,
last_combat_system,
repair_events,
total_repaired,
)
from .models import CombatLog

Expand Down Expand Up @@ -129,6 +131,10 @@ def analyze_parsed_log(content: str) -> LogAnalysis:
analysis.max_from = max_damage(dmg_events, "from")
analysis.max_to = max_damage(dmg_events, "to")

repairs = repair_events(events)
analysis.armor_repaired = total_repaired(repairs, "armor")
analysis.shield_repaired = total_repaired(repairs, "shield")

update_combat_time(dmg_events, analysis)

return analysis
Expand Down

0 comments on commit 63ff6a3

Please sign in to comment.