-
Notifications
You must be signed in to change notification settings - Fork 5
/
selectors.es
312 lines (274 loc) · 9.55 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import memoize from 'fast-memoize'
import { createSelector } from 'reselect'
import _, { sum, flatten, get } from 'lodash'
import { arraySum, arrayAdd, arrayMultiply } from 'views/utils/tools'
import {
constSelector,
fleetShipsIdSelectorFactory,
fleetShipsDataSelectorFactory,
fleetShipsEquipDataSelectorFactory,
extensionSelectorFactory,
} from 'views/utils/selectors'
const REDUCER_EXTENSION_KEY = 'poi-plugin-expedition'
const isDrum = equipData => get(equipData, [1, 'api_id'], -1) === 75
const shipNotHeavilyDamaged = ship => ship.api_nowhp * 4 >= ship.api_maxhp
// for toku daihatsu 特大発動艇, the calculation is seperated into 2 parts
// the former is to see it as normal daihatsu (5%)
// the latter is to calculate extra bonus introduced by itself
const bonusByItem = {
68: 5, // 大発動艇
166: 2, // 大発動艇(八九式中戦車&陸戦隊)
167: 1, // 特二式内火艇
193: 5, // 特大発動艇
}
// Kinu Kai 2
const bonusByShip = {
487: 5,
}
// calculates the bonus brought by ship herself
const shipFactor = constIds =>
constIds.reduce(
(factor, id) =>
factor + (bonusByShip[id] || 0)
, 0
)
const bonusItem = [193]
// calculates extra bonus from toku daihatsu, in percentage
const bonusFactor = (shipsEquipData) => {
const bonusCount = _(shipsEquipData)
.flatten()
.filter(equip => bonusItem.includes(get(equip, [1, 'api_id'], 0)))
.size()
switch (true) {
case (bonusCount === 3):
return 5
case (bonusCount >= 4):
return 5.4
case (bonusCount < 0):
return 0
default:
return bonusCount * 2
}
}
const fleetShipCountSelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsIdSelectorFactory(fleetId),
shipsId =>
(shipsId == null ? 0 : shipsId.length)
))
const fleetFlagshipLvSelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsDataSelectorFactory(fleetId),
shipsData => get(shipsData, [0, 0, 'api_lv'], 0)
))
const fleetTotalLvSelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsDataSelectorFactory(fleetId),
shipsData => _(shipsData).map(ship => get(ship, [0, 'api_lv'], 0)).sum()
))
const fleetTotalASWSelectorFactory = memoize(fleetId =>
createSelector([
fleetShipsDataSelectorFactory(fleetId),
fleetShipsEquipDataSelectorFactory(fleetId),
],
(shipsData, shipsEquipData = []) => {
const aws = _(shipsData).map(ship => get(ship, [0, 'api_taisen', 0], 0)).sum()
const equipData = _(shipsEquipData)
.flatten()
.filter(equip => [10, 11, 41].includes(get(equip, [1, 'api_type', 2])))
const extra = equipData
.sumBy(equip => get(equip, [1, 'api_tais'], 0))
return aws - extra
}
))
const fleetTotalAASelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsDataSelectorFactory(fleetId),
shipsData => _(shipsData).map(ship => get(ship, [0, 'api_taiku', 0], 0)).sum()
))
const fleetTotalLOSSelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsDataSelectorFactory(fleetId),
shipsData => _(shipsData).map(ship => get(ship, [0, 'api_sakuteki', 0], 0)).sum()
))
const fleetTotalFirepowerSelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsDataSelectorFactory(fleetId),
shipsData => _(shipsData).map(ship => get(ship, [0, 'api_karyoku', 0], 0)).sum()
))
const fleetShipsTypeSelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsDataSelectorFactory(fleetId),
shipsData => _(shipsData).map(ship => get(ship, [1, 'api_stype'], 0)).value()
))
const fleetFlagshipTypeSelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsTypeSelectorFactory(fleetId),
shipsType => shipsType[0] || 0
))
// Returns the total number of drums equipped in the fleet
const fleetDrumCountSelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsEquipDataSelectorFactory(fleetId),
shipsEquipData => _(shipsEquipData).flatten().filter(isDrum).size()
))
// Returns the total number of ships with a drum equipped in the fleet
const fleetDrumCarrierCountSelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsEquipDataSelectorFactory(fleetId),
shipsEquipData => _(shipsEquipData).filter(equips => _(equips).some(isDrum)).size()
))
// Returns false if the flagship is heavily damaged
const fleetFlagshipHealthySelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsDataSelectorFactory(fleetId),
shipsData => shipNotHeavilyDamaged(get(shipsData, [0, 0], {}))
))
const shipFullyResupplied = ([ship, $ship] = []) => (!ship || !$ship)
? true
: ship.api_fuel >= $ship.api_fuel_max && ship.api_bull >= $ship.api_bull_max
// Returns false if any ship is not fully resupplied
const fleetFullyResuppliedSelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsDataSelectorFactory(fleetId),
shipsData => _(shipsData).every(shipFullyResupplied)
))
const getShipMaxResupply = ([, $ship] = []) =>
!$ship ? [0, 0] : [$ship.api_fuel_max, $ship.api_bull_max]
// Returns [fuel, bull] consumed to fully resupply every ship from empty
const fleetMaxResupplySelectorFactory = memoize(fleetId =>
createSelector(
fleetShipsDataSelectorFactory(fleetId),
shipsData => arraySum(_(shipsData).map(getShipMaxResupply).value())
))
const fleetConstShipIdSelectorFactory = memoize(fleetId =>
createSelector(
[
fleetShipsDataSelectorFactory(fleetId),
], ships => _(ships).map(([ship] = []) => get(ship, 'api_id', 0)).value()
))
// Returns the bonus percentage brought by landing crafts
// e.g. 20 means 20% bonus, or x1.2 factor
const fleetLandingCraftFactorSelectorFactory = memoize(fleetId =>
createSelector(
[
fleetShipsEquipDataSelectorFactory(fleetId),
fleetConstShipIdSelectorFactory(fleetId),
],
(shipsEquipData = [], shipIds) => {
const data = _(shipsEquipData)
.flatten()
.filter(equip => bonusByItem[get(equip, [1, 'api_id'])] || 0)
const itemBonus = data
.sumBy(equip => bonusByItem[get(equip, [1, 'api_id'])] || 0)
const levels = data
.sumBy(equip => get(equip, [0, 'api_level'], 0))
const baseFactor = Math.min(itemBonus + shipFactor(shipIds), 20)
const avgStars = levels === 0 ? 0 : levels / data.size()
const starFactor = (avgStars / 100) * baseFactor
const bonus = bonusFactor(shipsEquipData)
return {
base: baseFactor,
star: starFactor,
bonus,
}
}
))
export const fleetPropertiesSelectorFactory = memoize(fleetId => createSelector([
fleetShipCountSelectorFactory(fleetId),
fleetFlagshipLvSelectorFactory(fleetId),
fleetTotalLvSelectorFactory(fleetId),
fleetShipsTypeSelectorFactory(fleetId),
fleetFlagshipTypeSelectorFactory(fleetId),
fleetDrumCountSelectorFactory(fleetId),
fleetDrumCarrierCountSelectorFactory(fleetId),
fleetFlagshipHealthySelectorFactory(fleetId),
fleetFullyResuppliedSelectorFactory(fleetId),
fleetTotalASWSelectorFactory(fleetId),
fleetTotalAASelectorFactory(fleetId),
fleetTotalLOSSelectorFactory(fleetId),
fleetTotalFirepowerSelectorFactory(fleetId),
], (
shipCount, flagshipLv, totalLv, shipsType, flagshipType,
drumCount, drumCarrierCount, flagshipHealthy, fullyResupplied,
totalAWS, totalAA, totalLOS, totalFirepower,
) => ({
shipCount,
flagshipLv,
totalLv,
shipsType,
flagshipType,
drumCount,
drumCarrierCount,
flagshipHealthy,
fullyResupplied,
totalAWS,
totalAA,
totalLOS,
totalFirepower,
})))
// Returns [f1, f2, f3] where fx is the fleetProperties of fleet x
// Notice that you should use the form "result[fleetId-1]"
export const fleetsPropertiesSelectorFactory = createSelector([
fleetPropertiesSelectorFactory(1),
fleetPropertiesSelectorFactory(2),
fleetPropertiesSelectorFactory(3),
], (f1, f2, f3) => [f1, f2, f3])
export const expeditionDataSelector = createSelector(
extensionSelectorFactory(REDUCER_EXTENSION_KEY),
(state = {}) => state.expeditions || {}
)
export const SupportExpeditionData = {
reward_fuel: 0,
reward_bullet: 0,
reward_steel: 0,
reward_alum: 0,
reward_items: [],
flagship_lv: 0,
fleet_lv: 0,
flagship_shiptype: 0,
ship_count: 2,
drum_ship_count: 0,
drum_count: 0,
required_shiptypes: [
{
shiptype: [2],
count: 2,
},
],
big_success: null,
}
// Returns [ normalRewards, greatRewards ]
// where rewards := [ fuel, ammo, steel, bauxite ]
// with errs, landing crafts and resupplies taken into account
export const fleetExpeditionRewardsSelectorFactory = memoize((fleetId, expeditionId) =>
createSelector([
constSelector,
expeditionDataSelector,
fleetLandingCraftFactorSelectorFactory(fleetId),
fleetMaxResupplySelectorFactory(fleetId),
], ({ $missions: $expeditions = {} }, expeditions, lcFactor, maxResupply) => {
const $expedition = $expeditions[expeditionId]
if (!$expedition) {
return [[0, 0, 0, 0], [0, 0, 0, 0]]
}
const expedition = expeditions[expeditionId] || SupportExpeditionData
const baseRewards =
['reward_fuel', 'reward_bullet', 'reward_steel', 'reward_alum']
.map(key => expedition[key])
const { base, star, bonus } = lcFactor
const totalFactor = sum([base, star, bonus])
const lcRewards = arrayMultiply(baseRewards, 1 + (totalFactor / 100))
const resupply = [
-maxResupply[0] * $expedition.api_use_fuel,
-maxResupply[1] * $expedition.api_use_bull,
0,
0]
const normalRewards = arrayAdd(lcRewards, resupply).map(Math.floor)
const greatRewards = arrayAdd(arrayMultiply(lcRewards, 1.5), resupply).map(Math.floor)
return {
normalRewards,
greatRewards,
lcFactor,
}
}))