kandimat-user-app/src/app/euromat/components/results.vue

181 lines
4.9 KiB
Vue
Raw Normal View History

2017-08-10 23:14:45 +02:00
<template>
<section>
2017-08-13 01:23:24 +02:00
<header class="results-header">
<h1>{{ $t('euromat.results.headline') }}</h1>
2017-08-13 01:23:24 +02:00
</header>
2017-08-10 23:47:40 +02:00
<ul class="party-results">
2017-08-13 23:11:48 +02:00
<li v-for="party of parties">
<router-link :to="{ path: getPartyPath(party.token.toLowerCase()) }">
2017-08-15 22:25:25 +02:00
<h2>{{ party.token }} ({{ getScorePercentage(party.score) }} %)</h2>
<!-- <svgicon :name="getPartyLogoName(item.token)" width="50" height="50" /> -->
<party-percentage
class="result-percentage"
:value="getScorePercentage(party.score)"
:max="totalScoredPoints" />
</router-link>
2017-08-10 23:47:40 +02:00
</li>
</ul>
2017-08-10 23:14:45 +02:00
</section>
</template>
<script>
2017-08-13 23:11:48 +02:00
import {
MAX_POINTS,
BASE_POINTS,
MIN_POINTS,
EMPHASIS_POINTS,
getScoringGrid
} from '@/app/euromat/scoring'
2017-08-14 22:28:12 +02:00
import { parties } from '@/data'
2017-08-13 01:23:24 +02:00
import Progress from '@/components/progress'
import '@/assets/icons'
2017-08-10 23:47:40 +02:00
2017-08-13 21:42:02 +02:00
// FIXME: There is a bug that not all theses are send to localStorage.
// We have 43 theses now, but only 42 are stored. Hence I check if
// answers is even an object.
2017-08-13 23:11:48 +02:00
const addUp = (a, b) => a + b
2017-08-10 23:14:45 +02:00
export default {
name: 'Results',
2017-08-13 01:23:24 +02:00
components: {
'party-percentage': Progress
},
2017-08-10 23:14:45 +02:00
data () {
return {
2017-08-13 23:11:48 +02:00
scoringGrid: [],
2017-08-13 21:42:02 +02:00
answers: [],
emphasized: [],
2017-08-13 23:11:48 +02:00
scores: [],
2017-08-14 22:28:12 +02:00
parties,
2017-08-13 21:42:02 +02:00
totalScoredPoints: 0
2017-08-10 23:47:40 +02:00
}
},
computed: {
isGermanLocale () {
return this.$i18n.locale === 'de'
}
},
2017-08-10 23:47:40 +02:00
methods: {
getPartyPath (token) {
return this.isGermanLocale
? `/partei/${token}`
: `/party/${token}`
},
getPartyLogoName (token) {
return `${token.toLowerCase()}-logo`
2017-08-13 21:42:02 +02:00
},
getScorePercentage (score) {
return (score / this.totalScoredPoints * 100).toFixed(2)
},
2017-08-13 23:11:48 +02:00
evalPoints (party, user, emphasis) {
2017-08-13 21:42:02 +02:00
let score = 0
2017-08-13 23:11:48 +02:00
if (user.position === party.position) {
score = MAX_POINTS
2017-08-13 21:42:02 +02:00
} else if (
2017-08-13 23:11:48 +02:00
(user.position === 'positive' && party.position === 'neutral') ||
(user.position === 'neutral' && party.position === 'positive') ||
(user.position === 'negative' && party.position === 'neutral')
2017-08-13 21:42:02 +02:00
) {
2017-08-13 23:11:48 +02:00
score = BASE_POINTS
2017-08-13 21:42:02 +02:00
} else if (
2017-08-13 23:11:48 +02:00
(user.position === 'positive' && party.position === 'negative') ||
(user.position === 'neutral' && party.position === 'negative') ||
(user.position === 'negative' && party.position === 'positive')
2017-08-13 21:42:02 +02:00
) {
2017-08-13 23:11:48 +02:00
score = MIN_POINTS
2017-08-13 21:42:02 +02:00
}
2017-08-13 23:11:48 +02:00
return {
party: party.party,
score: emphasis ? score * EMPHASIS_POINTS : score
}
},
getHighestScore (scores) {
const highestScore = Math.max(...scores.map(s => s.score))
2017-08-13 21:42:02 +02:00
2017-08-13 23:11:48 +02:00
if (!highestScore) {
return MIN_POINTS
}
return highestScore === 1
? MAX_POINTS
: highestScore
},
getScorePoints (grid) {
// 1. Iterate over scoringGrid
// 2. Get user and party positions of each thesis
// 3. Evaluate points based on calculation model for each party
// 4. Count the highest score per thesis
// 5. Return a new object for each thesis row with results
return grid.map(row => {
2017-08-14 22:28:12 +02:00
const partiesFromRow = row.positions.filter(p => p.type === 'party')
2017-08-13 23:11:48 +02:00
const user = row.positions[row.positions.length - 1]
2017-08-14 22:28:12 +02:00
const scores = partiesFromRow.map(party => this.evalPoints(party, user, row.emphasis))
2017-08-13 23:11:48 +02:00
const highestScore = this.getHighestScore(scores)
return { thesis: row.thesis, highestScore, scores }
})
},
getScorePerParty (party) {
return {
token: party.token,
score: this.scores
.map(t => t.scores.find(s => s.party === party.id).score)
.reduce(addUp, 0)
}
2017-08-10 23:14:45 +02:00
}
},
2017-08-13 21:42:02 +02:00
created () {
const emphasized = JSON.parse(sessionStorage.getItem('euromat-emphasized'))
const answers = JSON.parse(sessionStorage.getItem('euromat-answers'))
2017-08-13 21:42:02 +02:00
if (!emphasized) {
this.$router.push({ path: this.isGermanLocale ? '/thesen' : '/theses' })
}
this.emphasized = emphasized
this.answers = answers
2017-08-13 21:42:02 +02:00
2017-08-13 23:11:48 +02:00
this.scoringGrid = getScoringGrid(this.answers, this.emphasized)
this.scores = this.getScorePoints(this.scoringGrid)
this.parties = this.parties
.map(this.getScorePerParty)
.sort((a, b) => a.score - b.score)
.reverse()
this.totalScoredPoints = this.scores
.map(s => s.highestScore)
.reduce(addUp, 0)
2017-08-10 23:14:45 +02:00
}
}
</script>
2017-08-10 23:47:40 +02:00
<style lang="scss" scoped>
2017-08-13 01:23:24 +02:00
@import "~styles/layout";
.results-header {
margin-bottom: $base-gap;
}
2017-08-10 23:47:40 +02:00
.party-results {
list-style: none;
width: 100%;
li {
display: flex;
2017-08-13 01:23:24 +02:00
flex-direction: column;
margin-bottom: $base-gap;
2017-08-10 23:47:40 +02:00
}
2017-08-15 22:25:25 +02:00
.result-percentage {
height: 40px;
}
2017-08-10 23:47:40 +02:00
}
</style>