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

	// 重新輸出公會戰發獎清單 (合併後)
	$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 "(BEFORE) SID:$setID_orig,FID:$familyID_orig,RID:$rewardID,PID:$playerID_orig";
				$sql_select = "SELECT sid,r_fid,reward_id,pid FROM m12global_family_reward_merge_info_new WHERE sid_orig = $setID_orig AND r_fid_orig = $familyID_orig AND reward_id = $rewardID AND pid_orig = $playerID_orig";
				$pinfo_merge_info = analysis_db($sql_select);
				print_r($pinfo_merge_info);
				foreach($pinfo_merge_info as $pinfo){
					echo "(AFTER): SID:$pinfo[sid],FID:$pinfo[r_fid],RID:$pinfo[reward_id],PID:$pinfo[pid]";
					////$new_file = str_replace("_pre","",$file);
					$new_file = "temp/m12global_s".$pinfo['sid']."_familywar_seasonreward.log.2nd";
					$fpn = fopen($new_file,"a");
					echo fwrite($fpn,"$pinfo[r_fid],$pinfo[reward_id],$pinfo[pid]\n");
					fclose($fpn);
				}
			}
		}
		fclose($fp);
	}

?>
