Skip to content

Commit

Permalink
Implementation complete of synthetic events
Browse files Browse the repository at this point in the history
Closes #288
  • Loading branch information
aussig committed Dec 31, 2024
1 parent 56e192d commit 47aef92
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
### API Changes ([v1.6](https://studio-ws.apicur.io/sharing/xxxxxxxxxxxxxxxxxxxxxxxxxxxx)):

* New `/objectives` endpoint.
* `/events` endpoint: Synthetic events added for certain activities in game that the game itself doesn't log in the journal:
- `SyntheticCZ`: Sent when a Space CZ is won.
- `SyntheticCZObjective`: Sent when an objective is completed in a Space CZ (cap ship / spec ops / enemy captain / enemy correspondent).
- `SyntheticGroundCZ`: Sent when a Ground CZ is won.
- `SyntheticScenario`: Sent when a scenario is won (only Megaship scenarios for the moment, Installation scenarios cannot be tracked).


## v4.2.0 - 2024-12-22
Expand Down
5 changes: 4 additions & 1 deletion bgstally/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,14 @@ class Activity:
factions with their activity
"""

def __init__(self, bgstally, tick: Tick = None, sample: bool = False):
def __init__(self, bgstally, tick: Tick = None, sample: bool = False, cmdr = None):
"""Constructor
Args:
bgstally (BGSTally): The BGSTally object
tick (Tick, optional): The Tick object to instantiate from. If None, the last known tick is used. Defaults to None.
sample (bool, optional): Populate with sample data. Defaults to False.
cmdr (str, optional): The CMDR name. This is not done properly (yet) - the cmdr name is simply updated often to be the latest cmdr seen.
"""
self.bgstally = bgstally
if tick == None: tick = Tick(self.bgstally)
Expand All @@ -145,6 +146,8 @@ def __init__(self, bgstally, tick: Tick = None, sample: bool = False):
self.discord_notes: str = ""
self.dirty: bool = False

self.cmdr: str = cmdr # Not saved / loaded (yet) because it's not implemented properly

if sample:
self.systems: dict = {"Sample System ID": self.get_sample_system_data()}
else:
Expand Down
2 changes: 1 addition & 1 deletion bgstally/apimanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def _build_api_event(self, event:dict, activity:Activity, cmdr:str, mission:dict
if 'StationFaction' not in event: event['StationFaction'] = {'Name': self.bgstally.state.station_faction}
if 'StarSystem' not in event: event['StarSystem'] = get_by_path(activity.systems, [self.bgstally.state.current_system_id, 'System'], "")
if 'SystemAddress' not in event: event['SystemAddress'] = self.bgstally.state.current_system_id
if 'timestamp' not in event: event['timestamp'] = datetime.now(UTC).strftime(DATETIME_FORMAT_API),
if 'timestamp' not in event: event['timestamp'] = datetime.now(UTC).strftime(DATETIME_FORMAT_API)

# Event-specific enhancements
match event.get('event'):
Expand Down
6 changes: 6 additions & 0 deletions bgstally/bgstally.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ def journal_entry(self, cmdr, is_beta, system, station, entry, state):
return

activity: Activity = self.activity_manager.get_current_activity()

# Total hack for now. We need cmdr in Activity to allow us to send it to the API when the user changes values in the UI.
# What **should** happen is each Activity object should be associated with a single CMDR, and then all reporting
# kept separate per CMDR.
activity.cmdr = cmdr

dirty: bool = False

if entry.get('event') in ['StartUp', 'Location', 'FSDJump', 'CarrierJump']:
Expand Down
57 changes: 42 additions & 15 deletions bgstally/windows/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

from ttkHyperlinkLabel import HyperlinkLabel

from bgstally.activity import STATES_WAR, STATES_ELECTION, Activity
from bgstally.constants import (COLOUR_HEADING_1, FOLDER_ASSETS, FONT_HEADING_1, FONT_HEADING_2, FONT_TEXT, CheckStates, CZs, DiscordActivity, DiscordChannel,
DiscordPostStyle)
from bgstally.activity import STATES_ELECTION, STATES_WAR, Activity
from bgstally.constants import (COLOUR_HEADING_1, FOLDER_ASSETS, FONT_HEADING_1, FONT_HEADING_2, FONT_TEXT, ApiSizeLookup, ApiSyntheticEvent, CheckStates, CZs,
DiscordActivity, DiscordChannel, DiscordPostStyle)
from bgstally.debug import Debug
from bgstally.formatters.base import BaseActivityFormatterInterface
from bgstally.utils import _, __, human_format
Expand Down Expand Up @@ -560,18 +560,45 @@ def _cz_change(self, notebook: ScrollableNotebook, tab_index: int, CZVar: tk.Int
"""
Callback (set as a variable trace) for when a CZ Variable is changed
"""
if cz_type == CZs.SPACE_LOW:
faction['SpaceCZ']['l'] = CZVar.get()
elif cz_type == CZs.SPACE_MED:
faction['SpaceCZ']['m'] = CZVar.get()
elif cz_type == CZs.SPACE_HIGH:
faction['SpaceCZ']['h'] = CZVar.get()
elif cz_type == CZs.GROUND_LOW:
faction['GroundCZ']['l'] = CZVar.get()
elif cz_type == CZs.GROUND_MED:
faction['GroundCZ']['m'] = CZVar.get()
elif cz_type == CZs.GROUND_HIGH:
faction['GroundCZ']['h'] = CZVar.get()
match cz_type:
case CZs.SPACE_LOW:
event_type: ApiSyntheticEvent = ApiSyntheticEvent.CZ
event_size: str = ApiSizeLookup['l']
event_diff: int = int(CZVar.get()) - int(faction['SpaceCZ']['l'])
faction['SpaceCZ']['l'] = CZVar.get()
case CZs.SPACE_MED:
event_type: ApiSyntheticEvent = ApiSyntheticEvent.CZ
event_size: str = ApiSizeLookup['m']
event_diff: int = int(CZVar.get()) - int(faction['SpaceCZ']['m'])
faction['SpaceCZ']['m'] = CZVar.get()
case CZs.SPACE_HIGH:
event_type: ApiSyntheticEvent = ApiSyntheticEvent.CZ
event_size: str = ApiSizeLookup['h']
event_diff: int = int(CZVar.get()) - int(faction['SpaceCZ']['h'])
faction['SpaceCZ']['h'] = CZVar.get()
case CZs.GROUND_LOW:
event_type: ApiSyntheticEvent = ApiSyntheticEvent.GROUNDCZ
event_size: str = ApiSizeLookup['l']
event_diff: int = int(CZVar.get()) - int(faction['GroundCZ']['l'])
faction['GroundCZ']['l'] = CZVar.get()
case CZs.GROUND_MED:
event_type: ApiSyntheticEvent = ApiSyntheticEvent.GROUNDCZ
event_size: str = ApiSizeLookup['m']
event_diff: int = int(CZVar.get()) - int(faction['GroundCZ']['m'])
faction['GroundCZ']['m'] = CZVar.get()
case CZs.GROUND_HIGH:
event_type: ApiSyntheticEvent = ApiSyntheticEvent.GROUNDCZ
event_size: str = ApiSizeLookup['h']
event_diff: int = int(CZVar.get()) - int(faction['GroundCZ']['h'])
faction['GroundCZ']['h'] = CZVar.get()

# Send to API
event: dict = {
'event': event_type,
event_size: event_diff,
'Faction': faction['Faction']
}
if activity.cmdr is not None: self.bgstally.api_manager.send_event(event, activity, activity.cmdr)

activity.recalculate_zero_activity()
self._update_tab_image(notebook, tab_index, EnableAllCheckbutton, system)
Expand Down

0 comments on commit 47aef92

Please sign in to comment.