Skip to content

Commit

Permalink
serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
inker committed Jan 14, 2024
1 parent 9c81804 commit 5bb2b11
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 58 deletions.
96 changes: 90 additions & 6 deletions src/model/WorkerData.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,103 @@
import { type FixedArray } from './types'

export interface WorkerMessage<T> {
correlationId: any
data: T
}

export interface GsWorkerData<T> {
interface GsWorkerData<T> {
season: number
pots: readonly (readonly T[])[]
groups: readonly (readonly T[])[]
selectedTeam: T
}

export interface GsWorkerDataSerialized<T> {
season: number
teams: readonly T[]
pots: readonly (readonly number[])[]
groups: readonly (readonly number[])[]
selectedTeam: number
}

export const serializeGsWorkerData = <T>(
data: GsWorkerData<T>,
): GsWorkerDataSerialized<T> => {
const teams = [data.selectedTeam, ...data.pots.flat(), ...data.groups.flat()]

const indexByTeam = new Map(teams.map((item, i) => [item, i] as const))

const getIndexByTeam = (item: T) => indexByTeam.get(item)!

return {
...data,
teams,
pots: data.pots.map(pot => pot.map(getIndexByTeam)),
groups: data.groups.map(pot => pot.map(getIndexByTeam)),
selectedTeam: getIndexByTeam(data.selectedTeam),
}
}

export const deserializeGsWorkerData = <T>(
data: GsWorkerDataSerialized<T>,
): GsWorkerData<T> => {
const {
teams,
pots: potsSerialized,
groups: groupsSerialized,
selectedTeam: selectedTeamIndex,
} = data

return {
...data,
pots: potsSerialized.map(pot => pot.map(i => teams[i])),
groups: groupsSerialized.map(group => group.map(i => teams[i])),
selectedTeam: teams[selectedTeamIndex],
}
}

export interface KoWorkerData<T> {
season: number
pots: FixedArray<readonly T[], 2>
matchups: readonly (readonly [T, T])[]
}

export interface KoWorkerDataSerialized<T> {
season: number
teams: readonly T[]
pots: FixedArray<readonly number[], 2>
matchups: readonly (readonly [number, number])[]
}

export const serializeKoWorkerData = <T>(
data: KoWorkerData<T>,
): KoWorkerDataSerialized<T> => {
const teams = [...data.pots.flat(), ...data.matchups.flat()]

const indexByTeam = new Map(teams.map((item, i) => [item, i] as const))

const getIndexByTeam = (item: T) => indexByTeam.get(item)!

return {
...data,
teams,
pots: [data.pots[0].map(getIndexByTeam), data.pots[1].map(getIndexByTeam)],
matchups: data.matchups.map(
matchup =>
[getIndexByTeam(matchup[0]), getIndexByTeam(matchup[1])] as const,
),
}
}

export const deserializeKoWorkerData = <T>(
data: KoWorkerDataSerialized<T>,
): KoWorkerData<T> => {
const { teams, pots: potsSerialized, matchups: matchupsSerialized } = data

return {
...data,
pots: [
potsSerialized[0].map(i => teams[i]),
potsSerialized[1].map(i => teams[i]),
],
matchups: matchupsSerialized.map(matchup => [
teams[matchup[0]],
teams[matchup[1]],
]),
}
}
9 changes: 6 additions & 3 deletions src/pages/cl/gs/allPossibleGroupsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import memoizeOne from 'memoize-one'
import { allPossibleGroups } from 'engine/backtracking/gs'
import getPredicate from 'engine/predicates/uefa/gs'
import type Team from 'model/team/GsTeam'
import { type GsWorkerData } from 'model/WorkerData'
import {
type GsWorkerDataSerialized,
deserializeGsWorkerData,
} from 'model/WorkerData'
import exposeWorker, { type ExposedFuncType } from 'utils/worker/expose'

type GetPredicateParams = Parameters<typeof getPredicate>
Expand All @@ -19,8 +22,8 @@ const eqFunc = (newArgs: GetPredicateParams, oldArgs: GetPredicateParams) =>

const getPredicateMemoized = memoizeOne(getPredicate, eqFunc)

const func = (data: GsWorkerData<Team>) => {
const { season, pots, groups, selectedTeam } = data
const func = (data: GsWorkerDataSerialized<Team>) => {
const { season, pots, groups, selectedTeam } = deserializeGsWorkerData(data)

const predicate = getPredicateMemoized(season, pots.length)
return allPossibleGroups({
Expand Down
9 changes: 6 additions & 3 deletions src/pages/cl/gs/firstPossibleGroupWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import memoizeOne from 'memoize-one'
import { firstPossibleGroup } from 'engine/backtracking/gs'
import getPredicate from 'engine/predicates/uefa/gs'
import type Team from 'model/team/GsTeam'
import { type GsWorkerData } from 'model/WorkerData'
import {
type GsWorkerDataSerialized,
deserializeGsWorkerData,
} from 'model/WorkerData'
import exposeWorker, { type ExposedFuncType } from 'utils/worker/expose'

type GetPredicateParams = Parameters<typeof getPredicate>
Expand All @@ -19,8 +22,8 @@ const eqFunc = (newArgs: GetPredicateParams, oldArgs: GetPredicateParams) =>

const getPredicateMemoized = memoizeOne(getPredicate, eqFunc)

const func = (data: GsWorkerData<Team>) => {
const { season, pots, groups, selectedTeam } = data
const func = (data: GsWorkerDataSerialized<Team>) => {
const { season, pots, groups, selectedTeam } = deserializeGsWorkerData(data)

const predicate = getPredicateMemoized(season, pots.length)
return firstPossibleGroup({
Expand Down
29 changes: 17 additions & 12 deletions src/pages/cl/gs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { css } from 'styled-components'
import { random, sample, shuffle } from 'lodash'

import type Team from 'model/team/GsTeam'
import { serializeGsWorkerData } from 'model/WorkerData'

import usePopup from 'store/usePopup'
import useDrawId from 'store/useDrawId'
Expand Down Expand Up @@ -116,20 +117,24 @@ function CLGS({ season, pots: initialPots, isFirstPotShortDraw }: Props) {
let newPossibleGroups: number[]
try {
if (isDrawShort) {
const firstPossibleGroup = await getFirstPossibleGroupResponse({
season,
pots,
groups,
selectedTeam,
})
const firstPossibleGroup = await getFirstPossibleGroupResponse(
serializeGsWorkerData({
season,
pots,
groups,
selectedTeam,
}),
)
newPossibleGroups = [firstPossibleGroup]
} else {
const allPossibleGroups = await getAllPossibleGroupsResponse({
season,
pots,
groups,
selectedTeam,
})
const allPossibleGroups = await getAllPossibleGroupsResponse(
serializeGsWorkerData({
season,
pots,
groups,
selectedTeam,
}),
)
newPossibleGroups = allPossibleGroups
}
} catch (err) {
Expand Down
13 changes: 8 additions & 5 deletions src/pages/cl/ko/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { random, shuffle } from 'lodash'

import { type FixedArray } from 'model/types'
import type Team from 'model/team/KnockoutTeam'
import { serializeKoWorkerData } from 'model/WorkerData'

import usePopup from 'store/usePopup'
import useDrawId from 'store/useDrawId'
Expand Down Expand Up @@ -80,11 +81,13 @@ function CLKO({ season, pots: initialPots }: Props) {
newMatchups: readonly [Team, Team][],
) => {
try {
return await getPossiblePairingsResponse({
season,
pots: newPots,
matchups: newMatchups,
})
return await getPossiblePairingsResponse(
serializeKoWorkerData({
season,
pots: newPots,
matchups: newMatchups,
}),
)
} catch (err) {
setPopup({
error: 'Could not determine possible pairings',
Expand Down
9 changes: 6 additions & 3 deletions src/pages/cl/ko/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import memoizeOne from 'memoize-one'
import { getPossiblePairings } from 'engine/backtracking/ko'
import getPredicate from 'engine/predicates/uefa/ko'
import type Team from 'model/team/KnockoutTeam'
import { type KoWorkerData } from 'model/WorkerData'
import {
type KoWorkerDataSerialized,
deserializeKoWorkerData,
} from 'model/WorkerData'
import exposeWorker, { type ExposedFuncType } from 'utils/worker/expose'

type GetPredicateParams = Parameters<typeof getPredicate>
Expand All @@ -18,8 +21,8 @@ const eqFunc = (newArgs: GetPredicateParams, oldArgs: GetPredicateParams) =>

const getPredicateMemoized = memoizeOne(getPredicate, eqFunc)

const func = (data: KoWorkerData<Team>) => {
const { season, pots, matchups } = data
const func = (data: KoWorkerDataSerialized<Team>) => {
const { season, pots, matchups } = deserializeKoWorkerData(data)

const predicate = getPredicateMemoized(season)
return getPossiblePairings({
Expand Down
15 changes: 9 additions & 6 deletions src/pages/el/gs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { css } from 'styled-components'
import { random, shuffle } from 'lodash'

import type Team from 'model/team/GsTeam'
import { serializeGsWorkerData } from 'model/WorkerData'

import usePopup from 'store/usePopup'
import useDrawId from 'store/useDrawId'
Expand Down Expand Up @@ -88,12 +89,14 @@ function ELGS({ season, pots: initialPots }: Props) {

let newPickedGroup: number
try {
const firstPossibleGroup = await getFirstPossibleGroupResponse({
season,
pots,
groups,
selectedTeam,
})
const firstPossibleGroup = await getFirstPossibleGroupResponse(
serializeGsWorkerData({
season,
pots,
groups,
selectedTeam,
}),
)
newPickedGroup = firstPossibleGroup
} catch (err) {
console.error(err)
Expand Down
9 changes: 6 additions & 3 deletions src/pages/el/gs/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import memoizeOne from 'memoize-one'
import { firstPossibleGroup } from 'engine/backtracking/gs'
import getPredicate from 'engine/predicates/uefa/gs'
import type Team from 'model/team/GsTeam'
import { type GsWorkerData } from 'model/WorkerData'
import {
type GsWorkerDataSerialized,
deserializeGsWorkerData,
} from 'model/WorkerData'
import exposeWorker, { type ExposedFuncType } from 'utils/worker/expose'

type GetPredicateParams = Parameters<typeof getPredicate>
Expand All @@ -19,8 +22,8 @@ const eqFunc = (newArgs: GetPredicateParams, oldArgs: GetPredicateParams) =>

const getPredicateMemoized = memoizeOne(getPredicate, eqFunc)

const func = (data: GsWorkerData<Team>) => {
const { season, pots, groups, selectedTeam } = data
const func = (data: GsWorkerDataSerialized<Team>) => {
const { season, pots, groups, selectedTeam } = deserializeGsWorkerData(data)

const predicate = getPredicateMemoized(season, pots.length)
return firstPossibleGroup({
Expand Down
13 changes: 8 additions & 5 deletions src/pages/el/ko/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { random, shuffle } from 'lodash'

import { type FixedArray } from 'model/types'
import type Team from 'model/team/KnockoutTeam'
import { serializeKoWorkerData } from 'model/WorkerData'

import usePopup from 'store/usePopup'
import useDrawId from 'store/useDrawId'
Expand Down Expand Up @@ -85,11 +86,13 @@ function ELKO({ season, pots: initialPots }: Props) {
newMatchups: readonly [Team, Team][],
) => {
try {
return await getPossiblePairingsResponse({
season,
pots: newPots,
matchups: newMatchups,
})
return await getPossiblePairingsResponse(
serializeKoWorkerData({
season,
pots: newPots,
matchups: newMatchups,
}),
)
} catch (err) {
setPopup({
error: 'Could not determine possible pairings',
Expand Down
9 changes: 6 additions & 3 deletions src/pages/el/ko/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import memoizeOne from 'memoize-one'
import { getPossiblePairings } from 'engine/backtracking/ko'
import getPredicate from 'engine/predicates/uefa/ko'
import type Team from 'model/team/KnockoutTeam'
import { type KoWorkerData } from 'model/WorkerData'
import {
type KoWorkerDataSerialized,
deserializeKoWorkerData,
} from 'model/WorkerData'
import exposeWorker, { type ExposedFuncType } from 'utils/worker/expose'

type GetPredicateParams = Parameters<typeof getPredicate>
Expand All @@ -18,8 +21,8 @@ const eqFunc = (newArgs: GetPredicateParams, oldArgs: GetPredicateParams) =>

const getPredicateMemoized = memoizeOne(getPredicate, eqFunc)

const func = (data: KoWorkerData<Team>) => {
const { season, pots, matchups } = data
const func = (data: KoWorkerDataSerialized<Team>) => {
const { season, pots, matchups } = deserializeKoWorkerData(data)

const predicate = getPredicateMemoized(season)
return getPossiblePairings({
Expand Down
15 changes: 9 additions & 6 deletions src/pages/wc/gs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { css } from 'styled-components'
import { constant, random, shuffle } from 'lodash'

import type Team from 'model/team/NationalTeam'
import { serializeGsWorkerData } from 'model/WorkerData'

import usePopup from 'store/usePopup'
import useDrawId from 'store/useDrawId'
Expand Down Expand Up @@ -84,12 +85,14 @@ function WCGS({ season, pots: initialPots }: Props) {

let newPickedGroup: number
try {
const firstPossibleGroup = await getFirstPossibleGroupResponse({
season,
pots,
groups,
selectedTeam,
})
const firstPossibleGroup = await getFirstPossibleGroupResponse(
serializeGsWorkerData({
season,
pots,
groups,
selectedTeam,
}),
)
newPickedGroup = firstPossibleGroup
} catch (err) {
console.error(err)
Expand Down
Loading

0 comments on commit 5bb2b11

Please sign in to comment.