-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteamid.go
75 lines (60 loc) · 1.65 KB
/
steamid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package steamid
import (
"fmt"
"strconv"
"strings"
)
type SteamID uint64
const steamIDBase = 76561197960265728
// SteamID3 parses steam ids in the form '[U:1:22202]'
func SteamID3(steamString string) (SteamID, error) {
split := strings.Split(steamString[:len(steamString)-1], ":")
if len(split) != 3 {
return 0, fmt.Errorf("invalid SteamID3")
}
w, err := strconv.Atoi(split[2])
if err != nil {
return 0, fmt.Errorf("invalid SteamID3: %w", err)
}
return SteamID(w + steamIDBase), nil
}
// SteamID32 parses steam ids in the form 'STEAM_0:0:11101'
func SteamID32(steamString string) (SteamID, error) {
Y, err := strconv.Atoi(steamString[8:9])
if err != nil {
return 0, err
}
Z, err := strconv.Atoi(steamString[10:])
if err != nil {
return 0, err
}
i := int64((Z * 2) + steamIDBase + Y)
return SteamID(i), nil
}
// SteamID64 parses steam ids in the form '76561197960287930'
func SteamID64(steamString string) (SteamID, error) {
i, err := strconv.ParseInt(steamString, 10, 64)
if err != nil {
return 0, err
}
if i < steamIDBase {
return 0, fmt.Errorf("SteamID64 is too small")
}
return SteamID(i), nil
}
// SteamID32String returns the steam id in the form 'STEAM_0:0:11101'
func (s SteamID) SteamID32String() string {
s = s - steamIDBase
remainder := s % 2
s = s / 2
return fmt.Sprintf("STEAM_0:%d:%d", remainder, s)
}
// SteamID64String returns the steam id in the form '76561197960287930'
func (s SteamID) SteamID64String() string {
return strconv.FormatInt(int64(s), 10)
}
// SteamID3String returns the steam id in the form '[U:1:22202]'
func (s SteamID) SteamID3String() string {
s = s - steamIDBase
return fmt.Sprintf("[U:1:%d]", s)
}