reworked sharecode return
This commit is contained in:
@@ -62,7 +62,6 @@ linters:
|
|||||||
- govet
|
- govet
|
||||||
- lll
|
- lll
|
||||||
- misspell
|
- misspell
|
||||||
- nakedret
|
|
||||||
- noctx
|
- noctx
|
||||||
- nolintlint
|
- nolintlint
|
||||||
- staticcheck
|
- staticcheck
|
||||||
|
@@ -9,7 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
//goland:noinspection SpellCheckingInspection
|
//goland:noinspection SpellCheckingInspection
|
||||||
var DICT = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789"
|
const Base57Chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789"
|
||||||
|
|
||||||
var sharecodeRexEx = regexp.MustCompile(`^CSGO(?:-?\w{5}){5}$`)
|
var sharecodeRexEx = regexp.MustCompile(`^CSGO(?:-?\w{5}){5}$`)
|
||||||
|
|
||||||
func DecodeSharecode(code string) (matchID, outcomeID uint64, tokenID uint16, err error) {
|
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, ""))
|
chars := ReverseString(strings.Split(cleanCode, ""))
|
||||||
bigInt := new(big.Int)
|
bigInt := new(big.Int)
|
||||||
dictLenBig := big.NewInt(int64(len(DICT)))
|
dictLenBig := big.NewInt(int64(len(Base57Chars)))
|
||||||
|
|
||||||
for _, c := range chars {
|
for _, c := range chars {
|
||||||
bigInt.Mul(bigInt, dictLenBig)
|
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
|
if bigInt.BitLen() > 144 { //nolint:gomnd
|
||||||
|
2
main.go
2
main.go
@@ -238,7 +238,7 @@ func housekeeping() {
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
default:
|
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
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -432,7 +432,6 @@ func DeleteMatch(matchDel *ent.Match, db *ent.Client) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:nakedret
|
|
||||||
func GetWinLossTieForPlayer(dbPlayer *ent.Player) (wins, looses, ties int, err error) {
|
func GetWinLossTieForPlayer(dbPlayer *ent.Player) (wins, looses, ties int, err error) {
|
||||||
var res []struct {
|
var res []struct {
|
||||||
MatchResult int `json:"match_result"`
|
MatchResult int `json:"match_result"`
|
||||||
@@ -516,7 +515,7 @@ func GetNewShareCodesForPlayer(tPlayer *ent.Player, apiKey string, rl *rate.Limi
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var newShareCode string
|
var newShareCode *string
|
||||||
if oldestMatch.ShareCode == tPlayer.OldestSharecodeSeen {
|
if oldestMatch.ShareCode == tPlayer.OldestSharecodeSeen {
|
||||||
newShareCode, err = getNextShareCode(latestMatch.ShareCode, apiKey, tPlayer.AuthCode, tPlayer.ID, rl)
|
newShareCode, err = getNextShareCode(latestMatch.ShareCode, apiKey, tPlayer.AuthCode, tPlayer.ID, rl)
|
||||||
} else {
|
} else {
|
||||||
@@ -527,8 +526,8 @@ func GetNewShareCodesForPlayer(tPlayer *ent.Player, apiKey string, rl *rate.Limi
|
|||||||
}
|
}
|
||||||
|
|
||||||
var rCodes []string
|
var rCodes []string
|
||||||
for newShareCode != "n/a" {
|
for newShareCode != nil {
|
||||||
rCodes = append(rCodes, newShareCode)
|
rCodes = append(rCodes, *newShareCode)
|
||||||
newShareCode, err = getNextShareCode(rCodes[len(rCodes)-1], apiKey, tPlayer.AuthCode, tPlayer.ID, rl)
|
newShareCode, err = getNextShareCode(rCodes[len(rCodes)-1], apiKey, tPlayer.AuthCode, tPlayer.ID, rl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -543,53 +542,53 @@ func GetNewShareCodesForPlayer(tPlayer *ent.Player, apiKey string, rl *rate.Limi
|
|||||||
return rCodes, nil
|
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 {
|
if lastCode == "" || apiKey == "" || authCode == "" || steamID == 0 {
|
||||||
return "", fmt.Errorf("invalid arguments")
|
return nil, fmt.Errorf("invalid arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
if rl != nil {
|
if rl != nil {
|
||||||
err := rl.Wait(context.Background())
|
err := rl.Wait(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Debugf("[SC] STEAMPI with %s", fmt.Sprintf(shareCodeURLEntry, "REDACTED", steamID, "REDACTED", lastCode))
|
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
|
r, err := http.Get(fmt.Sprintf(shareCodeURLEntry, apiKey, steamID, authCode, lastCode)) //nolint:noctx
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch r.StatusCode {
|
switch r.StatusCode {
|
||||||
case http.StatusAccepted:
|
case http.StatusAccepted:
|
||||||
return "n/a", nil
|
return nil, nil
|
||||||
case http.StatusTooManyRequests:
|
case http.StatusTooManyRequests:
|
||||||
return "", ErrorAuthcodeRateLimit
|
return nil, ErrorAuthcodeRateLimit
|
||||||
case http.StatusServiceUnavailable:
|
case http.StatusServiceUnavailable:
|
||||||
return "", ErrorAuthcodeRateLimit
|
return nil, ErrorAuthcodeRateLimit
|
||||||
case http.StatusPreconditionFailed:
|
case http.StatusPreconditionFailed:
|
||||||
return "", ErrorSharecodeNoMatch
|
return nil, ErrorSharecodeNoMatch
|
||||||
case http.StatusForbidden:
|
case http.StatusForbidden:
|
||||||
return "", ErrorAuthcodeUnauthorized
|
return nil, ErrorAuthcodeUnauthorized
|
||||||
case http.StatusOK:
|
case http.StatusOK:
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
return "", errors.New("temporary steamapi error")
|
return nil, fmt.Errorf("temporary steamapi error (HTTP %d)", r.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
bJSON, err := io.ReadAll(r.Body)
|
bJSON, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
_ = r.Body.Close()
|
_ = r.Body.Close()
|
||||||
|
|
||||||
rJSON := new(ShareCodeResponse)
|
rJSON := new(ShareCodeResponse)
|
||||||
err = json.Unmarshal(bJSON, rJSON)
|
err = json.Unmarshal(bJSON, rJSON)
|
||||||
if err != nil {
|
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) {
|
func Player(db *ent.Client, id interface{}, apiKey string, rl *rate.Limiter) (*ent.Player, error) {
|
||||||
|
Reference in New Issue
Block a user