2024-09-27 14:46:45 +00:00
|
|
|
import {
|
|
|
|
Group,
|
|
|
|
ProcessedOperator,
|
|
|
|
} from '@/containers/Dashboard/DashboardContainer'
|
2024-09-27 15:41:52 +00:00
|
|
|
import { Archetype } from '../types/operators'
|
2024-09-27 14:46:45 +00:00
|
|
|
|
2024-09-27 15:41:52 +00:00
|
|
|
export function processOperators(
|
|
|
|
data: Group[],
|
|
|
|
selectedArchetypes: Archetype[],
|
|
|
|
): ProcessedOperator[] {
|
|
|
|
const hasSelectedArchetypes = selectedArchetypes.length > 0
|
|
|
|
|
|
|
|
return data?.flatMap((group) => {
|
|
|
|
const groupArchetype = group.name.slice(0, -1) as Archetype
|
|
|
|
|
|
|
|
const isSelectedArchetype = hasSelectedArchetypes
|
|
|
|
? selectedArchetypes.includes(groupArchetype)
|
|
|
|
: true
|
|
|
|
|
|
|
|
if (isSelectedArchetype) {
|
|
|
|
return group.operators.map((operator) => ({
|
|
|
|
id: operator.id.toString(),
|
|
|
|
image: operator.image_400_jpeg_url,
|
|
|
|
gif: operator.image_400_url,
|
2024-09-27 16:26:45 +00:00
|
|
|
name: operator.name,
|
2024-09-27 15:41:52 +00:00
|
|
|
pointsPerHour: Math.floor(Math.random() * 500),
|
2024-09-27 16:26:45 +00:00
|
|
|
comp: operator.comp,
|
|
|
|
background: operator.background,
|
|
|
|
skin: operator.skin,
|
|
|
|
helmet: operator.helmet,
|
|
|
|
jacket: operator.jacket,
|
|
|
|
archetype: groupArchetype,
|
2024-09-27 15:41:52 +00:00
|
|
|
isStaked: false,
|
|
|
|
isPinned: false,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return []
|
|
|
|
})
|
2024-09-27 14:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getRandomSubset<T>(array: T[], count: number): T[] {
|
|
|
|
const shuffled = array?.sort(() => 0.5 - Math.random())
|
|
|
|
return shuffled?.slice(0, count)
|
|
|
|
}
|
2024-09-27 16:26:45 +00:00
|
|
|
|
|
|
|
export function getAllIds(groups: Group[]): number[] {
|
|
|
|
const ids: number[] = []
|
|
|
|
|
|
|
|
// Extract group IDs
|
|
|
|
groups.forEach((group) => {
|
|
|
|
ids.push(group.id)
|
|
|
|
|
|
|
|
// Extract operator IDs within each group
|
|
|
|
group.operators.forEach((operator) => {
|
|
|
|
ids.push(operator.id)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findOperatorById(
|
|
|
|
operators: ProcessedOperator[],
|
|
|
|
operatorId: number | string,
|
|
|
|
): ProcessedOperator | undefined {
|
|
|
|
return operators.find(
|
|
|
|
(operator) => String(operator.id) === String(operatorId),
|
|
|
|
)
|
|
|
|
}
|
2024-10-03 05:04:28 +00:00
|
|
|
|
|
|
|
export function shuffleOperators(
|
|
|
|
array: ProcessedOperator[],
|
|
|
|
): ProcessedOperator[] {
|
|
|
|
for (let i = array?.length - 1; i > 0; i--) {
|
|
|
|
const j = Math.floor(Math.random() * (i + 1))
|
|
|
|
;[array[i], array[j]] = [array[j], array[i]]
|
|
|
|
}
|
|
|
|
return array
|
|
|
|
}
|
2024-10-04 00:26:36 +00:00
|
|
|
|
|
|
|
export function extractUniqueValues(data: any, field: string) {
|
|
|
|
if (!data || !field) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
const uniqueValues = new Set()
|
|
|
|
|
|
|
|
function traverse(item: any) {
|
|
|
|
if (item[field]) {
|
|
|
|
uniqueValues.add(item[field])
|
|
|
|
}
|
|
|
|
if (item.operators && Array.isArray(item.operators)) {
|
|
|
|
item.operators?.forEach(traverse)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data.forEach(traverse)
|
|
|
|
|
|
|
|
return Array.from(uniqueValues)
|
|
|
|
}
|