@@ -22,7 +22,7 @@ def players(self) -> list[list[float]]:
2222
2323class IFormationFileReader (ABC ):
2424 @abstractmethod
25- def read_file (self , path ):
25+ def read_file (self , path ) -> list [ FormationIndexData ] :
2626 pass
2727
2828class OldStaticFormationFileReader (IFormationFileReader ):
@@ -34,9 +34,41 @@ def read_file(self, lines):
3434 player = lines [i ].split ()
3535 players .append ([float (player [2 ]), float (player [3 ])])
3636
37- return FormationIndexData (None , players )
38-
37+ return [FormationIndexData (None , players )]
38+
39+ class OldDelaunayFormationFileReader (IFormationFileReader ):
40+ def read_file (self , lines ):
41+ indexes = []
42+ for i in range (len (lines )):
43+ if lines [i ].find ('Ball' ) >= 0 :
44+ indexes .append (self .read_sample (i , lines ))
45+ i += 11
46+ return indexes
47+
48+ def read_sample (self , i , lines ):
49+ ball = lines [i ].split (' ' )
50+ ball_x = float (ball [1 ])
51+ ball_y = float (ball [2 ])
52+ ball = [ball_x , ball_y ]
53+ players = []
54+ for j in range (1 , 12 ):
55+ player = lines [i + j ].split (' ' )
56+ player_x = float (player [1 ])
57+ player_y = float (player [2 ])
58+ players .append ([player_x , player_y ])
59+ return FormationIndexData (ball , players )
3960
61+ class FormationFileReaderFactory :
62+ def get_reader (self , lines ) -> list [IFormationFileReader , FormationType ]:
63+ if lines [0 ].find ('Static' ) >= 0 :
64+ return OldStaticFormationFileReader (), FormationType .Static
65+ return OldDelaunayFormationFileReader (), FormationType .DelaunayTriangulation2
66+
67+ def read_file (self , path ) -> list [FormationIndexData ]:
68+ file = open (path , 'r' )
69+ lines = file .readlines ()
70+ reader , formation_type = self .get_reader (lines )
71+ return reader .read_file (lines ), formation_type
4072
4173class FormationFile :
4274 def __init__ (self , path , logger : logging .Logger ):
@@ -51,44 +83,17 @@ def __init__(self, path, logger: logging.Logger):
5183 self .calculate ()
5284
5385 def read_file (self , path ):
54- file = open (path , 'r' )
55- lines = file .readlines ()
56- if lines [0 ].find ('Static' ) < 0 :
57- self ._formation_type = FormationType .DelaunayTriangulation2
86+ indexes , self ._formation_type = FormationFileReaderFactory ().read_file (path )
87+
5888 if self ._formation_type == FormationType .Static :
59- data = OldStaticFormationFileReader (). read_file ( lines )
89+ data = indexes [ 0 ]
6090 players = data .players ()
61- # self._players = data.players()
6291 for i in range (11 ):
6392 self ._target_players [i + 1 ] = Vector2D (float (players [i ][0 ]), float (players [i ][1 ]))
6493 else :
65- self .read_delaunay (lines )
66-
67- def read_static (self , lines ):
68- for i in range (len (lines )):
69- if i == 0 or lines [i ].startswith ('#' ):
70- continue
71- player = lines [i ].split ()
72- self ._target_players [i + 1 ] = Vector2D (float (player [2 ]), float (player [3 ]))
73-
74- def read_delaunay (self , lines ):
75- for i in range (len (lines )):
76- if lines [i ].find ('Ball' ) >= 0 :
77- self .read_sample (i , lines )
78- i += 11
79-
80- def read_sample (self , i , lines ):
81- ball = lines [i ].split (' ' )
82- ball_x = float (ball [1 ])
83- ball_y = float (ball [2 ])
84- self ._balls .append ([ball_x , ball_y ])
85- players = []
86- for j in range (1 , 12 ):
87- player = lines [i + j ].split (' ' )
88- player_x = float (player [1 ])
89- player_y = float (player [2 ])
90- players .append ([player_x , player_y ])
91- self ._players .append (players )
94+ for index in indexes :
95+ self ._balls .append (index .ball ())
96+ self ._players .append (index .players ())
9297
9398 def calculate (self ):
9499 if self ._formation_type == FormationType .Static :
0 commit comments