-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.go
69 lines (54 loc) · 1.54 KB
/
score.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
package main
import "log"
//This function adds the scores to the database.
func setScore(telegramID int64, score int, questionID int, categoryID int) {
db := dbConnect()
defer db.Close()
stmt, err := db.Prepare("INSERT INTO scores(telegram_id, score, qid, cid) VALUES(?, ?, ?, ?)")
if err != nil {
log.Fatal(err)
}
_, err = stmt.Exec(telegramID, score, questionID, categoryID)
if err != nil {
log.Fatal(err)
}
}
//This function returns all lifetraps which user has.
func showLifetraps(telegramID int64) []string {
ltChan := make(chan map[int]int)
go func() {
lts := maxScore(telegramID)
ltChan <- lts
}()
lt := <-ltChan
lifetraps := []string{}
for categoryID, maxScore := range lt {
if maxScore > 3 {
db := dbConnect()
defer db.Close()
var category string
err := db.QueryRow("SELECT category FROM categories WHERE cid=?", categoryID).Scan(&category)
if err != nil {
log.Fatal(err)
}
lifetraps = append(lifetraps, category)
}
}
return lifetraps
}
//This function gets max score of each lifetrap and returns a int map
//which key is the category ID of lifetrap and value is the max score of that lifetrap.
func maxScore(telegramID int64) map[int]int {
db := dbConnect()
defer db.Close()
liftraps := make(map[int]int)
for i := 1; i <= 11; i++ {
var maxScore, categoryID int
err := db.QueryRow("SELECT MAX(score), cid FROM scores WHERE telegram_id=? AND cid=?", telegramID, i).Scan(&maxScore, &categoryID)
if err != nil {
log.Fatal(err)
}
liftraps[categoryID] = maxScore
}
return liftraps
}