-
Notifications
You must be signed in to change notification settings - Fork 0
/
record-stats.php
321 lines (270 loc) · 11.6 KB
/
record-stats.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Record Stats</title>
<link rel="stylesheet" href="grid.css" />
</head>
<body>
<?php include('header.php');
include('db-helpers.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['action'])) {
if ($_POST['action'] == 'Record' && isset($_POST['game_id'])) {
$game_id = $_POST['game_id'];
$match = getMatch($game_id);
$team1 = getTeam($match['team1_id']);
$players1 = getTeamMembers($team1);
$team2 = getTeam($match['team2_id']);
$players2 = getTeamMembers($team2);
} else if ($_POST['action'] == 'Finalize'
&& isset($_POST['game_id'])) {
$team1 = getTeam($_POST['team1_id']);
$players1 = getTeamMembers($team1);
$team2 = getTeam($_POST['team2_id']);
$players2 = getTeamMembers($team2);
if ($_POST['winner'] == $_POST['team1_id']) {
$loser = $_POST['team2_id'];
} else {
$loser = $_POST['team1_id'];
}
// Build arrays for the finalized stats
$hits1 = [$_POST['hits1'], $_POST['hits2'], $_POST['hits3']];
$hits2 = [$_POST['hits4'], $_POST['hits5'], $_POST['hits6']];
$miss1 = [$_POST['miss1'], $_POST['miss2'], $_POST['miss3']];
$miss2 = [$_POST['miss4'], $_POST['miss5'], $_POST['miss6']];
$cc1 = [$_POST['cc1'], $_POST['cc2'], $_POST['cc3']];
$cc2 = [$_POST['cc4'], $_POST['cc5'], $_POST['cc6']];
// Record the stats to the DB
finalizeStats($_POST['game_id'], $players1, $players2,
$_POST['winner'], $loser, $hits1, $hits2, $miss1, $miss2, $cc1, $cc2);
header('Location: match-stats.php?game_id=' . $_POST['game_id']);
}
}
?>
<div class="grid-container">
<div class="grid-header">
<h1>Record Stats for Match: <?php echo $match['game_name']?></h1>
</div>
<form action="<?php $_SERVER['PHP_SELF'] ?>" id="stats_form" method="post">
<div class="grid-row">
<table class="table table-striped table-bordered">
<tr>
<th></th>
<th></th>
<th><?php echo $team1['team_name'] ?></th>
<th><?php echo $team2['team_name'] ?></th>
<th></th>
<th></th>
</tr>
<tr>
<th><?php echo $players1[0]['player_name'] ?></th>
<th><?php echo $players1[1]['player_name'] ?></th>
<th><?php echo $players1[2]['player_name'] ?></th>
<th><?php echo $players2[0]['player_name'] ?></th>
<th><?php echo $players2[1]['player_name'] ?></th>
<th><?php echo $players2[2]['player_name'] ?></th>
</tr>
<tr>
<td>
<label for="hits1">Hits:</label>
<input type="number" id="hits1" name="hits1" min="0" value="0" >
</td>
<td>
<label for="hits2">Hits:</label>
<input type="number" id="hits2" name="hits2" min="0" value="0" >
</td>
<td>
<label for="hits3">Hits:</label>
<input type="number" id="hits3" name="hits3" min="0" value="0" >
</td>
<td>
<label for="hits4">Hits:</label>
<input type="number" id="hits4" name="hits4" min="0" value="0" >
</td>
<td>
<label for="hits5">Hits:</label>
<input type="number" id="hits5" name="hits5" min="0" value="0" >
</td>
<td>
<label for="hits6">Hits:</label>
<input type="number" id="hits6" name="hits6" min="0" value="0" >
</td>
</tr>
<tr>
<td>
<label for="miss1">Misses:</label>
<input type="number" id="miss1" name="miss1" min="0" value="0" >
</td>
<td>
<label for="miss2">Misses:</label>
<input type="number" id="miss2" name="miss2" min="0" value="0" >
</td>
<td>
<label for="miss3">Misses:</label>
<input type="number" id="miss3" name="miss3" min="0" value="0" >
</td>
<td>
<label for="miss4">Misses:</label>
<input type="number" id="miss4" name="miss4" min="0" value="0" >
</td>
<td>
<label for="miss5">Misses:</label>
<input type="number" id="miss5" name="miss5" min="0" value="0" >
</td>
<td>
<label for="miss6">Misses:</label>
<input type="number" id="miss6" name="miss6" min="0" value="0" >
</td>
</tr>
<tr>
<td>
<label for="cc1">Called Cups:</label>
<input type="number" id="cc1" name="cc1" min="0" value="0" >
</td>
<td>
<label for="cc2">Called Cups:</label>
<input type="number" id="cc2" name="cc2" min="0" value="0" >
</td>
<td>
<label for="cc3">Called Cups:</label>
<input type="number" id="cc3" name="cc3" min="0" value="0" >
</td>
<td>
<label for="cc4">Called Cups:</label>
<input type="number" id="cc4" name="cc4" min="0" value="0" >
</td>
<td>
<label for="cc5">Called Cups:</label>
<input type="number" id="cc5" name="cc5" min="0" value="0" >
</td>
<td>
<label for="cc6">Called Cups:</label>
<input type="number" id="cc6" name="cc6" min="0" value="0" >
</td>
</tr>
</table>
<select name="winner" form="stats_form" required>
<option value="" disabled selected>Winning Team</option>
<option value="<?php echo $team1['team_id'] ?>"><?php echo $team1['team_name']; ?></option>
<option value="<?php echo $team2['team_id'] ?>"><?php echo $team2['team_name']; ?></option>
</select>
</div>
<div class="grid-row">
<br/>
<input type="submit" class="btn-grid" name="action" value="Finalize" />
<input type="hidden" name="game_id" value="<?php echo $game_id ?>" />
<input type="hidden" name="team1_id" value="<?php echo $team1['team_id'] ?>" />
<input type="hidden" name="team2_id" value="<?php echo $team2['team_id'] ?>" />
<br/>
</div>
</form>
</div>
</body>
</html>
<?php
// Write the final match stats to the database
function finalizeStats($game_id, $players1, $players2, $winner_id, $loser_id,
$hits1, $hits2, $miss1, $miss2, $cc1, $cc2) {
// Record winner/loser
recordWinnerForGame($game_id, $winner_id);
// Adjust team's W/L stats
recordWin($winner_id);
recordLoss($loser_id);
// Record stats for each player
for ($i = 0; $i <= 2; $i++) {
// Record each player's stats for this game
createGameStat($game_id, $players1[$i]['player_id'], $hits1[$i], $miss1[$i], $cc1[$i]);
createGameStat($game_id, $players2[$i]['player_id'], $hits2[$i], $miss2[$i], $cc2[$i]);
// Adjust each player's global stats
updatePlayerStats($players1[$i]['player_id'], $hits1[$i], $miss1[$i], $cc1[$i]);
updatePlayerStats($players2[$i]['player_id'], $hits2[$i], $miss2[$i], $cc2[$i]);
}
}
// Update a player's hits, misses, and called cups stats
function updatePlayerStats($player_id, $hits, $misses, $called_cups) {
global $db;
$query = "UPDATE player
SET hits = hits + " . $hits
. ", misses = misses + " . $misses
. ", called_cups = called_cups + " . $called_cups
. " WHERE player_id = " . $player_id;
$sql = $db->prepare($query);
$sql->execute();
$sql->closeCursor();
}
// Adjust a team's stats to reflect a win
function recordWin($winner_id) {
global $db;
$query = "UPDATE team SET wins = wins + 1
WHERE team_id = " . $winner_id;
$sql = $db->prepare($query);
$sql->execute();
$sql->closeCursor();
}
// Adjust a team's stats to reflect a loss
function recordLoss($loser_id) {
global $db;
$query = "UPDATE team SET losses = losses + 1
WHERE team_id = " . $loser_id;
$sql = $db->prepare($query);
$sql->execute();
$sql->closeCursor();
}
// Set the 'winner' column for the game
function recordWinnerForGame($game_id, $winner_id) {
global $db;
$query = "UPDATE game SET winner = " . $winner_id
. " WHERE game_id = " . $game_id;
$sql = $db->prepare($query);
$sql->execute();
$sql->closeCursor();
}
// Create a record of an individual player's performance in the game
function createGameStat($game_id, $player_id, $hits, $misses, $called_cups) {
global $db;
$query = "INSERT INTO gamestat (game_id, player_id, hits, misses, called_cups)
VALUES (:game_id, :player_id, :hits, :misses, :called_cups)";
$sql = $db->prepare($query);
$sql->bindValue(':game_id', $game_id);
$sql->bindValue(':player_id', $player_id);
$sql->bindValue(':hits', $hits);
$sql->bindValue(':misses', $misses);
$sql->bindValue(':called_cups', $called_cups);
$sql->execute();
$sql->closeCursor();
}
// Get the match to enter stats for
function getMatch($game_id) {
global $db;
$query = "SELECT * FROM game
WHERE game_id = " . $game_id;
$sql = $db->prepare($query);
$sql->execute();
$result = $sql->fetch();
$sql->closeCursor();
return $result;
}
// Get a team given its ID
function getTeam($team_id) {
global $db;
$query = "SELECT * FROM team
WHERE team_id = " . $team_id;
$sql = $db->prepare($query);
$sql->execute();
$team = $sql->fetch();
$sql->closeCursor();
return $team;
}
// Return an array containing the players for a given team
function getTeamMembers($team) {
global $db;
$query = "SELECT * FROM player
WHERE player_id = " . $team["player1_id"]
. " OR player_id = " . $team["player2_id"]
. " OR player_id = " . $team["player3_id"];
$sql = $db->prepare($query);
$sql->execute();
$players = $sql->fetchAll();
$sql->closeCursor();
return $players;
}
?>