refactorizacion leaderboard
This commit is contained in:
117
client/src/composables/useEventFilters.ts
Normal file
117
client/src/composables/useEventFilters.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { ref, computed, watch } from 'vue';
|
||||
|
||||
export interface DetailedEvent {
|
||||
kind: string;
|
||||
round?: number;
|
||||
gameVariant?: string;
|
||||
}
|
||||
|
||||
export type DataSource = 'aggregated' | 'active-rooms';
|
||||
export type RoundFilter = 'all' | 1 | 2 | 3;
|
||||
export type GameFilter = 'all' | 'G1' | 'G2' | 'G3' | 'G4' | 'G5';
|
||||
|
||||
export function useEventFilters() {
|
||||
// Filter states
|
||||
const dataSource = ref<DataSource>('aggregated');
|
||||
const roundFilter = ref<RoundFilter>('all');
|
||||
const gameFilter = ref<GameFilter>('all');
|
||||
|
||||
// Event data stores
|
||||
const detailedEventsAggregated = ref<DetailedEvent[]>([]);
|
||||
const detailedEventsActiveRooms = ref<DetailedEvent[]>([]);
|
||||
|
||||
// Global event counts
|
||||
const globalEventCounts = ref<Record<string, number>>({});
|
||||
const globalEventCountsAggregated = ref<Record<string, number>>({});
|
||||
const globalEventCountsActiveRooms = ref<Record<string, number>>({});
|
||||
|
||||
// Function to apply filters and recalculate counts
|
||||
function applyFilters(eventTypes: string[]) {
|
||||
const sourceEvents = dataSource.value === 'aggregated'
|
||||
? detailedEventsAggregated.value
|
||||
: detailedEventsActiveRooms.value;
|
||||
|
||||
// Filter events based on round and game
|
||||
const filteredEvents = sourceEvents.filter(event => {
|
||||
if (roundFilter.value !== 'all' && event.round !== roundFilter.value) {
|
||||
return false;
|
||||
}
|
||||
if (gameFilter.value !== 'all' && event.gameVariant !== gameFilter.value) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
// Recalculate counts from filtered events
|
||||
const counts: Record<string, number> = Object.fromEntries(eventTypes.map(k => [k, 0]));
|
||||
filteredEvents.forEach(event => {
|
||||
if (eventTypes.includes(event.kind)) {
|
||||
counts[event.kind] = (counts[event.kind] || 0) + 1;
|
||||
}
|
||||
});
|
||||
|
||||
globalEventCounts.value = counts;
|
||||
}
|
||||
|
||||
// Update aggregated data
|
||||
function updateAggregatedData(events: DetailedEvent[], counts: Record<string, number>) {
|
||||
detailedEventsAggregated.value = events;
|
||||
globalEventCountsAggregated.value = counts;
|
||||
}
|
||||
|
||||
// Update active rooms data
|
||||
function updateActiveRoomsData(events: DetailedEvent[], counts: Record<string, number>) {
|
||||
detailedEventsActiveRooms.value = events;
|
||||
globalEventCountsActiveRooms.value = counts;
|
||||
}
|
||||
|
||||
// Reset filters
|
||||
function resetFilters() {
|
||||
roundFilter.value = 'all';
|
||||
gameFilter.value = 'all';
|
||||
}
|
||||
|
||||
// Computed properties
|
||||
const currentSourceEvents = computed(() =>
|
||||
dataSource.value === 'aggregated' ? detailedEventsAggregated.value : detailedEventsActiveRooms.value
|
||||
);
|
||||
|
||||
const currentSourceCounts = computed(() =>
|
||||
dataSource.value === 'aggregated' ? globalEventCountsAggregated.value : globalEventCountsActiveRooms.value
|
||||
);
|
||||
|
||||
const hasActiveFilters = computed(() =>
|
||||
roundFilter.value !== 'all' || gameFilter.value !== 'all'
|
||||
);
|
||||
|
||||
const filterSummary = computed(() => {
|
||||
const parts = [];
|
||||
if (roundFilter.value !== 'all') parts.push(`Round ${roundFilter.value}`);
|
||||
if (gameFilter.value !== 'all') parts.push(`Game ${gameFilter.value}`);
|
||||
return parts.length > 0 ? parts.join(' + ') : 'Sin filtros';
|
||||
});
|
||||
|
||||
return {
|
||||
// State
|
||||
dataSource,
|
||||
roundFilter,
|
||||
gameFilter,
|
||||
detailedEventsAggregated,
|
||||
detailedEventsActiveRooms,
|
||||
globalEventCounts,
|
||||
globalEventCountsAggregated,
|
||||
globalEventCountsActiveRooms,
|
||||
|
||||
// Methods
|
||||
applyFilters,
|
||||
updateAggregatedData,
|
||||
updateActiveRoomsData,
|
||||
resetFilters,
|
||||
|
||||
// Computed
|
||||
currentSourceEvents,
|
||||
currentSourceCounts,
|
||||
hasActiveFilters,
|
||||
filterSummary
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user