<?php

	# WebTool Database connection setup.
	$GLOBALS['webtooldb_name'] = 'WebTool';
	$GLOBALS['webtooldb_ip'] = '10.8.16.3';
	$GLOBALS['webtooldb_user'] = 'webtool';
	$GLOBALS['webtooldb_pass'] = 'XAE>)78b=UM.$~xp';

	function analysis_db( $sql ) {
		$db = new PDO("pgsql:dbname=analysis;host=10.8.16.2", "spiritking", "2DH93@d{COw8Sr_C" );
		$rs = $db->query($sql);
		return $rs->fetchall(PDO::FETCH_ASSOC);
		//if(is_object($rs)){
		//	return $rs->fetchall(PDO::FETCH_ASSOC);
		//}
	}

	function webtool_db($sql_in, $old_mode = 0){
		$dbname = $GLOBALS['webtooldb_name'];
		$dbip = $GLOBALS['webtooldb_ip'];
		$dbuser = $GLOBALS['webtooldb_user'];
		$dbpass = $GLOBALS['webtooldb_pass'];
		$rs = pg_connect("host=$dbip port=5432 dbname=$dbname user=$dbuser password=$dbpass");
		$data = pg_query($rs, $sql_in);
		if(!$data){
		   echo "SQL ". pg_last_error(). '<br>';
		   echo "Query = $sql_in";
		}
		if($old_mode == 0){
		   return pg_fetch_all($data);
		}else{
		   $result[0] = $data;
		   $result[1] = pg_num_rows($data);
		   return $result;
		}
	}

        function game_db($sql, $set_id){
                $start_time = microtime(true);
                if(is_numeric($set_id)){
                        $connection_data = webtool_db("select game_db_name, game_db_ip, game_db_account, game_db_pass from db_setup where set_id=$set_id");
                        if($connection_data[0]['game_db_name']){
                                $conn = "host={$connection_data[0][game_db_ip]} port=5432 dbname={$connection_data[0][game_db_name]} user={$connection_data[0][game_db_account]} password={$connection_data[0][game_db_pass]}";
                                $link = pg_connect($conn);
                                $data = pg_query($link, $sql);
                                if(!$data){
                                        echo "SQL ". pg_last_error(). '<br>';
                                        echo "Query = $sql";
                                }
                                $end_time = microtime(true) - $start_time;
                                $_SESSION['game_db_time_takend'] += $end_time;
                                if($end_time >= 1){
                                        $sql_to_log = pg_escape_string($sql);
                                        //webtool_db("insert into slow_queries_log (target_db, sql, exec_time) values('game_db', '$sql_to_log', $end_time)");
                                }
                                return pg_fetch_all($data);
                        }
                }else{
                        d('收到不正確的 set_id');
                }
        }

/**
	analysis_db("CREATE TABLE IF NOT EXISTS m12global_family_reward_merge_info (set_id_orig integer, family_id_orig integer, reward_id integer, player_id_orig integer, family_name_orig text, player_name_orig text, family_id_orig_real integer, account_id integer, xluser_id text, set_id integer, family_id integer, player_id integer, family_id_real integer)");

	$insert_sql = null;
	$files = glob("temp/m12global_s*_familywar_seasonreward.log.2nd_pre", GLOB_BRACE);
	foreach($files as $file) {
		//echo "$file\n";
		$setID_orig = str_replace(array("temp/m12global_s","_familywar_seasonreward.log.2nd_pre"),"", $file);
		$fp = fopen($file,"r");
		while(!feof($fp)){
			list($familyID_orig,$rewardID,$playerID_orig) = split(",",fgets($fp));
			if($playerID_orig) {
				echo "$setID_orig,$familyID_orig,$rewardID,$playerID_orig";
				$sql_insert = "INSERT INTO m12global_family_reward_merge_info (set_id_orig, family_id_orig, reward_id, player_id_orig ) VALUES ($setID_orig, $familyID_orig, $rewardID, $playerID_orig)";
				analysis_db($sql_insert);
			}
		}
		fclose($fp);
	}
**/

/**
	//analysis_db("CREATE TABLE IF NOT EXISTS m12global_family_info_orig_merge (family_id integer, family_name text)");
	$get_orig_setid_info = analysis_db("SELECT DISTINCT set_id_orig FROM m12global_family_reward_merge_info ORDER BY set_id_orig");
	//print_r($get_orig_setid_info);

	foreach($get_orig_setid_info as $set_id) {
		$familys = null;
		$get_orig_familyid_info = analysis_db("SELECT DISTINCT family_id_orig FROM m12global_family_reward_merge_info WHERE set_id_orig = $set_id[set_id_orig] ORDER BY family_id_orig");
		//print_r($get_orig_familyid_info);
		foreach($get_orig_familyid_info as $family_id) {
			$familys .= $family_id['family_id_orig'] . ",";
		}
		$familys = rtrim($familys, ",");
		echo "SET_ID: $set_id[set_id_orig], FAMILY_ID: $familys\n";

		$sql = "SELECT id, name FROM family WHERE id IN ($familys)";
		$get_rs_finfo = game_db($sql, $set_id[set_id_orig]);
		//print_r($get_rs_finfo);
		foreach($get_rs_finfo as $family_info) {
			$sql_update = "UPDATE m12global_family_reward_merge_info SET family_name_orig = '$family_info[name]' WHERE family_id_orig = $family_info[id] AND set_id_orig = $set_id[set_id_orig]";
			analysis_db($sql_update);
		}
	}
**/

/**
	//analysis_db("CREATE TABLE IF NOT EXISTS m12global_player_info_orig_merge (account_id integer, account_name text, player_id integer, player_name text)");
	$get_orig_setid_info = analysis_db("SELECT DISTINCT set_id_orig FROM m12global_family_reward_merge_info ORDER BY set_id_orig");
	foreach($get_orig_setid_info as $set_id) {
		$players = null;
		$get_orig_playerid_info = analysis_db("SELECT player_id_orig FROM m12global_family_reward_merge_info WHERE set_id_orig = $set_id[set_id_orig] ORDER BY player_id_orig");

		//print_r($get_orig_playerid_info);
		foreach($get_orig_playerid_info as $player_id) {
			$players .= $player_id['player_id_orig'] . ",";
		}
		$players = rtrim($players, ",");
		echo "SET_ID: $set_id[set_id_orig], PLAYER_ID: $players\n";

		$sql = "SELECT account_id, account_name, id, given_name, family_id FROM player_characters WHERE id IN ($players)";
		$get_rs_pinfo = game_db($sql, $set_id[set_id_orig]);
		//print_r($get_rs_pinfo);
		foreach($get_rs_pinfo as $player_info) {
			$sql_update = "UPDATE m12global_family_reward_merge_info SET account_id = $player_info[account_id], xluser_id = '$player_info[account_name]', player_name_orig = '$player_info[given_name]', family_id_orig_real = $player_info[family_id] WHERE player_id_orig = $player_info[id] AND set_id_orig = $set_id[set_id_orig]";
			analysis_db($sql_update);
		}
	}
**/

/**
	$set_id_mapping = array('1'=>'1','2'=>'2','3'=>'3','4'=>'1','5'=>'5','6'=>'2','7'=>'3','8'=>'5','11'=>'1','14'=>'20','19'=>'5','20'=>'20','21'=>'20');
	$get_orig_setid_info = analysis_db("SELECT DISTINCT set_id_orig FROM m12global_family_reward_merge_info ORDER BY set_id_orig");
	foreach($get_orig_setid_info as $set_id) {
		$set_id_new = $set_id_mapping[$set_id[set_id_orig]];
		$sql_update = "UPDATE m12global_family_reward_merge_info SET set_id = $set_id_new WHERE set_id_orig = $set_id[set_id_orig]";
		analysis_db($sql_update);
	}
**/	

	$set_id_mapping = array('1'=>'1','2'=>'2','3'=>'3','4'=>'1','5'=>'5','6'=>'2','7'=>'3','8'=>'5','11'=>'1','14'=>'20','19'=>'5','20'=>'20','21'=>'20');
	$get_orig_setid_info = analysis_db("SELECT DISTINCT set_id_orig FROM m12global_family_reward_merge_info ORDER BY set_id_orig");
	foreach($get_orig_setid_info as $set_id) {
		$familys = array();
		$get_orig_familyid_info = analysis_db("SELECT DISTINCT family_id_orig, family_name_orig FROM m12global_family_reward_merge_info WHERE set_id_orig = $set_id[set_id_orig] ORDER BY family_id_orig");
		echo "SETID: " . $set_id['set_id_orig'] . "\n";
		//print_r($get_orig_familyid_info);
		foreach($get_orig_familyid_info as $family_info) {
			array_push($familys, array($family_info['family_id_orig'], $family_info['family_name_orig']));
		}
		print_r($familys);

		foreach($familys as $family) {
			$sql = "SELECT id, name FROM family WHERE id%100000000 = $family[0] AND name LIKE '%$family[1]%'";
			$get_rs_finfo = game_db($sql, $set_id_mapping[$set_id['set_id_orig']]);
			foreach($get_rs_finfo as $family_info) {
				$sql_update = "UPDATE m12global_family_reward_merge_info SET family_id = $family_info[id], family_name = '$family_info[name]' WHERE family_id_orig = $family[0] AND family_name_orig = '$family[1]' AND set_id_orig = $set_id[set_id_orig]";
				analysis_db($sql_update);
			}
		}
	}

	$set_id_mapping = array('1'=>'1','2'=>'2','3'=>'3','4'=>'1','5'=>'5','6'=>'2','7'=>'3','8'=>'5','11'=>'1','14'=>'20','19'=>'5','20'=>'20','21'=>'20');
	$get_orig_setid_info = analysis_db("SELECT DISTINCT set_id_orig FROM m12global_family_reward_merge_info ORDER BY set_id_orig");
	foreach($get_orig_setid_info as $set_id) {
		$players = array();
		$get_orig_player_info = analysis_db("SELECT player_id_orig, player_name_orig, account_id FROM m12global_family_reward_merge_info WHERE set_id_orig = $set_id[set_id_orig] ORDER BY player_id_orig");
		echo "SETID: " . $set_id['set_id_orig'] . "\n";
		//print_r($get_orig_playerid_info);
		foreach($get_orig_player_info as $player_info) {
			array_push($players, array($player_info['player_id_orig'], $player_info['player_name_orig'], $player_info['account_id']));
		}
		print_r($players);

		foreach($players as $p) {
			$sql = "SELECT id, given_name, family_id FROM player_characters WHERE id::text like '%$p[0]%' and given_name like '%$p[1]%' and account_id = $p[2]";
			$get_rs_finfo = game_db($sql, $set_id_mapping[$set_id['set_id_orig']]);
			foreach($get_rs_finfo as $player_info) {
				$sql_update = "UPDATE m12global_family_reward_merge_info SET player_id = $player_info[id], player_name = '$player_info[given_name]', family_id_real = $player_info[family_id] WHERE player_id_orig = $p[0] AND player_name_orig = '$p[1]' AND account_id = $p[2] AND set_id_orig = $set_id[set_id_orig]";
				analysis_db($sql_update);
			}
		}
	}
	
?>
