ripple-api/beatmapget/fullset.go

82 lines
1.9 KiB
Go
Raw Normal View History

2016-09-06 23:51:23 +00:00
package beatmapget
import (
"database/sql"
"errors"
2016-09-06 23:51:23 +00:00
"time"
2019-02-25 21:04:55 +00:00
"github.com/osuyozora/api/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
)
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)
if err != nil && err != sql.ErrNoRows {
2016-09-06 23:51:23 +00:00
return err
}
return set(s, lastUpdated, ranked)
}
// 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.
func Beatmap(b int) (int, error) {
2016-09-06 23:51:23 +00:00
var (
setID int
lastUpdated common.UnixTimestamp
ranked int
)
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)
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
}