-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFAContractFix.py
More file actions
215 lines (188 loc) · 12.8 KB
/
FAContractFix.py
File metadata and controls
215 lines (188 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import pandas as pd
import math
import random
# Your File Paths
free_agents_file_path = 'Files/Madden26/IE/Season1/Player_FreeAgents.xlsx'
player_file_path = 'Files/Madden26/IE/Season1/Player.xlsx'
expected_salary_file_path = 'Files/Madden26/IE/Season1/ExpectedContractLength.xlsx'
# Read data from the specified Excel files
free_agents_df = pd.read_excel(free_agents_file_path)
player_df = pd.read_excel(player_file_path)
expected_salary_df = pd.read_excel(expected_salary_file_path)
# Create a new column 'StatusCheck' indicating if ContractStatus is 'FreeAgent' in Player_FreeAgents and 'Signed' in Player
player_df['StatusCheck'] = (player_df['ContractStatus'].eq('Signed')) & (free_agents_df['ContractStatus'].eq('FreeAgent'))
# Perform the lookup for Expected Contract Length
def calculate_expected_contract_length(row):
position = row['Position']
overall_rating = row['OverallRating']
matching_row = expected_salary_df[(expected_salary_df['Position'] == position) &
(expected_salary_df['Rating Range Start'] <= overall_rating) &
(expected_salary_df['Rating Range End'] >= overall_rating)]
if not matching_row.empty:
return matching_row.iloc[0]['Expected Contract Length']
else:
return None
# Calculate ExpectedContractLength for all players
player_df['ExpectedContractLength'] = player_df.apply(calculate_expected_contract_length, axis=1)
# Create a new column 'AddedYears' by subtracting 'ContractLength' from 'ExpectedContractLength'
player_df['AddedYears'] = player_df['ExpectedContractLength'] - player_df['ContractLength']
# Compute YearlySalary and YearlyBonus for players with True StatusCheck and AddedYears >= -1, round up to nearest integer
def compute_yearly_salary(row):
if row['StatusCheck'] and row['AddedYears'] >= -1:
non_zero_salaries = [row[f'ContractSalary{i}'] for i in range(8) if row[f'ContractSalary{i}'] != 0]
non_zero_bonuses = [row[f'ContractBonus{i}'] for i in range(8) if row[f'ContractBonus{i}'] != 0]
if non_zero_salaries and non_zero_bonuses:
avg_salary = sum(non_zero_salaries) / len(non_zero_salaries)
avg_bonus = sum(non_zero_bonuses) / len(non_zero_bonuses)
return math.ceil(avg_salary), math.ceil(avg_bonus) # Round up to nearest integer
elif non_zero_salaries and not non_zero_bonuses:
avg_salary = sum(non_zero_salaries) / len(non_zero_salaries)
avg_bonus = 0
return math.ceil(avg_salary), None # Bonus is None since there are no non-zero bonuses
return None, None
# Apply compute_yearly_salary function to all players without filtering
player_df['YearlySalary'], player_df['YearlyBonus'] = zip(*player_df.apply(compute_yearly_salary, axis=1))
# Function to update the contract length for qualified players
def update_contractlength(row):
initial_contract_length = row['ContractLength'] # Get the initial value of ContractLength
if row['StatusCheck'] and row['AddedYears'] >= 2 and row['Position'] not in ['QB'] and 2 <= initial_contract_length <= 3:
new_contract_length = row['ContractLength']
# Apply randomness
random_number = random.random()
if random_number < 0.01: # 1% chance to subtract 1 from the contract length
new_contract_length -= 1
elif random_number >= 0.01 and random_number < 0.34: # 33% chance to add 1 to the contract length
new_contract_length += 1
elif random_number >= 0.34 and random_number < 0.50: # 16% chance to add 2 to the contract length
new_contract_length += 2
# Check if the new contract length is different from the original value and not NaN
if not pd.isna(new_contract_length) and new_contract_length != row['ContractLength']:
return new_contract_length, True # Return the updated length and True for ContractLengthChanged
elif row['StatusCheck'] and row['AddedYears'] >= 2 and initial_contract_length == 1:
new_contract_length = row['ContractLength']
# Apply randomness
random_number = random.random()
if random_number < 0.33: # 33% chance to add 1 to the contract length
new_contract_length += 1
elif random_number >= 0.33 and random_number < 0.50: # 17% chance to add 2 to the contract length
new_contract_length += 2
# Check if the new contract length is different from the original value and not NaN
if not pd.isna(new_contract_length) and new_contract_length != row['ContractLength']:
return new_contract_length, True # Return the updated length and True for ContractLengthChanged
elif row['StatusCheck'] and row['AddedYears'] == 1 and row['Position'] not in ['QB'] and 2 <= initial_contract_length <= 4:
new_contract_length = row['ContractLength']
# Apply randomness
random_number = random.random()
if random_number < 0.02: # 2% chance to subtract 1 from the contract length
new_contract_length -= 1
elif random_number >= 0.02 and random_number < 0.20: # 18% chance to add 1 to the contract length
new_contract_length += 1
# Check if the new contract length is different from the original value and not NaN
if not pd.isna(new_contract_length) and new_contract_length != row['ContractLength']:
return new_contract_length, True # Return the updated length and True for ContractLengthChanged
elif row['StatusCheck'] and row['AddedYears'] == 1 and initial_contract_length == 1:
new_contract_length = row['ContractLength']
# Apply randomness
random_number = random.random()
if random_number < 0.20: # 20% chance to add 1 to the contract length
new_contract_length += 1
# Check if the new contract length is different from the original value and not NaN
if not pd.isna(new_contract_length) and new_contract_length != row['ContractLength']:
return new_contract_length, True # Return the updated length and True for ContractLengthChanged
elif row['StatusCheck'] and row['AddedYears'] == 0 and row['Position'] not in ['QB'] and 2 <= initial_contract_length <= 4:
new_contract_length = row['ContractLength']
# Apply randomness
random_number = random.random()
if random_number < 0.07: # 7% chance to subtract 1 from the contract length
new_contract_length -= 1
elif random_number >= 0.07 and random_number < 0.15: # 8% chance to add 1 to the contract length
new_contract_length += 1
# Check if the new contract length is different from the original value and not NaN
if not pd.isna(new_contract_length) and new_contract_length != row['ContractLength']:
return new_contract_length, True # Return the updated length and True for ContractLengthChanged
elif row['StatusCheck'] and row['AddedYears'] == 0 and row['OverallRating'] >= 70 and row['ContractSalary0'] >= 150 and initial_contract_length == 1:
new_contract_length = row['ContractLength']
# Apply randomness
random_number = random.random()
if random_number < 0.05: # 5% chance to add 1 to the contract length
new_contract_length += 1
# Check if the new contract length is different from the original value and not NaN
if not pd.isna(new_contract_length) and new_contract_length != row['ContractLength']:
return new_contract_length, True # Return the updated length and True for ContractLengthChanged
return row['ContractLength'], False # Keep the existing contract length and mark ContractLengthChanged as False
# Function to edit ContractSalary based on new ContractLength value
def edit_contract_salary(row):
if row['ContractLengthChanged']:
original_salaries = [row[f'ContractSalary{i}'] for i in range(8)]
if not pd.isna(row['YearlySalary']): # Check if 'YearlySalary' is not NaN
new_contract_length = int(row['ContractLength'])
for i in range(new_contract_length, 8):
row[f'ContractSalary{i}'] = 0 # Zero out ContractSalary columns beyond new_contract_length
if row['ContractLength'] == 1:
row['ContractSalary0'] = int(row['YearlySalary'])
elif row['ContractLength'] == 2:
row['ContractSalary0'] = int(0.95 * row['YearlySalary'])
row['ContractSalary1'] = int(1.05 * row['YearlySalary'])
elif row['ContractLength'] == 3:
row['ContractSalary0'] = int(0.90 * row['YearlySalary'])
row['ContractSalary1'] = row['YearlySalary']
row['ContractSalary2'] = int(1.10 * row['YearlySalary'])
elif row['ContractLength'] == 4:
row['ContractSalary0'] = int(0.85 * row['YearlySalary'])
row['ContractSalary1'] = int(0.95 * row['YearlySalary'])
row['ContractSalary2'] = int(1.05 * row['YearlySalary'])
row['ContractSalary3'] = int(1.15 * row['YearlySalary'])
elif row['ContractLength'] == 5:
row['ContractSalary0'] = int(0.80 * row['YearlySalary'])
row['ContractSalary1'] = int(0.90 * row['YearlySalary'])
row['ContractSalary2'] = row['YearlySalary']
row['ContractSalary3'] = int(1.10 * row['YearlySalary'])
row['ContractSalary4'] = int(1.20 * row['YearlySalary'])
elif row['ContractLength'] == 6:
row['ContractSalary0'] = int(0.75 * row['YearlySalary'])
row['ContractSalary1'] = int(0.90 * row['YearlySalary'])
row['ContractSalary2'] = row['YearlySalary']
row['ContractSalary3'] = row['YearlySalary']
row['ContractSalary4'] = int(1.10 * row['YearlySalary'])
row['ContractSalary5'] = int(1.25 * row['YearlySalary'])
# Check if any ContractSalary(i) values were changed
new_salaries = [row[f'ContractSalary{i}'] for i in range(8)]
if original_salaries != new_salaries:
row['DidSalaryChange'] = True
else:
row['DidSalaryChange'] = False
return row
# Function to edit ContractBonus based on new ContractLength value
def edit_contract_bonus(row):
if row['ContractLengthChanged']:
if not pd.isna(row['YearlyBonus']): # Check if 'YearlyBonus' is not NaN
new_contract_length = int(row['ContractLength'])
for i in range(new_contract_length, 8):
row[f'ContractBonus{i}'] = 0 # Zero out ContractBonus columns beyond new_contract_length
if new_contract_length == 1:
row['ContractBonus0'] = row['YearlyBonus']
elif new_contract_length == 2:
row['ContractBonus1'] = row['YearlyBonus']
elif new_contract_length == 3:
row['ContractBonus2'] = row['YearlyBonus']
row['ContractBonus1'] = row['YearlyBonus']
elif new_contract_length == 4:
row['ContractBonus3'] = row['YearlyBonus']
row['ContractBonus2'] = row['YearlyBonus']
elif 5 <= new_contract_length <= 7:
row['ContractBonus4'] = row['YearlyBonus']
row['ContractBonus3'] = row['YearlyBonus']
return row
# Apply the update_contractlength function to modify ContractLength column for qualified players and add ContractLengthChanged column
player_df['ContractLength'], player_df['ContractLengthChanged'] = zip(*player_df.apply(update_contractlength, axis=1))
# Apply edit_contract_salary function to players with ContractLengthChanged as True
player_df['DidSalaryChange'] = False # Initialize the column with False
player_df.loc[player_df['ContractLengthChanged'], :] = player_df[player_df['ContractLengthChanged']].apply(edit_contract_salary, axis=1)
# Apply edit_contract_bonus function to players with ContractLengthChanged as True
player_df.loc[player_df['ContractLengthChanged'], :] = player_df[player_df['ContractLengthChanged']].apply(edit_contract_bonus, axis=1)
# Select only the columns you want to keep in the exported sheet
columns_to_export = ['Position', 'FirstName', 'LastName', 'ContractStatus', 'DidSalaryChange', 'ContractLengthChanged', 'StatusCheck', 'ContractSalary0', 'ContractSalary1', 'ContractSalary2', 'ContractSalary3', 'ContractSalary4', 'ContractSalary5', 'ContractSalary6', 'ContractSalary7',
'ContractBonus0', 'ContractBonus1', 'ContractBonus2', 'ContractBonus3', 'ContractBonus4', 'ContractBonus5', 'ContractBonus6', 'ContractBonus7', 'ContractLength']
# Export the modified data to a new Excel file named "Player_FAContractFix.xlsx"
output_filename = 'Files/Madden26/IE/Season1/Player_FAContractFix.xlsx'
player_df[columns_to_export].to_excel(output_filename, index=False)