<?php
require '../common.inc.php';
require 'GetPingYing.class.php';
header("Content-type:text/html; charset=utf-8");
$pingying = new GetPingYing(); 
$allgame = allgame();
$path ="info.js";
$a=file_put_contents($path,$allgame.PHP_EOL);
$b=file_put_contents($path,srcSearchWord(),FILE_APPEND);
if($a>0 && $b>0){
	$msg = "生成成功！";
}else{
	$msg = "生成失败，请检查程序错误！";
}
echo "66666666";
function allgame(){
   global $db;
   $set_list = array();
   $query = mysql_query("select * from {$db->pre}category where parentid in (4,6,7)");
   while($row=mysql_fetch_array($query)){
	$set_list[] = "{gameid:'".pc($row['parentid'],0)."".$row['catid']."', gamename:'".$row['catname']."', px:'".chineseFirst($row['catname'])."', hot:'".hot($row['level'])."', gametype:'".pc($row['parentid'],1)."', mobilegametype:'".iap($row['parentid'])."', camps:'', splxids:'".type_s($row['catid'])."'}";
}	
   $set_list = implode(",\n", $set_list);
   $set_list = 'var allgames = ['.$set_list.'];';
   return $set_list;
}

function srcSearchWord(){
   global $db, $pingying;
   $set_list = array();
   $query = mysql_query("select * from {$db->pre}category where parentid in (4,6,7)");
   while($row=mysql_fetch_array($query)){
   $ping = iconv("UTF-8","gb2312","我是中国人呀");
   $ping = chunk_split($ping,2,"'");
   $pinyin = $pingying->getAllPY("$ping");
	$set_list[] = '{"text":"'.$row['catname'].'", "pinyin":"'.$pinyin.'"}';
}	
   $set_list = implode(",\n", $set_list);
   $set_list = 'var srcSearchWord = ['.$set_list.'];';
   return $set_list;
}
//echo srcSearchWord();
//print_r(srcSearchWord());
/*
$ping = iconv("UTF-8","gb2312","我是中国人呀");
$ping = chunk_split($ping,2,"'");
echo $pingying->getAllPY("$ping");
*/
//print_r($allgame);

//属性类型
function type_s($catid){
	global $db;
	$catid = intval($catid);
	$cat = array();
	$result = $db->query("select oid,name,value from {$db->pre}category_option WHERE catid=$catid and parentid=0", 'CACHE');
	while($r = $db->fetch_array($result)) {
		$cat[] = "".$r['oid'].":".$r['name'].":".$r['value']."";
	}
	$list = implode(',',$cat);
	return $list;
}

//print_r(type_s(8));
//echo type_s(8);

//手游,PC游判断
function pc($pid,$type){
	$pid = intval($pid);
	if($type==1){
	  if($pid==6 || $pid==7){
		return "1";
	  }elseif($pid==4){
		return "0";
	 }	
	}elseif($type==0){
		if($pid==6 || $pid==7){
			return "A";
		}elseif($pid==4){
			return "G";
		}
	}
	
}
//IOS,android,pc端口判断
function iap($pid){
	$pid = intval($pid);
	  if($pid==6){
		return "1";
	  }elseif($pid==7){
		return "0";
	 }elseif($pid==4){
	 	return "-1";
	 }
}
//热门推荐
function hot($level){
	$level = intval($level);
	if($level==3){
		return "hot";
	}else{
		return "not";
	}
}

//首字母转换
function chineseFirst($str)
{

    $str= iconv("UTF-8","gb2312", $str);    //如果程序是gbk的，此行就要注释掉

    //判断字符串是否全都是中文
    if (preg_match("/^[\x7f-\xff]/", $str))
    {
        $fchar=ord($str{0});  
        if($fchar>=ord("A") and $fchar<=ord("z") )return strtoupper($str{0});
        $a = $str;
        $val=ord($a{0})*256+ord($a{1})-65536;
        if($val>=-20319 and $val<=-20284)return "A";  
        if($val>=-20283 and $val<=-19776)return "B";  
        if($val>=-19775 and $val<=-19219)return "C";  
        if($val>=-19218 and $val<=-18711)return "D";  
        if($val>=-18710 and $val<=-18527)return "E";  
        if($val>=-18526 and $val<=-18240)return "F";  
        if($val>=-18239 and $val<=-17923)return "G";  
        if($val>=-17922 and $val<=-17418)return "H";
        if($val>=-17417 and $val<=-16475)return "J";                
        if($val>=-16474 and $val<=-16213)return "K";                
        if($val>=-16212 and $val<=-15641)return "L";                
        if($val>=-15640 and $val<=-15166)return "M";                
        if($val>=-15165 and $val<=-14923)return "N";                
        if($val>=-14922 and $val<=-14915)return "O";                
        if($val>=-14914 and $val<=-14631)return "P";                
        if($val>=-14630 and $val<=-14150)return "Q";                
        if($val>=-14149 and $val<=-14091)return "R";                
        if($val>=-14090 and $val<=-13319)return "S";                
        if($val>=-13318 and $val<=-12839)return "T";                
        if($val>=-12838 and $val<=-12557)return "W";                
        if($val>=-12556 and $val<=-11848)return "X";                
        if($val>=-11847 and $val<=-11056)return "Y";                
        if($val>=-11055 and $val<=-10247)return "Z";
    } else
    {
        return false;
    }

}

?>
