-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
353 lines (322 loc) · 13.9 KB
/
main.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import base64
import datetime
import json
import re
from time import sleep
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.ocr.v20181119 import ocr_client, models
import api
import game
import room
def at_player_by_uid(uid):
return '[at,uid=' + str(uid) + ']'
class Bot(object):
def __init__(self):
self.kanaLink = 'https://kana.byha.top:444/'
config = json.load(open("config.json", 'r'))
self.username = config['username'] # 用户名
self.password = config['password'] # 密码
self.room_id = config['roomID'] # 房间号
self.secretId = config['secretId']
self.secretKey = config['secretKey']
self.default_user_remain_win_time = 10
self.last_update_time = 0
# 以下是每日更新的数据
self.user_remain_win_time = {} # 每个玩家的单挑剩余次数
self.game_count = [] # 每种对局的次数
self.user_score = {} # 每个玩家的分数
def enter_room(self):
"""进入房间"""
self.driver.get(
'https://kana.byha.top:444/checkmate/room/' + self.room.id)
if self.room.secret:
settingBtn = self.driver.find_element_by_class_name(
'form-check-input')
ac = ActionChains(self.driver)
ac.click(settingBtn).perform()
print('Bot已就位!')
self.url = self.driver.current_url
def logout(self): # 登出
print('正在登出…')
self.driver.get('https://kana.byha.top:444/logout')
self.driver.switch_to.default_content()
sleep(5)
self.driver.find_element_by_id('submitButton').click()
return
def del_element(self, a):
"""删除页面中元素"""
self.driver.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", a)
return
def login(self):
"""登录"""
print('正在登录…')
self.driver.get(self.kanaLink)
self.del_element(self.driver.find_element_by_class_name('redirect'))
usernameBox = self.driver.find_element_by_name('username')
passwordBox = self.driver.find_element_by_name('pwd')
ac = ActionChains(self.driver)
ac.send_keys_to_element(usernameBox, self.username)
ac.send_keys_to_element(passwordBox, self.password).perform()
cap_correction = {'丫': 'Y', '了': '3', '尺': 'R'} # 手动纠错
while True:
if self.driver.current_url == self.kanaLink:
break
self.driver.execute_script('document.getElementById("submitButton").style.display = "none"')
self.driver.execute_script('document.getElementById("cap").childNodes[1].style.width="150%"')
frame = self.driver.find_element_by_xpath('/html/body/div[2]/div/form/div[1]/object')
self.driver.switch_to.frame(frame)
try:
a = self.driver.find_element_by_css_selector('[fill="none"]')
self.del_element(a)
except:
sleep(1)
continue
self.driver.switch_to.default_content()
cap = -1
self.driver.get_screenshot_as_file('a.png')
try:
cred = credential.Credential(self.secretId, self.secretKey)
http_profile = HttpProfile()
http_profile.endpoint = 'ocr.tencentcloudapi.com'
client_profile = ClientProfile()
client_profile.httpProfile = http_profile
client = ocr_client.OcrClient(cred, 'ap-shanghai', client_profile)
req = models.GeneralBasicOCRRequest()
params = {
'ImageBase64': base64.b64encode(open('a.png', 'rb').read()).decode()
}
req.from_json_string(json.dumps(params))
resp = client.GeneralBasicOCR(req)
a = json.loads(resp.to_json_string())
except TencentCloudSDKException as err:
print(err)
self.driver.execute_script('document.getElementById("cap").childNodes[1].style.width="100%"')
for i in a['TextDetections']:
tmp = i['DetectedText']
s = ''
for j in tmp:
if j != '(' and j != ')': # 去除诡异括号
s += j
if j in cap_correction:
s += cap_correction[j]
tmp = s
if re.match(r'\w\w\w\w', tmp) and len(tmp) == 4:
cap = tmp
break
print(cap)
ac = ActionChains(self.driver)
ac.send_keys_to_element(self.driver.find_element_by_name("cap"), cap).perform()
self.driver.execute_script('document.getElementById("submitButton").style.display = ""')
self.driver.find_element_by_id("submitButton").click()
try:
WebDriverWait(self.driver, 10).until(EC.url_to_be(self.kanaLink))
break
except TimeoutException:
pass
print("登录成功!")
return
def analyze(self):
"""数据分析"""
analyze_data = {}
uid = 1
max_uid = 1
while True:
info = api.get_user_info_by_uid(uid)
if info == '数据库错误':
if uid <= 3800:
uid += 1
continue
else:
break
exp = api.get_user_exp_by_uid(uid)
analyze_data[uid] = {}
analyze_data[uid]['info'] = info
analyze_data[uid]['exp'] = exp
analyze_data[uid]['username'] = info['username']
max_uid = uid
uid += 1
ans = ''
cur_time = datetime.datetime.now()
ans += '更新于' + str(cur_time.year) + '.' + str(cur_time.month) + '.' + str(cur_time.day) + ',' + str(
cur_time.hour) + ':' + str(cur_time.minute) + ':' + str(cur_time.second) + '\n\n'
uid_keys = list(analyze_data.keys())
for i in range(len(uid_keys)):
uid_keys[i] = int(uid_keys[i])
tot = 0
for uid in uid_keys:
if uid != 1:
tot += int(analyze_data[uid].get('exp', 0))
ans += '### 经验值分布统计(排除admin)\n\n'
ans += '用户经验总和:' + str(tot) + '\n\n'
role = lambda a: a[1]
k = []
for i in range(1, max_uid // 1000 + 2):
l = max((i - 1) * 1000 + 1, 2)
r = min(i * 1000, max_uid)
if l > max_uid:
break
tot = 0
max_exp = 0
max_user = 0
for j in range(l, r + 1):
if j in uid_keys:
exp = int(analyze_data[j].get('exp', 0))
tot += exp
k.append([j, exp])
if exp > max_exp:
max_user = j
max_exp = exp
ans += str(l) + '~' + str(r) + '总和:' + str(tot) + ',最大值' + at_player_by_uid(max_user) + '(' + str(
max_exp) + '经验)\n\n'
k.sort(key=role, reverse=True)
ans += '经验排行榜前50名:\n\n'
for i in range(50):
ans += '#' + str(i + 1) + ':' + at_player_by_uid(k[i][0]) + ',' + str(k[i][1]) + '经验\n\n'
ans += '### 今日数据\n\n'
for i in range(2, 9):
ans += str(i) + '人局,' + str(self.game_count[i]) + '场\n\n'
ans += '单挑排行榜:(按剩余单挑次数排列)\n\n'
k = []
for i in uid_keys:
cur_uname = analyze_data[i].get('username', 'undefined')
cur_win_time = self.user_remain_win_time.get(cur_uname, self.default_user_remain_win_time)
if cur_win_time < self.default_user_remain_win_time:
k.append([i, cur_win_time])
k.sort(key=role)
for i in range(len(k)):
ans += '#' + str(i + 1) + ':' + at_player_by_uid(k[i][0]) + ',剩余' + str(k[i][1]) + '次\n\n'
ans += '分数排行榜\n\n'
k = []
for i in uid_keys:
cur_uname = analyze_data[i].get('username', 'undefined')
cur_score = self.user_score.get(cur_uname, 0)
if cur_score > 0:
k.append([i, cur_score])
k.sort(key=role, reverse=True)
for i in range(len(k)):
ans += '#' + str(i + 1) + ':' + at_player_by_uid(k[i][0]) + ',' + str(k[i][1]) + '分\n\n'
ans += '### 波特家族统计\n\n'
cnt = 0
for i in uid_keys:
cur_uname = analyze_data[i].get('username', 'undefined')
if cur_uname.lower().find('bot') != -1:
ans += at_player_by_uid(i) + ' '
cnt += 1
ans += '\n\n共' + str(cnt) + '个'
api.update_post(16903, ans)
def clear_data(self):
"""清除每日数据"""
self.user_remain_win_time = {}
self.game_count = [0, 0, 0, 0, 0, 0, 0, 0, 0]
self.user_score = {}
def main(self):
self.driver = webdriver.Firefox() # 浏览器
# self.driver = webdriver.Chrome()
self.game = game.Game(self.driver)
self.room = room.Room(self.driver, self.username)
self.room.id = self.room_id
self.login()
self.enter_room()
self.on = True
self.clear_data()
flag = False
tmp = self.driver.get_cookies()
for i in tmp:
if i['name'] == 'client_session':
api.cookie = 'client_session=' + i['value']
break
print(api.cookie)
while True:
cur_time = datetime.datetime.now()
if cur_time.hour not in range(8, 23):
self.on = False
self.driver.get('https://kana.byha.top:444/')
self.analyze()
self.logout()
self.driver.close()
del self.driver
del self.room
del self.game
return
if self.driver.current_url != self.url:
self.enter_room()
sleep(10)
continue
ac = ActionChains(self.driver)
ac.send_keys(Keys.CONTROL).perform() # 防踢
try:
if self.driver.find_element_by_id("game-status").get_attribute('innerHTML') != "游戏中":
if flag:
flag = False
sleep(0.2)
self.game.is_pre = False
else:
self.room.free_time = 0
self.game.bot_move()
continue
flag = True
except:
continue
try:
self.room.do_something_out_of_game()
except room.PlayerWinAction as e:
winner = e.winner
game_size = len(self.game.players)
self.game_count[game_size] += 1
if game_size == 2 and winner != self.username:
current_win_time = self.user_remain_win_time.get(winner, self.default_user_remain_win_time)
self.user_remain_win_time[winner] = current_win_time - 1
self.room.send_message('剩余单挑次数' + str(current_win_time - 1) + '次')
if game_size > 2:
current_score = self.user_score.get(winner, 0)
addition = 2 ** (game_size - 3)
self.user_score[winner] = current_score + addition
self.room.send_message(
'赢家' + winner + '目前' + str(self.user_score[winner]) + '分(+' + str(addition) + ')')
ban = False
if self.room.available_user_count == 1:
ban = True
elif self.room.available_user_count == 2:
if self.driver.find_element_by_id('ready').get_attribute('innerHTML') == '取消准备':
ac = ActionChains(self.driver)
ac.click(self.driver.find_element_by_id('ready')).perform()
self.room.get_user_in_room(api)
sleep(1)
if self.room.total_user_count < self.room.available_user_count:
continue
for username in self.room.users:
if self.user_remain_win_time.get(username, self.default_user_remain_win_time) <= 0:
ban = True
break
try:
if self.room.auto_ready and self.driver.find_element_by_id('ready').get_attribute(
'innerHTML') == '准备' and not ban:
ac = ActionChains(self.driver)
ac.click(self.driver.find_element_by_id('ready')).perform()
if self.driver.find_element_by_id('ready').get_attribute(
'innerHTML') == '取消准备' and ban:
ac = ActionChains(self.driver)
ac.click(self.driver.find_element_by_id('ready')).perform()
except:
pass
def main():
a = Bot()
while True:
cur_time = datetime.datetime.now()
if cur_time.hour in range(8, 23):
a.main()
sleep(60)
if __name__ == '__main__':
main()