-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit_edit_score.php
288 lines (256 loc) · 15 KB
/
submit_edit_score.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
<?php
include 'config.php';
// Initialize variables for result and redirect URL
$resultMessage = "";
$redirectURL = "view_matches.php";
// Check if form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check if all necessary fields are set and not empty
if (
isset($_POST['match_no']) && isset($_POST['inning1_runs']) && isset($_POST['inning1_wickets']) &&
isset($_POST['inning1_overs']) && isset($_POST['inning1_balls']) &&
isset($_POST['inning2_runs']) && isset($_POST['inning2_wickets']) &&
isset($_POST['inning2_overs']) && isset($_POST['inning2_balls'])
) {
// Get form data
$match_no = intval($_POST['match_no']);
$inning1_runs = intval($_POST['inning1_runs']);
$inning1_wickets = intval($_POST['inning1_wickets']);
$inning1_overs = intval($_POST['inning1_overs']);
$inning1_balls = intval($_POST['inning1_balls']);
$inning2_runs = intval($_POST['inning2_runs']);
$inning2_wickets = intval($_POST['inning2_wickets']);
$inning2_overs = intval($_POST['inning2_overs']);
$inning2_balls = intval($_POST['inning2_balls']);
// Calculate overs and balls for NRR based on wickets lost
if ($inning1_runs == 0) {
$inning1_overs_nrr = 20;
$inning1_balls_nrr = 0;
} else {
if ($inning1_wickets == 10 || ($inning1_overs != 20 && $inning1_wickets != 10)) {
$inning1_overs_nrr = 20;
$inning1_balls_nrr = 0;
} else {
$inning1_overs_nrr = $inning1_overs;
$inning1_balls_nrr = $inning1_balls;
}
}
if ($inning2_runs == 0) {
$inning2_overs_nrr = 20;
$inning2_balls_nrr = 0;
} else {
// Handle the case where all runs are scored via no balls and wides
if ($inning2_overs == 0 && $inning2_balls == 0 && $inning2_runs > 0) {
// Assign a value representing minimum one ball faced to avoid 'division by zero error' while calculating NRR
echo "<script>alert('Team batting second won without facing any legal deliveries. Balls played are set to 1 for NRR calculation.');</script>";
$inning2_overs_nrr = 0;
$inning2_balls_nrr = 1;
} elseif ($inning2_wickets == 10 || ($inning2_runs < $inning1_runs && $inning2_wickets != 10)) {
$inning2_overs_nrr = 20;
$inning2_balls_nrr = 0;
} else {
$inning2_overs_nrr = $inning2_overs;
$inning2_balls_nrr = $inning2_balls;
}
}
// Calculate NRR for both teams
$overs_played_inning1 = $inning1_overs_nrr + ($inning1_balls_nrr / 6);
$overs_played_inning2 = $inning2_overs_nrr + ($inning2_balls_nrr / 6);
// Fetch match details to get the teams
$stmt = $conn->prepare("SELECT home_team, away_team, toss, decision, winning_team, losing_team, nrr FROM tournament_data WHERE match_no = ?");
$stmt->bind_param("i", $match_no);
$stmt->execute();
$result_set = $stmt->get_result();
if ($result_set->num_rows > 0) {
$row = $result_set->fetch_assoc();
$home_team_short = $row['home_team'];
$away_team_short = $row['away_team'];
$toss_short = $row['toss'];
$decision = $row['decision'];
$existing_nrr = $row['nrr'];
// Initialize existing result, winning team, and losing team from $row
$existing_result = isset($row['result']) ? $row['result'] : NULL;
$existing_winning_team = isset($row['winning_team']) ? $row['winning_team'] : NULL;
$existing_losing_team = isset($row['losing_team']) ? $row['losing_team'] : NULL;
// Determine the batting team for inning 1
if ($decision === 'bowl') {
$batting_team_short = ($toss_short === $home_team_short) ? $away_team_short : $home_team_short;
$bowling_team_short = $toss_short;
} else {
$batting_team_short = $toss_short;
$bowling_team_short = ($toss_short === $home_team_short) ? $away_team_short : $home_team_short;
}
// Fetch team names based on short names
$stmt = $conn->prepare("SELECT team_name FROM teams WHERE short_name = ?");
$stmt->bind_param("s", $batting_team_short);
$stmt->execute();
$batting_team_result = $stmt->get_result();
$batting_team = $batting_team_result->fetch_assoc()['team_name'];
$stmt->bind_param("s", $bowling_team_short);
$stmt->execute();
$bowling_team_result = $stmt->get_result();
$bowling_team = $bowling_team_result->fetch_assoc()['team_name'];
// Calculate positive NRR
$nrr = abs(($inning1_runs / $overs_played_inning1) - ($inning2_runs / $overs_played_inning2));
// Determine winning and losing teams based on user inputs
if ($inning1_runs > $inning2_runs) {
$resultMessage = "$batting_team won by " . ($inning1_runs - $inning2_runs) . " runs";
$winning_team = $batting_team;
$losing_team = $bowling_team;
} elseif ($inning1_runs < $inning2_runs) {
$resultMessage = "$bowling_team won by " . (10 - $inning2_wickets) . " wickets";
$winning_team = $bowling_team;
$losing_team = $batting_team;
} else {
$resultMessage = "The match is tied";
$winning_team = NULL;
$losing_team = NULL;
}
// Determine short names for existing winning and losing teams
$existing_winning_team_short = NULL;
$existing_losing_team_short = NULL;
if ($existing_winning_team !== NULL) {
$stmt = $conn->prepare("SELECT short_name FROM teams WHERE team_name = ?");
$stmt->bind_param("s", $existing_winning_team);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$existing_winning_team_short = $result->fetch_assoc()['short_name'];
}
$stmt->close();
}
if ($existing_losing_team !== NULL) {
$stmt = $conn->prepare("SELECT short_name FROM teams WHERE team_name = ?");
$stmt->bind_param("s", $existing_losing_team);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$existing_losing_team_short = $result->fetch_assoc()['short_name'];
}
$stmt->close();
}
// Initialize short names
$winning_team_short = NULL;
$losing_team_short = NULL;
// Determine short names for winning and losing teams
if ($winning_team !== NULL) {
// Fetch short name for winning team
$stmt = $conn->prepare("SELECT short_name FROM teams WHERE team_name = ?");
$stmt->bind_param("s", $winning_team);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$winning_team_short = $result->fetch_assoc()['short_name'];
} else {
echo "No short name found for winning team<br>";
}
$stmt->close();
}
if ($losing_team !== NULL) {
// Fetch short name for losing team
$stmt = $conn->prepare("SELECT short_name FROM teams WHERE team_name = ?");
$stmt->bind_param("s", $losing_team);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$losing_team_short = $result->fetch_assoc()['short_name'];
} else {
echo "No short name found for losing team<br>";
}
$stmt->close();
}
// Prepare update statement for tournament_data based on home and away team
$stmt = $conn->prepare("UPDATE tournament_data SET result = ?, winning_team = ?, losing_team = ?, runs_scored_home = ?, runs_scored_away = ?, overs_played_home = ?, overs_played_away = ?, balls_played_home = ?, balls_played_away = ?, wickets_lost_home = ?, wickets_lost_away = ?, overs_played_home_nrr = ?, balls_played_home_nrr = ?, overs_played_away_nrr = ?, balls_played_away_nrr = ?, nrr = ? WHERE match_no = ?");
if ($batting_team_short == $home_team_short) {
$stmt->bind_param("sssiiiiiiiiiiiidi", $resultMessage, $winning_team, $losing_team, $inning1_runs, $inning2_runs, $inning1_overs, $inning2_overs, $inning1_balls, $inning2_balls, $inning1_wickets, $inning2_wickets, $inning1_overs_nrr, $inning1_balls_nrr, $inning2_overs_nrr, $inning2_balls_nrr, $nrr, $match_no);
} else {
$stmt->bind_param("sssiiiiiiiiiiiidi", $resultMessage, $winning_team, $losing_team, $inning2_runs, $inning1_runs, $inning2_overs, $inning1_overs, $inning2_balls, $inning1_balls, $inning2_wickets, $inning1_wickets, $inning2_overs_nrr, $inning2_balls_nrr, $inning1_overs_nrr, $inning1_balls_nrr, $nrr, $match_no);
}
if ($stmt->execute()) {
if ($winning_team === NULL && $losing_team === NULL) {
// Match is now tied
// Adjust for the previous winning team
$stmt_update_wins = $conn->prepare("UPDATE teams SET wins = wins - 1, points = points - 1, no_result = no_result + 1, nrr = nrr - ? WHERE short_name = ?");
$stmt_update_wins->bind_param("ds", $existing_nrr, $existing_winning_team_short);
if (!$stmt_update_wins->execute()) {
echo "Error executing statement for previous winning team: " . $stmt_update_wins->error;
}
$stmt_update_wins->close();
// Adjust for the previous losing team
$stmt_update_losses = $conn->prepare("UPDATE teams SET losses = losses - 1, points = points + 1, no_result = no_result + 1, nrr = nrr + ? WHERE short_name = ?");
$stmt_update_losses->bind_param("ds", $existing_nrr, $existing_losing_team_short);
if (!$stmt_update_losses->execute()) {
echo "Error executing statement for previous losing team: " . $stmt_update_losses->error;
}
$stmt_update_losses->close();
} else {
if ($winning_team == $existing_losing_team && $losing_team == $existing_winning_team) {
// Result was overturned
// Losing team won
$stmt_update_wins = $conn->prepare("UPDATE teams SET wins = wins + 1, losses = losses - 1, points = points + 2, nrr = nrr + ? + ? WHERE short_name = ?");
$stmt_update_wins->bind_param("dds", $nrr, $existing_nrr, $winning_team_short);
if (!$stmt_update_wins->execute()) {
echo "Error executing statement for winning team: " . $stmt_update_wins->error;
}
$stmt_update_wins->close();
// Winning team lost
$stmt_update_losses = $conn->prepare("UPDATE teams SET losses = losses + 1, wins = wins - 1, points = points - 2, nrr = nrr - ? - ? WHERE short_name = ?");
$stmt_update_losses->bind_param("dds", $nrr, $existing_nrr, $losing_team_short);
if (!$stmt_update_losses->execute()) {
echo "Error executing statement for losing team: " . $stmt_update_losses->error;
}
$stmt_update_losses->close();
} elseif ($winning_team == $existing_winning_team && $losing_team == $existing_losing_team) {
// Result is same
// Update the NRR for winning team
$stmt_update_wins = $conn->prepare("UPDATE teams SET nrr = nrr + ? - ? WHERE short_name = ?");
$stmt_update_wins->bind_param("dds", $nrr, $existing_nrr, $winning_team_short);
if (!$stmt_update_wins->execute()) {
echo "Error executing statement for winning team: " . $stmt_update_wins->error;
}
$stmt_update_wins->close();
// Update the NRR for losing team
$stmt_update_losses = $conn->prepare("UPDATE teams SET nrr = nrr - ? + ? WHERE short_name = ?");
$stmt_update_losses->bind_param("dds", $nrr, $existing_nrr, $losing_team_short);
if (!$stmt_update_losses->execute()) {
echo "Error executing statement for losing team: " . $stmt_update_losses->error;
}
$stmt_update_losses->close();
} elseif ($existing_winning_team == NULL && $existing_losing_team == NULL) {
// Match now has clear winner, previously tied
// Adjust for winning team
$stmt_update_wins = $conn->prepare("UPDATE teams SET wins = wins + 1, no_result = no_result - 1, points = points + 1, nrr = nrr + ? WHERE short_name = ?");
$stmt_update_wins->bind_param("ds", $nrr, $winning_team_short);
if (!$stmt_update_wins->execute()) {
echo "Error executing statement for winning team: " . $stmt_update_wins->error;
}
$stmt_update_wins->close();
// Adjust for losing team
$stmt_update_losses = $conn->prepare("UPDATE teams SET losses = losses + 1, no_result = no_result - 1, points = points - 1, nrr = nrr - ? WHERE short_name = ?");
$stmt_update_losses->bind_param("ds", $nrr, $losing_team_short);
if (!$stmt_update_losses->execute()) {
echo "Error executing statement for losing team: " . $stmt_update_losses->error;
}
$stmt_update_losses->close();
}
}
// Output JavaScript for alert and redirect
echo "<script>
alert('Result: $resultMessage');
window.location.replace('$redirectURL');
</script>";
exit;
} else {
echo "Error updating record: " . $conn->error;
}
// Close statement
$stmt->close();
} else {
echo "Match not found.";
}
$stmt->close();
} else {
echo '<script>alert("All form fields are required.")</script>';
}
}
$conn->close();