This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresult_season.php
286 lines (261 loc) · 9.19 KB
/
result_season.php
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
<? if(!defined("CONFIG")) exit(); ?>
<?
require_once("results_functions.php");
define("SHOW_POINTS", 0);
define("SHOW_INCREMENTAL", 1);
define("SHOW_POSITIONS", 2);
$season = $_GET['season'];
$show = isset($_GET['show']) ? $_GET['show'] : 0;
require_once("functions.php"); // import mysql function
$link = mysqlconnect(); // call mysql function to get the link to the database
// Get season information
$query = "SELECT s.*, d.name dname, COUNT(r.id) racecount FROM season s JOIN division d ON (s.division = d.id) LEFT JOIN race r ON (r.season = s.id) WHERE s.id='$season' GROUP BY s.id";
$result = mysqli_query($link,$query);
if(!$result) {
show_error("MySQL Error: " . mysqli_error($link) . "\n");
return;
}
if(mysqli_num_rows($result) == 0) {
show_error("Season does not exist\n");
return;
}
$item = mysqli_fetch_array($result);
// Get the rulesets
$rsquery = "SELECT * FROM point_ruleset";
$rsresult = mysqli_query($link,$rsquery);
if(!$rsresult) {
show_error("MySQL Error: " . mysqli_error($link) . "\n");
return;
}
if(mysqli_num_rows($rsresult) == 0) {
show_error("Ruleset does not exist\n");
return;
}
while($rsitem = mysqli_fetch_array($rsresult)) {
$ruleset[$rsitem['id']] = $rsitem;
}
// Get all teams and driver for this season
$drquery = "SELECT d.id did, d.name dname, t.id tid, t.name tname FROM season_team st JOIN team t ON (st.team = t.id) JOIN team_driver td ON (td.team = t.id) JOIN driver d ON (d.id = td.driver) WHERE st.season = '$season' ORDER BY t.name ASC, d.name ASC";
$drresult = mysqli_query($link,$drquery);
if(!$drresult) {
show_error("MySQL Error: " . mysqli_error($link) . "\n");
return;
}
$team = array();
$driver = array();
while($dritem = mysqli_fetch_array($drresult)) {
if(!isset($team[$dritem['tid']])) {
$team[$dritem['tid']]['name'] = $dritem['tname'];
$team[$dritem['tid']]['points'] = 0;
$team[$dritem['tid']]['pointsrace'] = array();
$team[$dritem['tid']]['pointsraceinc'] = array();
}
$driver[$dritem['did']]['name'] = $dritem['dname'];
$driver[$dritem['did']]['team'] = $dritem['tname'];
$driver[$dritem['did']]['points'] = 0;
$driver[$dritem['did']]['pointsrace'] = array();
$driver[$dritem['did']]['pointsraceinc'] = array();
}
$rquery = <<<EOF
SELECT
r.id race, r.name rname, r.track rtrack, r.ruleset, r.ruleset_qualifying,
td.driver, td.team,
rd.fastest_lap, rd.grid, rd.status
FROM race r
JOIN race_driver rd ON (rd.race = r.id)
JOIN team_driver td ON (td.id = rd.team_driver)
WHERE r.season='$season' AND r.progress = 2 AND (rd.status = 0 OR rd.status = 1)
ORDER BY r.date ASC, rd.position ASC
EOF;
$rresult = mysqli_query($link,$rquery);
if(!$rresult) {
show_error("MySQL Error: " . mysqli_error($link) . "\n");
return;
}
$show_qualifypoint = ($ruleset_qualifying != 0);
$last_race = 0;
$race = 0;
$races = array();
/* Creates an array of all drivers and team, and their race information (points, positions) */
while($ritem = mysqli_fetch_array($rresult)) {
if($last_race != $ritem['race']) {
$position = 0;
$race++;
$races[$race]['id'] = $ritem['race'];
$races[$race]['name'] = $ritem['rname'];
$races[$race]['track'] = $ritem['rtrack'];
$last_race = $ritem['race'];
}
if($ritem['status'] == 0) { // if status = OK (IE not DNF)
// Assign points and position to driver for the race
$position++;
$driver[$ritem['driver']]['points'] += points_total($position, $ritem['grid'], $ritem['fastest_lap'], $ruleset[$ritem['ruleset']]);
$driver[$ritem['driver']]['pointsrace'][$race] = points_total($position, $ritem['grid'], $ritem['fastest_lap'], $ruleset[$ritem['ruleset']]);
$driver[$ritem['driver']]['pointsraceinc'][$race] += $driver[$ritem['driver']]['points'];
$driver[$ritem['driver']]['position'][$race] = $position;
// Assign points for the team
$team[$ritem['team']]['points'] += $driver[$ritem['driver']]['pointsrace'][$race];
$team[$ritem['team']]['pointsrace'][$race] += $driver[$ritem['driver']]['pointsrace'][$race];
$team[$ritem['team']]['pointsraceinc'][$race] = $team[$ritem['team']]['points'];
}
if($ritem['ruleset_qualifying'] != 0) {
// Qualifying points if set
$show_qualifypoint = true;
$driver[$ritem['driver']]['pointsqualifying'] += points_race($ritem['grid'], $ruleset[$ritem['ruleset_qualifying']]);
$driver[$ritem['driver']]['pointsqualifyingrace'][$race] = points_race($ritem['grid'], $ruleset[$ritem['ruleset_qualifying']]);
$driver[$ritem['driver']]['pointsqualifyingraceinc'][$race] = $driver[$ritem['driver']]['pointsqualifying'];
}
}
usort($driver, "point_sort");
usort($team, "point_sort");
?>
<h1>Season results</h1>
<div class="w3-container">
<div class="w3-responsive">
<table class="w3-table-all">
<tr class="w3-dark-grey">
<td width="20%">Name:</td>
<td width="30%"><?=$item['name']?></td>
<td width="20%">Races:</td>
<td width="30%"><?=$item['racecount']?></td>
</tr>
<tr class="w3-grey">
<td width="20%">Division:</td>
<td width="30%"><?=$item['dname']?></td>
<td width="20%">Ruleset:</td>
<td width="30%"><?=$ruleset['name']?><?if(isset($ruleset_qualifying)) echo " (qual: " . $ruleset_qualifying['name'] . ")"?></td>
</tr>
<tr class="w3-green">
<td colspan="4" align="center">
<a href=".?page=result_season&season=<?=$season?>&show=<?=SHOW_POINTS?>">points per race</a> |
<a href=".?page=result_season&season=<?=$season?>&show=<?=SHOW_INCREMENTAL?>">points incremental</a> |
<a href=".?page=result_season&season=<?=$season?>&show=<?=SHOW_POSITIONS?>">positions</a>
</td>
</tr>
</table>
</div>
</div>
<h2>Drivers</h2>
<div class="w3-container">
<div class="w3-responsive">
<table class="w3-table-all">
<tr class="w3-dark-grey">
<td> </td>
<td>Driver</td>
<td>Team</td>
<? for($x = 1; $x <= $race; $x++) { ?>
<td width="1" align="right"><javascript:void(0)" class="tablink" title="Click to more details"><div class="w3-topbar w3-bottombar w3-hover-border-red"><a href="?page=result_race&race=<?=$races[$x]['id']?>"><img src="img_season_race.php?text=<?=urlencode($races[$x]['name'])?>&text2=<?=urlencode($races[$x]['track'])?>" alt="<?=$x?>"></a></td>
<? } ?>
<td width="1" align="right">Pts</td>
</tr>
<?
$style = "odd";
$pos = 0;
foreach($driver as $id => $ditem) {
?>
<tr class="w3-hover-green">
<td width="1" align="right"><?=++$pos?> </td>
<td><?=$ditem['name']?></td>
<td><?=$ditem['team']?></td>
<?
$total = 0;
for($x = 1; $x <= $race; $x++) {
switch($show) {
case SHOW_POINTS:
$data = !empty($ditem['pointsrace'][$x]) ? $ditem['pointsrace'][$x] : "-";
break;
case SHOW_INCREMENTAL:
$total += $ditem['pointsrace'][$x];
$data = $total;
break;
case SHOW_POSITIONS:
$data = !empty($ditem['position'][$x]) ? $ditem['position'][$x] : "-";
break;
}
?>
<td width="1" align="right"><?=$data?></td>
<? } ?>
<td width="1" align="right"><strong><?=!empty($ditem['points']) ? $ditem['points'] : "0" ?></strong></td>
</tr>
<?
} ?>
</table>
</div>
</div>
<h2>Teams</h2>
<div class="w3-container">
<div class="w3-responsive">
<table class="w3-table-all">
<tr class="w3-dark-grey">
<td> </td>
<td>Team</td>
<? for($x = 1; $x <= $race; $x++) { ?>
<td width="1" align="right"><javascript:void(0)" class="tablink" title="Click to more details"><div class="w3-topbar w3-bottombar w3-hover-border-red"><a href="?page=result_race&race=<?=$races[$x]['id']?>"><img src="img_season_race.php?text=<?=urlencode($races[$x]['name'])?>&text2=<?=urlencode($races[$x]['track'])?>" alt="<?=$x?>"></a></td>
<? } ?>
<td width="1" align="right">Pts</td>
</tr>
<?
$pos = 0;
foreach($team as $id => $titem) {
?>
<tr class="w3-hover-green">
<td width="1" align="right"><?=++$pos?> </td>
<td><?=$titem['name']?></td>
<?
$total = 0;
for($x = 1; $x <= $race; $x++) {
switch($show) {
case SHOW_POINTS:
case SHOW_POSITIONS:
$data = !empty($titem['pointsrace'][$x]) ? $titem['pointsrace'][$x] : "-";
break;
case SHOW_INCREMENTAL:
$total += $titem['pointsrace'][$x];
$data = $total;
break;
}
?>
<td width="1" align="right"><?=$data?></td>
<? } ?>
<td width="1" align="right"><strong><?=!empty($titem['points']) ? $titem['points'] : "0" ?></strong></td>
</tr>
<?
} ?>
</table>
</div>
</div>
<? if($show_qualifypoint) {
usort($driver, 'point_sort_qual');
?>
<h2>Drivers qualifying</h2>
<div class="w3-container">
<div class="w3-responsive">
<table class="w3-table-all">
<tr class="w3-dark-grey">
<td> </td>
<td>Driver</td>
<td>Team</td>
<? for($x = 1; $x <= $race; $x++) { ?>
<td width="1" align="right"><javascript:void(0)" class="tablink" title="Click to more details"><div class="w3-topbar w3-bottombar w3-hover-border-red"><a href="?page=result_race&race=<?=$races[$x]['id']?>"><img src="img_season_race.php?text=<?=urlencode($races[$x]['name'])?>&text2=<?=urlencode($races[$x]['track'])?>" alt="<?=$x?>"></a></td>
<? } ?>
<td width="1" align="right">Pts</td>
</tr>
<?
$pos = 0;
foreach($driver as $id => $ditem) {
?>
<tr class="w3-hover-green">
<td width="1" align="right"><?=++$pos?> </td>
<td><?=$ditem['name']?></td>
<td><?=$ditem['team']?></td>
<? for($x = 1; $x <= $race; $x++) { ?>
<td width="1" align="right"><?=!empty($ditem['pointsqualifyingrace'][$x]) ? $ditem['pointsqualifyingrace'][$x] : "-"?></td>
<? } ?>
<td width="1" align="right"><strong><?=!empty($ditem['pointsqualifying']) ? $ditem['pointsqualifying'] : "0" ?></strong></td>
</tr>
<?
} ?>
</table>
</div>
</div>
<? } ?>