This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_charts.py
319 lines (284 loc) · 10.7 KB
/
generate_charts.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
import os
from operator import itemgetter
# local imports
from constants import (EMPTY_TEAM_SHIELD, HIGHLIGHTS_PATH, LEAGUE_STATS_PATH,
PLAYERS_FOLDER_PATH, PLAYERS_MARKET_PATH, ROUNDS_PATH,
SCHEMES_PATH, TEAMS_PATH)
from helpers import read_json, write_json
def rank(ls, key, teams):
ls = sorted(ls, key=itemgetter(1), reverse=True)
for i, (name, _) in enumerate(ls):
teams[name][key].append(i+1)
def scout_getter(key):
return lambda x: itemgetter(key)(itemgetter('scout')(x))
def generate_highlights(teams):
best = max(teams.values(), key=itemgetter('melhor_da_rodada'))
worst = max(teams.values(), key=itemgetter('pior_da_rodada'))
rich = max(teams.values(), key=itemgetter('maior_valorização'))
poor = max(teams.values(), key=itemgetter('pior_valorização'))
scorer = max(teams.values(), key=scout_getter('G'))
waiter = max(teams.values(), key=scout_getter('A'))
solid_defense = max(teams.values(), key=scout_getter('SG'))
inverse_scorer = max(teams.values(), key=scout_getter('GS'))
wooden_leg = min(teams.values(), key=scout_getter('G'))
fair_player = min(teams.values(), key=scout_getter('CV'))
careless = max(teams.values(), key=scout_getter('CA'))
quarrelsom = max(teams.values(), key=scout_getter('CV'))
return {
'melhor_da_rodada': {
'nome': best['nome'],
'total': best['melhor_da_rodada'],
'desc': 'vezes',
'pos': 0
},
'pior_da_rodada': {
'nome': worst['nome'],
'total': worst['pior_da_rodada'],
'desc': 'vezes',
'pos': 1
},
'maior_valorização': {
'nome': rich['nome'],
'total': rich['maior_valorização'],
'desc': 'vezes',
'pos': 2
},
'pior_valorização': {
'nome': poor['nome'],
'total': poor['pior_valorização'],
'desc': 'vezes',
'pos': 3
},
'artilheiro': {
'nome': scorer['nome'],
'total': scorer['scout']['G'],
'desc': 'gols',
'pos': 4
},
'garçom': {
'nome': waiter['nome'],
'total': waiter['scout']['A'],
'desc': 'assitências',
'pos': 5
},
'retranqueiro': {
'nome': solid_defense['nome'],
'total': solid_defense['scout']['SG'],
'desc': 'SG garantidos',
'pos': 6
},
'chutou_é_gol': {
'nome': inverse_scorer['nome'],
'total': inverse_scorer['scout']['GS'],
'desc': 'gols sofridos',
'pos': 7
},
'perna_de_pau': {
'nome': wooden_leg['nome'],
'total': wooden_leg['scout']['G'],
'desc': 'gols',
'pos': 8
},
'fair_player': {
'nome': fair_player['nome'],
'total': fair_player['scout']['CV'],
'desc': 'cartões vermelhos',
'pos': 9
},
'descuidado': {
'nome': careless['nome'],
'total': careless['scout']['CA'],
'desc': 'cartões amarelos',
'pos': 10
},
'briguento': {
'nome': quarrelsom['nome'],
'total': quarrelsom['scout']['CV'],
'desc': 'cartões vermelhos',
'pos': 11
},
}
def update_scouts(obj, player):
for key, value in player['scout'].items():
try:
obj['scout'][key] += value
except KeyError:
obj['scout'][key] = value
return obj
def update_player_data(dest, _team, player):
actual = dest['atletas'].get(player['atleta_id'])
if actual is None:
actual = {'pontos': 0, 'scout': {}, 'escalado': 0}
actual['escalado'] += 1
actual['pontos'] += player['pontos_num']
actual['foto'] = player['foto']
actual['apelido'] = player['apelido']
actual['clube'] = _team
actual['posicao_id'] = player['posicao_id']
dest['atletas'][player['atleta_id']] = update_scouts(actual, player)
def update_team_data(dest, _team, player):
actual = dest['clubes'].get(_team['id'])
if actual is None:
actual = {'pontos': 0, 'scout': {}, 'escalado': 0}
actual.update(_team)
actual['escalado'] += 1
actual['pontos'] += player['pontos_num']
dest['clubes'][_team['id']] = update_scouts(actual, player)
def get_scouts(teams):
scouts = set()
for team in teams.values():
scouts.update(team['scout'].keys())
return scouts
def set_missing_scouts(obj, scouts):
for scout in scouts:
if scout not in obj['scout']:
obj['scout'][scout] = 0
return obj
def add_missing_scouts(dest, scouts):
for key, value in dest.items():
dest[key] = set_missing_scouts(value, scouts)
return dest
def new_team(team):
return {
'nome': team['time']['nome'],
'total': team['pontos'],
'rodadas': [0, team['rodada_atual']],
'pontos': [0, team['pontos']],
'posicoes': [],
'patrimonio': [100, team['patrimonio']],
'valorizacao': [team['patrimonio'] - 100],
'melhor_da_rodada': 0,
'pior_da_rodada': 0,
'maior_valorização': 0,
'pior_valorização': 0,
'scout': {},
'atletas': {},
'clubes': {},
}
def add_round(team, _round):
team['total'] = team['total'] + _round['pontos']
team['rodadas'].append(_round['rodada_atual'])
team['pontos'].append(team['total'])
team['valorizacao'].append(_round['patrimonio'] - team['patrimonio'][-1])
team['patrimonio'].append(_round['patrimonio'])
return team
def empty_team(_id):
return {
'id': _id,
'nome': 'Sem Clube',
'escudos': {'30x30': EMPTY_TEAM_SHIELD, '45x45': EMPTY_TEAM_SHIELD},
}
def calc_sectors(team):
team['defesa'] = {'total': 0, 'media': 0, 'escalados': 0}
team['meio_campo'] = {'total': 0, 'media': 0, 'escalados': 0}
team['ataque'] = {'total': 0, 'media': 0, 'escalados': 0}
team['técnico'] = {'total': 0, 'media': 0, 'escalados': 0}
for player in team['atletas'].values():
if player['posicao_id'] in [1, 2, 3]:
key = 'defesa'
elif player['posicao_id'] == 4:
key = 'meio_campo'
elif player['posicao_id'] == 5:
key = 'ataque'
else:
key = 'técnico'
team[key]['total'] += player['pontos']
team[key]['escalados'] += player['escalado']
team[key]['media'] = team[key]['total'] / team[key]['escalados']
def generate_teams_data():
rounds = read_json(ROUNDS_PATH, [])
teams = {}
league = {'atletas': {}, 'clubes': {}}
clubs = {'1': empty_team(1)}
clubs.update(read_json(PLAYERS_MARKET_PATH, {}).get('clubes', {}))
for _round in rounds:
positions = []
patrimony = []
for team in _round:
name = team['time']['nome']
team['pontos'] = team['pontos'] or 0
try:
teams[name] = add_round(teams[name], team)
except KeyError:
teams[name] = new_team(team)
for player in team['atletas']:
teams[name] = update_scouts(teams[name], player)
_team = clubs[str(player['clube_id'])]
if team['capitao_id'] == player['atleta_id']:
player['pontos_num'] *= 2
update_player_data(league, _team, player)
update_player_data(teams[name], _team, player)
update_team_data(league, _team, player)
update_team_data(teams[name], _team, player)
positions.append((name, teams[name]['total']))
patrimony.append((name, teams[name]['valorizacao'][-1]))
rank(positions, 'posicoes', teams)
team = max(_round, key=itemgetter('pontos'))
teams[team['time']['nome']]['melhor_da_rodada'] += 1
team = min(_round, key=itemgetter('pontos'))
teams[team['time']['nome']]['pior_da_rodada'] += 1
patrimony = sorted(patrimony, key=itemgetter(1))
teams[patrimony[-1][0]]['maior_valorização'] += 1
teams[patrimony[0][0]]['pior_valorização'] += 1
scouts = get_scouts(teams)
league['atletas'] = add_missing_scouts(league['atletas'], scouts)
league['clubes'] = add_missing_scouts(league['clubes'], scouts)
teams = add_missing_scouts(teams, scouts)
for team in teams.values():
team['atletas'] = add_missing_scouts(team['atletas'], scouts)
calc_sectors(team)
calc_sectors(league)
highlights = generate_highlights(teams)
write_json(TEAMS_PATH, teams)
write_json(HIGHLIGHTS_PATH, highlights)
write_json(LEAGUE_STATS_PATH, league)
return teams
def generate_schemes_best():
group = ['gol', 'lat', 'zag', 'mei', 'ata', 'tec']
schemes = read_json(SCHEMES_PATH)
for filename in os.listdir(PLAYERS_FOLDER_PATH):
round_path = os.path.join(PLAYERS_FOLDER_PATH, filename)
players = read_json(round_path)['atletas']
for player in players.values():
if 'pontos' not in player:
player['pontos'] = 0
teams = []
for scheme in schemes:
team = {
'esquema': scheme,
'jogadores': [],
'pontuacao': 0,
'preco': 0,
'valorizacao': 0
}
for pos, qty in scheme['posicoes'].items():
pos_index = group.index(pos) + 1
team['jogadores'].extend(
sorted(
[player for player in players.values()
if player['posicao_id'] == pos_index],
key=itemgetter('pontos'),
reverse=True
)[:qty]
)
captain_index = -1
captain_points = 0
for i, player in enumerate(team['jogadores']):
team['pontuacao'] += player['pontos']
team['preco'] += player['preco_anterior']
team['valorizacao'] += player['variacao']
player['capitao'] = False
if (player['pontos'] > captain_points and
player['posicao_id'] != 6):
captain_points = player['pontos']
captain_index = i
team['jogadores'][captain_index]['capitao'] = True
team['pontuacao'] += team['jogadores'][captain_index]['pontos']
teams.append(team)
schemes_path = round_path.replace('atletas', 'melhores-esquemas')
write_json(schemes_path, teams)
def main():
generate_teams_data()
generate_schemes_best()
if __name__ == '__main__':
main()