Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2025
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. // **šŸ”¹ Step 1: Fetch Players & Teams**
  2. console.log("Fetching players and teams...");
  3.  
  4. const players = await bbgm.idb.cache.players.getAll();
  5. const teams = await bbgm.idb.cache.teams.getAll();
  6. const teamSeasons = await bbgm.idb.cache.teamSeasons.getAll();
  7.  
  8. console.log(`Fetched ${players.length} players and ${teams.length} teams.`);
  9.  
  10. // **šŸ”¹ Step 2: Calculate Prestige for Every Team**
  11. async function calculateTeamPrestige() {
  12. console.log("Calculating Team Prestige...");
  13.  
  14. const seasonChampions = {};
  15. teamSeasons.forEach(season => {
  16. const year = season.season;
  17. if (!seasonChampions[year]) {
  18. seasonChampions[year] = Math.max(...teamSeasons
  19. .filter(ts => ts.season === year)
  20. .map(ts => ts.playoffRoundsWon || 0));
  21. }
  22. });
  23.  
  24. const teamPrestiges = teams.map(team => {
  25. const teamHistory = teamSeasons.filter(ts => ts.tid === team.tid);
  26. if (!teamHistory.length) return { tid: team.tid, prestige: 0 };
  27.  
  28. const totalWins = teamHistory.reduce((sum, season) => sum + (season.won || 0), 0);
  29. const totalChampionships = teamHistory.reduce((count, season) => {
  30. return count + (season.playoffRoundsWon === seasonChampions[season.season] ? 1 : 0);
  31. }, 0);
  32.  
  33. // Get last 3 seasons for recent performance
  34. const recentSeasons = teamHistory.slice(-3);
  35. const recentWins = recentSeasons.reduce((sum, season) => sum + (season.won || 0), 0);
  36. const recentChampionships = recentSeasons.reduce((count, season) => {
  37. return count + (season.playoffRoundsWon === seasonChampions[season.season] ? 1 : 0);
  38. }, 0);
  39.  
  40. // Use most recent season's budget values
  41. const latestSeason = teamHistory.at(-1);
  42. const { coaching, facilities, health, scouting } = team.budget;
  43.  
  44. // Prestige Formula
  45. let prestige = (coaching * 0.4) + (facilities * 0.3) + (health * 0.1) + (scouting * 0.2);
  46. prestige += (totalWins * 0.2);
  47. prestige += (totalChampionships * 15);
  48. prestige += (recentWins * 0.3);
  49. prestige += (recentChampionships * 20);
  50.  
  51. return { tid: team.tid, prestige: prestige.toFixed(1) };
  52. });
  53.  
  54. teamPrestiges.sort((a, b) => b.prestige - a.prestige);
  55. return teamPrestiges;
  56. }
  57.  
  58. const teamPrestige = await calculateTeamPrestige();
  59.  
  60. // **šŸ”¹ Step 3: Identify Last Season**
  61. const currentSeason = bbgm.g.get("season");
  62. const lastSeason = currentSeason - 1;
  63. console.log(`Last season detected: ${lastSeason}`);
  64.  
  65. // **šŸ”¹ Step 4: Analyze Team Depth**
  66. const teamDepth = {};
  67. players.forEach(player => {
  68. const { tid, ratings } = player;
  69. const pos = ratings?.at(-1)?.pos;
  70. if (!pos || tid < 0) return;
  71.  
  72. if (!teamDepth[tid]) teamDepth[tid] = {};
  73. if (!teamDepth[tid][pos]) teamDepth[tid][pos] = [];
  74.  
  75. teamDepth[tid][pos].push(ratings.at(-1).ovr);
  76. });
  77.  
  78. Object.keys(teamDepth).forEach(tid => {
  79. Object.keys(teamDepth[tid]).forEach(pos => {
  80. teamDepth[tid][pos].sort((a, b) => b - a);
  81. });
  82. });
  83.  
  84. // **šŸ”¹ Step 5: Find Players Who Should Transfer**
  85. const transferCandidates = [];
  86.  
  87. players.forEach(player => {
  88. const lastSeasonStats = player.stats?.find(stat => stat.season === lastSeason && !stat.playoffs);
  89. if (!lastSeasonStats) return;
  90.  
  91. const { tid, ratings } = player;
  92. const pos = ratings?.at(-1)?.pos;
  93. const overall = ratings?.at(-1)?.ovr;
  94. if (!pos || overall === undefined || tid < 0) return;
  95.  
  96. const isBenched = lastSeasonStats.min < 500;
  97. const isLowScorer = lastSeasonStats.pts < 12;
  98. const isNonStarter = lastSeasonStats.gs < 10;
  99. const betterTeammates = (teamDepth[tid]?.[pos] || []).filter(ovr => ovr > overall).length;
  100. const isOvercrowded = betterTeammates >= 3;
  101.  
  102. // **Elite Players (OVR 55+) Seeking Prestige Teams**
  103. const teamSeason = teamSeasons.find(ts => ts.tid === tid && ts.season === lastSeason);
  104. const isSeekingBetterOpportunities = overall >= 55 && teamSeason && teamSeason.won < 18;
  105.  
  106. let transferReason = "";
  107. if (isBenched && isLowScorer && isNonStarter) {
  108. transferReason = "Not enough playing time";
  109. } else if (isOvercrowded) {
  110. transferReason = "Too much competition at position";
  111. } else if (isSeekingBetterOpportunities) {
  112. transferReason = "Seeking better opportunities (Bad Team Performance)";
  113. }
  114.  
  115. if (transferReason) {
  116. transferCandidates.push({ player, transferReason });
  117. }
  118. });
  119.  
  120. console.log(`\nFound ${transferCandidates.length} players eligible for transfer.`);
  121.  
  122. // **šŸ”¹ Step 6: Assign Transfers & Log Prestige for All Players**
  123. let transferCount = 0;
  124. const transferLog = [];
  125.  
  126. for (const { player, transferReason } of transferCandidates) {
  127. const { tid, ratings, firstName, lastName } = player;
  128. const pos = ratings?.at(-1)?.pos;
  129. const overall = ratings?.at(-1)?.ovr;
  130. const oldTeam = teams.find(t => t.tid === tid);
  131.  
  132. if (!pos || overall === undefined || !oldTeam) continue;
  133.  
  134. // **Find All Possible Teams**
  135. const possibleTeams = teams.map(team => ({
  136. team,
  137. prestige: teamPrestige.find(p => p.tid === team.tid)?.prestige || "N/A",
  138. }));
  139.  
  140. // Pick a random team from filtered list
  141. const chosenTeamEntry = possibleTeams[Math.floor(Math.random() * possibleTeams.length)];
  142. const newTeam = chosenTeamEntry.team;
  143. const prestigeRating = chosenTeamEntry.prestige;
  144.  
  145. // Assign player to new team
  146. player.tid = newTeam.tid;
  147. await bbgm.idb.cache.players.put(player);
  148.  
  149. transferLog.push({ firstName, lastName, overall, oldTeam, newTeam, prestigeRating, transferReason });
  150. transferCount++;
  151.  
  152. console.log(`${firstName} ${lastName} (OVR: ${overall}) transferring from ${oldTeam.region} ${oldTeam.name}`);
  153. console.log(` āž”ļø Assigned to ${newTeam.region} ${newTeam.name} (Prestige: ${prestigeRating}) āœ…`);
  154. console.log(` šŸ“ Reason: ${transferReason}\n`);
  155. }
  156.  
  157. console.log(`āœ… Successfully transferred ${transferCount} players.`);
  158. console.log("Transfer process complete.");
  159.  
  160. // **šŸ† Display Top 5 Transfers & Log All Transfers**
  161. transferLog.sort((a, b) => b.overall - a.overall);
  162.  
  163. // **šŸ† Top 5 Transfers**
  164. console.log("\nšŸ† **Top 5 Transfers by OVR** šŸ†");
  165. transferLog.slice(0, 5).forEach(({ firstName, lastName, overall, oldTeam, newTeam, prestigeRating, transferReason }, index) => {
  166. console.log(`${index + 1}. ${firstName} ${lastName} (OVR: ${overall})`);
  167. console.log(` āž”ļø ${oldTeam.region} ${oldTeam.name} → ${newTeam.region} ${newTeam.name} (Prestige: ${prestigeRating})`);
  168. console.log(` šŸ“ Reason: ${transferReason}`);
  169. console.log("--------------------");
  170. });
  171.  
  172. // **šŸ“œ Log All Transfers**
  173. console.log("\nšŸ“œ **All Transfers** šŸ“œ");
  174. transferLog.forEach(({ firstName, lastName, overall, oldTeam, newTeam, prestigeRating, transferReason }) => {
  175. console.log(`āœ… ${firstName} ${lastName} (OVR: ${overall}) → ${newTeam.region} ${newTeam.name} (Prestige: ${prestigeRating})`);
  176. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement