reworked sharecode return

This commit is contained in:
2022-11-28 12:48:19 +01:00
parent 43c91b694e
commit 1ac07c2f5a
4 changed files with 21 additions and 22 deletions

View File

@@ -62,7 +62,6 @@ linters:
- govet
- lll
- misspell
- nakedret
- noctx
- nolintlint
- staticcheck

View File

@@ -9,7 +9,8 @@ import (
)
//goland:noinspection SpellCheckingInspection
var DICT = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789"
const Base57Chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789"
var sharecodeRexEx = regexp.MustCompile(`^CSGO(?:-?\w{5}){5}$`)
func DecodeSharecode(code string) (matchID, outcomeID uint64, tokenID uint16, err error) {
@@ -21,11 +22,11 @@ func DecodeSharecode(code string) (matchID, outcomeID uint64, tokenID uint16, er
chars := ReverseString(strings.Split(cleanCode, ""))
bigInt := new(big.Int)
dictLenBig := big.NewInt(int64(len(DICT)))
dictLenBig := big.NewInt(int64(len(Base57Chars)))
for _, c := range chars {
bigInt.Mul(bigInt, dictLenBig)
bigInt.Add(bigInt, big.NewInt(int64(strings.Index(DICT, c))))
bigInt.Add(bigInt, big.NewInt(int64(strings.Index(Base57Chars, c))))
}
if bigInt.BitLen() > 144 { //nolint:gomnd

View File

@@ -238,7 +238,7 @@ func housekeeping() {
}
continue
default:
log.Errorf("[HK] Error while requesting sharecodes for %d: %v", tPlayer.ID, err)
log.Warningf("[HK] Error while requesting sharecodes for %d: %v", tPlayer.ID, err)
continue
}
}

View File

@@ -432,7 +432,6 @@ func DeleteMatch(matchDel *ent.Match, db *ent.Client) error {
return nil
}
//nolint:nakedret
func GetWinLossTieForPlayer(dbPlayer *ent.Player) (wins, looses, ties int, err error) {
var res []struct {
MatchResult int `json:"match_result"`
@@ -516,7 +515,7 @@ func GetNewShareCodesForPlayer(tPlayer *ent.Player, apiKey string, rl *rate.Limi
return nil, err
}
var newShareCode string
var newShareCode *string
if oldestMatch.ShareCode == tPlayer.OldestSharecodeSeen {
newShareCode, err = getNextShareCode(latestMatch.ShareCode, apiKey, tPlayer.AuthCode, tPlayer.ID, rl)
} else {
@@ -527,8 +526,8 @@ func GetNewShareCodesForPlayer(tPlayer *ent.Player, apiKey string, rl *rate.Limi
}
var rCodes []string
for newShareCode != "n/a" {
rCodes = append(rCodes, newShareCode)
for newShareCode != nil {
rCodes = append(rCodes, *newShareCode)
newShareCode, err = getNextShareCode(rCodes[len(rCodes)-1], apiKey, tPlayer.AuthCode, tPlayer.ID, rl)
if err != nil {
return nil, err
@@ -543,53 +542,53 @@ func GetNewShareCodesForPlayer(tPlayer *ent.Player, apiKey string, rl *rate.Limi
return rCodes, nil
}
func getNextShareCode(lastCode, apiKey, authCode string, steamID uint64, rl *rate.Limiter) (string, error) {
func getNextShareCode(lastCode, apiKey, authCode string, steamID uint64, rl *rate.Limiter) (*string, error) {
if lastCode == "" || apiKey == "" || authCode == "" || steamID == 0 {
return "", fmt.Errorf("invalid arguments")
return nil, fmt.Errorf("invalid arguments")
}
if rl != nil {
err := rl.Wait(context.Background())
if err != nil {
return "", err
return nil, err
}
}
log.Debugf("[SC] STEAMPI with %s", fmt.Sprintf(shareCodeURLEntry, "REDACTED", steamID, "REDACTED", lastCode))
r, err := http.Get(fmt.Sprintf(shareCodeURLEntry, apiKey, steamID, authCode, lastCode)) //nolint:noctx
if err != nil {
return "", err
return nil, err
}
switch r.StatusCode {
case http.StatusAccepted:
return "n/a", nil
return nil, nil
case http.StatusTooManyRequests:
return "", ErrorAuthcodeRateLimit
return nil, ErrorAuthcodeRateLimit
case http.StatusServiceUnavailable:
return "", ErrorAuthcodeRateLimit
return nil, ErrorAuthcodeRateLimit
case http.StatusPreconditionFailed:
return "", ErrorSharecodeNoMatch
return nil, ErrorSharecodeNoMatch
case http.StatusForbidden:
return "", ErrorAuthcodeUnauthorized
return nil, ErrorAuthcodeUnauthorized
case http.StatusOK:
break
default:
return "", errors.New("temporary steamapi error")
return nil, fmt.Errorf("temporary steamapi error (HTTP %d)", r.StatusCode)
}
bJSON, err := io.ReadAll(r.Body)
if err != nil {
return "", err
return nil, err
}
_ = r.Body.Close()
rJSON := new(ShareCodeResponse)
err = json.Unmarshal(bJSON, rJSON)
if err != nil {
return "", err
return nil, err
}
return rJSON.Result.Code, nil
return &rJSON.Result.Code, nil
}
func Player(db *ent.Client, id interface{}, apiKey string, rl *rate.Limiter) (*ent.Player, error) {