-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
298 lines (250 loc) · 8.43 KB
/
functions.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
<?php
// <-------------------------BRACKET FUNCTIONS START--------------------------->
// function: generateBracket
function generateBracket($con, $shuffledParticipantsArray)
{
$bracket = selectTable(count($shuffledParticipantsArray));
// mysqli_query($con, "TRUNCATE $bracket");
// get bracket_size
$bracket_size=getBracketSize($bracket);
$byeCount = $bracket_size-count($shuffledParticipantsArray);
$byeCount2 = $byeCount;
$slot = 1;
$array_pos = 0;
for ($i = 0; $i < $bracket_size; $i++) {
if($byeCount > 0){
insertInTable($con, $bracket, 'spot', 'name', $slot, '*BYE*');
$slot += 2;
$byeCount--;
} else {
if ($array_pos == 0){
$slot = 2;
}
//insertInTable($con, $bracket, 'spot', 'name',
// $slot, $shuffledParticipantsArray[$array_pos]);
$name = $shuffledParticipantsArray[$array_pos];
$con->query("INSERT INTO $bracket (spot, name, race)
VALUES ($slot, '$name', 'Terran')");
$array_pos++;
$slot += 2;
if($slot == $bracket_size + 2){
$slot = $byeCount2 * 2 + 1;
}
}
}
// update data with root table info
updateTableSpecific($con, 'data', $bracket, 'data', 'value', 'bracket');
// Table is now generated. Will generate empty child Tables.
for ($i = $bracket_size; $i >= 2; $i = $i / 2) {
//mod bracket ~~~
switch ($bracket) {
case 'bracket_ro2048':
$bracket='bracket_ro1024';
break;
case 'bracket_ro1024':
$bracket='bracket_ro512';
break;
case 'bracket_ro512':
$bracket='bracket_ro256';
break;
case 'bracket_ro256':
$bracket='bracket_ro128';
break;
case 'bracket_ro128':
$bracket='bracket_ro64';
break;
case 'bracket_ro64':
$bracket='bracket_ro32';
break;
case 'bracket_ro32':
$bracket='bracket_ro16';
break;
case 'bracket_ro16':
$bracket='bracket_ro8';
break;
case 'bracket_ro8':
$bracket='bracket_ro4';
break;
case 'bracket_ro4':
$bracket='bracket_ro2';
break;
case 'bracket_ro2':
$bracket='bracket_ro1';
break;
default:
$bracket='none';
}
$bracket_size=getBracketSize($bracket);
// insert placeholders in child brackets
for ($j = 1; $j <= $bracket_size; $j++) {
insertInTable($con, $bracket, 'spot', 'name', $j, ' ');
}
}
$bracket = selectTable(count($shuffledParticipantsArray));
$bracket_size = getBracketSize($bracket);
$bracket_extension = $bracket_size/2;
for ($i = 0; $i < $bracket_size; $i++) {
if (getSpecificValue($con, $bracket, 'spot', 'name', $i) == '*BYE*') {
$byespot = $i;
}
if ($byespot % 2 == 0) {
$opponentSlot = $byespot-1;
}
else {
$opponentSlot = $byespot+1;
}
if(isset($byespot)){
$bye = getSpecificValue($con, $bracket, 'spot', 'name', $byespot);
}
$advance = getSpecificValue($con, $bracket,
'spot', 'name', $opponentSlot);
if ($bye == "*BYE*") {
updateTableSpecific($con, 'bracket_ro'.$bracket_extension*2, 2,
'spot', 'score', $opponentSlot);
updateTableSpecific($con, 'bracket_ro'.$bracket_extension, $advance,
'spot', 'name', $opponentSlot/2);
updateTableSpecific($con, 'bracket_ro'.$bracket_extension*2,
0, 'spot', 'score', $byespot);
}
}
}
function getBracketSize($bracket)
{
if ($bracket == "none") return "none";
else return explode("o", $bracket)[1];
}
// function: selectTable, returns correct Table according to number of participants
function selectTable($counter)
{
for ($i = 2; $i < 4096; $i = $i * 2) {
if ($counter <= $i) {
return 'bracket_ro' . "$i";
}
}
}
// function: getUserBracket
function getUserBracket($con, $user)
{
for ($i = 1; $i < 4096; $i = $i * 2) {
if (checkIfInBracket($con, "bracket_ro" . "$i", $user)) {
return "bracket_ro" . "$i";
}
}
return "none";
}
// function: checkIfInBracket
function checkIfInBracket($con, $bracket, $user)
{
for ($i = 1; $i <= count(getValueArray($con, $bracket)); $i++) {
if (getSpecificValue($con, $bracket, 'spot', 'name', $i) == $user) {
return true;
}
}
return false;
}
// function: insertInTable
function insertInTable($con, $table, $column1, $column2, $value1, $value2)
{
mysqli_query($con,"INSERT INTO $table ($column1, $column2)
VALUES ($value1, '$value2')");
}
// function: updateTableSpecific
function updateTableSpecific($con, $table, $SET, $column1, $column2, $value1)
{
mysqli_query($con,"UPDATE $table "
. "SET $column2 = '$SET' "
. "WHERE $column1='$value1'");
}
// function: deleteTableElement
function deleteTableElement($con, $table, $column, $value)
{
mysqli_query($con,"DELETE FROM $table "
. "WHERE $column='$value'");
}
// function: updateBracket, enter <VALUE>, <SPOT>,
// updates <VALUE> @[WHERE] <SPOT>
function updateBracket($con, $input, $spot)
{
mysqli_query($con,"UPDATE ro16 "
. "SET name = '$input' "
. "WHERE spot='$spot'");
}
// function: updateBracket8
function updateBracket8($con, $input, $spot)
{
mysqli_query($con,"UPDATE ro8 "
. "SET name = '$input' "
. "WHERE spot='$spot'");
}
// function: wipeTable
function wipeTable($con, $table)
{
mysqli_query($con,"TRUNCATE $table");
}
// function: getEmptySlotV2 - revision
function getEmptySlotV2($con, $bracket)
{
$valueArray = getValueArray($con, $bracket);
$emptySlot= count($valueArray);
for ($i = count($valueArray) - 1; $i >= 0; $i--) {
if ($valueArray[$i] == ' ') {
$emptySlot=$i+1;
}
}
return $emptySlot;
}
/* function: getValue returns specific Value. Enter <TABLE ID>, <SPOT>.
* Value=<NAME> WHERE <SPOT> */
function getValue($con, $bracket, $i)
{
$result = mysqli_query($con, "SELECT name "
. "FROM $bracket "
. "WHERE spot='$i'");
$valueArray = mysqli_fetch_array($result);
return $valueArray[0];
}
// function: getValue with specified columns
function getSpecificValue($con, $table, $column1, $column2, $value1)
{
$result = mysqli_query($con, "SELECT $column2 "
. "FROM $table "
. "WHERE $column1='$value1'");
$valueArray = mysqli_fetch_array($result);
return $valueArray[0];
}
function getValueArray($con, $bracket)
{
$result = mysqli_query($con, "SELECT name "
. "FROM $bracket");
$valueArray = array();
while($row = mysqli_fetch_array($result)){
$valueArray[] = $row['name'];
}
return $valueArray;
}
// <------------------------BRACKET FUNCTIONS END------------------------------>
// <------------------------USER FUNCTIONS START------------------------------->
function insertInTable_Users ($con, $table, $column1, $column2, $column3,
$value1, $value2, $value3)
{
mysqli_query($con,"INSERT INTO $table ($column1, $column2, $column3) "
. "VALUES ('$value1', '$value2', $value3) ");
}
function getInformation_Users($con, $table, $column, $value)
{
$result = mysqli_query($con, "SELECT $column "
. "FROM $table "
. "WHERE $column='$value'");
$valueArray = mysqli_fetch_array($result);
return $valueArray[0];
}
function getInformation_Users2($con, $table, $column1, $column2, $value)
{
$result = mysqli_query($con, "SELECT $column1 "
. "FROM $table "
. "WHERE $column2='$value'");
$valueArray = mysqli_fetch_array($result);
return $valueArray[0];
}
// <------------------------USER FUNCTIONS END--------------------------------->
?>