-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.go
106 lines (98 loc) · 2.14 KB
/
player.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package datatype
import "github.com/tidwall/gjson"
type Player struct {
Username string
Account string
Password string
UUID string
AccessToken string
ClientToken string
Id_ string
SharedSecret string
}
func (pl *Player) GetToken() bool {
if pl.authenticate() {
return true
} else {
//尝试连接5次
for i := 5; i < 0; i-- {
if pl.authenticate() {
return true
}
}
}
return false
}
func (pl *Player) authenticate() bool {
param := `{
"agent": {
"name": "Minecraft",
"version": 1
},
"username": "` + pl.Account + `",
"password": "` + pl.Password + `"
}`
if res, ok := TokenRequest("authenticate", param); ok {
jsonresp := gjson.Parse(res)
pl.AccessToken = jsonresp.Get("accessToken").String()
if pl.AccessToken == "" {
return false
}
pl.ClientToken = jsonresp.Get("clientToken").String()
selectedProfile := jsonresp.Get("selectedProfile")
pl.Id_ = selectedProfile.Get("id").String()
pl.Username = selectedProfile.Get("name").String()
return true
}
return false
}
func (pl *Player) Signout() bool {
if pl.Account == "" {
return false
}
param := `{
"username": "` + pl.Account + `",
"password": "` + pl.Password + `"
}`
if res, ok := TokenRequest("signout", param); ok && (res == "") {
return true
}
return false
}
func (pl *Player) validate() bool {
param := `{
"accessToken": "` + pl.AccessToken + `",
"clientToken": "` + pl.ClientToken + `"
}`
if res, ok := TokenRequest("validate", param); ok && (res == "") {
return true
}
return false
}
func (pl *Player) Invalidate() bool {
if pl.AccessToken == "" {
return false
}
param := `{
"accessToken": "` + pl.AccessToken + `",
"clientToken": "` + pl.ClientToken + `"
}`
if res, ok := TokenRequest("invalidate", param); ok && (res == "") {
return true
}
return false
}
func (pl *Player) SetSession(serverId string) bool {
if pl.AccessToken == "" {
pl.GetToken()
}
param := `{
"accessToken": "` + pl.AccessToken + `",
"selectedProfile": "` + pl.Id_ + `",
"serverId": "` + serverId + `"
}`
if res, ok := SessionRequest(param); ok && (res == "") {
return true
}
return false
}