<?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);
	}

	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');
                }
        }

/**
	// 合併前, 發獎 LOG 塞入資料表
	analysis_db("DROP TABLE IF EXISTS m12global_family_reward_merge_info_new");
	analysis_db("CREATE TABLE IF NOT EXISTS m12global_family_reward_merge_info_new (sid_orig integer, reward_id integer, aid integer, xluser_id text, pid_orig integer, pname_orig text, r_fid_orig integer, r_fname_orig text, fid_orig integer, sid integer, pid integer, pname text, r_fid integer, r_fname text, fid integer)");

	$files = glob("temp/m12global_s*_familywar_seasonreward.log.2nd_pre", GLOB_BRACE);
	foreach($files as $file) {
		$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_new (sid_orig, r_fid_orig, reward_id, pid_orig ) VALUES ($setID_orig, $familyID_orig, $rewardID, $playerID_orig)";
				analysis_db($sql_insert);
			}
		}
		fclose($fp);
	}

	// 合併前, 更新公會名稱
	$get_orig_set_info = analysis_db("SELECT DISTINCT sid_orig FROM m12global_family_reward_merge_info_new ORDER BY sid_orig");
	foreach($get_orig_set_info as $set_id) {
		$familys = null;
		$get_orig_family_info = analysis_db("SELECT DISTINCT r_fid_orig FROM m12global_family_reward_merge_info_new WHERE sid_orig = $set_id[sid_orig] ORDER BY r_fid_orig");
		foreach($get_orig_family_info as $family) {
			$familys .= $family['r_fid_orig'] . ",";
		}
		$familys = rtrim($familys, ",");
		echo "SET_ID: $set_id[sid_orig], FAMILY_ID: $familys\n";

		$sql = "SELECT id, name FROM family WHERE id IN ($familys)";
		$get_rs_finfo = game_db($sql, $set_id['sid_orig']);
		foreach($get_rs_finfo as $family_info) {
			$sql_update = "UPDATE m12global_family_reward_merge_info_new SET r_fname_orig = '$family_info[name]' WHERE r_fid_orig = $family_info[id] AND sid_orig = $set_id[sid_orig]";
			analysis_db($sql_update);
		}
	}

	// 合併前, 更新角色資訊
	$get_orig_set_info = analysis_db("SELECT DISTINCT sid_orig FROM m12global_family_reward_merge_info_new ORDER BY sid_orig");
	foreach($get_orig_set_info as $set_id) {
		$players = null;
		$get_orig_player_info = analysis_db("SELECT pid_orig FROM m12global_family_reward_merge_info_new WHERE sid_orig = $set_id[sid_orig] ORDER BY pid_orig");
		foreach($get_orig_player_info as $player_id) {
			$players .= $player_id['pid_orig'] . ",";
		}
		$players = rtrim($players, ",");
		echo "SET_ID: $set_id[sid_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['sid_orig']);
		foreach($get_rs_pinfo as $player_info) {
			$sql_update = "UPDATE m12global_family_reward_merge_info_new SET aid = $player_info[account_id], xluser_id = '$player_info[account_name]', pname_orig = '$player_info[given_name]', fid_orig = $player_info[family_id] WHERE pid_orig = $player_info[id] AND sid_orig = $set_id[sid_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_set_info = analysis_db("SELECT DISTINCT sid_orig FROM m12global_family_reward_merge_info_new ORDER BY sid_orig");
	foreach($get_orig_set_info as $set_id) {
		$set_id_new = $set_id_mapping[$set_id[sid_orig]];
		$sql_update = "UPDATE m12global_family_reward_merge_info_new SET sid = $set_id_new WHERE sid_orig = $set_id[sid_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_set_info = analysis_db("SELECT DISTINCT sid_orig FROM m12global_family_reward_merge_info_new ORDER BY sid_orig");
	foreach($get_orig_set_info as $set_id) {
		$familys = array();
		$get_orig_family_info = analysis_db("SELECT DISTINCT r_fid_orig, r_fname_orig FROM m12global_family_reward_merge_info_new WHERE sid_orig = $set_id[sid_orig] ORDER BY r_fid_orig");
		echo "SETID: " . $set_id['sid_orig'] . "\n";
		foreach($get_orig_family_info as $family_info) {
			array_push($familys, array($family_info['r_fid_orig'], $family_info['r_fname_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['sid_orig']]);
			foreach($get_rs_finfo as $family_info) {
				$sql_update = "UPDATE m12global_family_reward_merge_info_new SET r_fid = $family_info[id], r_fname = '$family_info[name]' WHERE r_fid_orig = $family[0] AND r_fname_orig = '$family[1]' AND sid_orig = $set_id[sid_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_set_info = analysis_db("SELECT DISTINCT sid_orig FROM m12global_family_reward_merge_info_new ORDER BY sid_orig");
	foreach($get_orig_set_info as $set_id) {
		$players = array();
		$get_orig_player_info = analysis_db("SELECT pid_orig, pname_orig, aid, xluser_id FROM m12global_family_reward_merge_info_new WHERE sid_orig = $set_id[sid_orig] ORDER BY pid_orig");
		echo "SETID: " . $set_id['sid_orig'] . "\n";
		foreach($get_orig_player_info as $player_info) {
			array_push($players, array($player_info['pid_orig'], $player_info['pname_orig'], $player_info['aid'], $player_info['xluser_id']));
		}
		print_r($players);

		foreach($players as $p) {
			$pid_like = $p[0]%100000000;
			$sql = "SELECT id, given_name, family_id FROM player_characters WHERE (id%100000000)::text LIKE '%$pid_like%' AND given_name LIKE '%$p[1]%' AND account_id = $p[2] AND account_name = '$p[3]'";
			$get_rs_finfo = game_db($sql, $set_id_mapping[$set_id['sid_orig']]);
			foreach($get_rs_finfo as $player_info) {
				$sql_update = "UPDATE m12global_family_reward_merge_info_new SET pid = $player_info[id], pname = '$player_info[given_name]', fid = $player_info[family_id] WHERE pid_orig = $p[0] AND pname_orig = '$p[1]' AND aid = $p[2] AND xluser_id = '$p[3]' AND sid_orig = $set_id[sid_orig]";
				analysis_db($sql_update);
			}
		}
	}
	
?>
