Location: PHPKode > scripts > StandAloneArcade - Free Arcade Script > StandAloneArcade/upload_1.4/include/scoring-mochi.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');
$oScore = new Scoring();

if( $oScore->settings['mochiEnabled'] == 1 && isset($_POST, $_POST['sessionID'], $_POST['gameID'], $_POST['userID'], $_POST['submission'], $_POST['score']) && is_numeric($_POST['userID']) )
{
	$sSession = mysql_real_escape_string($_POST['sessionID']);
	$iGameId = mysql_real_escape_string($_POST['gameID']);
	$iUserId = (int) $_POST['userID'];
	$iScore = mysql_real_escape_string($_POST['score']);
	$iTimeCheck = $oScore->Time - 3;
	$iPoints = $oScore->settings['activitypoints'];
	
	$a = $oScore->isValidScore($iUserId, $iGameId, $sSession, $iTimeCheck);
	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;
		}
	}
}
elseif( isset($_POST, $_POST['game_tag']) )
{
	if( $_POST['game_tag'] == "" )
		return false;
	if( strlen($oScore->settings['mochiPublisherId']) == 0 )
		exit();

	$aSplChr = array('/','#','\\','?','%','*',':','|','"',"'",'>','<','!',' ', '.', '(', ')');

	$sBool = array("True"=>1, "False"=>0);	
	$sUrl = "http://www.mochimedia.com/feeds/games/{$oScore->settings['mochiPublisherId']}/{$_POST['game_tag']}/";
	
	$xmlData = simplexml_load_file($sUrl);
	if( empty($xmlData) )
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $sUrl);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		$str = curl_exec($curl);
		curl_close($curl);
		$xmlData = simplexml_load_string($str);
	}
	$v = (array)$xmlData->entry[0];
	$sDim = (string)$v['summary']->div->dl->dd[2];
	$aDim = explode("x", $sDim);
	$sLink = (string)$v['summary']->div->dl->dd[11];
	$sSwf = (string)$v['link'][1]['href'];
	$sThumb = (string)$v['summary']->div->a->img['src'];
	$sDesc = (string)$v['summary']->div->dl->dd[1];
	$sDir = (string)$v['summary']->div->dl->dd[3];
	$sCon = (string)$v['summary']->div->dl->dd[5];
	$sCat = (string)$v['summary']->div->dl->dd[6];
	$sKey = (string)$v['summary']->div->dl->dd[7];
	$sScore = (string)$v['summary']->div->dl->dd[9];
	$sScore = $sBool[$sScore];
	$sFile = (string)$v['summary']->div->dl->dd[14];
	$sId = (string)$v['summary']->div->dl->dd[0];
	$aGame = array(	'link' => mysql_real_escape_string($sLink),
					'unique' => mysql_real_escape_string($sId),
					'base_category' => mysql_real_escape_string($sCat),
					'title' => mysql_real_escape_string($v['title']),
					'description' => mysql_real_escape_string($sDesc),
					'directions' => mysql_real_escape_string($sDir),
					'controls' => mysql_real_escape_string($sCon),
					'keywords' => mysql_real_escape_string($sKey),
					'link' => mysql_real_escape_string($sLink),
					'file' => mysql_real_escape_string($sSwf),
					'file_size' => mysql_real_escape_string($sFile),
					'image' => mysql_real_escape_string($sThumb),
					'width' => $aDim[0],
					'height' => $aDim[1],
					'has_scores' => mysql_real_escape_string($sScore),							
					);
	
	if( $oScore->settings['mochiFiles'] == 1 )
	{
		require('./classes/class_file.php');
		$oFile = new File();
		$aCol = array();
		$sql = "SELECT `game_id`, `image`, `file`, INSTR(`image`, 'mochiads.com') AS `imgCheck`, INSTR(`file`, 'mochiads.com') AS `swfCheck` FROM `arcade_games` WHERE `unique` = '{$aGame['unique']}' AND `type` = '1' LIMIT 1";
		$result = mysql_query($sql);
		if( ($aRow = mysql_fetch_assoc($result)) )
		{
			//Image Check
			if( $aRow['imgCheck'] != 0 )
			{
				$sImage = basename(stripslashes($aRow['image']));
				$intDot = strripos($sImage, '.');
				$sPt1 = str_replace($aSplChr, "-", $aGame['title']);
				$sPt2 = substr($sImage, $intDot);
				$sImage = $sPt1.'-'.$aRow['game_id'].$sPt2;
				$result = $oFile->getFromExternal(stripslashes($aRow['image']), '../file/images/', $sImage);
				if( $result )
					$aCol[] = "`image` = '".mysql_real_escape_string($sImage)."'";
			}
			
			//Swf Check
			if( $aRow['swfCheck'] != 0 )
			{
				$sSwf = basename(stripslashes($aRow['file']));
				$intDot = strripos($sSwf, '.');
				$sPt1 = str_replace($aSplChr, "-", $aGame['title']);
				$sPt2 = substr($sSwf, $intDot);
				$sSwf = $sPt1.'-'.$aRow['game_id'].$sPt2;
				$result = $oFile->getFromExternal(stripslashes($aRow['file']), '../file/swf/', $sSwf);
				if( $result )
					$aCol[] = "`file` = '".mysql_real_escape_string($sSwf)."'";
			}				
			
			if( count($aCol) )
			{
				$sCol = implode(", ", $aCol);
				$sql = "UPDATE `arcade_games` SET {$sCol} WHERE `game_id` = '{$aRow['game_id']}' ";
				$result = mysql_query($sql);
			}
		}
		else
		{
			$sql = "	INSERT INTO `arcade_games` ".
							"(	`type`, `source`, `unique`, `base_category`, `title`, `description`,".
							"	`directions`, `controls`, `keywords`, `link`,".
							"	`width`, `time_added`,".
							"	`height`, `has_scores`, `reverse`)".
					"	VALUES". 
							"(	'1', '1', '{$aGame['unique']}', '{$aGame['base_category']}', '{$aGame['title']}', '{$aGame['description']}',".
							"	'{$aGame['directions']}', '{$aGame['controls']}', '{$aGame['keywords']}', '{$aGame['link']}', ".
							"	'{$aGame['width']}', '".$oScore->Time."', ".
							"	'{$aGame['height']}', '{$aGame['has_scores']}', '0' ) ";
			$result = mysql_query($sql);
			$iCurrent = mysql_insert_id();
			
			//Move and rename image
			$sImage = basename($aGame['image']);
			$intDot = strripos($sImage, '.');
			$sPt1 = str_replace($aSplChr, "-", $aGame['title']);
			$sPt2 = substr($sImage, $intDot);
			$sImage = $sPt1.'-'.$iCurrent.$sPt2;
			$result = $oFile->getFromExternal(stripslashes($aGame['image']), '../file/images/', $sImage);
			if( $result )
				$sImage = mysql_real_escape_string($sImage);
			else
				$sImage = mysql_real_escape_string($aGame['image']);

			//move and rename swf
			$sSwf = basename($aGame['file']);
			$intDot = strripos($sSwf, '.');
			$sPt2 = substr($sSwf, $intDot);
			$sSwf = $sPt1.'-'.$iCurrent.$sPt2;
			$result = $oFile->getFromExternal(stripslashes($aGame['file']), '../file/swf/', $sSwf);
			if( $result )
				$sSwf = mysql_real_escape_string($sSwf);
			else
				$sSwf = mysql_real_escape_string($aGame['file']);
			
			$sql = "UPDATE `arcade_games` SET `image` = '{$sImage}', `file` = '{$sSwf}', `file_size` = '{$aGame['file_size']}' WHERE `game_id` = '{$iCurrent}' ";
			$result = mysql_query($sql);

		}	
	}
	else
	{
		$sql = "SELECT `game_id` FROM `arcade_games` WHERE `unique` = '{$aGame['unique']}' LIMIT 1";
		$result = mysql_query($sql);
		if( ($aRow = mysql_fetch_assoc($result)) )
			exit();
		else
		{
			$sql = "	INSERT INTO `arcade_games` ".
							"(	`type`, `source`, `unique`, `base_category`, `title`, `description`,".
							"	`directions`, `controls`, `keywords`, `link`,".
							"	`file`, `file_size`, `image`, `width`,".
							"	`height`, `has_scores`, `reverse`, `time_added`)".
					"	VALUES". 
							"(	'1', '1', '{$aGame['unique']}', '{$aGame['base_category']}', '{$aGame['title']}', '{$aGame['description']}',".
							"	'{$aGame['directions']}', '{$aGame['controls']}', '{$aGame['keywords']}', '{$aGame['link']}', ".
							"	'{$aGame['file']}', '{$aGame['file_size']}', '{$aGame['image']}', '{$aGame['width']}', ".
							"	'{$aGame['height']}', '{$aGame['has_scores']}', '0', '".$oScore->Time."') ";
			$result = mysql_query($sql);
		}
	}	
}else
	echo"What are you doing here?";
?>
Return current item: StandAloneArcade - Free Arcade Script