This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathranking.go
75 lines (64 loc) · 1.55 KB
/
ranking.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
package main
import (
"bytes"
"io/ioutil"
"os"
"strconv"
"strings"
)
// NewRanking create a new ranking
func NewRanking() *Ranking {
ranking := &Ranking{
scores: make([]uint64, 9),
}
if _, err := os.Stat(baseDir + rankingFileName); os.IsNotExist(err) {
for i := 0; i < 9; i++ {
ranking.scores[i] = 0
}
return ranking
}
scoreBytes, err := ioutil.ReadFile(baseDir + rankingFileName)
if err != nil {
logger.Println("NewRanking ReadFile error:", err)
}
scoreStrings := strings.Split(string(scoreBytes), ",")
for index, scoreString := range scoreStrings {
if index > 8 {
break
}
score, err := strconv.ParseUint(scoreString, 10, 64)
if err != nil {
logger.Println("NewRanking ParseUint error:", err)
score = 0
}
ranking.scores[index] = score
}
return ranking
}
// Save saves the rankings to a file
func (ranking *Ranking) Save() {
var buffer bytes.Buffer
for i := 0; i < 9; i++ {
if i != 0 {
buffer.WriteRune(',')
}
buffer.WriteString(strconv.FormatUint(ranking.scores[i], 10))
}
ioutil.WriteFile(baseDir+rankingFileName, buffer.Bytes(), 0644)
}
// InsertScore inserts a score into the rankings
func (ranking *Ranking) InsertScore(newScore uint64) {
for index, score := range ranking.scores {
if newScore > score {
ranking.slideScores(index)
ranking.scores[index] = newScore
return
}
}
}
// slideScores slides the scores down to make room for a new score
func (ranking *Ranking) slideScores(index int) {
for i := len(ranking.scores) - 1; i > index; i-- {
ranking.scores[i] = ranking.scores[i-1]
}
}