This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
forked from osuripple/ripple-cron-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replays.go
78 lines (66 loc) · 1.54 KB
/
replays.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
package main
import (
"os"
"strconv"
"strings"
"github.com/fatih/color"
)
func opCleanReplays() {
defer wg.Done()
if c.ReplayFolder == "" {
return
}
// we're using os.Open instead of ioutil.Readdir
// so that we can take advantage of Readdirnames (which uses far less
// memory)
dir, err := os.Open(c.ReplayFolder)
if err != nil {
color.Red("> CleanReplays: can't read dir %v", err)
return
}
names, err := dir.Readdirnames(-1)
dir.Close()
if err != nil {
color.Red("> CleanReplays: can't read names of dir %v", err)
return
}
repsFolder := replaysToIntSlice(names)
// get ids of all scores in database
var repsDB []int
const scoresQuery = "SELECT id FROM scores WHERE completed = 3"
err = db.Select(&repsDB, scoresQuery)
if err != nil {
queryError(err, scoresQuery)
return
}
// remove from repsFolder all replays
for _, i := range repsDB {
for pos, j := range repsFolder {
if i == j {
repsFolder[pos] = repsFolder[len(repsFolder)-1]
repsFolder = repsFolder[:len(repsFolder)-1]
break
}
}
}
for _, r := range repsFolder {
err = os.Remove(c.ReplayFolder + "/replay_" + strconv.Itoa(r) + ".osr")
if err != nil {
color.Red("> CleanReplays: %s: %v", r, err)
}
}
color.Green("> CleanReplays: done!")
}
func replaysToIntSlice(replays []string) []int {
i := make([]int, 0, len(replays))
var j int
var err error
for _, r := range replays {
j, err = strconv.Atoi(strings.TrimPrefix(strings.TrimSuffix(r, ".osr"), "replay_"))
if err != nil {
continue
}
i = append(i, j)
}
return i
}