-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathend_screen.go
73 lines (60 loc) · 1.79 KB
/
end_screen.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package quiz
import (
"fmt"
"github.com/crazcalm/term-quiz/interface"
"github.com/crazcalm/term-quiz/user"
"github.com/jroimartin/gocui"
"log"
"os"
)
//ESInit -- End Screen Initialization. Presents the results
func ESInit(g *gocui.Gui, u user.Answers) (err error) {
//End quiz when you run out of answers
if CurrentUserAnswer > len(UserAnswers) {
g.Close()
fmt.Println("Game Over")
os.Exit(0)
}
//Place holder for 'correct' string that gets placed
//in the gui
var correct string
numCorrect, err := u.TotalCorrect()
if err != nil {
log.Panicln(err)
}
//Score = num of correct answers over total
score := fmt.Sprintf("%d/%d", numCorrect, u.Total())
//Currently need a global counter...
questionCount := fmt.Sprintf("%d", CurrentUserAnswer)
currentUserAnswer := u[questionCount]
answerCorrect, err := currentUserAnswer.Correct()
if err != nil {
log.Panicln(err)
}
if answerCorrect {
correct = gui.Right
} else {
correct = gui.Wrong
}
//Get the correct answer
correctAnswer, err := currentUserAnswer.Question.CorrectAnswer()
if err != nil {
log.Panicln(err)
}
//Highlight the selected view and make it green
g.Highlight = true
g.SelFgColor = gocui.ColorGreen
//create widgets
scoreWidget := gui.NewScore(gui.ScoreName, score, questionCount)
explainWidget := gui.NewExplanation(gui.Explain, correct, currentUserAnswer.Question.Question, correctAnswer.Answer, currentUserAnswer.Question.Explanation)
infoBar := gui.NewInfoBar(gui.InfoBarName, gui.InfoBarEndScreen)
g.SetManager(scoreWidget, explainWidget, infoBar)
//Keybindings
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, gui.Quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyEnter, gocui.ModNone, NextUserAnswer); err != nil {
log.Panicln(err)
}
return nil
}