-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandLineTool.py
437 lines (395 loc) · 12.4 KB
/
commandLineTool.py
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import pandas as pd
import requests
import json
from fifa_app.type_map import projections
API_ENDPOINT = "http://127.0.0.1:5000/api/v1"
HEADERS = {'content-type': 'application/json'}
def main():
display = pd.options.display
display.max_columns = 999
display.max_rows = 1000
display.max_colwidth = 199
display.width = 1000
type_q = choose_query()
if type_q == 'Basic Search':
ret = basic_search()
print(ret)
main()
elif type_q == 'Advanced Search':
ret = advanced_search()
print(ret)
main()
elif type_q == 'Quit':
return 'Quit'
else:
ret = ultimate_team_handler()
def ultimate_team_handler():
queries = {1: 'Input Team', 2: 'Replace player', 3: 'Replacement recommender'}
prompting = True
q = prompt_builder(queries)
while prompting:
user_input = int(input(q))
if user_input in queries.keys():
prompting = False
else:
print('INPUT NOT RECOGNIZED')
if user_input == 1:
input_team()
main()
elif user_input == 2:
replace_player()
main()
elif user_input == 3:
recommendation()
main()
def input_team():
players = []
year = choose_year()
username = input("\nEnter a username: \n")
teamname = input("\nEnter a team name: \n")
while len(players) < 11:
player = input("\nInput a player that you would like to add to your team: First Last\n")
names = get_names(player)
rets = {'num_results': 0}
c = 0
while rets['num_results'] == 0 and c < len(names):
body = {'year': year, 'short_name': names[c]}
rets = requests.get(f"{API_ENDPOINT}/players/", params=body).json()
c += 1
if rets['num_results'] == 0:
print('Player not found')
else:
rets = list(rets["players"])[0]
players.append({rets['player_positions'][0]: rets['short_name']})
print("Team Successfully Inputted")
body = {
"user": username,
"team_name": teamname,
"year": year,
"players": players,
}
requests.post(f"{API_ENDPOINT}/team/", data=json.dumps(body), headers=HEADERS)
body = {"username":username,"team_name":teamname}
return print(pd.DataFrame(requests.get(f"{API_ENDPOINT}/team/", params=body).json()["players"]))
def replace_player():
team = None
while team == None:
username = input("\nInput your username\n")
teamname = input("\nInput your team name\n")
body = {"username":username,"team_name":teamname}
team = requests.get(f"{API_ENDPOINT}/team/", params=body).json()
if team == None:
print("Username Or Team Name not recognised\n")
year = choose_year()
player = input("\nInput the name of the player that you want to add to the team: First Last\n")
player_to_replace = input("\nInput the name of the player that you want to replace: First Last\n")
names = get_names(player)
rets = {'num_results': 0}
c = 0
while rets['num_results'] == 0 and c < len(names):
body = {'year': year, 'short_name': names[c]}
rets = requests.get(f"{API_ENDPOINT}/players/", params=body).json()
c += 1
if rets['num_results'] == 0:
print('Player not found')
else:
new_player = list(rets['players'])[0]["short_name"]
previous_team = pd.DataFrame(team["players"])
names_rep = get_names(player_to_replace)
for i in names_rep:
if i in list(previous_team['short_name']):
body = {
"user": username,
"team_name": teamname,
"year": year,
"original_player_name": i,
"replacing_player_name": new_player,
}
requests.put(f"{API_ENDPOINT}/team/edit/", data=json.dumps(body), headers=HEADERS)
print("Player successfully returned")
break
elif i == names_rep[-1]:
print('Player to replace not on team')
body = {"username": username, "team_name": teamname}
team = requests.get("http://127.0.0.1:5000/api/v1/team/", params=body).json()
df = pd.DataFrame(team["players"])
df = sort_dataframe(df)
print('\n')
print(df)
def recommendation():
year = choose_year()
position = None
stat = None
print("\nInput a position from the following list\n")
for x in POSITIONS:
print(x)
while position not in POSITIONS:
if position != None:
print("\nInvalid position, enter one from the list\n")
position = input().upper()
wage = int(input("\ninput a maximum salary\n"))
print("\nselect a stat to maxamize\n")
for x in STATSLIST:
print(x)
while stat not in STATSLIST:
if stat != None:
print("\nInvalid stat, enter one from the list\n")
stat = input().lower()
body = {
"year": year,
"wage": wage,
"position": position,
"stat_to_optimize": stat,
}
df = pd.DataFrame(requests.get(f"{API_ENDPOINT}/team/replace/", params=body).json()['players'])
df = sort_dataframe(df)
print(df)
def choose_query():
print("Fifa Search engine V1\n")
queries = {1: 'Basic Search', 2: 'Advanced Search', 3: 'Ultimate Team Recommender', 4: 'Quit'}
prompting = True
q = prompt_builder(queries)
while prompting:
user_input = int(input(q))
if user_input in queries.keys():
prompting = False
else:
print('INPUT NOT RECOGNIZED')
return queries[user_input]
def prompt_builder(queries):
prompt = 'Type: \n'
for num in queries.keys():
prompt = prompt + '%s for %s\n' % (num, queries[num])
return prompt
def basic_search():
year = choose_year()
projection = choose_projection()
constraints = choose_constraints()
constraints['year'] = year
constraints['projection'] = projection
players = requests.get(f"{API_ENDPOINT}/players/", params=constraints).json()["players"]
df = pd.DataFrame(players)
df = sort_dataframe(df)
print('\n')
return df
def sort_dataframe(df):
first_column = df.pop('short_name')
second_column = df.pop('overall')
df.insert(0, 'short_name', first_column)
df.insert(1, 'overall', second_column)
return df
def choose_projection():
projection = None
print('\nAvailable stat projections are: "basic", "defending", "attacking", "goalkeeping", "mentality", and "physical"')
while projection is None:
temp = input('Please enter a projection type (stat-group) to return. Options are above: \n')
if temp in list(projections.keys()):
break
else:
print('\nPlease input a projection type from the list above!')
return temp
def get_names(name):
names = name.split(" ")
try:
potential_names = [name, names[0][0]+". "+names[1], names[0], names[1]]
except:
potential_names = names
return potential_names
def advanced_search():
year = choose_year()
player = input("\nInput a player name: First Last\n")
names = get_names(player)
rets = {'num_results': 0}
c=0
while rets['num_results'] == 0 and c < len(names):
body = {'year':year, 'short_name': names[c]}
rets = requests.get(f"{API_ENDPOINT}/players/", params=body).json()
c+=1
if len(rets) == 0:
print('Player not found')
else:
projection = choose_projection()
constraints = choose_constraints(counter=1)
rets = pd.DataFrame(rets['players'])
rets = (rets[['club_position', 'overall', 'pace', 'shooting', 'passing', 'dribbling', 'defending']].iloc[0]).to_dict()
for key in constraints.keys():
rets[key] = constraints[key]
rets['year'] = year
rets['projection'] = projection
players = requests.get(f"{API_ENDPOINT}/players/", params=rets).json()["players"]
df = pd.DataFrame(players)
df = sort_dataframe(df)
print('\n')
return df
def choose_constraints(counter=0):
print("\nFor a list of fields to search on, type --help")
stop = False
constraints = {}
while not stop:
if len(constraints) != 0:
counter +=1
if counter == 0:
temp = input('Enter field and value separated by a comma. Value must be 1-99 for attributes.\n')
else:
temp = input('\nType \'search\' to search on inputted constraints or add another: Enter field and value separated by comma. Value must be 1-99 for attributes\n')
if temp.lower() == 'search':
stop = True
break
if temp == '--help':
for x in HELP:
print(x)
elif ',' in temp:
field, value = temp.split(',')
field, value = field.strip(), value.strip()
if field in NUMERICS:
if value.isnumeric():
value = int(value)
constraints[field] = value
else:
print("constraint for this field must be numeric")
elif field not in HELP:
print('Field not recognized')
else:
constraints[field] = value
else:
print("constraints need to be comma separated")
return constraints
def choose_year():
years = list(range(2015, 2023))
year = 0
while int(year) not in years:
year = input("\nInput a year to search 2015-2022\n")
return year
HELP = ['Basic Statistics',
'----------------',
'short_name',
'overall',
'potential',
'wage_eur',
'value_eur',
'club_name',
'league_name',
'club_position',
'player_positions (ls, st, rs, lw, lf, cf, rf, rw, lam, cam, ram, lm, lcm, cm, rcm, rm, lwb, ldm, cdm, rdm, rwb, lb, lcb, cb, rcb, rb)',
'nationality_name',
'pace',
'shooting',
'passing',
'dribbling',
'defending',
"\n",
'Attacking Statistics',
'--------------------',
'shooting',
'passing',
'attacking_crossing',
'attacking_finishing',
'attacking_heading_accuracy',
'attacking_short_passing',
'attacking_volleys',
"\n",
'Defense Statistics',
'------------------',
'defending_marking_awareness',
'defending_standing_tackle',
'defending_sliding_tackle',
"\n",
'Goalkeeping',
'-----------',
'goalkeeping_diving',
'goalkeeping_handling',
'goalkeeping_kicking',
'goalkeeping_positioning',
'goalkeeping_reflexes',
'goalkeeping_speed',
"\n",
'Physical Statistics',
'-------------------',
'physic',
'height_cm',
'weight_kg',
'age',
'power_shot_power',
'power_jumping',
'power_stamina',
'power_strength',
'power_long_shots',
"\n",
'Mentality Statistics',
'--------------------',
'mentality_aggression',
'mentality_interceptions',
'mentality_positioning',
'mentality_vision',
'mentality_penalties',
'mentality_composure',
]
NUMERICS = ['overall',
'potential',
'wage_eur',
'value_eur',
'pace',
'shooting',
'passing',
'dribbling',
'defending',
'shooting',
'passing',
'attacking_crossing',
'attacking_finishing',
'attacking_heading_accuracy',
'attacking_short_passing',
'attacking_volleys',
'defending_marking_awareness',
'defending_standing_tackle',
'defending_sliding_tackle',
'goalkeeping_diving',
'goalkeeping_handling',
'goalkeeping_kicking',
'goalkeeping_positioning',
'goalkeeping_reflexes',
'goalkeeping_speed',
'physic',
'height_cm',
'weight_kg',
'age',
'power_shot_power',
'power_jumping',
'power_stamina',
'power_strength',
'power_long_shots',
'mentality_aggression',
'mentality_interceptions',
'mentality_positioning',
'mentality_vision',
'mentality_penalties',
'mentality_composure',
]
STATSLIST= ['overall', 'pace', 'shooting', 'passing', 'attacking', 'dribbling', 'defending']
POSITIONS = ['LS',
'ST',
'RS',
'LW',
'LF',
'CF',
'RF',
'RW',
'LAM',
'CAM',
'RAM',
'LM',
'LCM',
'CM',
'RCM',
'RM',
'LWB',
'LDM',
'CDM',
'RDM',
'RWB',
'LB',
'LCB',
'CB',
'RCB',
'RB']
main()