Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // **š¹ Step 1: Fetch Players & Teams**
- console.log("Fetching players and teams...");
- const players = await bbgm.idb.cache.players.getAll();
- const teams = await bbgm.idb.cache.teams.getAll();
- const teamSeasons = await bbgm.idb.cache.teamSeasons.getAll();
- console.log(`Fetched ${players.length} players and ${teams.length} teams.`);
- // **š¹ Step 2: Calculate Prestige for Every Team**
- async function calculateTeamPrestige() {
- console.log("Calculating Team Prestige...");
- const seasonChampions = {};
- teamSeasons.forEach(season => {
- const year = season.season;
- if (!seasonChampions[year]) {
- seasonChampions[year] = Math.max(...teamSeasons
- .filter(ts => ts.season === year)
- .map(ts => ts.playoffRoundsWon || 0));
- }
- });
- const teamPrestiges = teams.map(team => {
- const teamHistory = teamSeasons.filter(ts => ts.tid === team.tid);
- if (!teamHistory.length) return { tid: team.tid, prestige: 0 };
- const totalWins = teamHistory.reduce((sum, season) => sum + (season.won || 0), 0);
- const totalChampionships = teamHistory.reduce((count, season) => {
- return count + (season.playoffRoundsWon === seasonChampions[season.season] ? 1 : 0);
- }, 0);
- // Get last 3 seasons for recent performance
- const recentSeasons = teamHistory.slice(-3);
- const recentWins = recentSeasons.reduce((sum, season) => sum + (season.won || 0), 0);
- const recentChampionships = recentSeasons.reduce((count, season) => {
- return count + (season.playoffRoundsWon === seasonChampions[season.season] ? 1 : 0);
- }, 0);
- // Use most recent season's budget values
- const latestSeason = teamHistory.at(-1);
- const { coaching, facilities, health, scouting } = team.budget;
- // Prestige Formula
- let prestige = (coaching * 0.4) + (facilities * 0.3) + (health * 0.1) + (scouting * 0.2);
- prestige += (totalWins * 0.2);
- prestige += (totalChampionships * 15);
- prestige += (recentWins * 0.3);
- prestige += (recentChampionships * 20);
- return { tid: team.tid, prestige: prestige.toFixed(1) };
- });
- teamPrestiges.sort((a, b) => b.prestige - a.prestige);
- return teamPrestiges;
- }
- const teamPrestige = await calculateTeamPrestige();
- // **š¹ Step 3: Identify Last Season**
- const currentSeason = bbgm.g.get("season");
- const lastSeason = currentSeason - 1;
- console.log(`Last season detected: ${lastSeason}`);
- // **š¹ Step 4: Analyze Team Depth**
- const teamDepth = {};
- players.forEach(player => {
- const { tid, ratings } = player;
- const pos = ratings?.at(-1)?.pos;
- if (!pos || tid < 0) return;
- if (!teamDepth[tid]) teamDepth[tid] = {};
- if (!teamDepth[tid][pos]) teamDepth[tid][pos] = [];
- teamDepth[tid][pos].push(ratings.at(-1).ovr);
- });
- Object.keys(teamDepth).forEach(tid => {
- Object.keys(teamDepth[tid]).forEach(pos => {
- teamDepth[tid][pos].sort((a, b) => b - a);
- });
- });
- // **š¹ Step 5: Find Players Who Should Transfer**
- const transferCandidates = [];
- players.forEach(player => {
- const lastSeasonStats = player.stats?.find(stat => stat.season === lastSeason && !stat.playoffs);
- if (!lastSeasonStats) return;
- const { tid, ratings } = player;
- const pos = ratings?.at(-1)?.pos;
- const overall = ratings?.at(-1)?.ovr;
- if (!pos || overall === undefined || tid < 0) return;
- const isBenched = lastSeasonStats.min < 500;
- const isLowScorer = lastSeasonStats.pts < 12;
- const isNonStarter = lastSeasonStats.gs < 10;
- const betterTeammates = (teamDepth[tid]?.[pos] || []).filter(ovr => ovr > overall).length;
- const isOvercrowded = betterTeammates >= 3;
- // **Elite Players (OVR 55+) Seeking Prestige Teams**
- const teamSeason = teamSeasons.find(ts => ts.tid === tid && ts.season === lastSeason);
- const isSeekingBetterOpportunities = overall >= 55 && teamSeason && teamSeason.won < 18;
- let transferReason = "";
- if (isBenched && isLowScorer && isNonStarter) {
- transferReason = "Not enough playing time";
- } else if (isOvercrowded) {
- transferReason = "Too much competition at position";
- } else if (isSeekingBetterOpportunities) {
- transferReason = "Seeking better opportunities (Bad Team Performance)";
- }
- if (transferReason) {
- transferCandidates.push({ player, transferReason });
- }
- });
- console.log(`\nFound ${transferCandidates.length} players eligible for transfer.`);
- // **š¹ Step 6: Assign Transfers & Log Prestige for All Players**
- let transferCount = 0;
- const transferLog = [];
- for (const { player, transferReason } of transferCandidates) {
- const { tid, ratings, firstName, lastName } = player;
- const pos = ratings?.at(-1)?.pos;
- const overall = ratings?.at(-1)?.ovr;
- const oldTeam = teams.find(t => t.tid === tid);
- if (!pos || overall === undefined || !oldTeam) continue;
- // **Find All Possible Teams**
- const possibleTeams = teams.map(team => ({
- team,
- prestige: teamPrestige.find(p => p.tid === team.tid)?.prestige || "N/A",
- }));
- // Pick a random team from filtered list
- const chosenTeamEntry = possibleTeams[Math.floor(Math.random() * possibleTeams.length)];
- const newTeam = chosenTeamEntry.team;
- const prestigeRating = chosenTeamEntry.prestige;
- // Assign player to new team
- player.tid = newTeam.tid;
- await bbgm.idb.cache.players.put(player);
- transferLog.push({ firstName, lastName, overall, oldTeam, newTeam, prestigeRating, transferReason });
- transferCount++;
- console.log(`${firstName} ${lastName} (OVR: ${overall}) transferring from ${oldTeam.region} ${oldTeam.name}`);
- console.log(` ā”ļø Assigned to ${newTeam.region} ${newTeam.name} (Prestige: ${prestigeRating}) ā `);
- console.log(` š Reason: ${transferReason}\n`);
- }
- console.log(`ā Successfully transferred ${transferCount} players.`);
- console.log("Transfer process complete.");
- // **š Display Top 5 Transfers & Log All Transfers**
- transferLog.sort((a, b) => b.overall - a.overall);
- // **š Top 5 Transfers**
- console.log("\nš **Top 5 Transfers by OVR** š");
- transferLog.slice(0, 5).forEach(({ firstName, lastName, overall, oldTeam, newTeam, prestigeRating, transferReason }, index) => {
- console.log(`${index + 1}. ${firstName} ${lastName} (OVR: ${overall})`);
- console.log(` ā”ļø ${oldTeam.region} ${oldTeam.name} ā ${newTeam.region} ${newTeam.name} (Prestige: ${prestigeRating})`);
- console.log(` š Reason: ${transferReason}`);
- console.log("--------------------");
- });
- // **š Log All Transfers**
- console.log("\nš **All Transfers** š");
- transferLog.forEach(({ firstName, lastName, overall, oldTeam, newTeam, prestigeRating, transferReason }) => {
- console.log(`ā ${firstName} ${lastName} (OVR: ${overall}) ā ${newTeam.region} ${newTeam.name} (Prestige: ${prestigeRating})`);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement