-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranks.go
111 lines (92 loc) · 2.68 KB
/
ranks.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
_ "embed"
"fmt"
"github.com/gocarina/gocsv"
)
//go:embed resources/testing/spamrank.csv
var spamranksCsv string
//go:embed resources/testing/karmarank.csv
var karmarankCsv string
//rankName,emoji,picture,minMessages,congratulationMessage,showRankCongrats
type SpamRank struct {
ChatId string `csv:"-" json:"chatId"`
RankName string `csv:"rankName" json:"rankName"`
Emoji string `csv:"emoji" json:"emoji"`
Picture string `csv:"picture" json:"picture"`
MinMessages int `csv:"minMessages" json:"minMessages"`
CongratulationMessage string `csv:"congratulationMessage" json:"congratulationMessage"`
ShowRankCongratsTimeout int `csv:"showRankCongrats" json:"showRankCongrats"`
}
type KarmaRank struct {
Karma int `csv:"karma" json:"karma"`
Rank string `csv:"rank" json:"rank"`
}
func getRanksCSVEmbeded() []*SpamRank {
spamRanks := []*SpamRank{}
err := gocsv.UnmarshalString(spamranksCsv, &spamRanks)
fmt.Printf("%+v, err: %+v", spamRanks, err)
return spamRanks
}
func getKarmaRanksCsvEmbedded() []*KarmaRank {
karmaRanks := []*KarmaRank{}
err := gocsv.UnmarshalString(karmarankCsv, &karmaRanks)
if err != nil {
fmt.Printf("%+v, err: %+v", karmaRanks, err)
}
return karmaRanks
}
func calculateDesignation(karma int, spam int) string {
kRank := findKRankByVal(karma)
sRank := findRankByVal(spam)
return fmt.Sprintf("%s %s %s", sRank.Emoji, kRank.Rank, sRank.RankName)
}
func findKRankByVal(val int) KarmaRank {
mRank := KarmaRank{Karma: 0}
//Find minimal karma Rank
for _, r := range getKarmaRanksCsvEmbedded() {
if r.Karma < mRank.Karma {
mRank = *r
}
}
//Find rank that fits
for _, r := range getKarmaRanksCsvEmbedded() {
if r.Karma > mRank.Karma && r.Karma <= val {
mRank = *r
}
}
return mRank
}
func findRankByVal(val int) SpamRank {
nextRank := SpamRank{MinMessages: -900000}
for _, r := range getRanksCSVEmbeded() {
if r.MinMessages > nextRank.MinMessages && r.MinMessages <= val {
nextRank = *r
}
}
return nextRank
}
func getRanksDescriptionsSplitted() [][]*SpamRank {
ranks := getRanksCSVEmbeded()
rankChunks := [][]*SpamRank{}
for i := 0; i < len(ranks); i += 15 {
if i+15 < len(ranks) {
rankChunks = append(rankChunks, ranks[i:i+15])
} else {
rankChunks = append(rankChunks, ranks[i:])
}
}
return rankChunks
}
func getKarmaRanksDescriptionsSplitted() [][]*KarmaRank {
ranks := getKarmaRanksCsvEmbedded()
rankChunks := [][]*KarmaRank{}
for i := 0; i < len(ranks); i += 15 {
if i+15 < len(ranks) {
rankChunks = append(rankChunks, ranks[i:i+15])
} else {
rankChunks = append(rankChunks, ranks[i:])
}
}
return rankChunks
}