Location: PHPKode > scripts > StandAloneArcade - Free Arcade Script > StandAloneArcade/upload_1.4/include/scoring-gamersafe.php
<?php
$pathCache = '..';
$sIncludeFolder = "http://".$_SERVER['SERVER_NAME'].str_replace('scoring-mochi.php', '', $_SERVER['SCRIPT_NAME']);
$sMainFolder = str_replace('include/', '', $sIncludeFolder);

define('SAA_ON', TRUE);
// db info
include_once('./config.php');
include_once('./setup/setup_db.php');
require('./classes/class_scoring.php');
require('./classes/class_cache.php');

if( isset($_REQUEST, $_REQUEST['sessionID'], $_REQUEST['score'], $_REQUEST['userID']) )
{
	$oScore = new Scoring();
	
	$aPart = explode("-", $_REQUEST['userID']);
	if( !isset($aPart[1], $aPart[0]) )
		exit();

	$sSession = mysql_real_escape_string($_REQUEST['sessionID']);
	$iUserId = (int) $aPart[0];
	$iGameId = (int) $aPart[1];
	$iScore = mysql_real_escape_string($_REQUEST['score']);
	$iTimeCheck = $oScore->Time - 3;
	
	$iPoints = $oScore->settings['activitypoints'];
	
	$a = $oScore->isValidScore($iUserId, $iGameId, $sSession, $iTimeCheck, true);
	if( $a == false || isset($a['score_check']) )
		exit();
	
		//add score
	mysql_query("INSERT INTO `arcade_scores` (`game_id`, `user_id`, `score`, `time`) VALUES ('{$a['game_id']}', '{$iUserId}', '{$iScore}', '{$oScore->Time}')");
	
	//add activity point?
	if( $oScore->settings['activitypoints'] > 0 )
		mysql_query("UPDATE `arcade_users` SET `points` = `points` + ".(int) $oScore->settings['activitypoints']." WHERE `id` = '{$iUserId}'");
	
	//Is Highscore ?
	if( $a['reverse'] == 0 )
		$bConditonal = ($iScore > $a['score_high_score']);
	else
		$bConditonal = ($iScore < $a['score_high_score']);

	if( !isset($a['score_high_id']) || $bConditonal || $iScore == $a['score_high_score'] && $oScore->settings['tieGoesTo'] == 2 )
	{
		if( isset($a['score_high_id']) )
		{
			mysql_query("UPDATE `arcade_scores_high` SET `user_id` = '{$iUserId}', `score` = '{$iScore}', `time` = '{$oScore->Time}' WHERE `game_id` = '{$a['game_id']}'");
			
			$sql = 
			 "INSERT INTO `arcade_news`".
			 " (`type`, `time`, `winner`, `loser`, `score`, `game_id`)".
			 " VALUES".
			 " ('3', '{$oScore->Time}', '{$iUserId}', '{$a['score_high_user']}', '{$iScore}', '{$a['game_id']}')";
			mysql_query($sql);
			
			mysql_query("UPDATE `arcade_users` SET `arcade_champs` = `arcade_champs` - 1 WHERE `id` = '{$iUserId}' AND `arcade_champs` > 0");
		}
		else
		{
			mysql_query("INSERT INTO `arcade_scores_high` (`game_id`, `user_id`, `score`, `time`) VALUES ('{$a['game_id']}', '{$iUserId}', '{$iScore}', '{$oScore->Time}')");
			
			$sql = 
			 "INSERT INTO `arcade_news`".
			 " (`type`, `time`, `winner`, `loser`, `score`, `game_id`)".
			 " VALUES".
			 " ('4', '{$oScore->Time}', '{$iUserId}', '', '{$iScore}', '{$a['game_id']}')";
			mysql_query($sql);
		}
		
		mysql_query("UPDATE `arcade_users` SET `arcade_champs` = `arcade_champs` + 1 WHERE `id` = '{$iUserId}'");
	}

	//is user high score?
	if( !isset($a['score_user_id']) )
		mysql_query("INSERT INTO `arcade_scores_user` (`game_id`, `user_id`, `score`, `time`) VALUES ('{$a['game_id']}', '{$iUserId}', '{$iScore}', '{$oScore->Time}')");
	elseif( $iScore >= $a['score_user_score'] )
		mysql_query("UPDATE `arcade_scores_user` SET `score` = '{$iScore}', `time` = '{$oScore->Time}' WHERE `score_id` = '{$a['score_user_id']}'");

	$sql = "SELECT `challenge_id` FROM `arcade_challenge_ini` WHERE `game_id` = '{$a['game_id']}' AND `user_id` = '{$iUserId}' LIMIT 1";
	$result = mysql_query($sql);
	$aRow = mysql_fetch_assoc($result);
	if( !is_array($aRow) )
		exit();

	mysql_query("DELETE FROM `arcade_challenge_ini` WHERE `game_id` = '{$a['game_id']}' AND `user_id` = '{$iUserId}'");
	
	$sql = 
		"SELECT ".
			"`c`.`id`, ".
			"`c`.`user1`, ".
			"`u1`.`user` as `user1_name`, ".
			"`c`.`user2`, ".
			"`u2`.`user` as `user2_name`, ".
			"`c`.`score1`, ".
			"`c`.`score2`, ".
			"`c`.`type`, ".
			"`c`.`winner` ".
		"FROM `arcade_challenges` AS `c` ".
		"INNER JOIN `arcade_users` as `u1` ".
		"ON `u1`.`id` = `c`.`user1` ".
		"INNER JOIN `arcade_users` as `u2` ".
		"ON `u2`.`id` = `c`.`user2` ".
		"WHERE ".
			"`c`.`id` = '{$aRow['challenge_id']}' ";
	$result = mysql_query($sql);
	if( ($aRow = mysql_fetch_assoc($result) ) )
	{		
		if($aRow['user1'] == $iUserId)
		{
			$sCol =	'score1';
			$sCol2 = 'score2';
			$sName = 'user1_name';
			$sName2 = 'user2_name';
			$sKey =	'user1';
			$sKey2 = 'user2';
		}
		else
		{
			$sCol =	'score2';
			$sCol2 = 'score1';
			$sName = 'user2_name';
			$sName2 = 'user1_name';
			$sKey =	'user2';
			$sKey2 = 'user1';
		}

		switch($aRow['type'])
		{
			case 0:
				mysql_query("UPDATE `arcade_challenges` SET `type` = '1', `{$sCol}` = '{$iScore}' WHERE `id` = '{$aRow['id']}'");
				
				$mess = "<p><b>".$aRow[$sName]." has submitted a score!</b></p>";
				$mess .= $aRow[$sName]." has submitted a score of ".( $oScore->settings['challenge_hide_scores'] ? "?" : $iScore)." for your arcade duel at {$a['title']}! <p> You can play to submit your opposing score by clicking ";
				$turl = $sMainFolder."game.php?play={$a['game_id']}&cid=".$aRow['id'];
				$mess .= "<a href=\"".$turl."\">here.</a>";

				$sql = "INSERT INTO `arcade_message` (`to_id`, `sent_id`, `message`) VALUES ('".$aRow[$sKey2]."', '{$iUserId}', '{$mess}')";
				$result = mysql_query($sql) or die(mysql_error());				
				break;
			case 1:
				if( isset($aRow[$sCol]) )
					exit();
				
				if($a['reverse'] == 0)
					$iWinner = ($aRow[$sCol2] <= $iScore ? $iUserId : $aRow[$sKey2]);
				else
					$iWinner = ($aRow[$sCol2] >= $iScore ? $iUserId : $aRow[$sKey2]);
				
				if($iWinner == $iUserId)
				{
					$iLoser = $aRow[$sKey2];
					$sLoser = $aRow[$sName2];
					$sLoserScore = $aRow[$sCol2];
					$sWinner = $aRow[$sName];
					$sWinnerScore = $aRow[$sCol];
				}
				else
				{
					$iLoser = $aRow[$sKey];
					$sLoser = $aRow[$sName];
					$sLoserScore = $aRow[$sCol];
					$sWinner = $aRow[$sName2];
					$sWinnerScore = $aRow[$sCol2];
				}
				
				mysql_query("UPDATE `arcade_challenges` SET `type` = '2', `{$sCol}` = '{$iScore}', `winner` = '{$iWinner}' WHERE `id` = '{$aRow['id']}'");
				
				$sql = 
					 "INSERT INTO `arcade_news`".
					 " (`type`, `time`, `winner`, `loser`, `score`, `game_id`)".
					 " VALUES".
					 " ('1', '{$oScore->Time}', '{$iWinner}', '{$iLoser}', '{$iScore}', '{$a['game_id']}')";
				mysql_query($sql);
				
				$challengeUrl = $sMainFolder."challenge.php";
				
			// send loser a challenge message
				$mess = "<p><b>{$sWinner} has defeated you at {$a['title']}</b></p>";
				$mess .= $sWinner." has defeated your score of {$sLoserScore} at {$a['title']} with a score of {$sWinnerScore}. <p>You can view more details by clicking ";
				$challengeUrl = $sMainFolder."challenge.php";
				$mess .= "<a href=\"{$challengeUrl}\">here.</a>";

				$sql = "INSERT INTO `arcade_message` (`to_id`, `sent_id`, `message`) VALUES ('{$iLoser}', '{$iWinner}', '{$mess}')";
				$result = mysql_query($sql) or die(mysql_error());

			// send winner a challenge message
				$mess = "<p><b>You have just defeated {$sLoser} at {$a['title']}!</b></p>";
				$mess .= "Your score of {$sWinnerScore} defeated {$sLoser}\'s score of {$sLoserScore} at {$a['title']}! <p>You can view more details by clicking ";
				$mess .= "<a href=\"{$challengeUrl}\">here.</a>";
				$sql = "INSERT INTO `arcade_message` (`to_id`, `sent_id`, `message`) VALUES ('{$iWinner}', '{$iLoser}', '{$mess}')";
				$result = mysql_query($sql) or die(mysql_error());

				break;
		}
	}
}
?>
Return current item: StandAloneArcade - Free Arcade Script