-
Notifications
You must be signed in to change notification settings - Fork 3
/
selectors.es
137 lines (123 loc) · 3.79 KB
/
selectors.es
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { createSelector } from 'reselect'
import _, { get, keyBy } from 'lodash'
import { toRomaji } from 'wanakana'
import { configSelector } from 'views/utils/selectors'
const $shipsSelector = state => get(state, ['const', '$ships'], {})
const graphsSelector = state =>
get(state, ['const', '$graphs']) || keyBy(get(state, ['const', '$shipgraph']), 'api_id')
const fleetsSelector = state => get(state, ['info', 'fleets'], [])
const shipsSelector = state => get(state, ['info', 'ships'], {})
const ourShipsSelector = createSelector(
[$shipsSelector],
ships =>
_(ships)
.pickBy(({ api_sortno }) => Boolean(api_sortno))
.value(),
)
export const shipDataSelector = createSelector(
[ourShipsSelector, graphsSelector],
(ships, graphs) =>
_(ships)
.mapValues(ship => ({
...(graphs[ship.api_id] || {}),
...ship,
romaji: toRomaji(ship.api_yomi),
}))
.value(),
)
export const shipFleetMapSelector = createSelector(
[fleetsSelector, shipsSelector],
(fleets, ships) =>
_(fleets)
.filter(Boolean)
.flatMap(fleet =>
_(fleet.api_ship)
.filter(id => id > 0)
.map(id => [get(ships, [id, 'api_ship_id']), fleet.api_id])
.value(),
)
.fromPairs()
.value(),
)
export const fleetShipDataSelector = createSelector(
[fleetsSelector, shipsSelector, shipDataSelector],
(fleets, ships, shipData) =>
_(fleets)
.filter(Boolean)
.flatMap(fleet =>
_(fleet.api_ship)
.filter(id => id > 0)
.map(id => [id, get(ships, [id, 'api_ship_id'])])
.map(([id, shipId]) => [id, get(shipData, shipId)])
.value(),
)
.fromPairs()
.value(),
)
export const secretaryShipIdSelector = createSelector(
[fleetsSelector, shipsSelector],
(fleets, ships) => get(ships, [get(fleets, [0, 'api_ship', 0], 0), 'api_ship_id'], 0),
)
const beforeShipMapSelector = createSelector(
[ourShipsSelector],
$ships =>
_($ships)
.filter(ship => +(ship.api_aftershipid || 0) > 0)
.map(ship => [ship.api_aftershipid, ship.api_id])
.fromPairs()
.value(),
)
// the chain starts from each ship, thus incomplete if the ship is not the starting one
// the adjustedRemodelChainsSelector will return complete chains for all ships
const remodelChainsSelector = createSelector(
[ourShipsSelector],
$ships =>
_($ships)
.mapValues(({ api_id: shipId }) => {
let current = $ships[shipId]
let next = +(current.api_aftershipid || 0)
let same = [shipId]
while (!same.includes(next) && next > 0) {
same = [...same, next]
current = $ships[next] || {}
next = +(current.api_aftershipid || 0)
}
return same
})
.value(),
)
export const uniqueShipIdsSelector = createSelector(
[ourShipsSelector, beforeShipMapSelector],
($ships, beforeShipMap) =>
_($ships)
.filter(({ api_id }) => !(api_id in beforeShipMap))
.map(({ api_id }) => api_id)
.value(),
)
export const shipUniqueMapSelector = createSelector(
[uniqueShipIdsSelector, remodelChainsSelector],
(shipIds, chains) =>
_(shipIds)
.flatMap(shipId =>
_(chains[shipId])
.map(id => [id, shipId])
.value(),
)
.fromPairs()
.value(),
)
export const adjustedRemodelChainsSelector = createSelector(
[remodelChainsSelector, shipUniqueMapSelector],
(remodelChains, uniqueMap) =>
_(uniqueMap)
.mapValues(uniqueId => remodelChains[uniqueId])
.value(),
)
export const notifySecretaryIdSelector = createSelector(
[configSelector],
config => _.get(config, 'plugin.secretary.ship', 0),
)
export const enableHoulyVoiceSelector = createSelector(
[configSelector],
config => _.get(config, 'plugin.secretary.hourly_voice_enable', false),
)