diff --git a/PLUGINS.md b/PLUGINS.md index e09e8359..d596a337 100644 --- a/PLUGINS.md +++ b/PLUGINS.md @@ -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: diff --git a/companion.py b/companion.py index f1f38bd6..4243f817 100644 --- a/companion.py +++ b/companion.py @@ -680,13 +680,24 @@ 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') - monitor.state['SuitLoadouts'] = suit_loadouts + # 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: """Close CAPI authorization session."""