-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerate.py
165 lines (125 loc) · 6.62 KB
/
Generate.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
import Functions.DataStructures as DataStructures
import Functions.GenerationFunctions as GenerationFunctions
import random
import datetime
import csv
import os
import argparse
GENERATION_DATA_DIR = "GeneratorData"
CITIES_FILE_NAME = "cities.txt"
MALE_NAMES_FILE_NAME = "firstMaleNames.txt"
LAST_NAMES_FILE_NAME = "lastNames.txt"
PLAYER_POSITIONS_FILE_NAME = "playerPositions.txt"
TEAM_NAMES_FILE_NAME = "teamNames.txt"
GENERATED_DATA_DIR = "output"
CITIES_OUTPUT = "cities.txt"
PEOPLES_OUTPUT = "peoples.txt"
PLAYERS_POSITIONS_OUTPUT = "playerPositions.txt"
PLAYERS_OUTPUT = "players.txt"
TEAMS_OUTPUT = "teams.txt"
TEAM_PLAYERS_OUTPUT = "teamPlayers.txt"
MATCHES_OUTPUT = "matches.txt"
MATCH_RESULTS_OUTPUT = "matchResults.txt"
NUMBER_OF_TEAMS_TO_GENERATE = 10
REFEREES_NUMBER = 5
CoachPositionId = 1
RefereePositionId = 2
citiesNames, maleNames, surnames, playerPositionsNames, teamNames = ([] for i in range(5))
cities, peoples, playerPositions, teams, coaches, teamPlayers, referees, matches, matchResults = ([] for i in range(9))
def loadDataFromFile(filePath):
with open(filePath, encoding="utf8") as file:
lines = file.read().splitlines()
return lines
def writeGeneratedDataToFile(filePath, data):
with open(filePath, "w", newline='', encoding="utf8") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for line in data:
writer.writerow(line)
def getmatchingReferees():
referee = random.choice(referees)
return referee
def initGenerationData():
global citiesNames, maleNames, surnames, playerPositionsNames, teamNames
citiesNames = loadDataFromFile(GENERATION_DATA_DIR + '/' + CITIES_FILE_NAME)
maleNames = loadDataFromFile(GENERATION_DATA_DIR + '/' + MALE_NAMES_FILE_NAME)
surnames = loadDataFromFile(GENERATION_DATA_DIR + '/' + LAST_NAMES_FILE_NAME)
playerPositionsNames = loadDataFromFile(GENERATION_DATA_DIR + '/' + PLAYER_POSITIONS_FILE_NAME)
teamNames = loadDataFromFile(GENERATION_DATA_DIR + '/' + TEAM_NAMES_FILE_NAME)
def createTeams():
global cities, peoples, playerPositions, playerPositionsNames, teams, coaches, teamPlayers, referees, matches, matchResults
alreadyUsedTeamNames = []
for teamCounter in range(NUMBER_OF_TEAMS_TO_GENERATE):
teamId = teamCounter + 1
teams.append(GenerationFunctions.generateTeam(teamId, teamNames, alreadyUsedTeamNames, cities))
alreadyUsedTeamNames.append(teams[-1].name)
numberOfPlayersInTeam = len(playerPositionsNames) + random.randint(0, 4)
peoplesCount = len(peoples)
for playerId in range(numberOfPlayersInTeam):
personId = peoplesCount + playerId + 1
positionId = 0
if playerId >= len(playerPositionsNames) :
positionId = random.randint(1, len(playerPositionsNames)) + 2
else:
positionId = playerId + 3
peoples.append(GenerationFunctions.generatePerson(personId, positionId, maleNames, surnames, playerPositionsNames, False))
teamPlayers.append(GenerationFunctions.generateTeamPlayer(personId, teamId))
coachId = len(peoples) + 1
peoples.append(GenerationFunctions.generatePerson(coachId, CoachPositionId, maleNames, surnames, playerPositionsNames, True))
coaches.append(GenerationFunctions.generateCoach(peoples[-1], teamId))
teamPlayers.append(GenerationFunctions.generateTeamPlayer(coachId, teamId))
def createReferees():
global cities, peoples, playerPositions, teams, coaches, teamPlayers, referees, matches, matchResults
peoplesCount = len(peoples)
for refereeCounter in range(REFEREES_NUMBER):
personId = peoplesCount + 1 + refereeCounter
peoples.append(GenerationFunctions.generatePerson(personId, RefereePositionId, maleNames, surnames, playerPositionsNames, True))
referees.append(GenerationFunctions.generateReferee(peoples[-1]))
def createTournaments():
global cities, peoples, playerPositions, teams, coaches, teamPlayers, referees, matches, matchResults
startDate = GenerationFunctions.randomDate(datetime.date.today() - datetime.timedelta(days=50), datetime.date.today() - datetime.timedelta(days=30))
endDate = startDate + datetime.timedelta(days=85)
for matchCounter in range(len(teams)):
for opponentCounter in range(len(teams)):
if matchCounter == opponentCounter:
continue
matchId = len(matches) + 1
refs = getmatchingReferees()
chosenDate = GenerationFunctions.randomDate(startDate, endDate)
matches.append(GenerationFunctions.generateMatch(matchId, teams[matchCounter], teams[opponentCounter],
refs.id, chosenDate))
if chosenDate > datetime.date.today():
continue
matchResults.append(GenerationFunctions.generateMatchResult(matches[-1]))
def generateData():
global cities, peoples, playerPositions, teams, coaches, teamPlayers, referees, matches, matchResults
cities = GenerationFunctions.generateCities(citiesNames)
playerPositions = GenerationFunctions.generatePlayerPositions(playerPositionsNames)
createTeams()
createReferees()
createTournaments()
def writeData():
global cities, peoples, playerPositions, teams, coaches, teamPlayers, referees, matches, matchResults
if not os.path.exists(GENERATED_DATA_DIR):
os.makedirs(GENERATED_DATA_DIR)
writeGeneratedDataToFile(GENERATED_DATA_DIR + '/' + CITIES_OUTPUT, cities)
writeGeneratedDataToFile(GENERATED_DATA_DIR + '/' + PEOPLES_OUTPUT, peoples)
writeGeneratedDataToFile(GENERATED_DATA_DIR + '/' + PLAYERS_POSITIONS_OUTPUT, playerPositions)
writeGeneratedDataToFile(GENERATED_DATA_DIR + '/' + TEAMS_OUTPUT, teams)
writeGeneratedDataToFile(GENERATED_DATA_DIR + '/' + TEAM_PLAYERS_OUTPUT, teamPlayers)
writeGeneratedDataToFile(GENERATED_DATA_DIR + '/' + MATCHES_OUTPUT, matches)
writeGeneratedDataToFile(GENERATED_DATA_DIR + '/' + MATCH_RESULTS_OUTPUT, matchResults)
def main():
global NUMBER_OF_TEAMS_TO_GENERATE, REFEREES_NUMBER
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--teams", help="number of team to generate", type=int)
parser.add_argument("-r", "--referees", help="number of referees to generate", type=int)
args = parser.parse_args()
if args.teams and args.teams > 1:
NUMBER_OF_TEAMS_TO_GENERATE = args.teams
if args.referees and args.referees > 0:
REFEREES_NUMBER = args.tournaments
initGenerationData()
generateData()
writeData()
if __name__ == "__main__":
main()