DRY up the updateRatings function

This commit is contained in:
Maciej Pędzich 2023-06-10 12:43:16 +02:00
parent 89003918d8
commit 993c5b98f7

View File

@ -1,4 +1,4 @@
import { computed, reactive, ref, toRefs, watchEffect } from 'vue'; import { reactive, ref, toRefs, watchEffect } from 'vue';
import glicko2 from 'glicko2-lite'; import glicko2 from 'glicko2-lite';
import defaultPhotos from '@/photos.json'; import defaultPhotos from '@/photos.json';
@ -14,21 +14,17 @@ const db = reactive<Database>(
const photosInCurrentVote = ref<Photo[]>([]); const photosInCurrentVote = ref<Photo[]>([]);
const photosForFirstPick = computed(() => const pickPhotosForNewVote = () => {
db.photos.filter(({ fileName }) => { const photosForFirstPick = db.photos.filter(({ fileName }) => {
const appearanceCount = db.votes.filter((vote) => const appearanceCount = db.votes.filter((vote) =>
vote.photos.includes(fileName) vote.photos.includes(fileName)
).length; ).length;
return appearanceCount !== db.photos.length - 1; return appearanceCount !== db.photos.length - 1;
}) });
);
const pickPhotosForNewVote = () => {
const firstPick = const firstPick =
photosForFirstPick.value[ photosForFirstPick[randomNumber(0, photosForFirstPick.length - 1)];
randomNumber(0, photosForFirstPick.value.length - 1)
];
const photosForSecondPick = db.photos.filter(({ fileName }) => { const photosForSecondPick = db.photos.filter(({ fileName }) => {
const votesWithFirstPick = db.votes.filter(({ photos }) => const votesWithFirstPick = db.votes.filter(({ photos }) =>
@ -37,7 +33,7 @@ const pickPhotosForNewVote = () => {
const fileNamesToExclude = [ const fileNamesToExclude = [
firstPick.fileName, firstPick.fileName,
...new Set(votesWithFirstPick.flatMap(({ photos }) => photos)) ...votesWithFirstPick.flatMap(({ photos }) => photos)
]; ];
return !fileNamesToExclude.includes(fileName); return !fileNamesToExclude.includes(fileName);
@ -50,11 +46,11 @@ const pickPhotosForNewVote = () => {
}; };
const submitVote = (result: 0 | 0.5 | 1) => { const submitVote = (result: 0 | 0.5 | 1) => {
const fileNames = [...photosInCurrentVote.value].map( const photos = [...photosInCurrentVote.value].map(
({ fileName }) => fileName ({ fileName }) => fileName
) as [string, string]; ) as [string, string];
db.votes.unshift({ photos: fileNames, result }); db.votes.unshift({ photos, result });
if (db.votes.length % 12 === 0) { if (db.votes.length % 12 === 0) {
updateRatings(); updateRatings();
@ -67,38 +63,23 @@ const updateRatings = () => {
const twelveMostRecentVotes = db.votes.slice(0, 12).reverse(); const twelveMostRecentVotes = db.votes.slice(0, 12).reverse();
const votesGrouppedByPhotos = twelveMostRecentVotes.reduce((obj, vote) => { const votesGrouppedByPhotos = twelveMostRecentVotes.reduce((obj, vote) => {
const [firstFileName, secondFileName] = vote.photos; for (const [index, fileName] of vote.photos.entries()) {
const opponentFileName = vote.photos[1 - index];
const opponent = db.photos.find(
({ fileName }) => fileName === opponentFileName
)!;
const secondPhoto = db.photos.find( const voteParams = [
({ fileName }) => fileName === secondFileName opponent.rating,
)!; opponent.rd,
index === 0 ? vote.result : 1 - vote.result
] as [number, number, number];
const paramsVersusSecondPhoto = [ if (obj[fileName]) {
secondPhoto.rating, obj[fileName].push(voteParams);
secondPhoto.rd, } else {
vote.result obj[fileName] = [voteParams];
] as [number, number, number]; }
if (obj[firstFileName]) {
obj[firstFileName].push(paramsVersusSecondPhoto);
} else {
obj[firstFileName] = [paramsVersusSecondPhoto];
}
const firstPhoto = db.photos.find(
({ fileName }) => fileName === firstFileName
)!;
const paramsVersusFirstPhoto = [
firstPhoto.rating,
firstPhoto.rd,
1 - vote.result
] as [number, number, number];
if (obj[secondFileName]) {
obj[secondFileName].push(paramsVersusFirstPhoto);
} else {
obj[secondFileName] = [paramsVersusFirstPhoto];
} }
return obj; return obj;
@ -124,7 +105,6 @@ watchEffect(() => localStorage.setItem('db', JSON.stringify(db)));
export function useVote() { export function useVote() {
return { return {
photosInCurrentVote, photosInCurrentVote,
photosForFirstPick,
pickPhotosForNewVote, pickPhotosForNewVote,
submitVote, submitVote,
updateRatings, updateRatings,