-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_gen.py
141 lines (110 loc) · 4.64 KB
/
database_gen.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
import json
# Converts a formatted value string to an integer, e.g. 35.5M gets converted to 35500000
def ConvertValueString(valueStr: str):
output = 0
if valueStr[-1] == "M":
output = int(float(valueStr[:len(valueStr) - 1]) * 1000000)
elif valueStr[-1] == "K":
output = int(float(valueStr[:len(valueStr) - 1]) * 1000)
else:
output = int(valueStr)
return output
##################################### Club JSON Database Generation #####################################
parsedClubsCSV = []
# Open the club csv database file and tokenise each loaded file line
csvClubDatabase = open("clubDatabase.csv", encoding="utf8")
for fileLine in csvClubDatabase:
fileLine = fileLine.replace("\n", "")
tokens = fileLine.split(",")
parsedClubsCSV.append(tokens)
csvClubDatabase.close()
# Create and store a dictionary object for each club using the parsed token data
clubsDatabase = {}
for index in range(len(parsedClubsCSV)):
tokens = parsedClubsCSV[index]
clubData = {
"name": tokens[0],
"leagueID": tokens[1],
"transferBudget": 0
}
clubsDatabase[str(index)] = clubData
# Sync the league ID for each club to the ones defined in the league JSON database
leagueJsonFile = open("leagues.json")
leagueDatabase = json.loads(leagueJsonFile.read())
leagueJsonFile.close()
for clubID in range(len(clubsDatabase)):
for leagueID in range(len(leagueDatabase)):
if leagueDatabase[str(leagueID)]["name"] == clubsDatabase[str(clubID)]["leagueID"]:
clubsDatabase[str(clubID)]["leagueID"] = leagueID
break
# Finally dump the player JSON database to file
clubsJsonOutput = json.dumps(clubsDatabase, indent=4)
clubsJsonFile = open("clubs.json", "w")
clubsJsonFile.write(clubsJsonOutput)
#################################### Player JSON Database Generation ####################################
parsedPlayersCSV = []
# Open the player csv database file and tokenise each loaded file line
csvPlayerDatabase = open("playerDatabase.csv", encoding="utf8")
for fileLine in csvPlayerDatabase:
if fileLine.find("Free") == -1:
fileLine = fileLine.replace("\n", "")
tokens = fileLine.split(",")
if len(tokens) > 12:
tokens.pop(6)
parsedPlayersCSV.append(tokens)
csvPlayerDatabase.close()
# Open the clubs and positions JSON database files and load them
clubsJsonFile = open("clubs.json")
positionsJsonFile = open("positions.json")
clubsDatabase = json.loads(clubsJsonFile.read())
positionsDatabase = json.loads(positionsJsonFile.read())
clubsJsonFile.close()
positionsJsonFile.close()
# Create and store a dictionary object for each player using the parsed token data
playerDatabase = {}
playerIDIndex = 0
for tokens in parsedPlayersCSV:
playerData = {
"name": tokens[0],
"nation": tokens[1],
"age": int(tokens[2]),
"overall": int(tokens[3]),
"potential": int(tokens[4]),
"clubID": tokens[5],
"expiryYear": tokens[6],
"preferredFoot": tokens[7],
"positionID": tokens[8],
"value": tokens[9],
"wage": tokens[10],
"releaseClause": tokens[11]
}
# Find the ID of the player's club
clubFound = False
for clubID in range(len(clubsDatabase)):
if clubsDatabase[str(clubID)]["name"] == playerData["clubID"]:
playerData["clubID"] = clubID
clubFound = True
break
if clubFound == False: # Club wasn't found so skip the player
continue
# Find the ID of the player's position
for positionID in range(len(positionsDatabase)):
if positionsDatabase[str(positionID)]["position"] == playerData["positionID"]:
playerData["positionID"] = positionID
break
# Convert the player's expiry year string into an integer format
if playerData["expiryYear"].find("On loan") > -1:
playerData["expiryYear"] = 2024
else:
playerData["expiryYear"] = int(playerData["expiryYear"][7:])
# Convert transfer, wage and release clause string values to integer format
playerData["value"] = ConvertValueString(playerData["value"])
playerData["wage"] = ConvertValueString(playerData["wage"])
playerData["releaseClause"] = ConvertValueString(playerData["releaseClause"])
playerDatabase[str(playerIDIndex)] = playerData # Add player data to database
playerIDIndex += 1
# Finally dump the player JSON database to file
playersJsonOutput = json.dumps(playerDatabase, indent=4)
playersJsonFile = open("players.json", "w")
playersJsonFile.write(playersJsonOutput)
#########################################################################################################