James McCool commited on
Commit
a7f801a
·
1 Parent(s): 9e5b61e

Enhance player eligibility checks and lineup processing in load_contest_file function

Browse files

- Added debug prints to track position mapping and lineup processing, improving traceability during execution.
- Refined the logic for assigning players to required positions, ensuring accurate filling of both required and outfield positions.
- Implemented fallback handling for unassigned positions, enhancing data integrity and user experience.
- These changes contribute to ongoing efforts to improve player data handling within the application.

Files changed (1) hide show
  1. global_func/load_contest_file.py +51 -41
global_func/load_contest_file.py CHANGED
@@ -61,49 +61,59 @@ def load_contest_file(upload, sport):
61
  # Create position mapping dictionary
62
  pos_dict = dict(zip(pos_df['Player'], pos_df['Pos']))
63
 
64
- # Create empty columns for each position
65
- if sport == 'MLB':
66
- position_columns = ['SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS', 'OF1', 'OF2', 'OF3']
67
- for col in position_columns:
68
- df[col] = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- # Function to check if player is eligible for position
71
- def is_eligible_for_position(player, target_pos):
72
- if player not in pos_dict:
73
- return False
74
- player_positions = pos_dict[player].split('/')
75
- # Handle special cases
76
- if target_pos.startswith('SP') and 'P' in player_positions:
77
- return True
78
- if target_pos.startswith('OF') and 'OF' in player_positions:
79
- return True
80
- return target_pos in player_positions
81
 
82
- # Process each lineup
83
- for idx, row in df.iterrows():
84
- # Get all players in the lineup
85
- players = row['Lineup'].split(',')
86
- players = [p.strip() for p in players if p.strip()]
87
-
88
- # First pass: fill required positions (SP1, SP2, C, 1B, 2B, 3B, SS)
89
- required_positions = ['SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS']
90
- for pos in required_positions:
91
- for player in players:
92
- if is_eligible_for_position(player, pos):
93
- df.at[idx, pos] = player
94
- players.remove(player)
95
- break
96
-
97
- # Second pass: fill OF positions with remaining players
98
- of_positions = ['OF1', 'OF2', 'OF3']
99
- for pos in of_positions:
100
- for player in players:
101
- if is_eligible_for_position(player, 'OF'):
102
- df.at[idx, pos] = player
103
- players.remove(player)
104
- break
105
- if not players: # If no more players, fill with -1
106
- df.at[idx, pos] = '-1'
107
 
108
  cleaned_df = df.drop(columns=['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Pos', 'Own', 'FPTS', 'Salary', 'Team'])
109
  cleaned_df = cleaned_df[['BaseName', 'EntryCount', 'SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS', 'OF1', 'OF2', 'OF3']]
 
61
  # Create position mapping dictionary
62
  pos_dict = dict(zip(pos_df['Player'], pos_df['Pos']))
63
 
64
+ # Debug prints
65
+ print("\nPosition Dictionary:")
66
+ print(pos_dict)
67
+
68
+ print("\nSample Lineup String:")
69
+ print(df['Lineup'].iloc[0]) # Print first lineup
70
+
71
+ # Function to check if player is eligible for position
72
+ def is_eligible_for_position(player, target_pos):
73
+ if player not in pos_dict:
74
+ print(f"Player not found in pos_dict: {player}")
75
+ return False
76
+ player_positions = pos_dict[player].split('/')
77
+ print(f"Checking {player} for {target_pos}. Player positions: {player_positions}")
78
+ # Handle special cases
79
+ if target_pos.startswith('SP') and 'P' in player_positions:
80
+ return True
81
+ if target_pos.startswith('OF') and 'OF' in player_positions:
82
+ return True
83
+ return target_pos in player_positions
84
+
85
+ # Process each lineup
86
+ for idx, row in df.iterrows():
87
+ # Get all players in the lineup
88
+ players = row['Lineup'].split(',')
89
+ players = [p.strip() for p in players if p.strip()]
90
+ print(f"\nProcessing lineup {idx}:")
91
+ print(f"Players found: {players}")
92
 
93
+ # First pass: fill required positions
94
+ required_positions = ['SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS']
95
+ for pos in required_positions:
96
+ for player in players:
97
+ if is_eligible_for_position(player, pos):
98
+ print(f"Assigning {player} to {pos}")
99
+ df.at[idx, pos] = player
100
+ players.remove(player)
101
+ break
102
+ else:
103
+ print(f"No player found for {pos}")
104
 
105
+ # Second pass: fill OF positions
106
+ of_positions = ['OF1', 'OF2', 'OF3']
107
+ for pos in of_positions:
108
+ for player in players:
109
+ if is_eligible_for_position(player, 'OF'):
110
+ print(f"Assigning {player} to {pos}")
111
+ df.at[idx, pos] = player
112
+ players.remove(player)
113
+ break
114
+ else:
115
+ print(f"No player found for {pos}, using -1")
116
+ df.at[idx, pos] = '-1'
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  cleaned_df = df.drop(columns=['EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Pos', 'Own', 'FPTS', 'Salary', 'Team'])
119
  cleaned_df = cleaned_df[['BaseName', 'EntryCount', 'SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS', 'OF1', 'OF2', 'OF3']]