-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrivateCamera.py
718 lines (594 loc) · 31 KB
/
PrivateCamera.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
import csv
import json
from datetime import datetime
import os
import time
import pyautogui
import shutil
import re
import math
from generate_scoreboard import generate_scoreboard
# 配置
LOG_DIR = "D:\\Oculus\\Software\\Software\\for-fun-labs-eleven-table-tennis-vr\\logs\\"
CSV_OUTPUT = "D:\\Oculus\\Software\\Software\\for-fun-labs-eleven-table-tennis-vr\\RPA\\log_analysis.csv"
OBS_OUTPUT = "D:\\Oculus\\Software\\Software\\for-fun-labs-eleven-table-tennis-vr\\RPA\\obs.log"
MATCH_JSON = "D:\\Oculus\\Software\\Software\\for-fun-labs-eleven-table-tennis-vr\\RPA\\match_data.json"
SCOREBOARD_OUTPUT = "D:\\Oculus\\Software\\Software\\for-fun-labs-eleven-table-tennis-vr\\RPA\\scoreboard.html"
LAST_POSITION_FILE = "D:\\Oculus\\Software\\Software\\for-fun-labs-eleven-table-tennis-vr\\RPA\\.last_position" # 保存本次处理的日志中处理到的行
# PERFORMANCE_LOG = "D:\\Oculus\\Software\\Software\\for-fun-labs-eleven-table-tennis-vr\\RPA\\performance.log" # 性能日志
PLAYER_ID = "1418464" # VIOLENTPANDA 的 ID
CAMERA_ID = "1461990" # CHN_CAMERA 的 ID
PLAYER_NAME = "VIOLENTPANDA"
CAMERA_NAME = "CHN_CAMERA"
# PLAYER_NAME = "justwe"
# PLAYER_ID = "1686306"
# CSV_COLUMNS = ["Timestamp", "Analysis Time", "Event", "Details", "Delay (seconds)"]
CSV_COLUMNS = ["Event", "Details"]
# 全局变量
in_room = False
camera_in_room = False
last_ball_hit_time = None
ALIVE_TIMEOUT = 60 # 60秒无击球则判定为可能掉线
# 屏幕坐标(根据您的屏幕分辨率调整)
PLAYER_NAME_POS = (-520, 320)
JOIN_ROOM_POS = (-620, 530)
EXIT_ROOM_POS = (-1120, 600)
EXIT_ROOM_OK_POS = (-1060, 520)
REJOIN_NO_POS = (-920, 520)
SCREEN_CORNER_POSE = (-1,1)
# def force_flush_file(file_path):
# try:
# with open(file_path, 'a') as f:
# os.fsync(f.fileno())
# # print(f"Successfully flushed file: {file_path}")
# except Exception as e:
# print(f"Error flushing file {file_path}: {e}")
def simulate_mouse_move(x, y):
pyautogui.moveTo(x, y, duration=0.05)
time.sleep(0.5)
def simulate_keypress(key):
pyautogui.keyDown(key)
time.sleep(0.01)
pyautogui.keyUp(key)
time.sleep(0.01)
def simulate_click(x, y):
# 移动鼠标
pyautogui.moveTo(x, y, duration=0.05)
time.sleep(0.02)
# 模拟鼠标按下
pyautogui.mouseDown(x, y)
time.sleep(0.02)
# 模拟鼠标松开
pyautogui.mouseUp(x, y)
time.sleep(0.02)
def enter_room():
# 切换到侧面视角
simulate_keypress('0')
# 呼出菜单
simulate_keypress('m')
# 点击被观战者名称位置
simulate_click(*PLAYER_NAME_POS)
# 点击JOIN ROOM按钮
simulate_click(*JOIN_ROOM_POS)
# 切换到观战视角
simulate_keypress('6')
simulate_mouse_move(*SCREEN_CORNER_POSE)
def exit_room():
# 切换到侧面视角
simulate_keypress('0')
# 点击退出房间按钮
simulate_click(*EXIT_ROOM_POS)
# 点击弹窗OK按钮
simulate_click(*EXIT_ROOM_OK_POS)
# 关闭菜单
simulate_keypress('m')
# 点击弹窗NO按钮
simulate_click(*REJOIN_NO_POS)
simulate_click(*REJOIN_NO_POS) # 实际运行中发现有时需要点击两次,可能因为点击太快。 这里点两次
simulate_mouse_move(*SCREEN_CORNER_POSE)
# 等待退出完成
time.sleep(0.1)
def get_latest_log_file():
log_files = [f for f in os.listdir(LOG_DIR) if f.endswith('.log')]
if not log_files:
return None
return os.path.join(LOG_DIR, max(log_files, key=lambda f: os.path.getctime(os.path.join(LOG_DIR, f))))
# 保存本次处理的日志中处理到的行
def get_last_position(file_path):
if os.path.exists(LAST_POSITION_FILE):
with open(LAST_POSITION_FILE, 'r') as f:
last_file, position = f.read().split(',')
if last_file == file_path:
return int(position)
return 0
def save_last_position(file_path, position):
with open(LAST_POSITION_FILE, 'w') as f:
f.write(f"{file_path},{position}")
# 添加新函数用于解析击球数据
def parse_ball_hit_data(content):
match = re.search(r'pos:\((.*?)\) vel:\((.*?)\) rrate:\((.*?)\)', content)
if match:
pos = [float(x) for x in match.group(1).split(',') if x]
vel = [float(x) for x in match.group(2).split(',') if x]
rrate = [float(x) for x in match.group(3).split(',') if x]
speed = math.sqrt(sum(v**2 for v in vel))
rotation = math.sqrt(sum((r/360)**2 for r in rrate))
# 确保vel是3维的
while len(vel) < 3:
vel.append(0.0)
# 如果vel超过3维,只保留前3个值
vel = vel[:3]
# 确保rrate是3维的
while len(rrate) < 3:
rrate.append(0.0)
# 如果rrate超过3维,只保留前3个值
rrate = rrate[:3]
x_rotation, y_rotation, z_rotation = rrate
# 设定阈值
threshold = 3600 # 这个阈值可能需要根据实际情况调整
# 判断球的运动方向
direction = "Forward" if vel[2] > 0 else "Backward"
# 判断旋转方向
horizontal_spin = ""
vertical_spin = ""
if abs(x_rotation) > threshold:
# 根据运动方向和x轴旋转方向判断上下旋
if (direction == "Forward" and x_rotation < 0) or (direction == "Backward" and x_rotation > 0):
vertical_spin = "Back"
else:
vertical_spin = "Top"
if abs(y_rotation) > threshold:
# 根据运动方向和y轴旋转方向判断左右旋
if (direction == "Forward" and y_rotation > 0) or (direction == "Backward" and y_rotation < 0):
horizontal_spin = "Left"
else:
horizontal_spin = "Right"
# 组合旋转描述,根据旋转强度决定顺序
if horizontal_spin and vertical_spin:
if abs(x_rotation) > abs(y_rotation):
spin_direction = f"{vertical_spin} {horizontal_spin} spin"
else:
spin_direction = f"{horizontal_spin} {vertical_spin} spin"
elif vertical_spin:
spin_direction = f"{vertical_spin} spin"
elif horizontal_spin:
spin_direction = f"{horizontal_spin} spin"
else:
spin_direction = "No significant spin"
return {
'position': pos,
'velocity': vel,
'rotation_rate': rrate,
'speed': speed,
'rotation': rotation,
'spin_direction': spin_direction,
'direction': direction
}
return None
last_score = []
match_info = None
def parse_snapshot(content):
print(content)
try:
data = json.loads(content.replace("Snapshot reads: ", ""))
current_scores = data["RoundScores"]
match_data = {
"playerNames": data["PlayerNames"],
"playerELOs": data["PlayerELOs"],
"currentScores": current_scores,
"matchId": data["MatchId"],
"bestOf": data["BestOf"],
"ranked": data["Ranked"],
"matchWinner": data["MatchWinner"]
}
update_match_data(match_data)
global last_score, match_info
# 如果是新的比赛或者新的对手,重置 match info、last score 和 current Set Status
if not match_info or match_info["MatchId"] != data["MatchId"]:
match_info = {
"PlayerNames": data["PlayerNames"],
"PlayerELOs": data["PlayerELOs"],
"MatchId": data["MatchId"],
"BestOf": data["BestOf"],
"Ranked": data["Ranked"]
}
last_score = []
event = "Match Start"
details = f"New match started: {match_info}"
# 重置 current Set Status 和 match Score
try:
with open(MATCH_JSON, 'r', encoding='utf-8') as f:
match_data = json.load(f)
match_data['currentSetStatus'] = [0, 0]
# match_data['matchScore'] = [0, 0] 这里不需要更新,重新进房间才重置
# 更新玩家信息
match_data['playerNames'] = data["PlayerNames"]
match_data['playerELOs'] = data["PlayerELOs"]
# 如果有其他需要重置或更新的字段,也在这里处理
with open(MATCH_JSON, 'w', encoding='utf-8') as f:
json.dump(match_data, f, ensure_ascii=False, indent=4)
except Exception as e:
print(f"Error resetting match data: {e}")
# 生成新的记分牌
generate_scoreboard()
return event, details
if last_score != current_scores:
current_round = len(current_scores)
if current_scores:
current_round_score = current_scores[-1]
scoring_side = data["LastPointReasonSide"]
scoring_player = data["PlayerNames"][0] if scoring_side == "SideA" else data["PlayerNames"][1]
losing_player = data["PlayerNames"][1] if scoring_side == "SideA" else data["PlayerNames"][0]
server_id = int(data["CurrentServer"])
server_name = data["PlayerNames"][0] if server_id == data["PlayerIds"][0] else data["PlayerNames"][1]
reason = data["LastPointReason"]
# 检查是否是新的Round开始
if len(current_scores) > len(last_score or []):
event = "New Round"
details = f"Round {current_round} started. "
if len(current_scores) == 1:
details += f"First round of the match, "
else:
previous_score = current_scores[-2]
details += f"Previous round score: {previous_score[0]}-{previous_score[1]}, New round of the match, "
# 更新 current Set Status
try:
with open(MATCH_JSON, 'r', encoding='utf-8') as f:
match_data = json.load(f)
current_set_status = match_data.get('currentSetStatus', [0, 0])
# 确定上一轮的获胜者
winner_index = 0 if previous_score[0] > previous_score[1] else 1
current_set_status[winner_index] += 1
update_match_data({'currentSetStatus': current_set_status})
except Exception as e:
print(f"Error updating currentSetStatus: {e}")
# 生成新的记分牌
generate_scoreboard()
else:
event = "Score Update"
details = ""
# 更新 last_score
last_score = current_scores
details += f"Current round score: {current_round_score[0]}-{current_round_score[1]}. "
else:
event = "Match Reset"
details = "Scores have been reset."
if data["MatchWinner"] != "0":
event = "Match End"
winner_index = 0 if int(data["MatchWinner"]) == data["PlayerIds"][0] else 1
winner_name = data["PlayerNames"][winner_index]
details += f", Match Winner: {winner_name}"
# 更新 match Score
try:
with open(MATCH_JSON, 'r', encoding='utf-8') as f:
match_data = json.load(f)
match_score = match_data.get('matchScore', [0, 0])
match_score[winner_index] += 1
update_match_data({'matchScore': match_score})
# 这里也要更新 current Set Status
current_set_status = match_data.get('currentSetStatus', [0, 0])
current_set_status[winner_index] += 1
update_match_data({'currentSetStatus': current_set_status})
except Exception as e:
print(f"Error updating match score and currentSetStatus: {e}")
else:
if data["MatchWinner"] != "0":
event = "Match End"
winner_index = 0 if int(data["MatchWinner"]) == data["PlayerIds"][0] else 1
winner_name = data["PlayerNames"][winner_index]
details = f"Match ended. Winner: {winner_name}, Final score: {' '.join([f'{s[0]}-{s[1]}' for s in current_scores])}"
# 更新 match Score
try:
with open(MATCH_JSON, 'r', encoding='utf-8') as f:
match_data = json.load(f)
match_score = match_data.get('matchScore', [0, 0])
match_score[winner_index] += 1
update_match_data({'matchScore': match_score})
# 这里也要更新 current Set Status
current_set_status = match_data.get('currentSetStatus', [0, 0])
current_set_status[winner_index] += 1
update_match_data({'currentSetStatus': current_set_status})
except Exception as e:
print(f"Error updating match score and currentSetStatus: {e}")
else:
return "Unknown", None # 如果比分没有变化且比赛未结束,不记录
# 生成新的记分牌
generate_scoreboard()
return event, details
except json.JSONDecodeError:
print(f"Failed to parse snapshot data: {content}")
return "Unknown", None
def parse_log_line(line):
if '"msgType":"Message"' in line or '"msgType":"Response"' in line:
try:
timestamp, content = line.split("]", 1)
timestamp = timestamp.strip("[")
content = content.strip()
if "TCPSERVERCOMMUNICATIONS]Received message from server:" in content:
_, json_content = content.split("server:", 1)
return timestamp, json.loads(json_content)
except (ValueError, json.JSONDecodeError):
pass
elif "[MPMatch]Received ball hit from opponent:" in line:
match = re.match(r'\[(.*?)\].*?\[MPMatch\](.*)', line)
if match:
timestamp, content = match.groups()
# print(f"Timestamp: {timestamp}")
# print(f"Content: {content}")
return timestamp, content.strip()
elif line.strip().startswith("Snapshot reads:"):
# match = re.search(r'Snapshot reads: (.*)', line)
# if match:
# content = match.group(1)
# # 使用当前时间作为时间戳,因为日志中没有提供
# timestamp = datetime.now().strftime("%m/%d/%Y %H:%M:%S")
# return timestamp, content.strip()
timestamp = datetime.now().strftime("%m/%d/%Y %H:%M:%S")
return timestamp, line.strip()
return None, None
def backup_and_reset_files():
# 备份 CSV_OUTPUT 文件
if os.path.exists(CSV_OUTPUT):
backup_time = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_file = f"{CSV_OUTPUT[:-4]}_{backup_time}.csv"
shutil.copy2(CSV_OUTPUT, backup_file)
print(f"已备份 CSV 输出文件到: {backup_file}")
# 清空原 CSV 文件
open(CSV_OUTPUT, 'w').close()
print(f"已清空 CSV 输出文件: {CSV_OUTPUT}")
# 删除 LAST_POSITION_FILE
if os.path.exists(LAST_POSITION_FILE):
os.remove(LAST_POSITION_FILE)
print(f"已删除上次位置文件: {LAST_POSITION_FILE}")
def check_alive_status():
"""检查是否需要退出房间"""
global last_ball_hit_time, in_room
if not in_room or not last_ball_hit_time:
return
current_time = time.time()
time_since_last_hit = current_time - last_ball_hit_time
if time_since_last_hit > ALIVE_TIMEOUT:
print(f"Warning: No ball hit detected for {time_since_last_hit:.1f} seconds. Exiting room...")
try:
exit_room()
in_room = False
last_ball_hit_time = None
# 记录到CSV
with open(CSV_OUTPUT, 'a', newline='', encoding='utf-8') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow([
"Connection Lost",
f"No activity for {time_since_last_hit:.1f} seconds"
])
except Exception as e:
print(f"Error during force exit: {e}")
def analyze_log():
global in_room, camera_in_room, last_ball_hit_time
start_time = datetime.now()
# print(f"开始分析日志: {start_time}")
log_file_path = get_latest_log_file()
if not log_file_path:
print("No log file found.")
return
# print(f"正在分析最新日志: {log_file_path}")
# 强制刷新日志文件
# force_flush_file(log_file_path)
last_position = get_last_position(log_file_path)
with open(log_file_path, 'r', encoding='utf-8') as log_file:
log_file.seek(last_position)
new_lines = log_file.readlines()
lines_processed = len(new_lines)
if new_lines:
with open(CSV_OUTPUT, 'a', newline='', encoding='utf-8') as csv_file:
csv_writer = csv.writer(csv_file)
if os.path.getsize(CSV_OUTPUT) == 0:
csv_writer.writerow(CSV_COLUMNS)
for line in new_lines:
analysis_time = datetime.now()
timestamp, data = parse_log_line(line)
event = "Unknown"
details = "Unknown"
if timestamp and data:
log_timestamp = datetime.strptime(timestamp, "%m/%d/%Y %H:%M:%S")
delay = (analysis_time - log_timestamp).total_seconds()
if isinstance(data, dict) and data["msgType"] == "Message":
# 处理 StateChange (摄像头状态变化)
if data["key"] == "StateChange" and any(user["UserId"] == CAMERA_ID for user in data["data"]):
old_state = data["data"][0]["Location"]["locationType"]
new_state = data["data"][1]["Location"]["locationType"]
if new_state == "room" and old_state != "room":
camera_in_room = True
event = "Camera Enter Room"
elif old_state == "room" and new_state != "room":
camera_in_room = False
event = "Camera Exit Room"
details = f"From {old_state} to {new_state}"
# 处理 FriendStateChange (被跟踪玩家状态变化)
elif data["key"] == "FriendStateChange" and any(user["UserId"] == PLAYER_ID for user in data["data"]):
old_state = data["data"][0]["Location"]["locationType"]
new_state = data["data"][1]["Location"]["locationType"]
if new_state == "room" and old_state != "room":
in_room = True
event = "Player Enter Room"
enter_room() # 执行进房间操作
elif old_state == "room" and new_state != "room":
in_room = False
event = "Player Exit Room"
exit_room() # 执行退出房间操作
else:
continue
details = f"From {old_state} to {new_state}"
elif data["key"] == "RoomJoined":
roomId = data["data"][0]["Id"]
hostId = data["data"][0]["HostId"]
matchId = data["data"][0]["MatchId"]
viewCount = data["data"][0]["ViewCount"]
players = data["data"][0]["Players"]
playerIds = data["data"][0]["PlayerIds"]
print(f"Room Joined: {roomId}, Host: {hostId}, Match: {matchId}, Viewers: {viewCount}, Players: {playerIds}")
# 提取玩家信息,排除摄像机
player_data = {
'playerNames': [],
'playerELOs': [],
'playerCountryCodes': [],
'playerRanks': [],
'playerWins': [],
'playerLosses': []
}
valid_players = [p for p in players if p['Id'] != CAMERA_ID and p['UserName'] != CAMERA_NAME]
if len(valid_players) == 2:
# 根据玩家在数组中的位置确定主客顺序
home_player = valid_players[0]
away_player = valid_players[1]
# 读取现有的 match_data(如果存在)
try:
with open(MATCH_JSON, 'r', encoding='utf-8') as f:
old_match_data = json.load(f)
except FileNotFoundError:
old_match_data = {}
# 检查玩家是否相同,并调整顺序和分数
old_players = old_match_data.get('playerNames', [])
new_players = [home_player['UserName'], away_player['UserName']]
if set(old_players) == set(new_players):
# 玩家相同,但可能顺序改变
if old_players != new_players:
# 顺序改变,调整大局分
old_match_score = old_match_data.get('matchScore', [0, 0])
match_score = old_match_score[::-1] # 反转大局分
else:
# 顺序相同,保持原有大局分
match_score = old_match_data.get('matchScore', [0, 0])
else:
# 新的玩家组合,重置大局分
match_score = [0, 0]
# 第一局的局分初始化0:0
current_set_status = [0, 0]
# 更新玩家数据
for player in [home_player, away_player]:
player_data['playerNames'].append(player['UserName'])
player_data['playerELOs'].append(player['ELO'])
player_data['playerCountryCodes'].append(player['CountryCode'])
player_data['playerRanks'].append(player['Rank'])
player_data['playerWins'].append(player['Wins'])
player_data['playerLosses'].append(player['Losses'])
player_data['matchScore'] = match_score
player_data['currentSetStatus'] = current_set_status
# 更新 match_data
update_match_data(player_data)
generate_scoreboard()
else:
print("Warning: Unexpected number of players detected. Scoreboard not updated.")
# 处理 RoomExitUser
# elif data["key"] == "RoomExitUser":
# if data["data"][0]["Id"] == PLAYER_ID:
# event = "Player Left Room"
# else:
# event = "Other Player Left Room"
# details = f"Player ID: {data['data'][0]['Id']}, Player Name: {data['data'][0]['UserName']}"
elif isinstance(data, str) and "Received ball hit from opponent:" in data:
ball_data = parse_ball_hit_data(data)
if ball_data and ball_data['speed'] >= 1:
last_ball_hit_time = time.time() # 更新最后击球时间
event = "Ball Hit"
details = (f"{ball_data['speed']:.2f} m/s, "
f"{ball_data['rotation']:.2f} rev/s, "
f"{ball_data['spin_direction']}, "
f"{ball_data['direction']}")
print(f"Ball hit detected at {timestamp}")
print(f"Speed: {ball_data['speed']:.2f} m/s")
print(f"Rotation: {ball_data['rotation']:.2f} rev/s")
print(f"Spin direction: {ball_data['spin_direction']}")
print(f"Ball direction: {ball_data['direction']}")
print(f"Velocity vector: {ball_data['velocity']}")
print(f"Rotation rate vector: {ball_data['rotation_rate']}")
print(f"Delay: {delay:.3f} seconds")
print("---")
else:
# print(f"Failed to parse ball hit data : {data}")
continue
elif isinstance(data, str) and data.startswith("Snapshot reads:"):
content = data.replace("Snapshot reads:", "").strip()
event, details = parse_snapshot(content)
print(f"Event: {event}")
print(f"Details: {details}")
print(f"Log Timestamp: {timestamp}")
print(f"Analysis Time: {analysis_time}")
print(f"Delay: {delay:.3f} seconds")
if event == "New Round":
print("=" * 50) # 添���一个分隔线突出显示新的Round开始
elif event == "Match End":
print("#" * 50) # 比赛结束添加特殊的分隔线
else:
print("-" * 30)
else:
continue
if (event != "Unknown"):
log_timestamp = datetime.strptime(timestamp, "%m/%d/%Y %H:%M:%S")
delay = (analysis_time - log_timestamp).total_seconds()
csv_writer.writerow([
# timestamp,
# analysis_time.strftime("%Y-%m-%d %H:%M:%S.%f"),
event,
details
# details,
# f"{delay:.3f}"
])
print(f"Event: {event}")
print(f"Log Timestamp: {timestamp}")
print(f"Analysis Time: {analysis_time}")
print(f"Delay: {delay:.3f} seconds")
print("---")
else:
return # 没有新行,直接返回
# 保存最后处理的位置
save_last_position(log_file_path, log_file.tell())
end_time = datetime.now()
duration = (end_time - start_time).total_seconds()
# 记录性能信息
# with open(PERFORMANCE_LOG, 'a', encoding='utf-8') as perf_log:
# perf_log.write(f"开始时间: {start_time}, 结束时间: {end_time}, 持续时间: {duration:.2f}秒, 处理行数: {lines_processed}\n")
print(f"结束分析日志: {end_time} 本次分析处理了 {lines_processed} 行日志,耗时 {duration:.2f} 秒")
def read_last_lines(file_path, num_lines=5):
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()[-num_lines:]
return ''.join(lines)
def set_text_source(text):
# 把text写入OBS_OUTPUT文件,如果已有则直接覆盖
with open(OBS_OUTPUT, 'w', encoding='utf-8') as f:
f.write(text)
def update_match_data(new_data):
try:
with open(MATCH_JSON, 'r', encoding='utf-8') as f:
match_data = json.load(f)
except FileNotFoundError:
match_data = {}
# 更新所有新数据,包括玩家信息
for key, value in new_data.items():
match_data[key] = value
# 保留其他可能存在的字段
for key in match_data.keys():
if key not in new_data:
match_data[key] = match_data[key]
with open(MATCH_JSON, 'w', encoding='utf-8') as f:
json.dump(match_data, f, ensure_ascii=False, indent=4)
if __name__ == "__main__":
# 在脚本启动时执行上次运行输出文件的备份和重置
backup_and_reset_files()
simulate_mouse_move(*SCREEN_CORNER_POSE)
time.sleep(0.1)
simulate_click(*SCREEN_CORNER_POSE)
time.sleep(0.1)
# 一开始先呼出 -> 关闭菜单一下 确保好友列表中我名字加载出来了。
# 呼出菜单
simulate_keypress('m')
time.sleep(3)
# 关闭菜单
simulate_keypress('m')
while True:
analyze_log()
check_alive_status() # 添加存活状态检查
text_to_display = read_last_lines(CSV_OUTPUT,20)
# 设置到obs的文本源
set_text_source(text_to_display)
generate_scoreboard() # 生成新的 scoreboard.html
time.sleep(1) # 每1秒运行一次