Skip to content

Commit

Permalink
Merge branch 'release/3.1.0-b1'
Browse files Browse the repository at this point in the history
  • Loading branch information
aussig committed Jul 18, 2023
2 parents abde084 + 0cc2a08 commit c0396c5
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 52 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# Change Log

## v3.1.0-b1 - 2023-07-18

### New Features:

* Thargoid War Revenant kills are now tracked.

### Changes:

* Exploration data tallying now takes into account not just the `TotalEarnings` logged but also the `BaseValue` and `Bonus`. The larger value is used if these differ. Note this is now the same logic that EDDiscovery uses.

### Bug Fixes:

* TW kills were not being logged to the correct system if it was a zero-population system. This was because historically BGST only dealt with BGS logging, so ignored zero-pop systems. We now create tracking entries for these systems.
* TW search and rescue hand-ins were being carried forward to the next tick for systems where items had been both scooped and delivered - escape pods, black boxes and tissue samples. Delivered items are now cleared on a new tick.
* Harden all file loading and JSON parsing to protect against corrupted data on disk.


### API Changes ([v1.1](https://studio-ws.apicur.io/sharing/281a84ad-dca9-42da-a08b-84e4b9af1b7e)):

* `/events` endpoint: `StationFaction` is now an empty string "" when undocked.


## v3.1.0-a3 - 2023-06-23

### Bug Fixes:
Expand Down
64 changes: 36 additions & 28 deletions bgstally/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
CZ_GROUND_LOW_CB_MAX = 5000
CZ_GROUND_MED_CB_MAX = 38000

TW_CBS = {65000: 's', 75000: 's', 6500000: 'c', 20000000: 'b', 25000000: 'o', 34000000: 'm', 50000000: 'h'}
TW_CBS = {25000: 'r', 65000: 's', 75000: 's', 6500000: 'c', 20000000: 'b', 25000000: 'o', 34000000: 'm', 50000000: 'h'}


class Activity:
Expand Down Expand Up @@ -124,9 +124,12 @@ def load(self, filepath: str):
"""
Load an activity file
"""
with open(filepath) as activityfile:
self._from_dict(json.load(activityfile))
self.recalculate_zero_activity()
try:
with open(filepath) as activityfile:
self._from_dict(json.load(activityfile))
self.recalculate_zero_activity()
except Exception as e:
Debug.logger.info(f"Unable to load {filepath}")


def save(self, filepath: str):
Expand Down Expand Up @@ -167,7 +170,9 @@ def clear_activity(self, mission_log: MissionLog):
for faction_name, faction_data in system['Factions'].items():
system['Factions'][faction_name] = self._get_new_faction_data(faction_name, faction_data['FactionState'])
system['TWKills'] = self._get_new_tw_kills_data()
# Note: system['TWSandR'] data is carried forward
# Note: system['TWSandR'] scooped data is carried forward, delivered data is cleared
for d in system['TWSandR'].values():
d['delivered'] = 0
else:
# Delete the whole system
del self.systems[system_address]
Expand All @@ -181,9 +186,6 @@ def system_entered(self, journal_entry: Dict, state: State):
"""
The user has entered a system
"""
try: test = journal_entry['Factions']
except KeyError: return

self.dirty = True
current_system = None

Expand All @@ -200,28 +202,29 @@ def system_entered(self, journal_entry: Dict, state: State):

self._update_system_data(current_system)

for faction in journal_entry['Factions']:
if faction['Name'] == "Pilots' Federation Local Branch": continue
if 'Factions' in journal_entry:
for faction in journal_entry['Factions']:
if faction['Name'] == "Pilots' Federation Local Branch": continue

# Ignore conflict states in FactionState as we can't trust they always come in pairs. We deal with conflicts separately below.
faction_state = faction['FactionState'] if faction['FactionState'] not in STATES_WAR and faction['FactionState'] not in STATES_ELECTION else "None"
# Ignore conflict states in FactionState as we can't trust they always come in pairs. We deal with conflicts separately below.
faction_state = faction['FactionState'] if faction['FactionState'] not in STATES_WAR and faction['FactionState'] not in STATES_ELECTION else "None"

if faction['Name'] in current_system['Factions']:
# We have this faction, ensure it's up to date with latest state
faction_data = current_system['Factions'][faction['Name']]
self._update_faction_data(faction_data, faction_state)
else:
# We do not have this faction, create a new clean entry
current_system['Factions'][faction['Name']] = self._get_new_faction_data(faction['Name'], faction_state)
if faction['Name'] in current_system['Factions']:
# We have this faction, ensure it's up to date with latest state
faction_data = current_system['Factions'][faction['Name']]
self._update_faction_data(faction_data, faction_state)
else:
# We do not have this faction, create a new clean entry
current_system['Factions'][faction['Name']] = self._get_new_faction_data(faction['Name'], faction_state)

# Set war states for pairs of factions in War / Civil War / Elections
for conflict in journal_entry.get('Conflicts', []):
if conflict['Status'] != "active": continue
# Set war states for pairs of factions in War / Civil War / Elections
for conflict in journal_entry.get('Conflicts', []):
if conflict['Status'] != "active": continue

if conflict['Faction1']['Name'] in current_system['Factions'] and conflict['Faction2']['Name'] in current_system['Factions']:
conflict_state = "War" if conflict['WarType'] == "war" else "CivilWar" if conflict['WarType'] == "civilwar" else "Election" if conflict['WarType'] == "election" else "None"
current_system['Factions'][conflict['Faction1']['Name']]['FactionState'] = conflict_state
current_system['Factions'][conflict['Faction2']['Name']]['FactionState'] = conflict_state
if conflict['Faction1']['Name'] in current_system['Factions'] and conflict['Faction2']['Name'] in current_system['Factions']:
conflict_state = "War" if conflict['WarType'] == "war" else "CivilWar" if conflict['WarType'] == "civilwar" else "Election" if conflict['WarType'] == "election" else "None"
current_system['Factions'][conflict['Faction1']['Name']]['FactionState'] = conflict_state
current_system['Factions'][conflict['Faction2']['Name']]['FactionState'] = conflict_state

self.recalculate_zero_activity()
state.current_system_id = str(current_system['SystemAddress'])
Expand Down Expand Up @@ -361,7 +364,12 @@ def exploration_data_sold(self, journal_entry: Dict, state: State):

faction = current_system['Factions'].get(state.station_faction)
if faction:
faction['CartData'] += journal_entry['TotalEarnings']
base_value:int = journal_entry.get('BaseValue', 0)
bonus:int = journal_entry.get('Bonus', 0)
total_earnings:int = journal_entry.get('TotalEarnings', 0)
if total_earnings < base_value + bonus: total_earnings = base_value + bonus

faction['CartData'] += total_earnings
self.recalculate_zero_activity()


Expand Down Expand Up @@ -733,7 +741,7 @@ def _get_new_tw_kills_data(self):
"""
Get a new data structure for storing Thargoid War Kills
"""
return {'s': 0, 'c': 0, 'b': 0, 'm': 0, 'h': 0, 'o': 0}
return {'r': 0, 's': 0, 'c': 0, 'b': 0, 'm': 0, 'h': 0, 'o': 0}


def _get_new_tw_sandr_data(self):
Expand Down
25 changes: 15 additions & 10 deletions bgstally/apimanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from bgstally.activity import Activity
from bgstally.api import API
from bgstally.constants import DATETIME_FORMAT_JOURNAL, FOLDER_DATA
from bgstally.debug import Debug

FILENAME = "apis.json"

Expand Down Expand Up @@ -34,11 +35,14 @@ def load(self):
"""
file:str = path.join(self.bgstally.plugin_dir, FOLDER_DATA, FILENAME)
if path.exists(file):
with open(file) as json_file:
apis_json:list = json.load(json_file)
try:
with open(file) as json_file:
apis_json:list = json.load(json_file)

for api_json in apis_json:
self.apis.append(API(self.bgstally, api_json))
for api_json in apis_json:
self.apis.append(API(self.bgstally, api_json))
except Exception as e:
Debug.logger.info(f"Unable to load {file}")


def save(self):
Expand Down Expand Up @@ -201,12 +205,13 @@ def _build_api_activity(self, activity:Activity, cmdr:str):

if sum(system.get('TWKills', {}).values()) > 0:
api_system['twkills'] = {
'basilisk': system['TWKills']['b'],
'cyclops': system['TWKills']['c'],
'hydra': system['TWKills']['h'],
'medusa': system['TWKills']['m'],
'orthrus': system['TWKills']['o'],
'scout': system['TWKills']['s']
'revenant': system['TWKills'].get('r', 0),
'basilisk': system['TWKills'].get('b', 0),
'cyclops': system['TWKills'].get('c', 0),
'hydra': system['TWKills'].get('h', 0),
'medusa': system['TWKills'].get('m', 0),
'orthrus': system['TWKills'].get('o', 0),
'scout': system['TWKills'].get('s', 0)
}

if sum(int(d['delivered']) for d in system.get('TWSandR', {}).values()) > 0:
Expand Down
4 changes: 4 additions & 0 deletions bgstally/bgstally.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ def journal_entry(self, cmdr, is_beta, system, station, entry, state):
self.target_log.ship_targeted(entry, system)
dirty = True

case 'Undocked':
self.state.station_faction = ""
self.state.station_type = ""

if dirty:
self.save_data()
self.api_manager.send_activity(activity, cmdr)
Expand Down
7 changes: 5 additions & 2 deletions bgstally/fleetcarrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ def load(self):
"""
file = path.join(self.bgstally.plugin_dir, FOLDER_DATA, FILENAME)
if path.exists(file):
with open(file) as json_file:
self._from_dict(json.load(json_file))
try:
with open(file) as json_file:
self._from_dict(json.load(json_file))
except Exception as e:
Debug.logger.info(f"Unable to load {file}")


def save(self):
Expand Down
18 changes: 12 additions & 6 deletions bgstally/missionlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,22 @@ def load(self):
# New location
file = path.join(self.bgstally.plugin_dir, FOLDER_DATA, FILENAME)
if path.exists(file):
with open(file) as json_file:
self.missionlog = json.load(json_file)
return
try:
with open(file) as json_file:
self.missionlog = json.load(json_file)
return
except Exception as e:
Debug.logger.info(f"Unable to load {file}")

# Legacy location
file = path.join(self.bgstally.plugin_dir, FILENAME_LEGACY)
if path.exists(file):
with open(file) as json_file:
self.missionlog = json.load(json_file)
remove(file)
try:
with open(file) as json_file:
self.missionlog = json.load(json_file)
remove(file)
except Exception as e:
Debug.logger.info(f"Unable to load and remove {file}")


def save(self):
Expand Down
7 changes: 5 additions & 2 deletions bgstally/targetlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ def load(self):
"""
file = os.path.join(self.bgstally.plugin_dir, FOLDER_DATA, FILENAME)
if os.path.exists(file):
with open(file) as json_file:
self.targetlog = json.load(json_file)
try:
with open(file) as json_file:
self.targetlog = json.load(json_file)
except Exception as e:
Debug.logger.info(f"Unable to load {file}")


def save(self):
Expand Down
11 changes: 8 additions & 3 deletions bgstally/windows/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,14 @@ def _generate_tw_system_discord_text(self, system:Dict):
if kills > 0 or sandr > 0:
system_discord_text += f"🍀 System activity\n"
if kills > 0:
system_discord_text += f" 💀 (kills): {red('S')} x {green(system['TWKills']['s'])}, {red('C')} x {green(system['TWKills']['c'])}, " \
+ f"{red('B')} x {green(system['TWKills']['b'])}, {red('M')} x {green(system['TWKills']['m'])}, " \
+ f"{red('H')} x {green(system['TWKills']['h'])}, {red('O')} x {green(system['TWKills']['o'])} \n"
system_discord_text += f" 💀 (kills): " \
+ f"{red('R')} x {green(system['TWKills'].get('r', 0))}, " \
+ f"{red('S')} x {green(system['TWKills'].get('s', 0))}, " \
+ f"{red('C')} x {green(system['TWKills'].get('c', 0))}, " \
+ f"{red('B')} x {green(system['TWKills'].get('b', 0))}, " \
+ f"{red('M')} x {green(system['TWKills'].get('m', 0))}, " \
+ f"{red('H')} x {green(system['TWKills'].get('h', 0))}, " \
+ f"{red('O')} x {green(system['TWKills'].get('o', 0))} \n"
if sandr > 0:
system_discord_text += " "
pods:int = system['TWSandR']['dp']['delivered'] + system['TWSandR']['op']['delivered']
Expand Down
2 changes: 1 addition & 1 deletion load.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import semantic_version

PLUGIN_NAME = "BGS-Tally"
PLUGIN_VERSION = semantic_version.Version.coerce("3.1.0-a3")
PLUGIN_VERSION = semantic_version.Version.coerce("3.1.0-b1")

# Initialise the main plugin class
this:BGSTally = BGSTally(PLUGIN_NAME, PLUGIN_VERSION)
Expand Down

0 comments on commit c0396c5

Please sign in to comment.