rendered paste body<?php
##############################################################
# FILENAME : ucp_rivals_tournaments.php
# COPYRIGHT : (c) 2008, Tyler N. King <aibotca@yahoo.ca>
# http://opensource.org/licenses/gpl-license.php GNU Public License
##############################################################
if ( !defined ( 'IN_PHPBB' ) )
{
exit;
}
/**
* Manage Tournaments
* Called from ucp_rivals with mode == 'tournaments'
*/
function ucp_rivals_tournaments ( $id, $mode, $u_action )
{
global $config, $db, $user, $auth, $template;
global $phpbb_root_path, $phpbb_admin_path, $phpEx;
$clan = new clan ( );
$tournament = new tournament ( );
// Check if the user is logged into a clan.
if ( empty ( $clan->data[ 'clan_id' ] ) )
{
// They are not. Redirect them to the login.
redirect ( append_sid ( "{$phpbb_root_path}ucp.$phpEx", 'i=rivals&mode=clan_login' ) );
}
// Are we submitting a form?
$submit = request_var ( 'submit', '' );
if ( !empty ( $submit ) )
{
// Yes, handle the form.
$tournament_id = request_var ( 'tournament_id', 0 );
$quit = request_var ( 'quit', 0 );
// Check for a quit.
if ( $quit == 1 )
{
// The clan wishes to quit the tournament.Get the tournament's details.
$sql = "SELECT * FROM " . TOURNAMENTS_TABLE . " WHERE tournament_id = " . $tournament_id;
$result = $db->sql_query ( $sql );
$row = $db->sql_fetchrow ( $result );
if ( $row[ 'tournament_status' ] == 1 )
{
// Remove the clan.
$sql = "DELETE FROM " . TCLANS_TABLE . " WHERE clan_tournament = $tournament_id AND clan_id = " . $clan->data[ 'clan_id' ];
$db->sql_query ( $sql );
$tournaments = unserialize ( $clan->data[ 'clan_tournaments' ] );
for ( $i = 0; $i < sizeof ( $tournaments ); $i++ )
{
if ( $tournaments[ $i ] == $tournament_id )
{
// Remove the tournament from the array.
unset ( $tournaments[ $i ] );
}
}
// Update the clan's data.
$tournaments = ( sizeof ( $tournaments ) > 0 ) ? serialize ( $tournaments ) : '';
$sql = "UPDATE " . CLANS_TABLE . " SET clan_tournaments = '$tournaments' WHERE clan_id = " . $clan->data[ 'clan_id' ];
$db->sql_query ( $sql );
}
// Compeleted. Let the user know.
trigger_error ( 'TOURNAMENT_QUITTED' );
}
$win_loss = $db->sql_escape ( request_var ( 'win_loss', '' ) );
// Get the clan's tournament data.
$sql = "SELECT * FROM " . TCLANS_TABLE . " WHERE clan_tournament = $tournament_id AND clan_id = {$clan->data[ 'clan_id' ]} ORDER BY clan_bracket DESC";
$result = $db->sql_query_limit ( $sql, 1 );
$row = $db->sql_fetchrow ( $result );
// Get the clan's opponent.
$opp = $tournament->get_matchup ( $row[ 'clan_bracket' ], $row[ 'clan_position' ] );
// Check if a report was made by the opponent.
$sql = "SELECT * FROM " . TREPORTS_TABLE . " WHERE report_tournament = $tournament_id AND clan_1id = $opp AND clan_2id = 0";
$result = $db->sql_query ( $sql );
$row = $db->sql_fetchrow ( $result );
if ( sizeof ( $row ) > 0 )
{
// A report does exist.
$sql = "UPDATE " . TREPORTS_TABLE . " SET clan_2 = {$clan->data[ 'clan_name' ]}, clan_r2 = '$win_loss', clan_2id = {$clan->data[ 'clan_id' ]} WHERE report_id = " . $row[ 'report_id' ];
$db->sql_query ( $sql );
// Completed. Let the user know.
trigger_error ( 'TOURNAMENT_REPORTED' );
}
else
{
// A report does not exist. Create a new one.
$sql = "INSERT INTO " . TREPORTS_TABLE . " (report_tournament, clan_1, clan_2, clan_r1, clan_r2, report_time, clan_1id, clan_2id) VALUES ($tournament_id, '{$clan->data[ 'clan_name' ]}', '', '$win_loss', '', " . time ( ) . ", {$clan->data[ 'clan_id' ]}, 0)";
$db->sql_query ( $sql );
// Completed. Let the user know.
trigger_error ( 'TOURNAMENT_REPORTED' );
}
}
else
{
// Loop through the clan's tournaments.
$i = 0;
$tournaments = unserialize ( $clan->data[ 'clan_tournaments' ] );
if ( !empty ( $clan->data[ 'clan_tournaments' ] ) )
{
foreach ( $tournaments AS $value )
{
$has_tournaments = true;
// Get the tournament's details.
$sql = "SELECT * FROM " . TOURNAMENTS_TABLE . " WHERE tournament_id = " . $value;
$result = $db->sql_query ( $sql );
$row = $db->sql_fetchrow ( $result );
// Get the clan's details for the tournament.
$sql_2 = "SELECT * FROM " . TCLANS_TABLE . " WHERE clan_tournament = $value AND clan_id = {$clan->data[ 'clan_id' ]} ORDER BY clan_position, clan_bracket DESC";
$result_2 = $db->sql_query_limit ( $sql_2, 1 );
$row_2 = $db->sql_fetchrow ( $result_2 );
// Check to see if the tournament is started.
if ( $row[ 'tournament_status' ] != 1 )
{
// Started. Check if this is a single or double elimination tournament.
if ( $row[ 'tournament_type' ] == 1 )
{
// Single. Check to see if they were eliminated.
$tournament_finishedclans = ( !empty ( $row[ 'tournament_finishedclans' ] ) ) ? unserialize ( $row[ 'tournament_finishedclans' ] ) : array ( );
if ( in_array ( $clan->data[ 'clan_id' ], $tournament_finishedclans ) )
{
// They were. Continue on.
continue;
}
else
{
// Get the clan's opponent for the current tournament and position.
$opp = $tournament->get_matchup ( $row_2[ 'clan_bracket' ], $row_2[ 'clan_position' ], $row_2[ 'clan_tournament' ] );
if ( $opp != false )
{
// Assign each tournament to the template.
$template->assign_block_vars ( 'block_tournaments', array (
'U_ACTION' => $u_action,
'BG_COLOR' => ( $i % 2 ) ? 'bg1' : 'bg2',
'ROW_COLOR' => ( $i % 2 ) ? 'bg1' : 'bg2',
'TOURNAMENT_ID' => $row[ 'tournament_id' ],
'TOURNAMENT_NAME' => $row[ 'tournament_name' ],
'TOURNAMENT_OPP' => $clan->data ( 'clan_name', $opp ) )
);
}
}
}
else
{
// Double. Check if the clan is in the winner or looser bracket.
$sql_2 = "SELECT * FROM " . TCLANS_TABLE . " WHERE clan_tournament = {$row[ 'tournament_id' ]} AND clan_id = {$clan->data[ 'clan_id' ]} AND clan_looser = 1 ORDER BY clan_position, clan_bracket DESC";
$result_2 = $db->sql_query_limit ( $sql_2, 1 );
$row_2 = $db->sql_fetchrow ( $result_2 );
if ( !empty ( $row_2 ) )
{
// In the looser bracket. Suckers :P.
// Check to see if they were eliminated.
$tournament_finishedclans = ( !empty ( $row[ 'tournament_finishedclans' ] ) ) ? unserialize ( $row[ 'tournament_finishedclans' ] ) : array ( );
if ( in_array ( $clan->data[ 'clan_id' ], $tournament_finishedclans ) )
{
// They were. Continue on.
continue;
}
else
{
// Get the clan's opponent for the current tournament and position.
$opp = $tournament->get_matchup ( $row_2[ 'clan_bracket' ], $row_2[ 'clan_position' ], $row_2[ 'clan_tournament' ], 1 );
if ( $opp != false )
{
// Assign each tournament to the template.
$template->assign_block_vars ( 'block_tournaments', array (
'U_ACTION' => $u_action,
'BG_COLOR' => ( $i % 2 ) ? 'bg1' : 'bg2',
'ROW_COLOR' => ( $i % 2 ) ? 'bg1' : 'bg2',
'TOURNAMENT_ID' => $row[ 'tournament_id' ],
'TOURNAMENT_NAME' => $row[ 'tournament_name' ],
'TOURNAMENT_OPP' => $clan->data ( 'clan_name', $opp ) )
);
}
}
}
else
{
// In the winner bracket. Check to see if they were eliminated.
$tournament_finishedclans = ( !empty ( $row[ 'tournament_finishedclans' ] ) ) ? unserialize ( $row[ 'tournament_finishedclans' ] ) : array ( );
if ( in_array ( $clan->data[ 'clan_id' ], $tournament_finishedclans ) )
{
// They were. Continue on.
continue;
}
else
{
// Get the clan's opponent for the current tournament and position.
$opp = $tournament->get_matchup ( $row_2[ 'clan_bracket' ], $row_2[ 'clan_position' ], $row_2[ 'clan_tournament' ] );
if ( $opp != false )
{
// Assign each tournament to the template.
$template->assign_block_vars ( 'block_tournaments', array (
'U_ACTION' => $u_action,
'BG_COLOR' => ( $i % 2 ) ? 'bg1' : 'bg2',
'ROW_COLOR' => ( $i % 2 ) ? 'bg1' : 'bg2',
'TOURNAMENT_ID' => $row[ 'tournament_id' ],
'TOURNAMENT_NAME' => $row[ 'tournament_name' ],
'TOURNAMENT_OPP' => $clan->data ( 'clan_name', $opp ) )
);
}
}
}
}
}
else
{
// Not started. Assign each tournament to the template.
$template->assign_block_vars ( 'block_tournaments', array (
'U_QUIT' => $u_action . '&quit=1&submit=1&tournament_id=' . $row[ 'tournament_id' ],
'BG_COLOR' => ( $i % 2 ) ? 'bg1' : 'bg2',
'ROW_COLOR' => ( $i % 2 ) ? 'bg1' : 'bg2',
'TOURNAMENT_NAME' => $row[ 'tournament_name' ],
'TOURNAMENT_OPP' => '-' )
);
}
$i++;
}
}
else
{
// No tournaments.
$has_tournaments= false;
}
// Assign the other variables to the template.
$template->assign_var ( array ( 'HAS_TOURNAMENTS' => $has_tournaments ) );
}
}
?>