Skip to content

Commit 99a8199

Browse files
committed
Add PlayerRole class and update formation file readers to handle player roles
1 parent 0e510dc commit 99a8199

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

src/strategy/formation_file.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,62 @@ def ball(self) -> list[float]:
2020
def players(self) -> list[list[float]]:
2121
return self._players
2222

23+
class PlayerRole:
24+
def __init__(self, name, type, side, pair):
25+
self._name = name
26+
self._type = type
27+
self._side = side
28+
self._pair = pair
29+
2330
class IFormationFileReader(ABC):
2431
@abstractmethod
2532
def read_file(self, path) -> list[FormationIndexData]:
2633
pass
2734

2835
class OldStaticFormationFileReader(IFormationFileReader):
2936
def read_file(self, lines):
30-
players = []
37+
players = {}
38+
roles = {}
3139
for i in range(len(lines)):
3240
if i == 0 or lines[i].startswith('#'):
3341
continue
3442
player = lines[i].split()
35-
players.append([float(player[2]), float(player[3])])
43+
players[int(player[0])] = ([float(player[2]), float(player[3])])
44+
roles[int(player[0])] = PlayerRole(player[1], None, None, None)
3645

37-
return [FormationIndexData(None, players)]
46+
return [FormationIndexData(None, players)], roles
3847

3948
class OldDelaunayFormationFileReader(IFormationFileReader):
4049
def read_file(self, lines):
50+
roles = {}
51+
begin_roles = False
52+
for i in range(len(lines)):
53+
if lines[i].startswith('Begin Roles'):
54+
begin_roles = True
55+
continue
56+
if lines[i].startswith('End Roles'):
57+
break
58+
if begin_roles:
59+
player = lines[i].split()
60+
roles[int(player[0])] = PlayerRole(player[1], None, None, int(player[2]))
4161
indexes = []
4262
for i in range(len(lines)):
4363
if lines[i].find('Ball') >= 0:
44-
indexes.append(self.read_sample(i, lines))
64+
indexes.append(self.read_index(i, lines))
4565
i += 11
46-
return indexes
66+
return indexes, roles
4767

48-
def read_sample(self, i, lines):
68+
def read_index(self, i, lines):
4969
ball = lines[i].split(' ')
5070
ball_x = float(ball[1])
5171
ball_y = float(ball[2])
5272
ball = [ball_x, ball_y]
53-
players = []
73+
players = {}
5474
for j in range(1, 12):
5575
player = lines[i + j].split(' ')
5676
player_x = float(player[1])
5777
player_y = float(player[2])
58-
players.append([player_x, player_y])
78+
players[j] = ([player_x, player_y])
5979
return FormationIndexData(ball, players)
6080

6181
class FormationFileReaderFactory:
@@ -68,7 +88,8 @@ def read_file(self, path) -> list[FormationIndexData]:
6888
file = open(path, 'r')
6989
lines = file.readlines()
7090
reader, formation_type = self.get_reader(lines)
71-
return reader.read_file(lines), formation_type
91+
indexes, roles = reader.read_file(lines)
92+
return indexes, roles, formation_type
7293

7394
class FormationFile:
7495
def __init__(self, path, logger: logging.Logger):
@@ -83,13 +104,13 @@ def __init__(self, path, logger: logging.Logger):
83104
self.calculate()
84105

85106
def read_file(self, path):
86-
indexes, self._formation_type = FormationFileReaderFactory().read_file(path)
107+
indexes, roles, self._formation_type = FormationFileReaderFactory().read_file(path)
87108

88109
if self._formation_type == FormationType.Static:
89110
data = indexes[0]
90111
players = data.players()
91-
for i in range(11):
92-
self._target_players[i + 1] = Vector2D(float(players[i][0]), float(players[i][1]))
112+
for i in range(1, 12):
113+
self._target_players[i] = Vector2D(float(players[i][0]), float(players[i][1]))
93114
else:
94115
for index in indexes:
95116
self._balls.append(index.ball())
@@ -131,7 +152,7 @@ def update(self, B:Vector2D):
131152
n2 = lineProj.dist(B)
132153

133154
self._target_players.clear()
134-
for p in range(11):
155+
for p in range(1, 12):
135156
OPa = Vector2D(self._players[ids[0]][p][0], self._players[ids[0]][p][1])
136157
OPb = Vector2D(self._players[ids[1]][p][0], self._players[ids[1]][p][1])
137158
OPc = Vector2D(self._players[ids[2]][p][0], self._players[ids[2]][p][1])
@@ -141,7 +162,7 @@ def update(self, B:Vector2D):
141162
OB = (OI - OPa)
142163
OB *= (m2 / (m2 + n2))
143164
OB += OPa
144-
self._target_players[p + 1] = OB
165+
self._target_players[p] = OB
145166

146167
def get_pos(self, unum):
147168
return self._target_players[unum]

0 commit comments

Comments
 (0)