Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // **Step 1: Fetch Data**
- console.log("π Fetching players, teams, and team seasons...");
- const teams = await bbgm.idb.cache.teams.getAll();
- const teamSeasons = await bbgm.idb.cache.teamSeasons.getAll();
- const freeAgents = await bbgm.idb.cache.players.getAll();
- console.log(`β Found ${teams.length} teams, ${teamSeasons.length} teamSeasons, and ${freeAgents.length} players.`);
- // **Step 2: Calculate Team Prestige**
- console.log("π Fetching and analyzing teamSeasons data...");
- // Extract valid seasons
- const latestSeason = Math.max(...teamSeasons.map(ts => ts.season));
- // **Determine Actual Season Champions**
- const seasonChampions = {};
- teamSeasons.forEach(season => {
- if (season.playoffRoundsWon > 0) {
- if (!seasonChampions[season.season] || season.playoffRoundsWon > seasonChampions[season.season].roundsWon) {
- seasonChampions[season.season] = { tid: season.tid, roundsWon: season.playoffRoundsWon };
- }
- }
- });
- // **Calculate Prestige**
- const teamPrestiges = teams.map(team => {
- const teamHistory = teamSeasons.filter(ts => ts.tid === team.tid);
- if (!teamHistory.length) return { tid: team.tid, prestige: 0 };
- // **All-Time Wins & Championships**
- const totalWins = teamHistory.reduce((sum, season) => sum + (season.won || 0), 0);
- const totalChampionships = teamHistory.reduce((count, season) => {
- return count + (seasonChampions[season.season]?.tid === team.tid ? 1 : 0);
- }, 0);
- // **Recent Wins & Championships (Last 3 Seasons)**
- const recentSeasons = teamHistory.filter(ts => ts.season >= latestSeason - 2);
- const recentWins = recentSeasons.reduce((sum, season) => sum + (season.won || 0), 0);
- const recentChampionships = recentSeasons.reduce((count, season) => {
- return count + (seasonChampions[season.season]?.tid === team.tid ? 1 : 0);
- }, 0);
- // **Fetch budget values from the team object**
- const { budget } = team;
- const coaching = budget?.coaching ?? 50;
- const facilities = budget?.facilities ?? 50;
- const health = budget?.health ?? 50;
- const scouting = budget?.scouting ?? 50;
- // **Balanced Prestige Formula**
- let prestige = 0;
- prestige += (coaching * 0.4) + (facilities * 0.3) + (health * 0.1) + (scouting * 0.2);
- prestige += (totalWins * 0.08);
- prestige += (totalChampionships * 4);
- prestige += (recentWins * 0.4);
- prestige += (recentChampionships * 6);
- return { tid: team.tid, prestige, totalWins, totalChampionships, recentWins, recentChampionships };
- });
- // **Sort teams by prestige**
- teamPrestiges.sort((a, b) => b.prestige - a.prestige);
- // **Step 3: Get Recruits**
- const rankedRecruits = freeAgents
- .filter(p => p.tid === -1) // Only consider unsigned recruits
- .sort((a, b) => b.ratings.at(-1).ovr - a.ratings.at(-1).ovr);
- const top5Recruits = rankedRecruits.slice(0, 5);
- // **Step 4: Assign Recruits to Schools (Needs + Prestige + Balance)**
- console.log("\nπ **Recruiting Process Start** π");
- let recruitSignings = [];
- const teamAssignments = {}; // Track recruits per team
- // Initialize team assignments
- for (const team of teams) {
- teamAssignments[team.tid] = 0;
- }
- // **Keep Track of Which Top 5 Schools Already Signed a Top Recruit**
- const takenTopSchools = new Set();
- // **First Pass: Assign Top 5 Recruits (Each Has a 5% Chance for Unexpected Commitment)**
- for (const recruit of top5Recruits) {
- let assignedTeam;
- let unexpected = false; // Default to false
- if (Math.random() < 0.05) {
- // **Unexpected School Selection**
- const availableUnexpectedTeams = teams
- .filter(t => teamAssignments[t.tid] < 4 && !teamPrestiges.slice(0, 5).some(topTeam => topTeam.tid === t.tid));
- if (availableUnexpectedTeams.length > 0) {
- assignedTeam = availableUnexpectedTeams[Math.floor(Math.random() * availableUnexpectedTeams.length)];
- unexpected = true;
- }
- }
- if (!assignedTeam) {
- // **Select a Top 5 School that hasn't already signed a top recruit**
- const availableTopTeams = teamPrestiges
- .slice(0, 5)
- .map(entry => teams.find(t => t.tid === entry.tid))
- .filter(t => teamAssignments[t.tid] < 4 && !takenTopSchools.has(t.tid));
- if (availableTopTeams.length > 0) {
- assignedTeam = availableTopTeams[Math.floor(Math.random() * availableTopTeams.length)];
- takenTopSchools.add(assignedTeam.tid); // Mark this team as taken
- }
- }
- if (assignedTeam) {
- recruit.tid = assignedTeam.tid;
- await bbgm.idb.cache.players.put(recruit);
- teamAssignments[assignedTeam.tid]++;
- recruitSignings.push({ recruit, assignedTeam, unexpected });
- }
- }
- // **Second Pass: Assign Remaining Players**
- for (const recruit of rankedRecruits.slice(5)) {
- let assignedTeam;
- const availablePrestigeTeams = teamPrestiges
- .map(entry => teams.find(t => t.tid === entry.tid))
- .filter(t => teamAssignments[t.tid] < 4);
- if (availablePrestigeTeams.length === 0) continue;
- assignedTeam = availablePrestigeTeams[Math.floor(Math.random() * availablePrestigeTeams.length)];
- recruit.tid = assignedTeam.tid;
- await bbgm.idb.cache.players.put(recruit);
- teamAssignments[assignedTeam.tid]++;
- recruitSignings.push({ recruit, assignedTeam, unexpected: false });
- }
- // **Final Report for Top 5 Recruits**
- console.log("\nπ **Top 5 Recruit Considerations** π");
- for (const { recruit, assignedTeam, unexpected } of recruitSignings.slice(0, 5)) {
- console.log(`π ${recruit.firstName} ${recruit.lastName} (OVR: ${recruit.ratings.at(-1).ovr}) was considering:`);
- const topConsideredTeams = teamPrestiges.slice(0, 5);
- for (const teamData of topConsideredTeams) {
- const team = teams.find(t => t.tid === teamData.tid);
- console.log(` - ${team.region} ${team.name} (Prestige: ${teamData.prestige.toFixed(1)})`);
- }
- console.log(`π― **Final Decision:** ${assignedTeam.region} ${assignedTeam.name} ${unexpected ? "πUNEXPECTEDπ" : ""}\n`);
- }
- // **Log ALL Recruit Signings**
- console.log("\nπ **All Recruit Signings** π");
- for (const { recruit, assignedTeam, unexpected } of recruitSignings) {
- console.log(`β ${recruit.firstName} ${recruit.lastName} (OVR: ${recruit.ratings.at(-1).ovr}) β ${assignedTeam.region} ${assignedTeam.name} ${unexpected ? "πUNEXPECTEDπ" : ""}`);
- }
- // **Final Report for ALL Teams**
- console.log("\nπ **Final Team Recruitment Report** π");
- for (const team of teams) {
- console.log(`${team.region} ${team.name}: ${teamAssignments[team.tid]} recruits signed.`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement