-
Notifications
You must be signed in to change notification settings - Fork 0
/
req.py
257 lines (227 loc) · 6.92 KB
/
req.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# -*- coding:utf-8 -*-
import requests
import json
import card_order
def status_display(key):
dict = {
0: '成功',
1001: '用户名已被使用',
1002: '学号已绑定',
1003: '教务处认证失败',
1004: 'Token过期',
2001: '未结束战局过多',
2002: '出千!!!',
2003: '不合法墩牌',
2003: '战局不存在或已结束',
3001: '战局不存在或未结束',
3002: '玩家不存在'
}
if key in dict:
print(dict[key])
else:
print('KeyError %d' % key)
# 玩家类
class Player(object):
def __init__(self, user, psw):
self.user = user
self.psw = psw
self.user_id = None
self.token = None
self.is_register = None
self.is_log = None
self.id = None
self.cards = None
def show(self):
print()
print(self.user)
print(self.psw)
print(self.user_id)
print(self.token)
print(self.is_register)
print(self.is_log)
print(self.id)
print(self.cards)
print()
# 注册
def register(self):
url = 'https://api.shisanshui.rtxux.xyz/auth/register'
headers = {'Content-Type': 'application/json'}
data = {
'username': self.user,
'password': self.psw
}
r = requests.post(url, headers=headers, data=json.dumps(data))
status = r.json()['status']
if status == 0 and 'user_id' in r.json()['data']:
self.user_id = r.json()['data']['user_id']
self.is_register = True
print('register may be successful')
print(r.text)
else:
print('register fialed!')
status_display(status)
print(r.text)
# 登录
def login(self):
url = "https://api.shisanshui.rtxux.xyz/auth/login"
headers = {
'content-type': 'application/json'
}
data = {
"username": self.user,
"password": self.psw
}
r = requests.post(url, data=json.dumps(data), headers=headers)
status = r.json()['status']
if status == 0 and 'user_id' in r.json()['data']:
self.user_id = r.json()['data']['user_id']
self.token = r.json()['data']['token']
self.is_log = True
print('Login may be successful')
print(r.text)
else:
print('login fialed!')
status_display(status)
print(r.text)
# 注销
def logout(self):
url = "https://api.shisanshui.rtxux.xyz/auth/logout"
headers = {
'x-auth-token': self.token
}
r = requests.post(url, headers=headers)
status = r.json()['status']
if status == 0:
self.logged = False
print('Logout may be successful')
print(r.text)
else:
print('logout fialed!')
status_display(status)
print(r.text)
# 登录验证
def validate_token(self):
url = 'https://api.shisanshui.rtxux.xyz/auth/validate'
headers = {
'X-Auth-Token': self.token
}
r = requests.get(url, headers=headers)
status = r.json()['status']
if status == 0 and 'user_id' in r.json()['data']:
if 'token' in r.json()['data']:
self.token = r.json()['data']['token']
print('validate may be successful')
print(r.text)
else:
print('validate fialed!')
status_display(status)
print(r.text)
# 返回登陆状态
def check_login_status(self):
return self.is_log
def check_register_status(self):
return self.is_register
# 开局
def game_open(self):
url = "https://api.shisanshui.rtxux.xyz/game/open"
headers = {
'x-auth-token': self.token
}
r = requests.post(url, headers=headers)
status = r.json()['status']
if status == 0:
self.id = r.json()['data']['id']
self.cards = r.json()['data']['card']
print('game_open may be successful')
print(r.text)
else:
print('game_open fialed!')
status_display(status)
print(r.text)
return self.cards
# 出牌
def game_submit(self, submit_card):
url = "https://api.shisanshui.rtxux.xyz/game/submit"
data = {
'id': self.id,
'card': submit_card
}
headers = {
'content-type': "application/json",
'x-auth-token': self.token
}
r = requests.post(url, data=json.dumps(data), headers=headers)
status = r.json()['status']
print('game_submit may be successful')
print(r.text)
if status != 0:
print('game_submit fialed!')
status_display(status)
print(r.text)
# 历史
def get_history(self, page, limit, play_id):
url = "https://api.shisanshui.rtxux.xyz/history"
querystring = {
"page": page,
"limit": limit,
"player_id": play_id
}
headers = {
'x-auth-token': self.token
}
r = requests.get(url, headers=headers, params=querystring)
data = []
status = r.json()['status']
if status == 0:
data = r.json()['data']
print('get_history may be successful')
print(r.text)
else:
print('get_history fialed!')
status_display(status)
print(r.text)
return data
def get_history_detail(self, id):
url = "https://api.shisanshui.rtxux.xyz/history/" + str(id)
headers = {
'x-auth-token': self.token
}
r = requests.get(url, headers=headers)
data = []
status = r.json()['status']
if status == 0:
data = r.json()['data']['detail']
print('get_history_detail may be successful')
status_display(status)
print(r.text)
else:
print('get_history_detail fialed!')
status_display(status)
print(r.text)
return data
def get_rank(self):
url = "https://api.shisanshui.rtxux.xyz/rank"
headers = {
'x-auth-token': self.token
}
r = requests.get(url, headers=headers)
data = []
data = r.json()
print('get_rank may be successful')
return data
def main():
p1 = Player('sddd12', '87654321')
# p1.register()
p1.login()
card = p1.game_open()
s = card_order.AI(card)
p1.game_submit(s)
data = p1.get_history(0, 20, p1.user_id)
# data = p1.get_rank()
# print(data)
data = p1.get_history_detail(14785)
# print(data)
p1.validate_token()
p1.logout()
if __name__ == '__main__':
main()