Skip to content

Commit

Permalink
fix: bunch of poracle webhook stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Jan 27, 2024
1 parent 854a218 commit fa4e628
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 83 deletions.
31 changes: 22 additions & 9 deletions server/src/graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,16 +507,9 @@ const resolvers = {
}))
}
if (category === 'invasion') {
const { invasions } = Event.masterfile
result.invasion = result.invasion.map((x) => ({
...x,
real_grunt_id:
+Object.keys(invasions).find(
(key) =>
invasions[key]?.type?.toLowerCase() ===
x.grunt_type.toLowerCase() &&
invasions[key].gender === (x.gender || 1),
) || 0,
real_grunt_id: PoracleAPI.getRealGruntId(x, Event.invasions),
}))
}
if (category === 'raid') {
Expand All @@ -525,7 +518,6 @@ const resolvers = {
allMoves: x.move === 9000,
}))
}

return result
}
return {}
Expand Down Expand Up @@ -664,6 +656,27 @@ const resolvers = {
status,
data,
)
if (category === 'pokemon') {
result.pokemon = result.pokemon.map((x) => ({
...x,
allForms: !x.form,
pvpEntry: !!x.pvp_ranking_league,
xs: x.max_weight !== 9000000,
xl: x.min_weight !== 0,
}))
}
if (category === 'invasion') {
result.invasion = result.invasion.map((x) => ({
...x,
real_grunt_id: PoracleAPI.getRealGruntId(x, Event.invasions),
}))
}
if (category === 'raid') {
result.raid = result.raid.map((x) => ({
...x,
allMoves: x.move === 9000,
}))
}
return result
}
return {}
Expand Down
17 changes: 17 additions & 0 deletions server/src/services/api/Poracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,23 @@ class PoracleAPI {
// this.disabledHooks.forEach((hook) => delete poracleUiObj[hook])
return poracleUiObj
}

/**
*
* @param {import('@rm/types').PoracleInvasion} item
* @param {import('@rm/types').Masterfile['invasions']} invasions
*/
static getRealGruntId(item, invasions) {
return (
+Object.keys(invasions).find(
(key) =>
invasions[key]?.type?.toLowerCase() ===
item.grunt_type.toLowerCase() &&
(invasions[key]?.gender === 0 ||
invasions[key].gender === (item.gender || 1)),
) || 0
)
}
}

module.exports = PoracleAPI
110 changes: 61 additions & 49 deletions src/components/layout/dialogs/webhooks/WebhookAdv.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const skipFields = new Set([
'shiny',
'everything_individually',
'all',
'real_grunt_id',
])

const wildCards = {
Expand All @@ -97,6 +98,20 @@ export default function WebhookAdvanced() {
const { pokemon, moves, types } = useMemory((s) => s.masterfile)
const isMobile = useMemory((s) => s.isMobile)

const [filterValues, setFilterValues] = React.useState(
tempFilters?.template
? Poracle.reactMapFriendly(tempFilters)
: {
...Poracle.reactMapFriendly(info?.defaults),
profile_no: human.current_profile_no,
},
)
const [poracleValues, setPoracleValues] = React.useState(
tempFilters?.template
? tempFilters
: { ...info?.defaults, profile_no: human.current_profile_no },
)

Utility.analytics(`/poracle/${category}`)

const [search, { data, previousData, loading }] = useLazyQuery(
Expand All @@ -112,19 +127,6 @@ export default function WebhookAdvanced() {
},
)
const fetchedData = data || previousData
const [filterValues, setFilterValues] = React.useState(
tempFilters?.template
? Poracle.reactMapFriendly(tempFilters)
: {
...Poracle.reactMapFriendly(info?.defaults),
profile_no: human.current_profile_no,
},
)
const [poracleValues, setPoracleValues] = React.useState(
tempFilters?.template
? tempFilters
: { ...info?.defaults, profile_no: human.current_profile_no },
)

React.useEffect(() => {
setPoracleValues(
Expand All @@ -142,6 +144,13 @@ export default function WebhookAdvanced() {
)
}, [tempFilters, id, human.current_profile_no, info?.defaults])

React.useEffect(() => {
setPoracleValues((prev) => ({
...prev,
everything_individually: !!selectedIds.length,
}))
}, [selectedIds])

const handleSlider = React.useCallback(
(low, high) => (name, values) => {
setFilterValues((prev) => ({ ...prev, [name]: values }))
Expand Down Expand Up @@ -758,47 +767,50 @@ export default function WebhookAdvanced() {

const handleClose = (save, filterId, filterToSave) => {
if (save) {
if (filterId === 'global' && filterToSave) {
const newFilters = {}
const wc = wildCards[category] || ['0-0']
if (filterToSave.everything_individually !== false) {
selectedIds.forEach((item) => {
if (!wc.includes(item)) {
useWebhookStore.setState((prev) => {
if (filterId === 'global' && filterToSave) {
const newFilters = {}
const wc = wildCards[category] || ['0-0']
if (filterToSave.everything_individually !== false) {
selectedIds.forEach((item) => {
if (!wc.includes(item)) {
newFilters[item] = {
...prev.tempFilters[item],
...filterToSave,
enabled: true,
}
}
})
} else {
wc.forEach((item) => {
newFilters[item] = {
...tempFilters[item],
...prev.tempFilters[item],
...filterToSave,
enabled: true,
}
}
})
} else {
wc.forEach((item) => {
newFilters[item] = {
...tempFilters[item],
...filterToSave,
enabled: true,
}
})
})
}
return {
tempFilters: {
...prev.tempFilters,
...newFilters,
[filterId]: { ...filterToSave },
},
}
}
useWebhookStore.setState((prev) => ({
tempFilters: {
...prev.tempFilters,
...newFilters,
[filterId]: { ...filterToSave },
},
}))
} else if (filterId && filterToSave) {
useWebhookStore.setState((prev) => ({
tempFilters: {
...prev.tempFilters,
[filterId]: {
...prev.tempFilters[id],
...filterToSave,
enabled: true,
if (filterId && filterToSave) {
return {
tempFilters: {
...prev.tempFilters,
[filterId]: {
...prev.tempFilters[id],
...filterToSave,
enabled: true,
},
},
},
}))
}
}
}
})
} else {
useWebhookStore.setState((prev) => ({
tempFilters: { ...prev.tempFilters, [filterId]: { ...info?.defaults } },
Expand All @@ -818,7 +830,7 @@ export default function WebhookAdvanced() {
icon: 'Save',
},
],
[id, poracleValues],
[id, poracleValues, selectedIds],
)

if (!info || !tempFilters) return null
Expand Down
16 changes: 16 additions & 0 deletions src/components/layout/dialogs/webhooks/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,19 @@ export const setSelected = (id) => () => {
selected: id ? { ...prev.selected, [id]: !prev.selected[id] } : {},
}))
}

/**
* @param {boolean} enabled
* @param {string[]} ids
*/
export const applyToAllWebhooks = (enabled, ids) => {
const selected = new Set(ids)
useWebhookStore.setState((prev) => ({
tempFilters: Object.fromEntries(
Object.entries(prev.tempFilters).map(([k, v]) => [
k,
selected.has(k) ? { ...v, enabled } : v,
]),
),
}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function TrackedTile({ index }) {
},
}))
}
}, [advOpen, id])
}, [advOpen, id, item])

const onClose = React.useCallback(
(newFilter) => {
Expand Down
29 changes: 14 additions & 15 deletions src/components/layout/general/Menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { applyToAll } from '@services/filtering/applyToAll'
import OptionsContainer from '../dialogs/filters/OptionsContainer'
import { VirtualGrid } from './VirtualGrid'
import { GenericSearch } from '../drawer/ItemSearch'
import { useWebhookStore } from '../dialogs/webhooks/store'
import { applyToAllWebhooks, useWebhookStore } from '../dialogs/webhooks/store'

/**
* @param {{
Expand Down Expand Up @@ -86,10 +86,10 @@ export default function Menu({
name: 'apply_to_all',
action: () =>
(webhookCategory ? useWebhookStore : useLayoutStore).setState({
advancedFilter: {
[webhookCategory ? 'advanced' : 'advancedFilter']: {
open: true,
id: 'global',
category,
category: webhookCategory || category,
selectedIds: filteredArr,
},
}),
Expand All @@ -98,24 +98,23 @@ export default function Menu({
{
name: 'disable_all',
action: () =>
applyToAll(
{ enabled: false },
category,
filteredArr,
!webhookCategory,
),
webhookCategory
? applyToAllWebhooks(false, filteredArr)
: applyToAll({ enabled: false }, category, filteredArr),
icon: 'Clear',
color: 'error',
},
{
name: 'enable_all',
action: () =>
applyToAll(
{ enabled: true },
category,
filteredArr,
!webhookCategory,
),
webhookCategory
? applyToAllWebhooks(true, filteredArr)
: applyToAll(
{ enabled: true },
category,
filteredArr,
!webhookCategory,
),
icon: 'Check',
color: 'success',
},
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/useTranslateById.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export function useTranslateById(options = {}) {
if (id === 'global' || id === 'all') {
return i18n.t(id)
}
if (
id === '0-0' ||
id === 'e90' ||
id === 'r90' ||
id === 't4' ||
id === 'i0'
)
return i18n.t('poke_global')
switch (id.charAt(0)) {
case 'b':
// event stops
Expand Down
19 changes: 10 additions & 9 deletions src/services/Poracle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @ts-check
import { t } from 'i18next'
import { useWebhookStore } from '@components/layout/dialogs/webhooks/store'
import { useMemory } from '@hooks/useMemory'

export default class Poracle {
static getMapCategory(poracleCategory) {
Expand Down Expand Up @@ -50,7 +49,6 @@ export default class Poracle {

static getId(item) {
if (!item) return ''
const { invasions } = useMemory.getState().masterfile
const { category } = useWebhookStore.getState()

switch (category) {
Expand All @@ -63,12 +61,7 @@ export default class Poracle {
? 'kecleon'
: item.grunt_type === 'showcase'
? 'showcase'
: `i${Object.keys(invasions).find(
(x) =>
invasions[x].type?.toLowerCase() ===
item.grunt_type.toLowerCase() &&
invasions[x].gender === (item.gender || 1),
)}`
: `i${item.real_grunt_id}`
case 'lure':
return `l${item.lure_id}`
case 'gym':
Expand Down Expand Up @@ -317,7 +310,15 @@ export default class Poracle {
switch (category) {
case 'invasion': {
let name = t(
item.real_grunt_id ? `grunt_${item.real_grunt_id}` : 'poke_global',
item.grunt_type === 'gold-stop'
? 'gold_stop'
: item.grunt_type === 'kecleon'
? 'poke_352'
: item.grunt_type === 'showcase'
? 'showcase'
: item.real_grunt_id
? `grunt_${item.real_grunt_id}`
: 'poke_global',
)
if (!item.gender) name = name.replace(/\(.+?\)/g, `(${t('all')})`)
return `${name}${item.clean ? ` | ${t('clean')} ` : ''}${
Expand Down

0 comments on commit fa4e628

Please sign in to comment.