-
Notifications
You must be signed in to change notification settings - Fork 0
/
market.js
166 lines (145 loc) · 6.75 KB
/
market.js
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
const fs = require('fs');
const getPlayerProbs = require('./probs');
const ProgressBar = require('progress');
// Mapa de tiers a valores numéricos
const tierValues = {
'S': 3,
'A': 2,
'B': 1,
'C': 0
};
// Cargar data.json y calendar.json
const data = JSON.parse(fs.readFileSync('data.json', 'utf8'));
const calendar = JSON.parse(fs.readFileSync('calendar.json', 'utf8'));
async function getPlayerProbability(url) {
try {
const probability = await getPlayerProbs(url);
return parseFloat(probability.replace('%', ''));
} catch (error) {
console.log(`No se han encontrado datos de probabilidad para la URL ${url}`);
return null;
}
}
function findTeamAndPlayer(playerName) {
const nameParts = playerName.toLowerCase().split(' ');
return data.flatMap(team => team.jugadores.map(player => ({ team, player })))
.find(({ player }) => nameParts.every(part => player.nombre.toLowerCase().includes(part)));
}
function normalizePosition(position) {
const pos = position.toLowerCase();
if (pos === 'portero') return 'Portero';
if (pos === 'defensa') return 'Defensa';
if (pos === 'centrocampista') return 'Centrocampista';
if (pos === 'delantero') return 'Delantero';
return null;
}
function shouldDiscardPlayer(player, team, opponentTeam, probability) {
const teamTierValue = tierValues[team.tier];
const opponentTeamTierValue = tierValues[opponentTeam.tier];
const playerTierValue = tierValues[player.tier];
if (teamTierValue >= opponentTeamTierValue) {
return false; // No se descarta si el equipo del jugador tiene un tier mayor o igual que el del equipo oponente
} else {
// Tier del equipo del jugador es menor
if (playerTierValue === 0) { // Tier C
console.log(`Player ${player.nombre} discarded because player tier is C and opponent team has a higher tier (${opponentTeam.tier})`);
return true;
} else if (playerTierValue === 1 && probability < 90) { // Tier B
console.log(`Player ${player.nombre} discarded because player tier is B and probability (${probability}%) is less than 90%`);
return true;
} else if (playerTierValue === 2 && probability < 80) { // Tier A
console.log(`Player ${player.nombre} discarded because player tier is A and probability (${probability}%) is less than 80%`);
return true;
}
}
return false;
}
async function recommendPlayers(players) {
// Crear barra de progreso
const bar = new ProgressBar('Processing [:bar] :percent :etas', { total: players.length });
const recommendedPlayers = [];
const allPlayers = { Portero: [], Defensa: [], Centrocampista: [], Delantero: [] };
for (const playerName of players) {
const playerData = findTeamAndPlayer(playerName);
if (!playerData) {
console.log(`Player ${playerName} not found`);
bar.tick();
continue;
}
const { team, player } = playerData;
const probability = await getPlayerProbability(player.url);
if (probability !== null) {
const normalizedPosition = normalizePosition(player.position);
allPlayers[normalizedPosition].push({ ...player, team: team.nombre, probability });
if (probability > 50) {
const matchup = calendar.jornadas.flatMap(j => j.partidos).find(m => m.local === team.nombre || m.visitante === team.nombre);
if (matchup) {
const opponentTeam = matchup.local === team.nombre ? data.find(t => t.nombre === matchup.visitante) : data.find(t => t.nombre === matchup.local);
if (opponentTeam) {
if (!shouldDiscardPlayer(player, team, opponentTeam, probability)) {
recommendedPlayers.push({ ...player, team: team.nombre, probability, position: normalizedPosition });
}
} else {
console.log(`Opponent team for ${team.nombre} not found`);
}
}
} else {
console.log(`Player ${player.nombre} from ${team.nombre} discarded because probability (${probability}%) is not greater than 50%`);
}
} else {
console.log(`Player ${player.nombre} from ${team.nombre} discarded because probability data is not available`);
}
bar.tick();
}
// Ordenar todos los jugadores por probabilidad
for (const position in allPlayers) {
allPlayers[position].sort((a, b) => b.probability - a.probability);
}
// Ordenar por tier y probabilidad para la recomendación
recommendedPlayers.sort((a, b) => {
const tierComparison = tierValues[b.tier] - tierValues[a.tier];
if (tierComparison !== 0) {
return tierComparison;
} else {
return b.probability - a.probability;
}
});
// Formatear y mostrar la lista de todos los jugadores
console.log('All players in the market:');
for (const position in allPlayers) {
console.log(position + ':');
allPlayers[position].forEach(player => {
console.log(`- ${player.nombre} (${player.probability}%)`);
});
}
// Formatear y mostrar la lista de jugadores recomendados
const formattedRecommendedPlayers = {
Portero: [],
Defensas: [],
Centrocampistas: [],
Delanteros: []
};
for (const player of recommendedPlayers) {
if (player.position === 'Portero') {
formattedRecommendedPlayers.Portero.push(`${player.nombre} (${player.probability}%)`);
} else if (player.position === 'Defensa') {
formattedRecommendedPlayers.Defensas.push(`${player.nombre} (${player.probability}%)`);
} else if (player.position === 'Centrocampista') {
formattedRecommendedPlayers.Centrocampistas.push(`${player.nombre} (${player.probability}%)`);
} else if (player.position === 'Delantero') {
formattedRecommendedPlayers.Delanteros.push(`${player.nombre} (${player.probability}%)`);
}
}
console.log('Recommended players:');
console.log('Portero:');
console.log(`- ${formattedRecommendedPlayers.Portero.join('\n- ')}`);
console.log('Defensas:');
console.log(`- ${formattedRecommendedPlayers.Defensas.join('\n- ')}`);
console.log('Centrocampistas:');
console.log(`- ${formattedRecommendedPlayers.Centrocampistas.join('\n- ')}`);
console.log('Delanteros:');
console.log(`- ${formattedRecommendedPlayers.Delanteros.join('\n- ')}`);
}
// Uso del programa
const marketPlayers = ['Le Normand', 'Marcos André', 'Aihen', 'Larin', 'Maffeo', 'Mandi', 'Kubo', 'Iñaki Peña', 'Unai G', 'Odriozola'];
recommendPlayers(marketPlayers);