Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2025
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. // **Step 1: Fetch Data**
  2. console.log("πŸ” Fetching players, teams, and team seasons...");
  3. const teams = await bbgm.idb.cache.teams.getAll();
  4. const teamSeasons = await bbgm.idb.cache.teamSeasons.getAll();
  5. const freeAgents = await bbgm.idb.cache.players.getAll();
  6.  
  7. console.log(`βœ… Found ${teams.length} teams, ${teamSeasons.length} teamSeasons, and ${freeAgents.length} players.`);
  8.  
  9. // **Step 2: Calculate Team Prestige**
  10. console.log("πŸ” Fetching and analyzing teamSeasons data...");
  11.  
  12. // Extract valid seasons
  13. const latestSeason = Math.max(...teamSeasons.map(ts => ts.season));
  14.  
  15. // **Determine Actual Season Champions**
  16. const seasonChampions = {};
  17. teamSeasons.forEach(season => {
  18. if (season.playoffRoundsWon > 0) {
  19. if (!seasonChampions[season.season] || season.playoffRoundsWon > seasonChampions[season.season].roundsWon) {
  20. seasonChampions[season.season] = { tid: season.tid, roundsWon: season.playoffRoundsWon };
  21. }
  22. }
  23. });
  24.  
  25. // **Calculate Prestige**
  26. const teamPrestiges = teams.map(team => {
  27. const teamHistory = teamSeasons.filter(ts => ts.tid === team.tid);
  28. if (!teamHistory.length) return { tid: team.tid, prestige: 0 };
  29.  
  30. // **All-Time Wins & Championships**
  31. const totalWins = teamHistory.reduce((sum, season) => sum + (season.won || 0), 0);
  32. const totalChampionships = teamHistory.reduce((count, season) => {
  33. return count + (seasonChampions[season.season]?.tid === team.tid ? 1 : 0);
  34. }, 0);
  35.  
  36. // **Recent Wins & Championships (Last 3 Seasons)**
  37. const recentSeasons = teamHistory.filter(ts => ts.season >= latestSeason - 2);
  38. const recentWins = recentSeasons.reduce((sum, season) => sum + (season.won || 0), 0);
  39. const recentChampionships = recentSeasons.reduce((count, season) => {
  40. return count + (seasonChampions[season.season]?.tid === team.tid ? 1 : 0);
  41. }, 0);
  42.  
  43. // **Fetch budget values from the team object**
  44. const { budget } = team;
  45. const coaching = budget?.coaching ?? 50;
  46. const facilities = budget?.facilities ?? 50;
  47. const health = budget?.health ?? 50;
  48. const scouting = budget?.scouting ?? 50;
  49.  
  50. // **Balanced Prestige Formula**
  51. let prestige = 0;
  52. prestige += (coaching * 0.4) + (facilities * 0.3) + (health * 0.1) + (scouting * 0.2);
  53. prestige += (totalWins * 0.08);
  54. prestige += (totalChampionships * 4);
  55. prestige += (recentWins * 0.4);
  56. prestige += (recentChampionships * 6);
  57.  
  58. return { tid: team.tid, prestige, totalWins, totalChampionships, recentWins, recentChampionships };
  59. });
  60.  
  61. // **Sort teams by prestige**
  62. teamPrestiges.sort((a, b) => b.prestige - a.prestige);
  63.  
  64. // **Step 3: Get Recruits**
  65. const rankedRecruits = freeAgents
  66. .filter(p => p.tid === -1) // Only consider unsigned recruits
  67. .sort((a, b) => b.ratings.at(-1).ovr - a.ratings.at(-1).ovr);
  68.  
  69. const top5Recruits = rankedRecruits.slice(0, 5);
  70.  
  71. // **Step 4: Assign Recruits to Schools (Needs + Prestige + Balance)**
  72. console.log("\nπŸ€ **Recruiting Process Start** πŸ€");
  73.  
  74. let recruitSignings = [];
  75. const teamAssignments = {}; // Track recruits per team
  76.  
  77. // Initialize team assignments
  78. for (const team of teams) {
  79. teamAssignments[team.tid] = 0;
  80. }
  81.  
  82. // **Keep Track of Which Top 5 Schools Already Signed a Top Recruit**
  83. const takenTopSchools = new Set();
  84.  
  85. // **First Pass: Assign Top 5 Recruits (Each Has a 5% Chance for Unexpected Commitment)**
  86. for (const recruit of top5Recruits) {
  87. let assignedTeam;
  88. let unexpected = false; // Default to false
  89.  
  90. if (Math.random() < 0.05) {
  91. // **Unexpected School Selection**
  92. const availableUnexpectedTeams = teams
  93. .filter(t => teamAssignments[t.tid] < 4 && !teamPrestiges.slice(0, 5).some(topTeam => topTeam.tid === t.tid));
  94.  
  95. if (availableUnexpectedTeams.length > 0) {
  96. assignedTeam = availableUnexpectedTeams[Math.floor(Math.random() * availableUnexpectedTeams.length)];
  97. unexpected = true;
  98. }
  99. }
  100.  
  101. if (!assignedTeam) {
  102. // **Select a Top 5 School that hasn't already signed a top recruit**
  103. const availableTopTeams = teamPrestiges
  104. .slice(0, 5)
  105. .map(entry => teams.find(t => t.tid === entry.tid))
  106. .filter(t => teamAssignments[t.tid] < 4 && !takenTopSchools.has(t.tid));
  107.  
  108. if (availableTopTeams.length > 0) {
  109. assignedTeam = availableTopTeams[Math.floor(Math.random() * availableTopTeams.length)];
  110. takenTopSchools.add(assignedTeam.tid); // Mark this team as taken
  111. }
  112. }
  113.  
  114. if (assignedTeam) {
  115. recruit.tid = assignedTeam.tid;
  116. await bbgm.idb.cache.players.put(recruit);
  117. teamAssignments[assignedTeam.tid]++;
  118. recruitSignings.push({ recruit, assignedTeam, unexpected });
  119. }
  120. }
  121.  
  122. // **Second Pass: Assign Remaining Players**
  123. for (const recruit of rankedRecruits.slice(5)) {
  124. let assignedTeam;
  125. const availablePrestigeTeams = teamPrestiges
  126. .map(entry => teams.find(t => t.tid === entry.tid))
  127. .filter(t => teamAssignments[t.tid] < 4);
  128.  
  129. if (availablePrestigeTeams.length === 0) continue;
  130.  
  131. assignedTeam = availablePrestigeTeams[Math.floor(Math.random() * availablePrestigeTeams.length)];
  132.  
  133. recruit.tid = assignedTeam.tid;
  134. await bbgm.idb.cache.players.put(recruit);
  135. teamAssignments[assignedTeam.tid]++;
  136. recruitSignings.push({ recruit, assignedTeam, unexpected: false });
  137. }
  138.  
  139. // **Final Report for Top 5 Recruits**
  140. console.log("\nπŸ€ **Top 5 Recruit Considerations** πŸ€");
  141. for (const { recruit, assignedTeam, unexpected } of recruitSignings.slice(0, 5)) {
  142. console.log(`πŸ” ${recruit.firstName} ${recruit.lastName} (OVR: ${recruit.ratings.at(-1).ovr}) was considering:`);
  143.  
  144. const topConsideredTeams = teamPrestiges.slice(0, 5);
  145. for (const teamData of topConsideredTeams) {
  146. const team = teams.find(t => t.tid === teamData.tid);
  147. console.log(` - ${team.region} ${team.name} (Prestige: ${teamData.prestige.toFixed(1)})`);
  148. }
  149.  
  150. console.log(`🎯 **Final Decision:** ${assignedTeam.region} ${assignedTeam.name} ${unexpected ? "🌟UNEXPECTED🌟" : ""}\n`);
  151. }
  152.  
  153. // **Log ALL Recruit Signings**
  154. console.log("\nπŸ€ **All Recruit Signings** πŸ€");
  155. for (const { recruit, assignedTeam, unexpected } of recruitSignings) {
  156. console.log(`βœ… ${recruit.firstName} ${recruit.lastName} (OVR: ${recruit.ratings.at(-1).ovr}) β†’ ${assignedTeam.region} ${assignedTeam.name} ${unexpected ? "🌟UNEXPECTED🌟" : ""}`);
  157. }
  158.  
  159. // **Final Report for ALL Teams**
  160. console.log("\nπŸ€ **Final Team Recruitment Report** πŸ€");
  161. for (const team of teams) {
  162. console.log(`${team.region} ${team.name}: ${teamAssignments[team.tid]} recruits signed.`);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement