/
collect.go
63 lines (51 loc) · 1.87 KB
/
collect.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
package statistics
import (
"fmt"
"time"
"github.com/bwmarrin/discordgo"
"github.com/zaptross/countula/internal/database"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func Collect(db *gorm.DB, s *discordgo.Session, m *discordgo.MessageCreate, channelID string, ct database.Turn) {
var stats []*database.StatisticRow
db.Where("user_id = ?", ct.UserID).Find(&stats)
allStatistics := database.GetAllStatistics()
if len(stats) == 0 {
stats = make([]*database.StatisticRow, len(allStatistics))
for i, stat := range allStatistics {
stats[i] = &database.StatisticRow{
UserID: ct.UserID,
Stat: stat.Key,
Value: 0,
}
}
}
userStats := fromStatisticRowArray(stats)
// explicitly don't store "games" stat, instead we'll just count
// the number of rows in the turns table on demand
userStats[database.StatTurns.Key]++ // turns
if ct.Correct {
userStats[database.StatCorrect.Key]++ // correct
userStats[database.StatStreak.Key]++ // streak (current)
if userStats[database.StatStreak.Key] > userStats[database.StatStreakMax.Key] {
userStats[database.StatStreakMax.Key] = userStats[database.StatStreak.Key] // streak (max)
}
} else {
userStats[database.StatIncorrect.Key]++ // incorrect
userStats[database.StatStreak.Key] = 0 // streak (current)
if ct.Turn > userStats[database.StatGameStreakBreak.Key] {
userStats[database.StatGameStreakBreak.Key] = ct.Turn // game streak break
}
}
stats = updateStatisticRowArray(stats, userStats) // apply updates to rows
err := db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "user_id"}, {Name: "stat"}},
DoUpdates: clause.AssignmentColumns([]string{"value"}),
}).Create(&stats).Error
if err != nil {
s.ChannelMessageSend(channelID, "Error updating statistics.")
println(fmt.Sprintf("%s - Failed to update statistics: %s", time.Now().Format(time.RFC3339), err.Error()))
return
}
}