-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnswersPanel.ts
55 lines (41 loc) · 1.7 KB
/
AnswersPanel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {autoinject, bindable, observable} from 'aurelia-framework';
import {EventAggregator} from 'aurelia-event-aggregator';
import type { PuzzleAnswer } from "./lib"
@autoinject
export class AnswersPanel {
@bindable @observable puzzle_answers: PuzzleAnswer[]
correct_answer_count: number
total_answer_count: number
total_answers_score: number
answers_counts_text: string
constructor(private ea: EventAggregator) {}
// The Aurelia life-cycle method called as soon as the data from the containing component is bound to this component.
bind() {
this.puzzle_answersChanged()
}
// Called by Aurelia when puzzle_answers changes.
puzzle_answersChanged() {
this.setAnswerCounts()
this.setAnswerCountsText()
}
setAnswerCounts() {
this.correct_answer_count = 0
this.total_answer_count = 0
this.total_answers_score = 0
this.puzzle_answers.forEach((puzzle_answer) => {
this.total_answer_count++
if (puzzle_answer.scrabble_score) {
this.correct_answer_count++
this.total_answers_score += puzzle_answer.scrabble_score
if (puzzle_answer.boggle_score) {
this.total_answers_score += puzzle_answer.boggle_score
}
}
})
}
setAnswerCountsText() {
const count_noun_for_answer = (this.correct_answer_count == 1) ? "answer" : "answers"
const count_noun_for_try = (this.total_answer_count == 1) ? "try" : "tries"
this.answers_counts_text = `You have ${this.correct_answer_count} correct ${count_noun_for_answer}, after ${this.total_answer_count} ${count_noun_for_try}.`
}
}