1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-18 18:07:37 +03:00

Suits/Loadouts: Always convert to dict form for simpler downstream code.

This commit is contained in:
Athanasius 2021-04-29 11:30:55 +01:00
parent addb178d3d
commit bd1b86d3a3
2 changed files with 20 additions and 10 deletions

View File

@ -537,16 +537,15 @@ Content of `state` (updated to the current journal entry):
| `Data` | `dict` | 'Data' MicroResources in Odyssey, `int` count each. |
| `BackPack` | `dict` | `dict` of Odyssey MicroResources in backpack. |
| `SuitCurrent` | `dict` | CAPI-returned data of currently worn suit. NB: May be `None` if no data. |
| `Suits` | `dict` or `list`[1] | CAPI-returned data of owned suits. NB: Type depends on if array is sparse or not. May be `None` if no data. |
| `Suits` | `dict`[1] | CAPI-returned data of owned suits. NB: May be `None` if no data. |
| `SuitLoadoutCurrent` | `dict` | CAPI-returned data of current Suit Loadout. NB: May be `None` if no data. |
| `SuitLoadouts` | `dict` | CAPI-returned data of all Suit Loadouts. NB: May be `None` if no data. |
| `SuitLoadouts` | `dict`[1] | CAPI-returned data of all Suit Loadouts. NB: May be `None` if no data. |
[1] - With `Suits` there's a caveat depending on all the slots from 0 are
contiguously used or not. If they are then the type is `list` and
indexing is purely numeric. If not then it's a `dict` and you need to index it
with a string. There is `companion.index_possibly_sparse_list()` to aid
with this, or if you wish to iterate over it use `companion.listify()` to
fill in any gaps and have a `list` to operate on.
[1] - Some data from the CAPI is sometimes returned as a `list` (when all
members are present) and other times as an integer-keyed `dict` (when at
least one member is missing, so the indices are not contiguous). We choose to
always convert to the integer-keyed `dict` form so that code utilising the data
is simpler.
New in version 4.1.6:

View File

@ -680,12 +680,23 @@ class Session(object):
return
monitor.state['SuitCurrent'] = current_suit
monitor.state['Suits'] = data.get('suits')
# It's easier to always have this in the 'sparse array' dict form
suits = data.get('suits')
if isinstance(suits, list):
monitor.state['Suits'] = dict(enumerate(suits))
else:
monitor.state['Suits'] = suits
if (suit_loadouts := data.get('loadouts')) is None:
logger.warning('CAPI data had "suit" but no (suit) "loadouts"')
monitor.state['SuitLoadoutCurrent'] = data.get('loadout')
# It's easier to always have this in the 'sparse array' dict form
if isinstance(suit_loadouts, list):
monitor.state['SuitLoadouts'] = dict(enumerate(suit_loadouts))
else:
monitor.state['SuitLoadouts'] = suit_loadouts
def close(self) -> None: