2016-09-06 23:51:23 +00:00
|
|
|
package beatmapget
|
|
|
|
|
|
|
|
import (
|
2016-09-20 16:14:02 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2016-09-06 23:51:23 +00:00
|
|
|
"time"
|
|
|
|
|
2019-02-23 13:09:10 +00:00
|
|
|
"github.com/osuYozora/rippleapi/common"
|
2016-09-06 23:51:23 +00:00
|
|
|
"gopkg.in/thehowl/go-osuapi.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Set checks if an update is required for all beatmaps in a set.
|
|
|
|
func Set(s int) error {
|
|
|
|
var (
|
|
|
|
lastUpdated common.UnixTimestamp
|
|
|
|
ranked int
|
|
|
|
)
|
2016-09-20 16:14:02 +00:00
|
|
|
err := DB.QueryRow("SELECT latest_update, ranked FROM beatmaps WHERE beatmapset_id = ? LIMIT 1", s).
|
2016-09-06 23:51:23 +00:00
|
|
|
Scan(&lastUpdated, &ranked)
|
2016-09-20 16:14:02 +00:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2016-09-06 23:51:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return set(s, lastUpdated, ranked)
|
|
|
|
}
|
|
|
|
|
2016-09-20 16:14:02 +00:00
|
|
|
// ErrBeatmapNotFound is returned by Beatmap if a beatmap could not be found.
|
|
|
|
var ErrBeatmapNotFound = errors.New("beatmapget: beatmap not found")
|
|
|
|
|
2016-09-06 23:51:23 +00:00
|
|
|
// Beatmap check if an update is required for all beatmaps in the set
|
|
|
|
// containing this beatmap.
|
2016-09-20 16:14:02 +00:00
|
|
|
func Beatmap(b int) (int, error) {
|
2016-09-06 23:51:23 +00:00
|
|
|
var (
|
|
|
|
setID int
|
|
|
|
lastUpdated common.UnixTimestamp
|
|
|
|
ranked int
|
|
|
|
)
|
2016-09-20 16:14:02 +00:00
|
|
|
err := DB.QueryRow("SELECT beatmapset_id, latest_update, ranked FROM beatmaps WHERE beatmap_id = ? LIMIT 1", b).
|
2016-09-06 23:51:23 +00:00
|
|
|
Scan(&setID, &lastUpdated, &ranked)
|
2016-09-20 16:14:02 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return setID, set(setID, lastUpdated, ranked)
|
|
|
|
case sql.ErrNoRows:
|
|
|
|
beatmaps, err := Client.GetBeatmaps(osuapi.GetBeatmapsOpts{
|
|
|
|
BeatmapID: b,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if len(beatmaps) == 0 {
|
|
|
|
return 0, ErrBeatmapNotFound
|
|
|
|
}
|
|
|
|
return beatmaps[0].BeatmapSetID, set(beatmaps[0].BeatmapSetID, common.UnixTimestamp(time.Time{}), 0)
|
|
|
|
default:
|
|
|
|
return setID, err
|
2016-09-06 23:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func set(s int, updated common.UnixTimestamp, ranked int) error {
|
|
|
|
expire := Expire
|
|
|
|
if ranked == 2 {
|
|
|
|
expire *= 6
|
|
|
|
}
|
|
|
|
if time.Now().Before(time.Time(updated).Add(expire)) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
beatmaps, err := Client.GetBeatmaps(osuapi.GetBeatmapsOpts{
|
|
|
|
BeatmapSetID: s,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, beatmap := range beatmaps {
|
|
|
|
err := UpdateIfRequired(BeatmapDefiningQuality{
|
|
|
|
ID: beatmap.BeatmapID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|