Skip to content

Commit

Permalink
Score Calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ltouroumov committed Sep 3, 2023
1 parent cc07c16 commit 4dbfa8e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
22 changes: 12 additions & 10 deletions components/viewer/ViewScoreStatus.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<template>
<div class="d-flex gap-1" :class="{ 'flex-column': vertical }">
<span
v-for="score in activeScores"
v-for="{ score, value } in activeScores"
:key="score.id"
class="d-flex flex-row gap-2"
>
<span v-if="score.beforeText">{{ score.beforeText }}</span>
<span>{{ score.startingSum }}</span>
<span>{{ value }}</span>
<span v-if="score.afterText">{{ score.afterText }}</span>
</span>
</div>
Expand All @@ -23,16 +23,18 @@ const { vertical } = defineProps<{
vertical?: boolean;
}>();
const { selected, pointTypes } = useProjectRefs();
const { selected, pointTypes, points } = useProjectRefs();
const activeScores = computed<PointType[]>(() => {
const activeScores = computed<{ score: PointType; value: number }[]>(() => {
const scores: PointType[] = pointTypes.value;
return R.filter(
(score: PointType) =>
R.isEmpty(score.activatedId) ||
R.includes(score.activatedId, selected.value),
scores,
);
return R.pipe(
R.filter(
(score: PointType) =>
R.isEmpty(score.activatedId) ||
R.includes(score.activatedId, selected.value),
),
R.map((score) => ({ score, value: points.value[score.id] ?? 0 })),
)(scores);
});
</script>
38 changes: 37 additions & 1 deletion composables/store/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { defineStore, storeToRefs } from 'pinia';
import * as R from 'ramda';
import { ComputedRef, computed } from 'vue';

import { buildConditions } from '~/composables/conditions';
import {
PointType,
Project,
ProjectFile,
ProjectObj,
ProjectRow,
Score,
} from '~/composables/project';

export const useProjectStore = defineStore('project', () => {
Expand Down Expand Up @@ -83,12 +85,46 @@ export const useProjectStore = defineStore('project', () => {
}
};

const points = computed<Record<string, number>>(() => {
const _selected = selected.value;

const startingSums: Record<string, number> = R.pipe(
R.map(({ id, startingSum }: PointType): [string, number] => [
id,
startingSum,
]),
R.fromPairs,
)(pointTypes.value);

return R.pipe(
R.map((id: string) => getObject.value(id)),
R.chain(({ scores }: ProjectObj) => {
return R.pipe(
R.filter((score: Score) => {
const cond = buildConditions(score);
return cond(_selected);
}),
R.map(({ id, value }: Score): [string, number] => [
id,
-Number.parseInt(value),
]),
)(scores);
}),
R.reduceBy(
(acc, [_, value]) => acc + value,
0,
([id, _]) => id,
),
)(_selected);
});

return {
project,
projectRows,
backpack,
selected,
pointTypes,
selected,
points,
isLoaded,
loadProject,
unloadProject,
Expand Down

0 comments on commit 4dbfa8e

Please sign in to comment.