-
Notifications
You must be signed in to change notification settings - Fork 1
/
mirror_t_collect_script.py
executable file
·62 lines (49 loc) · 2.31 KB
/
mirror_t_collect_script.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
import os
import csv
import sys
def get_last_player_id(csv_file):
if not os.path.exists(csv_file):
return 0 # Return 0 if file doesn't exist
else:
with open(csv_file, mode="r", newline="") as csvfile:
reader = csv.DictReader(csvfile)
last_row = None
for row in reader:
last_row = row
if last_row is None:
return 0 # Return 0 if file is empty
return int(last_row["Player_ID"])
def create_csv(logs_folder, csv_file):
log_files = [f for f in os.listdir(logs_folder) if os.path.isfile(os.path.join(logs_folder, f))]
log_files.sort()
total_players = len(log_files) - 1
# csv_file = "mirror_telepathy_results.csv"
last_player_id = get_last_player_id(csv_file)
with open(csv_file, mode="a", newline="") as csvfile: # Open file in append mode
fieldnames = ["Player_ID", "Total_Players", "RoundTripDelay_ms"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if os.stat(csv_file).st_size == 0: # Check if file is empty
writer.writeheader() # Write header if file is empty
for log_file in log_files:
if log_file.startswith("player_log"):
with open(os.path.join(logs_folder, log_file), mode="r") as file:
next(file)
player_id = last_player_id + 1 # Start new player ID from the next number
for line in file:
data = line.strip().split(",")
writer.writerow({
"Player_ID": player_id,
"Total_Players": total_players,
"RoundTripDelay_ms": data[-1]
})
last_player_id = player_id # Update last player ID for the next player
def main(logs_folder, csv_file):
create_csv(logs_folder, csv_file)
print("Script execution completed.") # Print message indicating script completion
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python script_name.py <logs_folder>")
sys.exit(1)
logs_folder = sys.argv[1]
csv_file = sys.argv[2]
main(logs_folder, csv_file)