diff --git a/main.go b/main.go index 063b447..070d5fc 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "encoding/gob" "entgo.io/ent/dialect" "entgo.io/ent/dialect/sql" + "errors" "flag" "fmt" "git.harting.dev/csgowtf/csgowtfd/csgo" @@ -215,18 +216,24 @@ func housekeeping() { for _, tPlayer := range tPlayerNeedShareCodeUpdate { shareCodes, err := utils.GetNewShareCodesForPlayer(tPlayer, conf.Steam.APIKey, rL) if err != nil { - switch err.(type) { - case utils.AuthcodeUnauthorizedError: + if errors.Is(err, utils.AuthcodeUnauthorizedError) { log.Infof("[HK] authCode for player %d is no longer valid", tPlayer.ID) err = tPlayer.Update().ClearAuthCode().ClearSharecodeUpdated().Exec(context.Background()) if err != nil { log.Errorf("[HK] Unable to clear authcode for player %d: %v", tPlayer.ID, err) } continue - case utils.SharecodeNoMatchError: + } else if errors.Is(err, utils.SharecodeNoMatchError) { log.Warningf("[HK] last shareCode for player %d does not match player", tPlayer.ID) continue - default: + } else if errors.Is(err, utils.NoMatchError) { + log.Infof("[HK] tracked player with no matched found, untrack him") + err = tPlayer.Update().ClearAuthCode().ClearSharecodeUpdated().Exec(context.Background()) + if err != nil { + log.Errorf("[HK] Unable to clear authcode for player %d: %v", tPlayer.ID, err) + } + continue + } else { log.Errorf("[HK] Error while requesting sharecodes for %d: %v", tPlayer.ID, err) continue } @@ -477,16 +484,14 @@ func deletePlayerTrack(c *gin.Context) { _, err = utils.IsAuthCodeValid(tPlayer, conf.Steam.APIKey, "", authCode, nil) if err != nil { - switch e := err.(type) { - case utils.AuthcodeUnauthorizedError: - log.Infof("[DPT] authCode provided for player %s is invalid: %v", id, e) + if errors.Is(err, utils.AuthcodeUnauthorizedError) { + log.Infof("[DPT] authCode provided for player %s is invalid: %v", id, err) c.Status(http.StatusUnauthorized) return - default: - log.Infof("[DPT] Temporary Steam-API problem: %v", e) - c.Status(http.StatusServiceUnavailable) - return } + log.Infof("[DPT] Temporary Steam-API problem: %v", err) + c.Status(http.StatusServiceUnavailable) + return } err = tPlayer.Update().ClearAuthCode().ClearSharecodeUpdated().Exec(context.Background()) @@ -528,17 +533,16 @@ func postPlayerTrack(c *gin.Context) { _, err = utils.IsAuthCodeValid(tPlayer, conf.Steam.APIKey, shareCode, authCode, rL) if err != nil { - switch e := err.(type) { - case utils.AuthcodeUnauthorizedError: - log.Infof("[PPT] authCode provided for player %s is invalid: %v", id, e) + if errors.Is(err, utils.AuthcodeUnauthorizedError) { + log.Infof("[PPT] authCode provided for player %s is invalid: %v", id, err) c.Status(http.StatusUnauthorized) return - case utils.SharecodeNoMatchError: - log.Infof("[PPT] shareCode provided for player %s (%s) is invalid: %v", id, shareCode, e) + } else if errors.Is(err, utils.SharecodeNoMatchError) { + log.Infof("[PPT] shareCode provided for player %s (%s) is invalid: %v", id, shareCode, err) c.Status(http.StatusPreconditionFailed) return - default: - log.Infof("[PPT] problem with request: %v", e) + } else { + log.Infof("[PPT] problem with request: %v", err) c.Status(http.StatusServiceUnavailable) return } diff --git a/utils/utils.go b/utils/utils.go index f2885e5..df6608c 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -217,16 +217,11 @@ type MatchResponse struct { TickRate float64 `json:"tick_rate,omitempty"` } -type ( - AuthcodeUnauthorizedError struct { - error - } - AuthcodeRateLimitError struct { - error - } - SharecodeNoMatchError struct { - error - } +var ( + SharecodeNoMatchError = errors.New("sharecode not provided") + AuthcodeRateLimitError = errors.New("temporary rate-limited/unavailable") + AuthcodeUnauthorizedError = errors.New("authcode unauthorized") + NoMatchError = errors.New("no match found") ) const ( @@ -485,7 +480,7 @@ func IsAuthCodeValid(player *ent.Player, apiKey string, shareCode string, authCo if shareCode == "" { tMatch, err = player.QueryMatches().Order(ent.Asc(match.FieldDate)).First(context.Background()) if err != nil { - return false, SharecodeNoMatchError{errors.New("sharecode not provided")} + return false, SharecodeNoMatchError } _, err := getNextShareCode(tMatch.ShareCode, apiKey, authCode, player.ID, rl) @@ -505,11 +500,17 @@ func IsAuthCodeValid(player *ent.Player, apiKey string, shareCode string, authCo func GetNewShareCodesForPlayer(player *ent.Player, apiKey string, rl *rate.Limiter) ([]string, error) { latestMatch, err := player.QueryMatches().Order(ent.Desc(match.FieldDate)).First(context.Background()) if err != nil { + if ent.IsNotFound(err) { + return nil, NoMatchError + } return nil, err } oldestMatch, err := player.QueryMatches().Order(ent.Asc(match.FieldDate)).First(context.Background()) if err != nil { + if ent.IsNotFound(err) { + return nil, NoMatchError + } return nil, err } @@ -561,13 +562,13 @@ func getNextShareCode(lastCode string, apiKey string, authCode string, steamId u case http.StatusAccepted: return "n/a", nil case http.StatusTooManyRequests: - return "", AuthcodeRateLimitError{errors.New("temporary ratelimited")} + return "", AuthcodeRateLimitError case http.StatusServiceUnavailable: - return "", AuthcodeRateLimitError{errors.New("temporary unavailable")} + return "", AuthcodeRateLimitError case http.StatusPreconditionFailed: - return "", SharecodeNoMatchError{errors.New("sharecode not from player history")} + return "", SharecodeNoMatchError case http.StatusForbidden: - return "", AuthcodeUnauthorizedError{errors.New("authcode unauthorized")} + return "", AuthcodeUnauthorizedError case http.StatusOK: break default: