PageRenderTime 30ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/admin_update_tournaments.php

https://gitlab.com/zanzilan/alp
PHP | 439 lines | 428 code | 6 blank | 5 comment | 158 complexity | 8d71f3f3565847b2e70c52811ed7bc95 MD5 | raw file
  1<?php
  2require_once 'include/_universal.php';
  3$x = new universal('tournaments','tournament',1);
  4if ($x->is_secure()) {
  5	if (empty($_GET) && empty($_POST)) {
  6		$x->display_slim('incorrect usage.','disp_tournament.php');
  7	} elseif (!empty($_POST) && !empty($_POST["id"])) {
  8		$tournament = $dbc->database_fetch_assoc($dbc->database_query('SELECT * FROM tournaments WHERE tourneyid='.(int)$_POST['id']));
  9		if (current_security_level() >= 2 || (current_security_level() >= 1 && $tournament['moderatorid'] == $_COOKIE['userid'])) {
 10			$allgood = true;
 11			if (!empty($_POST['matchid']) && !empty($_POST['server'])) {
 12				// change server for a match
 13				if (!$dbc->database_query("UPDATE tournament_matches SET server='".$_POST['server']."' WHERE id='".$_POST['matchid']."'")) {
 14					$allgood = false;
 15				}
 16				if ($allgood) {
 17					$str = 'success.';
 18				} else {
 19					$str = 'error!';
 20				}
 21			}
 22			if($tournament['ttype']==12&&!empty($_POST['matchid'])&&!empty($_POST['onlyscore'])) {
 23				// update boiloff score for one team.
 24				if(!$dbc->database_query("UPDATE tournament_matches_teams SET score='".$_POST['onlyscore']."' WHERE tourneyid='".$tournament['tourneyid']."' AND id='".$_POST['matchid']."'")) {
 25					$str = 'unknown error';
 26				} else {
 27					$str = 'success';
 28				}
 29			} elseif ($tournament['ffa']) { 
 30				require_once 'include/_top_smallwindow.php';
 31				if (!empty($_GET['matchid']) && !empty($_GET['i']) && !empty($_GET['j'])) {
 32					$str = 'incorrect usage';
 33				} else {
 34					$data = $dbc->database_query("SELECT * FROM tournament_matches_teams WHERE tourneyid='".$tournament['tourneyid']."' AND matchid='".$_POST['matchid']."'");
 35					while ($row = $dbc->database_fetch_assoc($data)) {
 36						if ($_POST[$row['id'].'_score']===0) $temp = "'0.0'";
 37						elseif($_POST[$row['id'].'_score']==='') $temp = 'NULL';
 38						else $temp = "'".$_POST[$row['id'].'_score']."'";
 39						if(!$dbc->database_query("UPDATE tournament_matches_teams SET score=".$temp." WHERE id='".$row['id']."'")) {
 40							$allgood = false;
 41						}
 42					}
 43					$nextmatch = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_POST['id']."' AND rnd='".($_POST['i']+1)."' AND mtc='".ceil($_POST['j']/4)."' AND bracket='".$_POST['b']."'"));
 44					$previouswinners = array();
 45					$data = $dbc->database_query("SELECT * FROM tournament_matches_teams WHERE tourneyid='".$tournament['tourneyid']."' AND matchid='".$nextmatch['id']."' AND top='".($_POST['j']%4==2?'1':'0')."'");
 46					while ($row = $dbc->database_fetch_assoc($data)) {
 47						if ($row['team']!=0) $previouswinners[] = $row['team'];
 48					}
 49					$nextmatch_slots = ceil($dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_matches_teams WHERE tourneyid='".$tournament['tourneyid']."' AND matchid='".$nextmatch['id']."'"))/2);
 50					$data = $dbc->database_query("SELECT * FROM tournament_matches_teams WHERE tourneyid='".$tournament['tourneyid']."' AND matchid='".$_POST['matchid']."' ORDER BY score DESC LIMIT ".$nextmatch_slots);
 51					$baseid = $dbc->database_fetch_assoc($dbc->database_query("SELECT MIN(id) AS id FROM tournament_matches_teams WHERE tourneyid='".$tournament['tourneyid']."' AND matchid='".$nextmatch['id']."' AND top='".($_POST['j']%4==2?'1':'0')."'"));
 52					$counter = 0;
 53					$newwinners = array();
 54					while($row = $dbc->database_fetch_assoc($data)) {
 55						if ($row['team'] != 0) { $newwinners[] = $row['team']; }
 56						if (!$dbc->database_query("UPDATE tournament_matches_teams SET team='".$row['team']."' WHERE tourneyid='".$tournament['tourneyid']."' AND matchid='".$nextmatch['id']."' AND id='".($baseid['id']+$counter)."'")) {
 57							$allgood = false;
 58						}
 59						$counter++;
 60					}
 61					$garbage = array_diff($previouswinners,$newwinners);
 62					if(sizeof($garbage)>0) {
 63						$query = "UPDATE tournament_matches_teams SET team='0',score=NULL WHERE tourneyid='".$tournament['tourneyid']."' AND (";
 64						$counter = 1;
 65						foreach($garbage as $val) {
 66							$query .= "team='".$val."'";
 67							if(sizeof($garbage)!=$counter) $query .= ' OR ';
 68							$counter++;
 69						}	
 70						$query .= ") AND matchid<'".$_POST['matchid']."'";
 71						if(!$dbc->database_query($query)) {
 72							$allgood = false;
 73						}
 74					}
 75					if($allgood) {
 76						$str = 'success.';
 77					} else {
 78						$str = 'error!';
 79					}
 80				} ?>
 81				<br />
 82				<div align="center"><b><?php echo $str; ?></b></div>
 83				<script language="javascript"><!--
 84				opener.location.reload(true);
 85				setTimeout('window.close()',1000);
 86				// --></script>
 87				<?php
 88				require_once "include/_bot_smallwindow.php"; 
 89			} else {
 90				if(!empty($_POST["i"])&&!empty($_POST["j"])&&isset($_POST["topscore"])&&isset($_POST["bottomscore"])&&!empty($_POST["matchid"])&&!empty($_POST["b"])) {
 91					if($tournament["per_team"]==1) {
 92						$n = $dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_players WHERE tourneyid='".$tournament["tourneyid"]."'"));
 93					} elseif($tournament["per_team"]>1) {
 94						$n = $dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_teams WHERE tourneyid='".$tournament["tourneyid"]."'"));
 95					}
 96					$NHPT = pow(2,ceil(log($n)/log(2)));
 97					$NLPT = pow(2,floor(log($n-1)/log(2)));
 98	
 99					if($_POST["b"]=="w") {
100						$temp = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_POST["id"]."' AND rnd='".$_POST["i"]."' AND bracket='w' ORDER BY id LIMIT 1"));
101						$matchinfo = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_POST["id"]."' AND rnd='".($_POST["i"]+1)."' AND mtc='".ceil($_POST["j"]/4)."' AND bracket='w'"));
102					} else {
103						if($n>($NHPT-(($NHPT-$NLPT)/2))) { 
104							if(($_POST["i"]+1)%2==0&&$_POST["i"]>1) $matchinfo = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_POST["id"]."' AND rnd='".($_POST["i"]+1)."' AND mtc='".ceil($_POST["j"]/2)."' AND bracket='".$_POST["b"]."'"));
105							else $matchinfo = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_POST["id"]."' AND rnd='".($_POST["i"]+1)."' AND mtc='".ceil($_POST["j"]/4)."' AND bracket='".$_POST["b"]."'"));
106						} else {
107							if(($_POST["i"]+1)%2==1&&$_POST["i"]>1)	$matchinfo = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_POST["id"]."' AND rnd='".($_POST["i"]+1)."' AND mtc='".ceil($_POST["j"]/2)."' AND bracket='".$_POST["b"]."'"));
108							else $matchinfo = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_POST["id"]."' AND rnd='".($_POST["i"]+1)."' AND mtc='".ceil($_POST["j"]/4)."' AND bracket='".$_POST["b"]."'"));
109						}
110					}
111					if($_POST["topscore"]===0) $temp = "'0.0'";
112					elseif($_POST["topscore"]==='') $temp = "NULL";
113					else $temp = "'".$_POST["topscore"]."'";
114					if(!$dbc->database_query("UPDATE tournament_matches_teams SET score=".$temp." WHERE matchid='".$_POST["matchid"]."' AND top='1'")) {
115						$allgood = false;
116					}
117					if($_POST["bottomscore"]===0) $temp = "'0.0'";
118					elseif($_POST["bottomscore"]==='') $temp = "NULL";
119					else $temp = "'".$_POST["bottomscore"]."'";
120					if(!$dbc->database_query("UPDATE tournament_matches_teams SET score=".$temp." WHERE matchid='".$_POST["matchid"]."' AND top='0'")) {
121						$allgood = false;
122					}
123
124					if($_POST["bottomscore"]==''&&$_POST["topscore"]=='') {
125						$currwinner["team"] = 0;
126					} else {
127						$currwinner = $dbc->database_fetch_assoc($dbc->database_query("SELECT team FROM tournament_matches_teams WHERE matchid='".$_POST["matchid"]."' AND top='".($_POST["topscore"]>$_POST["bottomscore"]?"1":"0")."'"));
128					}
129					
130					$temp = $dbc->database_fetch_assoc($dbc->database_query("SELECT team FROM tournament_matches_teams WHERE matchid='".$matchinfo["id"]."' AND top='".(($_POST["j"]%4==1)||($_POST["j"]%4==2))."'"));
131					$previouswinner = $temp["team"];
132					if($_POST["b"]=="l") {
133						if($tournament["per_team"]==1) {
134							$n = $dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_players WHERE tourneyid='".$tournament["tourneyid"]."'"));
135						} elseif($tournament["per_team"]>1) {
136							$n = $dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_teams WHERE tourneyid='".$tournament["tourneyid"]."'"));
137						}
138						$maxrounds = $dbc->database_fetch_assoc($dbc->database_query("SELECT MAX(rnd) as rnd FROM tournament_matches WHERE tourneyid='".$tournament["tourneyid"]."' AND bracket='l'"));
139						if(($_POST["i"]+1)==$maxrounds["rnd"]) {
140							$top = 0;
141						} else {
142							$NHPT = pow(2,ceil(log($n)/log(2)));
143							$NLPT = pow(2,floor(log($n-1)/log(2)));
144							if($n>($NHPT-(($NHPT-$NLPT)/2))) { 
145								if(($_POST["i"]+1)%2==0) $top = 1;
146								else $top = (($_POST["j"]%4==1)||($_POST["j"]%4==2));
147							} else {
148								if(($_POST["i"]+1)%2==1) $top = 1;
149								else $top = (($_POST["j"]%4==1)||($_POST["j"]%4==2));
150							}
151						}
152					} else {
153					 	$top = (($_POST["j"]%4==1)||($_POST["j"]%4==2));
154					}
155	
156					if(($_POST["topscore"]!=$_POST["bottomscore"]||($_POST["bottomscore"]==''&&$_POST["topscore"]==''))&&$previouswinner!=$currwinner["team"]) {
157						if($previouswinner!=0) {
158							$data = $dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_POST["id"]."' AND rnd>'".$_POST["i"]."' AND bracket='".$_POST["b"]."' ORDER BY id DESC");
159							while($row = $dbc->database_fetch_assoc($data)) {
160								if($dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_matches_teams WHERE matchid='".$row["id"]."' AND team='".$previouswinner."'"))) {
161									if($row["rnd"]>($_POST["i"]+1)) {
162										if(!$dbc->database_query("UPDATE tournament_matches_teams SET team='0' WHERE matchid='".$row["id"]."' AND team='".$previouswinner."'")) {
163											$allgood = false;
164										}
165									}
166									if(!$dbc->database_query("UPDATE tournament_matches_teams SET score=NULL WHERE matchid='".$row["id"]."'")) {
167										$allgood = false;
168									}
169								}
170							}
171						}
172						if(!$dbc->database_query("UPDATE tournament_matches_teams SET team='".$currwinner["team"]."' WHERE matchid='".$matchinfo["id"]."' AND top='".$top."'")) {
173							$allgood = false;
174						}
175						if($_POST["b"]=="w"&&!empty($_POST["L_rnd"])&&!empty($_POST["L_mtc"])&&isset($_POST["L_top"])&&!empty($_POST["matchid"])) {
176							$loser = $dbc->database_fetch_assoc($dbc->database_query("SELECT team FROM tournament_matches_teams WHERE tourneyid='".$tournament["tourneyid"]."' AND matchid='".$_POST["matchid"]."' AND team!='".$currwinner["team"]."'"));
177							$losers_match = $dbc->database_fetch_assoc($dbc->database_query("SELECT id FROM tournament_matches WHERE tourneyid='".$tournament["tourneyid"]."' AND rnd='".$_POST["L_rnd"]."' AND mtc='".$_POST["L_mtc"]."' AND bracket='l'"));
178							if(!$dbc->database_query("UPDATE tournament_matches_teams SET team='".$loser["team"]."' WHERE matchid='".$losers_match["id"]."' AND top='".$_POST["L_top"]."'")) {
179								$allgood = false;
180							}
181						}
182					}
183				} elseif($_POST["i"]==0&&($_POST["j"]==0||$_POST["j"]==1)&&$_POST["b"]=="l"&&isset($_POST["topscore"])&&isset($_POST["bottomscore"])) {
184					$allgood = true;
185					$winners_id = $dbc->database_fetch_assoc($dbc->database_query("SELECT id FROM tournament_matches WHERE tourneyid='".$tournament["tourneyid"]."' AND bracket='w' ORDER BY rnd DESC LIMIT 1"));
186					$losers_id = $dbc->database_fetch_assoc($dbc->database_query("SELECT id FROM tournament_matches WHERE tourneyid='".$tournament["tourneyid"]."' AND bracket='l' ORDER BY rnd DESC LIMIT 1"));
187					$matchid = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$tournament["tourneyid"]."' AND rnd='0' AND mtc='".$_POST["j"]."' AND bracket='l'"));
188					if(!$dbc->database_query("UPDATE tournament_matches_teams SET score='".$_POST["topscore"]."' WHERE tourneyid='".$tournament["tourneyid"]."' AND top='1' AND matchid='".$winners_id["id"]."'")) {
189						$allgood = false;
190					}
191					if(!$dbc->database_query("UPDATE tournament_matches_teams SET score='".$_POST["bottomscore"]."' WHERE tourneyid='".$tournament["tourneyid"]."' AND top='0' AND matchid='".$losers_id["id"]."'")) {
192						$allgood = false;
193					}
194					if($_POST["topscore"]!=$_POST["bottomscore"]) {
195						$winner = $dbc->database_fetch_assoc($dbc->database_query("SELECT team FROM tournament_matches_teams WHERE tourneyid='".$tournament["tourneyid"]."' AND matchid='".($_POST["topscore"]>$_POST["bottomscore"]?$winners_id["id"]:$losers_id["id"])."'"));
196						$query = "UPDATE tournament_matches_teams SET team='".$winner["team"]."' WHERE matchid='".$matchid["id"]."' AND tourneyid='".$tournament["tourneyid"]."' AND top='1'";
197						if(!$dbc->database_query($query)) {
198							$allgood = false;
199						}
200					}
201					if($allgood) {
202						$str = "success.";
203					} else {
204						$str = "error!";
205					}
206				} elseif(!empty($_POST["matchid"])&&isset($_POST["leftscore"])&&isset($_POST["rightscore"])) {
207					$allgood = true;
208					if($_POST["leftscore"]>$_POST["rightscore"]) {
209						$temp = $dbc->database_fetch_assoc($dbc->database_query("SELECT team FROM tournament_matches_teams WHERE matchid='".$_POST["matchid"]."' AND top='1'"));
210						$winner = $temp["team"];
211					} elseif($_POST["leftscore"]<$_POST["rightscore"]) {
212						$temp = $dbc->database_fetch_assoc($dbc->database_query("SELECT team FROM tournament_matches_teams WHERE matchid='".$_POST["matchid"]."' AND top='0'"));
213						$winner = $temp["team"];
214					}
215					if(!$dbc->database_query("UPDATE tournament_matches SET top_x_advance='".($_POST["leftscore"]!=$_POST["rightscore"]?$winner:"0")."' WHERE id='".$_POST["matchid"]."'")) {
216						$allgood = false;
217					}
218					if(!$dbc->database_query("UPDATE tournament_matches_teams SET score='".$_POST["leftscore"]."' WHERE matchid='".$_POST["matchid"]."' AND top='1'")||!$dbc->database_query("UPDATE tournament_matches_teams SET score='".$_POST["rightscore"]."' WHERE matchid='".$_POST["matchid"]."' AND top='0'")) {
219						$allgood = false;
220					}
221				} else { 
222					$str = "incorrect usage.";
223				}
224				if($allgood) {
225					$str = "success.";
226				} else {
227					$str = "error!";
228				}
229			}
230			if(!$tournament["ffa"]) $x->display_slim($str,"disp_tournament.php".(!empty($_POST["id"])?"?id=".$_POST["id"]:""),1);
231		} else {
232			$x->display_slim("unauthorized.","disp_tournament.php".(!empty($_POST["id"])?"?id=".$_POST["id"]:""),1);
233		}
234	} elseif(!empty($_GET)&&!empty($_GET["id"])) {
235		$tournament = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournaments WHERE tourneyid='".$_GET["id"]."'"));
236		if(current_security_level()>=2||(current_security_level()>=1&&$tournament["moderatorid"]==$_COOKIE["userid"])) {
237			if($tournament['ttype']==12) {
238				if(!empty($_GET['matchid'])&&!empty($_GET['w'])&&empty($_GET['act'])) {
239					$current_match = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_GET['id']."' AND id='".$_GET['matchid']."'"));
240					$next_match = $dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_GET['id']."' AND rnd='".($current_match['rnd']+1)."'");
241					if($dbc->database_num_rows($next_match)) {
242						$next_match_info = $dbc->database_fetch_assoc($next_match);
243						$next_match_id = $next_match_info['id'];
244					} else {
245						$servers = $dbc->database_num_rows($dbc->database_query("SELECT * FROM servers WHERE tourneyid='".$tournament['tourneyid']."'"));
246						$dbc->database_query("INSERT INTO tournament_matches (tourneyid, rnd, mtc, server) VALUES ('".$tournament['tourneyid']."','".($current_match['rnd']+1)."','1','".($servers?"1":"0")."')");
247						$next_match_id = $dbc->database_insert_id();
248					}
249					if($dbc->database_query("INSERT INTO tournament_matches_teams (tourneyid, matchid, team) VALUES ('".$tournament['tourneyid']."','".$next_match_id."','".$_GET['w']."')")) {
250						$str = 'success.';
251					} else {
252						$str = 'unknown error.';
253					}
254				} elseif(!empty($_GET['matchid'])&&!empty($_GET['team'])&&!empty($_GET['act'])&&$_GET['act']=='del') {
255					$t_bool = true;
256					if($dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_matches_teams WHERE tourneyid='".$tournament['tourneyid']."' AND matchid='".$_GET['matchid']."'"))==1
257						&&$dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_matches_teams WHERE tourneyid='".$tournament['tourneyid']."' AND matchid='".$_GET['matchid']."' AND team='".$_GET['team']."'"))) {
258						if(!$dbc->database_query("DELETE FROM tournament_matches WHERE tourneyid='".$tournament['tourneyid']."' AND id='".$_GET['matchid']."'")) {
259							$t_bool = false;
260						}
261					}
262					if(!$dbc->database_query("DELETE FROM tournament_matches_teams WHERE tourneyid='".$tournament['tourneyid']."' AND matchid>='".$_GET['matchid']."' AND team='".$_GET['team']."'")) {
263						$t_bool = false;
264					}
265					$t_data = $dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$tournament['tourneyid']."' AND rnd>1");
266					while($t_row = $dbc->database_fetch_assoc($t_data)) {
267						if(!$dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_matches_teams WHERE tourneyid='".$tournament['tourneyid']."' AND matchid='".$t_row['id']."'"))) {
268							if(!$dbc->database_query("DELETE FROM tournament_matches WHERE tourneyid='".$tournament['tourneyid']."' AND id='".$t_row['id']."'")) {
269								$t_bool = false;
270							}
271						}
272					}
273					if($t_bool) {
274						$str = 'success.';
275					} else {
276						$str = 'unknown error.';
277					}	
278				} else {
279					$str = 'incorrect usage';
280				}
281			} elseif(!empty($_GET["act"])&&$_GET["act"]=="del"&&!empty($_GET["matchid"])) {
282				$allgood = true;
283				if($tournament["ttype"]==10) {
284					if(!$dbc->database_query("UPDATE tournament_matches SET top_x_advance='0' WHERE tourneyid='".$_GET["id"]."' AND id='".$_GET["matchid"]."'")) {
285						$allgood = false;
286					}				
287					if(!$dbc->database_query("UPDATE tournament_matches_teams SET score=NULL WHERE tourneyid='".$_GET["id"]."' AND matchid='".$_GET["matchid"]."'")) {
288						$allgood = false;
289					}
290				} else {
291					if(!$dbc->database_query("UPDATE tournament_matches_teams SET team='0' WHERE tourneyid='".$_GET["id"]."' AND id='".$_GET["matchid"]."'")) {
292						$allgood = false;
293					}
294				}
295				
296				if($allgood) {
297					$str = "success.";
298				} else {
299					$str = "error!";
300				}			
301			} elseif(!empty($_GET["i"])&&!empty($_GET["j"])&&!empty($_GET["w"])&&!empty($_GET["b"])) {
302				//($i+1),ceil($j/4),($j%4==3)||($j%4==0)
303				$allgood = true;
304				if($tournament["per_team"]==1) {
305					$n = $dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_players WHERE tourneyid='".$tournament["tourneyid"]."'"));
306				} elseif($tournament["per_team"]>1) {
307					$n = $dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_teams WHERE tourneyid='".$tournament["tourneyid"]."'"));
308				}
309				$NHPT = pow(2,ceil(log($n)/log(2)));
310				$NLPT = pow(2,floor(log($n-1)/log(2)));
311				if($_GET["b"]=="w") {
312					$temp = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_GET["id"]."' AND rnd='".$_GET["i"]."' AND bracket='w' ORDER BY id LIMIT 1"));
313					$firstmatchid = $temp["id"];
314					$lastmatchid = $temp["id"]-1+$dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_GET["id"]."' AND rnd='".$_GET["i"]."' AND bracket='w'"));
315					$winnersmatch = $dbc->database_fetch_assoc($dbc->database_query("SELECT matchid FROM tournament_matches_teams WHERE team='".$_GET["w"]."' AND tourneyid='".$tournament["tourneyid"]."' AND matchid<=".$lastmatchid." AND matchid>=".$firstmatchid));
316					$oldmatch = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_GET["id"]."' AND rnd='".($_GET["i"])."' AND id='".$winnersmatch["matchid"]."' AND bracket='w'"));
317					$matchinfo = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_GET["id"]."' AND rnd='".($_GET["i"]+1)."' AND mtc='".ceil($_GET["j"]/4)."' AND bracket='w'"));
318				} else {
319					if($n>($NHPT-(($NHPT-$NLPT)/2))) { 
320						if(($_GET["i"]+1)%2==0&&$_GET["i"]>1) $matchinfo = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_GET["id"]."' AND rnd='".($_GET["i"]+1)."' AND mtc='".ceil($_GET["j"]/2)."' AND bracket='".$_GET["b"]."'"));
321						else $matchinfo = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_GET["id"]."' AND rnd='".($_GET["i"]+1)."' AND mtc='".ceil($_GET["j"]/4)."' AND bracket='".$_GET["b"]."'"));
322					} else {
323						if(($_GET["i"]+1)%2==1&&$_GET["i"]>1) $matchinfo = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_GET["id"]."' AND rnd='".($_GET["i"]+1)."' AND mtc='".ceil($_GET["j"]/2)."' AND bracket='".$_GET["b"]."'"));
324						else $matchinfo = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_GET["id"]."' AND rnd='".($_GET["i"]+1)."' AND mtc='".ceil($_GET["j"]/4)."' AND bracket='".$_GET["b"]."'"));
325					}
326				}
327				
328				if($tournament["ffa"]) {
329					if($dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_matches_teams WHERE team='".$_GET["w"]."' AND matchid='".$matchinfo["id"]."'"))) {
330						// do nothing
331					} elseif($dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_matches_teams WHERE team='0' AND matchid='".$matchinfo["id"]."' AND top='".(($_GET["j"]%4==1)||($_GET["j"]%4==2))."'"))) {
332						if(!$dbc->database_query("UPDATE tournament_matches_teams SET team='".$_GET["w"]."' WHERE matchid='".$matchinfo["id"]."' AND team='0' AND top='".(($_GET["j"]%4==1)||($_GET["j"]%4==2))."' LIMIT 1")) {
333							$allgood = false;
334						}
335					} else {
336						$temp = $dbc->database_fetch_assoc($dbc->database_query("SELECT team FROM tournament_matches_teams WHERE matchid='".$matchinfo["id"]."' AND top='".(($_GET["j"]%4==1)||($_GET["j"]%4==2))."'"));
337						$previouswinner = $temp["team"];
338						if(!$dbc->database_query("UPDATE tournament_matches_teams SET team='".$_GET["w"]."' WHERE matchid='".$matchinfo["id"]."' AND team='".$temp["team"]."' AND top='".(($_GET["j"]%4==1)||($_GET["j"]%4==2))."' LIMIT 1")) {
339							$allgood = false;
340						}
341					}
342				} else {
343					if($tournament["per_team"]==1) {
344						$n = $dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_players WHERE tourneyid='".$tournament["tourneyid"]."'"));
345					} elseif($tournament["per_team"]>1) {
346						$n = $dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_teams WHERE tourneyid='".$tournament["tourneyid"]."'"));
347					}
348					$temp = $dbc->database_fetch_assoc($dbc->database_query("SELECT team FROM tournament_matches_teams WHERE matchid='".$matchinfo["id"]."' AND top='".(($_GET["j"]%4==1)||($_GET["j"]%4==2))."'"));
349					$previouswinner = $temp["team"];
350					if($_GET["b"]=="l") {
351						$maxrounds = $dbc->database_fetch_assoc($dbc->database_query("SELECT MAX(rnd) as rnd FROM tournament_matches WHERE tourneyid='".$tournament["tourneyid"]."' AND bracket='l'"));
352						if(($_GET["i"]+1)==$maxrounds["rnd"]) {
353							$top = 0;
354						} else {
355							$NHPT = pow(2,ceil(log($n)/log(2)));
356							$NLPT = pow(2,floor(log($n-1)/log(2)));
357							if($n>($NHPT-(($NHPT-$NLPT)/2))) { 
358								if(($_GET["i"]+1)%2==0) $top = 1;
359								else $top = (($_GET["j"]%4==1)||($_GET["j"]%4==2));
360							} else {
361								if(($_GET["i"]+1)%2==1) $top = 1;
362								else $top = (($_GET["j"]%4==1)||($_GET["j"]%4==2));
363							}
364						}
365					} else {
366					 	$top = (($_GET["j"]%4==1)||($_GET["j"]%4==2));
367					}
368					if(!$dbc->database_query("UPDATE tournament_matches_teams SET team='".$_GET["w"]."' WHERE matchid='".$matchinfo["id"]."' AND top='".$top."'")) {
369						$allgood = false;
370					}
371					if($_GET["b"]=="w"&&!empty($_GET["L_rnd"])&&!empty($_GET["L_mtc"])&&isset($_GET["L_top"])) {
372						$loser = $dbc->database_fetch_assoc($dbc->database_query("SELECT team FROM tournament_matches_teams WHERE tourneyid='".$tournament["tourneyid"]."' AND matchid='".$oldmatch["id"]."' AND team!='".$_GET["w"]."'"));
373						$losers_match = $dbc->database_fetch_assoc($dbc->database_query("SELECT id FROM tournament_matches WHERE tourneyid='".$tournament["tourneyid"]."' AND rnd='".$_GET["L_rnd"]."' AND mtc='".$_GET["L_mtc"]."' AND bracket='l'"));
374						if(!$dbc->database_query("UPDATE tournament_matches_teams SET team='".$loser["team"]."' WHERE matchid='".$losers_match["id"]."' AND top='".$_GET["L_top"]."'")) {
375							$allgood = false;
376						}
377					}
378				}
379				if($previouswinner!=0&&($previouswinner!=$_GET["w"]||(!empty($_GET["act"])&&$_GET["act"]=="del"))) {
380					$data = $dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$_GET["id"]."' AND rnd>='".$_GET["i"]."' AND bracket='".$_GET["b"]."' ORDER BY id DESC");
381					while($row = $dbc->database_fetch_assoc($data)) {
382						if($dbc->database_num_rows($dbc->database_query("SELECT * FROM tournament_matches_teams WHERE matchid='".$row["id"]."' AND team='".$previouswinner."'"))) {
383							if(!empty($_GET["act"])&&$_GET["act"]=="del") $temp = 0;
384							else $temp = 1;
385							if($row["rnd"]>($_GET["i"]+$temp)) {
386								if(!$dbc->database_query("UPDATE tournament_matches_teams SET team='0' WHERE matchid='".$row["id"]."' AND team='".$previouswinner."'")) {
387									$allgood = false;
388								}
389							}
390							if(!$dbc->database_query("UPDATE tournament_matches_teams SET score=NULL WHERE matchid='".$row["id"]."'")) {
391								$allgood = false;
392							}
393						}
394					}
395				}
396				if($allgood) {
397					$str = "success.";
398				} else {
399					$str = "error!";
400				}
401			} elseif($_GET["i"]==0&&($_GET["j"]==0||$_GET["j"]==1)&&!empty($_GET["w"])&&$_GET["b"]=="l") {
402				$allgood = true;
403				$matchid = $dbc->database_fetch_assoc($dbc->database_query("SELECT * FROM tournament_matches WHERE tourneyid='".$tournament["tourneyid"]."' AND rnd='0' AND mtc='".$_GET["j"]."' AND bracket='l'"));
404				$query = "UPDATE tournament_matches_teams SET team='".$_GET["w"]."' WHERE matchid='".$matchid["id"]."' AND tourneyid='".$tournament["tourneyid"]."' AND top='1'";
405				if(!$dbc->database_query($query)) {
406					$allgood = false;
407				}
408				if($allgood) {
409					$str = "success.";
410				} else {
411					$str = "error!";
412				}
413			} elseif(!empty($_GET["matchid"])&&!empty($_GET["w"])) {
414				$allgood = true;
415				$matchinfo = $dbc->database_fetch_assoc($dbc->database_query("SELECT top_x_advance FROM tournament_matches WHERE id='".$_GET["matchid"]."'"));
416				if($matchinfo["top_x_advance"]!=$_GET["w"]) {
417					if(!$dbc->database_query("UPDATE tournament_matches SET top_x_advance='".$_GET["w"]."' WHERE id='".$_GET["matchid"]."'")) {
418						$allgood = false;
419					}
420					if(!$dbc->database_query("UPDATE tournament_matches_teams SET score=NULL WHERE matchid='".$_GET["matchid"]."'")) {
421						$allgood = false;
422					}
423				}
424				if($allgood) {
425					$str = "success.";
426				} else {
427					$str = "error!";
428				}
429			} else {
430				$str = "incorrect usage.";
431			}
432			$x->display_slim($str,"disp_tournament.php".(!empty($_GET["id"])?"?id=".$_GET["id"]:""),1);
433		} else {
434			$x->display_slim("unauthorized.","disp_tournament.php".(!empty($_GET["id"])?"?id=".$_GET["id"]:""),1);
435		}
436	} else {
437		$x->display_slim("incorrect usage.","disp_tournament.php");
438	}
439} ?>