forked from ddm999/gt7info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
241 lines (207 loc) · 8.72 KB
/
db.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
##################################################
# load databases
##################################################
db_cars = [] # type: list[str]
with open(f"_data/db/cars.csv") as f:
db_cars = f.readlines()
db_cars = db_cars[1:] # remove headers
db_cargrp = [] # type: list[str]
with open(f"_data/db/cargrp.csv") as f:
db_cargrp = f.readlines()
db_cargrp = db_cargrp[1:] # remove headers
db_country = [] # type: list[str]
with open(f"_data/db/country.csv") as f:
db_country = f.readlines()
db_country = db_country[1:] # remove headers
db_course = [] # type: list[str]
with open(f"_data/db/course.csv") as f:
db_course = f.readlines()
db_course = db_course[1:] # remove headers
db_crsbase = [] # type: list[str]
with open(f"_data/db/crsbase.csv") as f:
db_crsbase = f.readlines()
db_crsbase = db_crsbase[1:] # remove headers
db_maker = [] # type: list[str]
with open(f"_data/db/maker.csv") as f:
db_maker = f.readlines()
db_maker = db_maker[1:] # remove headers
db_stockperf = [] # type: list[str]
with open(f"_data/db/stockperf.csv") as f:
db_stockperf = f.readlines()
db_stockperf = db_stockperf[1:] # remove headers
##################################################
# database queries
##################################################
def cardb_id_to_name(input_id : int):
input_id = int(input_id)
for line in db_cars:
id, name, maker = line.strip().split(",")
if int(id) == input_id:
return name
raise ValueError(f"carid {input_id} not found!")
def cardb_id_to_maker(input_id : int):
input_id = int(input_id)
for line in db_cars:
id, name, maker = line.strip().split(",")
if int(id) == input_id:
return int(maker)
raise ValueError(f"carid {input_id} not found!")
def cargrpdb_id_to_group(input_id : int):
input_id = int(input_id)
for line in db_cargrp:
id, group = line.strip().split(",")
if int(id) == input_id:
return group
raise ValueError(f"carid {input_id} not found!")
def countrydb_id_to_name(input_id : int):
input_id = int(input_id)
for line in db_country:
id, name, code = line.strip().split(",")
if int(id) == input_id:
return name
raise ValueError(f"countryid {input_id} not found!")
def countrydb_id_to_code(input_id : int):
input_id = int(input_id)
for line in db_country:
id, name, code = line.strip().split(",")
if int(id) == input_id:
return code
raise ValueError(f"countryid {input_id} not found!")
def coursedb_id_to_name(input_id : int):
input_id = int(input_id)
for line in db_course:
id, name, base, country, category, length, straight, elevation, altitude, minH, minM, minS, maxH, maxM, maxS, layout, rev, pitlane, oval, corner, dryOnly = line.strip().split(",")
if int(id) == input_id:
return name
raise ValueError(f"courseid {input_id} not found!")
def coursedb_id_to_base(input_id : int):
input_id = int(input_id)
for line in db_course:
id, name, base, country, category, length, straight, elevation, altitude, minH, minM, minS, maxH, maxM, maxS, layout, rev, pitlane, oval, corner, dryOnly = line.strip().split(",")
if int(id) == input_id:
return int(base)
raise ValueError(f"courseid {input_id} not found!")
def coursedb_id_to_country(input_id : int):
input_id = int(input_id)
for line in db_course:
id, name, base, country, category, length, straight, elevation, altitude, minH, minM, minS, maxH, maxM, maxS, layout, rev, pitlane, oval, corner, dryOnly = line.strip().split(",")
if int(id) == input_id:
return int(country)
raise ValueError(f"courseid {input_id} not found!")
def coursedb_all_data():
data = {}
for line in db_course:
id, name, base, country, category, length, straight, elevation, altitude, minH, minM, minS, maxH, maxM, maxS, layout, rev, pitlane, oval, corner, dryOnly = line.strip().split(",")
data[id] = {'ID':id, 'Name':name, 'Base':base, 'Country':country, 'Category':category, 'Length':length,
'LongestStraight':straight, 'ElevationDiff':elevation, 'Altitude':altitude,
'MinTimeH':minH, 'MinTimeM':minM, 'MinTimeS':minS, 'MaxTimeH':maxH, 'MaxTimeM':maxM, 'MaxTimeS':maxS,
'LayoutNumber':layout, 'IsReverse':rev, 'PitLaneDelta':pitlane, 'IsOval':oval, 'NumCorners':corner, 'NoRain':dryOnly}
return data
def crsbasedb_id_to_name(input_id : int):
input_id = int(input_id)
for line in db_crsbase:
id, name, logo = line.strip().split(",")
if int(id) == input_id:
return name
raise ValueError(f"crsbaseid {input_id} not found!")
def crsbasedb_id_to_logo(input_id : int):
input_id = int(input_id)
for line in db_crsbase:
id, name, logo = line.strip().split(",")
if int(id) == input_id:
return logo
raise ValueError(f"crsbaseid {input_id} not found!")
def crsbase_all_data():
data = {}
first_crs = {}
crsdata = coursedb_all_data()
for crs in crsdata:
if crs['Base'] not in first_crs.keys():
first_crs[crs['Base']] = crs
for line in db_crsbase:
id, name, logo = line.strip().split(",")
data[id] = {'Name':name, 'Logo':logo, 'FirstLayoutID':first_crs[id]['ID']}
return data
def makerdb_id_to_name(input_id : int):
input_id = int(input_id)
for line in db_maker:
id, name, country = line.strip().split(",")
if int(id) == input_id:
return name
raise ValueError(f"makerid {input_id} not found!")
def makerdb_id_to_country(input_id : int):
input_id = int(input_id)
for line in db_maker:
id, name, country = line.strip().split(",")
if int(id) == input_id:
return int(country)
raise ValueError(f"makerid {input_id} not found!")
def stockperfdb_id_tyre_to_pp(input_id : int, input_tyre : str):
input_id = int(input_id)
for line in db_stockperf:
id, pp, tyre = line.strip().split(",")
if int(id) == input_id and tyre == input_tyre:
return pp
return "0" # no entry is provided when the car has "⚠️ PP"
#raise ValueError(f"carid {input_id} / tyre {input_tyre} not found!")
##################################################
# combined db queries
##################################################
def cardb_id_to_makername(input_id : int):
maker = cardb_id_to_maker(input_id)
return makerdb_id_to_name(maker)
def cardb_id_to_countrycode(input_id : int):
maker = cardb_id_to_maker(input_id)
return makerdb_id_to_countrycode(maker)
def coursedb_id_to_basename(input_id : int):
crsbase = coursedb_id_to_base(input_id)
return crsbasedb_id_to_name(crsbase)
def coursedb_id_to_logoname(input_id : int):
crsbase = coursedb_id_to_base(input_id)
return crsbasedb_id_to_logo(crsbase)
def coursedb_id_to_countrycode(input_id : int):
country = coursedb_id_to_country(input_id)
return countrydb_id_to_code(country)
def makerdb_id_to_countrycode(input_id : int):
country = makerdb_id_to_country(input_id)
return countrydb_id_to_code(country)
def stockperfdb_id_to_pp_dict(input_id : int):
tyres = ["CH","CM","CS","SH","SM","SS","RH","RM","RS","IM","W","D"]
ppdict = {}
for tyre in tyres:
pp = stockperfdb_id_tyre_to_pp(input_id, tyre)
ppdict[tyre] = pp
return ppdict
##################################################
# db reverse query (item -> id)
##################################################
def makerdb_name_to_id(input_name : str):
input_name_upper = input_name.upper()
for line in db_maker:
id, name, country = line.strip().split(",")
if name.upper() == input_name_upper:
return id
raise ValueError(f"maker name '{input_name}' not found!")
def cardb_name_to_id(input_name : str):
input_name_upper = input_name.upper()
for line in db_cars:
id, name, maker = line.strip().split(",")
if name.upper() == input_name_upper:
return id
raise ValueError(f"car name '{input_name}' not found!")
def coursedb_name_to_id(input_name : str):
input_name_upper = input_name.upper()
for line in db_course:
id, name, base, country, category, length, straight, elevation, altitude, minH, minM, minS, maxH, maxM, maxS, layout, rev, pitlane, oval, corner, dryOnly = line.strip().split(",")
if name.upper() == input_name_upper:
return id
raise ValueError(f"course name '{input_name}' not found!")
def cargrpdb_list_ids_from_group(input_group : str):
idlist = []
for line in db_cargrp:
id, group = line.strip().split(",")
if group == input_group:
idlist.append(int(id))
if len(idlist) == 0:
raise ValueError(f"group '{input_group}' not found!")
return idlist