Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
},
"api_secret_key": "",
"api_base_url": "http://localhost:8082",
"internal_api_secret": "",
"api_bot_jwt": ""
}
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Configuration struct {
DiscordWebhookSubmissionErrors ConfigurationDiscordWebhook `json:"discord_webhook_submission_errors"`
DiscordWebhookAnticheat ConfigurationDiscordWebhook `json:"discord_webhook_anticheat"`
APIBaseUrl string `json:"api_base_url"`
InternalAPISecret string `json:"internal_api_secret"`
APISecretKey string `json:"api_secret_key"`
APIBotJWT string `json:"api_bot_jwt"`
}
Expand Down
86 changes: 30 additions & 56 deletions db/scoreboard.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,45 @@
package db

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/go-redis/redis/v8"
"github.com/Swan/Nameless/config"
)

type scoreboardScore struct {
PerformanceRating float64 `json:"performance_rating"`
var scoreboardCacheClient = &http.Client{
Timeout: 10 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}

// UpdateScoreboardCache Updates the redis cache for a particular score
func UpdateScoreboardCache(s *Score, m *Map) error {
patterns := []string{
fmt.Sprintf("quaver:scores:%v_*", m.Id),
fmt.Sprintf("quaver:scoreboard:%v:*", m.MD5),
// UpdateScoreboardCache clears a map's scoreboard cache through api-v2.
func UpdateScoreboardCache(_ *Score, m *Map) error {
endpoint := strings.TrimRight(config.Data.APIBaseUrl, "/") +
"/v2/private/scoreboards/" + url.PathEscape(m.MD5) + "/cache"
req, err := http.NewRequest(http.MethodDelete, endpoint, nil)
if err != nil {
return fmt.Errorf("create scoreboard cache invalidation request: %w", err)
}
req.Header.Set("X-Internal-Secret", config.Data.InternalAPISecret)
req.Header.Set("Accept", "application/json")

for _, pattern := range patterns {
iter := Redis.Scan(RedisCtx, 0, pattern, 100).Iterator()
for iter.Next(RedisCtx) {
key := iter.Val()
str, err := Redis.Get(RedisCtx, key).Result()

if err != nil {
// The key does not exist anymore.
if err == redis.Nil {
continue
}

return err
}

var scores []scoreboardScore
err = json.Unmarshal([]byte(str), &scores)

if err != nil {
return err
}

if len(scores) < 50 {
err = Redis.Del(RedisCtx, key).Err()

if err != nil {
return err
}

continue
}

for _, score := range scores {
if s.PerformanceRating > score.PerformanceRating {
err = Redis.Del(RedisCtx, key).Err()

if err != nil {
return err
}

break
}
}
}
response, err := scoreboardCacheClient.Do(req)
if err != nil {
return fmt.Errorf("invalidate scoreboard cache: %w", err)
}
defer response.Body.Close()

if err := iter.Err(); err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return fmt.Errorf("scoreboard cache invalidation failed with status - %v", response.StatusCode)
}
if _, err := io.Copy(io.Discard, response.Body); err != nil {
return fmt.Errorf("read scoreboard cache invalidation response: %w", err)
}

return nil
Expand Down