/
main.go
261 lines (210 loc) · 5.9 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/garyburd/redigo/redis"
"github.com/lycoris0731/go-groovecoaster/groovecoaster"
)
var c redis.Conn
type item struct {
name string
diff []string
}
func (i item) String() string {
text := i.name + "\n"
for j, v := range i.diff {
switch groovecoaster.Difficulty(j) {
case groovecoaster.Simple:
text += " [Simple] "
case groovecoaster.Normal:
text += " [Normal] "
case groovecoaster.Hard:
text += " [Hard] "
case groovecoaster.Extra:
text += " [Extra] "
}
text += v + "\n"
}
return text + "\n"
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
summary := generateSummary()
if summary == "" {
summary = "No change...\nWhy don't play GrooveCoaster?\n"
}
io.WriteString(w, summary)
})
log.Println("Listen in", ":"+os.Getenv("PORT"))
http.ListenAndServe(":"+os.Getenv("PORT"), nil)
}
func generateSummary() string {
client := groovecoaster.New()
var err error
log.Println("Connecting to Redis server...")
c, err = redis.DialURL(os.Getenv("REDISTOGO_URL"))
if err != nil {
log.Fatal(err)
}
log.Println("Connected to Redis server")
defer func() {
c.Close()
log.Println("Redis Connection closed")
}()
lastDate, err := lastDateFromRedis()
if err != nil {
log.Println(err)
return ""
}
log.Println("last updated:", lastDate)
log.Println("Fetching musics summary from mypage...")
summary, err := client.MusicSummary()
if err != nil {
log.Println(err)
return ""
}
log.Println("Fetching musics summary was successful")
splittedSummary := splitMusicSummary(summary, lastDate)
items := make([]item, len(splittedSummary))
for _, music := range splittedSummary {
oldMusic, err := musicFromRedis(strconv.Itoa(music.ID))
if err != nil {
log.Println(err)
return ""
}
newMusic, err := client.Music(music.ID)
if err != nil {
log.Println(err)
return ""
}
var item item
item.name = music.Title
if oldMusic.ID == "" {
oldMusic = newMusic
}
results := []groovecoaster.Result{newMusic.Simple, newMusic.Normal, newMusic.Hard}
if newMusic.HasEx {
results = append(results, newMusic.Extra)
}
for i, diff := range results {
if diff.PlayCount == 0 {
continue
}
var oldMusicDiff groovecoaster.Result
archived := []string{}
switch groovecoaster.Difficulty(i) {
case groovecoaster.Simple:
oldMusicDiff = oldMusic.Simple
case groovecoaster.Normal:
oldMusicDiff = oldMusic.Normal
case groovecoaster.Hard:
oldMusicDiff = oldMusic.Hard
case groovecoaster.Extra:
if !newMusic.HasEx {
continue
}
oldMusicDiff = oldMusic.Extra
}
// どれかにあてはまる場合
switch {
case diff.Perfect == 1:
archived = append(archived, "Perfect")
case diff.FullChain == 1:
archived = append(archived, "FullChain")
case diff.NoMiss == 1:
archived = append(archived, "NoMiss")
}
if diff.MaxChain > oldMusicDiff.MaxChain {
archived = append(archived, fmt.Sprintf(":chains: Max Chain +%d", diff.MaxChain-oldMusicDiff.MaxChain))
}
if diff.PlayCount == 100 {
archived = append(archived, ":tada: Total played time is over 100")
}
if diff.Score > oldMusicDiff.Score {
archived = append(archived, fmt.Sprintf(":chart_with_upwards_trend: Total Score +%d", diff.MaxChain-oldMusicDiff.MaxChain))
}
if len(archived) == 0 {
continue
}
item.diff = append(item.diff, strings.Join(archived, ", "))
}
bytes, err := json.Marshal(newMusic)
if err != nil {
log.Println(err)
return ""
}
c.Do("SET", music.ID, string(bytes))
log.Printf("Updated music cache: %s", music.Title)
items = append(items, item)
}
text := fmt.Sprintf("GrooveCoaster Summary\n(From %s)\n\n", lastDate.String())
for _, item := range items {
if len(item.diff) == 0 {
continue
}
text += item.String() + "\n"
}
log.Println(text)
c.Do("SET", "lastDate", time.Now().Format("2006-01-02 15:04:05"))
log.Println("Updated lastDate")
log.Println("Completed")
return strings.TrimSpace(text + "\n")
}
func lastDateFromRedis() (time.Time, error) {
log.Println("Fetching last played date from Redis...")
exists, err := redis.Bool(c.Do("EXISTS", "lastDate"))
if err != nil {
return time.Time{}, fmt.Errorf("cannot read lastDate from Redis: %s", err)
}
if !exists {
log.Println("Seted lastDate")
c.Do("SET", "lastDate", time.Now().Format("2006-01-02 15:04:05"))
}
loc, _ := time.LoadLocation("Asia/Tokyo")
lastDateString, err := redis.String(c.Do("GET", "lastDate"))
if err != nil {
return time.Time{}, fmt.Errorf("cannot get lastDate from Redis: %s", err)
}
lastDate, err := time.ParseInLocation("2006-01-02 15:04:05", lastDateString, loc)
log.Printf("Last played date: %s", lastDate)
return lastDate, nil
}
func splitMusicSummary(summary []groovecoaster.MusicSummary, lastDate time.Time) []groovecoaster.MusicSummary {
loc, _ := time.LoadLocation("Asia/Tokyo")
for i, m := range summary {
date, err := time.ParseInLocation("2006-01-02 15:04:05", m.LastPlayTime, loc)
if err != nil {
log.Println(err)
return nil
}
if date.Equal(lastDate) || lastDate.After(date) {
return summary[:i]
}
}
return []groovecoaster.MusicSummary{}
}
func musicFromRedis(id string) (groovecoaster.MusicDetail, error) {
exists, err := redis.Bool(c.Do("EXISTS", id))
if err != nil {
return groovecoaster.MusicDetail{}, fmt.Errorf("cannot confirm whether music is exists from Redis: %s", err)
}
if !exists {
return groovecoaster.MusicDetail{}, nil
}
musicString, err := redis.String(c.Do("GET", id))
if err != nil {
return groovecoaster.MusicDetail{}, fmt.Errorf("cannot read music from Redis: %s", err)
}
var music groovecoaster.MusicDetail
if err := json.Unmarshal([]byte(musicString), &music); err != nil {
return groovecoaster.MusicDetail{}, fmt.Errorf("cannot unmarshal music: %s", err)
}
return music, nil
}