Skip to content

Commit

Permalink
Store character and system names for combat logs (minmatarfleet#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
silvatek authored Dec 9, 2024
1 parent dfcb926 commit 1b86f83
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
9 changes: 9 additions & 0 deletions backend/combatlog/combatlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class LogAnalysis(BaseModel):
fitting_id: int = None
fleet_id: int = None
character_name: str = None
final_system: str = None
max_from: DamageEvent = None
max_to: DamageEvent = None

Expand Down Expand Up @@ -300,3 +301,11 @@ def max_damage(events: List[DamageEvent], direction: str) -> DamageEvent:
max_event = event
max_dmg = event.damage
return max_event


def final_system_name(events: List[LogEvent]) -> str:
system = ""
for event in events:
if event.location:
system = event.location
return system
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1.2 on 2024-12-09 16:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("combatlog", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="combatlog",
name="character_name",
field=models.CharField(blank=True, max_length=255),
),
migrations.AddField(
model_name="combatlog",
name="solar_system_name",
field=models.CharField(blank=True, max_length=255),
),
]
3 changes: 3 additions & 0 deletions backend/combatlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ class CombatLog(models.Model):
created_at = models.DateTimeField(auto_now_add=True)

log_text = models.TextField()

character_name = models.CharField(max_length=255, blank=True)
solar_system_name = models.CharField(max_length=255, blank=True)
35 changes: 25 additions & 10 deletions backend/combatlog/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
total_damage,
update_combat_time,
weapon_analysis,
final_system_name,
)
from .models import CombatLog

Expand All @@ -33,7 +34,7 @@
@router.post(
"",
description="Process an Eve combat log",
response={200: LogAnalysis, 400: ErrorResponse, 500: ErrorResponse},
response={200: LogAnalysis, 400: ErrorResponse, 403: ErrorResponse},
auth=AuthOptional(),
openapi_extra={
"requestBody": {
Expand All @@ -51,6 +52,7 @@
)
def analyze_logs(
request,
store: bool = False,
fleet_id: int = 0,
fitting_id: int = 0,
start_time: str = "",
Expand All @@ -60,6 +62,14 @@ def analyze_logs(
log.info("Combat log fleet ID = %d, fitting ID = %d", fleet_id, fitting_id)
log.info("Combat log time range = %s to %s", start_time, end_time)

store = store or fleet_id > 0 or fitting_id > 0

if store and not request.user.id:
return 403, ErrorResponse(
status=403,
detail="Cannot store without user ID",
)

if request.content_type == "text/plain":
content = request.body.decode("utf-8")
elif request.content_type == "application/zip":
Expand All @@ -79,19 +89,17 @@ def analyze_logs(

analysis = analyze_parsed_log(content)

if fleet_id > 0 or fitting_id > 0:
combat_log = CombatLog(log_text=content)
if store:
combat_log = CombatLog(
log_text=content,
created_by_id=request.user.id,
character_name=analysis.character_name,
solar_system_name=analysis.final_system,
)
if fleet_id > 0:
combat_log.fleet_id = fleet_id
if fitting_id > 0:
combat_log.fitting_id = fitting_id
if request.user.id:
combat_log.created_by_id = request.user.id
else:
return 500, ErrorResponse(
status=500,
detail="Could not determine user ID",
)

combat_log.save()

Expand All @@ -107,6 +115,7 @@ def analyze_parsed_log(content: str) -> LogAnalysis:
analysis = LogAnalysis()
analysis.logged_events = len(events)
analysis.character_name = character_name(events)
analysis.final_system = final_system_name(events)

dmg_events = damage_events(events)

Expand All @@ -130,6 +139,8 @@ class LogSummary(BaseModel):
user_id: int = None
fleet_id: int = None
fitting_id: int = None
character_name: str = None
system_name: str = None


@router.get(
Expand Down Expand Up @@ -166,6 +177,10 @@ def query_saved_logs(request, user_id: int = None, fleet_id: int = None):
summary.fleet_id = record.fleet_id
if record.fitting_id:
summary.fitting_id = record.fitting_id
if record.character_name:
summary.character_name = record.character_name
if record.solar_system_name:
summary.system_name = record.solar_system_name

results.append(summary)

Expand Down

0 comments on commit 1b86f83

Please sign in to comment.