1+ import os
2+ from datetime import datetime , timedelta
3+ from objects .match import Match
4+
5+
6+ class LogAnalyser :
7+
8+ def __init__ (self , path_csv , name ) -> None :
9+
10+ self .path_csv = path_csv
11+ self .name = name
12+
13+ self .date = self .name2datetime ()
14+
15+ self .match = None
16+
17+ self .actions = {"match_start" : self .process_match_start }
18+
19+ def run (self ):
20+
21+ with open (self .path_csv , encoding = 'utf-8' ) as my_file :
22+ file = my_file .read ()
23+ lines = file .split ("\n " )
24+ for line in lines :
25+ line_split = line .split ("," )
26+
27+ if len (line_split ) > 1 :
28+ type = line_split [1 ]
29+ if type in self .actions :
30+ self .actions [type ](line_split )
31+
32+ with open ("../logs_process/{}.json" .format (self .name .split ("." )[0 ]), "w" ) as file :
33+ file .write (self .match .export_json ())
34+
35+
36+ def name2datetime (self ):
37+
38+ date_string = self .name .split ("." )[0 ].split ("Log-" )[1 ]
39+ date_object = datetime .strptime (date_string , '%Y-%m-%d-%H-%M-%S' )
40+
41+ return date_object
42+
43+ def process_match_start (self , data ):
44+
45+ self .match = Match .from_json ({"rounds" : [],
46+ "date" : self .date ,
47+ "map_name" : data [3 ],
48+ "map_type" : data [4 ],
49+ "team1_name" : data [5 ],
50+ "team2_name" : data [6 ],
51+ "score_team1" : 0 ,
52+ "score_team2" : 0 ,
53+ })
54+
55+ self .actions = {"match_start" : self .process_match_start ,
56+ "round_start" : self .match .add_round ,
57+ "round_end" : self .match .end_round ,
58+ "hero_spawn" : self .process_hero_spawn ,
59+ "hero_swap" : self .process_hero_swap ,
60+ "kill" : self .match .add_kill ,
61+ "ultimate_charged" : self .match .add_ultimate_charged ,
62+ "ultimate_start" : self .match .add_ultimate_start ,
63+ "ultimate_end" : self .match .add_ultimate_end ,
64+ "objective_captured" : self .match .add_objective_captured ,
65+ "player_stat" : self .match .add_player_stat ,
66+ "point_progress" : self .match .add_objective_progress ,
67+ "payload_progress" : self .match .add_objective_progress ,
68+ }
69+
70+ def process_hero_spawn (self , data ):
71+
72+ player_data = {"time" : data [2 ], "team_name" : data [3 ], "player_name" : data [4 ], "character_name" : data [5 ]}
73+ self .match .add_player (player_data )
74+
75+ def process_hero_swap (self , data ):
76+
77+ hero_data = {"time" : data [2 ], "team_name" : data [3 ], "player_name" : data [4 ], "character_name" : data [5 ], "character_swap" : data [6 ]}
78+ self .match .add_hero_swap (hero_data )
79+
80+ def convert_timefile_to_datetime (self , time_string ):
81+
82+ # Utilisation de strptime pour convertir la chaîne en datetime
83+ time_delta = datetime .strptime (time_string , "[%H:%M:%S]" )
84+
85+ # Conversion en timedelta (représentation de la durée)
86+ duration = timedelta (hours = time_delta .hour , minutes = time_delta .minute , seconds = time_delta .second )
87+ return duration
88+
89+
90+ for file in os .listdir ("../logs" ):
91+ if file .endswith (".txt" ):
92+ print (file )
93+ la = LogAnalyser ('../logs/{}' .format (file ), file )
94+ la .run ()
95+
96+ # la = LogAnalyser('../logs/Log-2023-12-22-21-12-32.txt', "Log-2023-12-22-21-12-32.txt")
97+ # la.run()
0 commit comments