<?php
/*
		[FuShow.cn] (C) 2013-2014 HXWY Inc.
		FuShow Friend V3.0				Id: uump_audio.php
		Email <service@fushow.cn>		http://fushow.cn/
*/
if(!defined('IN_FUSHOW')) {
	exit('Access Denied');
}
include_once(S_ROOT.'./source/function_common.php');												

//发起者ID
$postid = intval($_GET['postid']);
//接受者ID
$receiveid = intval($_GET['receiveid']);
//获得域名
$domain = $_SERVER['SERVER_NAME'];
//语音判断
$audio = intval($_GET['audio']);
//当前用户ID
$supeUid = $_SGLOBAL['supe_uid'];
//接受关闭信息
$close = getstr($_GET['myClose']);
//查看接受者是否是主播
$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('major')." m JOIN ".tname('collate')." c ON m.u_id=c.u_id WHERE m.u_id='$receiveid' AND c.collated=1"),0);
//获取不是主播的语音金豆数
$rea = getreward('audio', 0);
$experience = $rea['experience'];
$reaNum = $rea['credit'];

//判断用户是否为发起者
if($supeUid==$postid) {
	//获得发起者金豆数
	$postcredit = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT credit FROM ".tname('space')." WHERE uid='$postid'"),0);
	//判断是否为主播
	if($count>=1) {
		//主播私聊金豆数
		$fee = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT m.fee FROM ".tname('major')." m JOIN ".tname('collate')." c ON m.u_id=c.u_id WHERE m.u_id='$receiveid' AND c.collated=1"),0);
		//判断金豆是否充足
		if($postcredit>=$fee) {
			$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
			$_SGLOBAL['db']->query("INSERT INTO ".tname('avcall')." (workerid,clientid,isAudio) VALUES ('$postid','$receiveid','$audio')");//插入
			//媒体开启
			include_once template("uump_audio");
		}else{
			$str = "私聊该主播将扣除".$fee."个金豆，您的金豆不足，请及时充值！";
			showmessage1($str);
		}
	}else{
		if($postcredit>=$reaNum) {
			$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
			$_SGLOBAL['db']->query("INSERT INTO ".tname('avcall')." (workerid, clientid,isAudio) VALUES ('$postid','$receiveid','$audio')");//插入
			//媒体开启
			include_once template("uump_audio");
		}else{
			$str = "当前主播每次在线私聊将扣除".$reaNum."个金豆，您的金豆不足，请及时充值！";
			showmessage1($str);
		}
	}
//判断用户是否为接受者
}elseif($supeUid==$receiveid) {
	//是否存在通讯信息
	$exist = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'"),0);
	if($exist>=1) {
		//获得发起者金豆数
		$postcredit = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT credit FROM ".tname('space')." WHERE uid='$postid'"),0);
		//判断是否为主播
		if($count>=1) {
			//主播私聊金豆数
			$fee = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT m.fee FROM ".tname('major')." m JOIN ".tname('collate')." c ON m.u_id=c.u_id WHERE m.u_id='$receiveid' AND c.collated=1"),0);
			//判断发起者金豆是否足够
			if($postcredit>=$fee) {
				//开启媒体
				include_once template("uump_audio");
				$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
				//--------------减掉对方豆---------------自身豆加----------------系统豆转换--------------------------
				$expr = getreward('expert', 0);
				$exprule =floor ($fee * (1- $expr['credit'] /100));
				$_SGLOBAL['db']->query('UPDATE '.tname('space')." SET credit=credit-$fee, experience=experience+$experience WHERE uid='$postid'");
				$_SGLOBAL['db']->query('UPDATE '.tname('space')." SET credit=credit+$exprule  WHERE uid='$receiveid'");	
			}else{
				$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
				$str = "".$postid."用户金豆不足".$fee."，无法开启视频";
				showmessage1($str);
			}
		}else{
			if($postcredit>=$reaNum) {
				//开启媒体
				include_once template("uump_audio");
				$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
				//--------------减掉对方豆---------------自身豆加-----------------------系统豆转换-------------------
				$sysrule = getreward('system', 0);
				$_SGLOBAL['db']->query('UPDATE '.tname('space')." SET credit=credit-$reaNum, experience=experience+$experience WHERE uid='$postid'");
				$_SGLOBAL['db']->query('UPDATE '.tname('space')." SET credit=credit+$reaNum-$sysrule[credit] WHERE uid='$receiveid'");
			}else{
				$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
				$str = "".$postid."用户金豆不足".$reaNum."，无法开启视频";
				showmessage1($str);
			}
		}
	}else{
				$str = "对方已关闭音频";
				showmessage1($str);
	}
}else{
	$str = "提示您没有相关权限，请联系厂商";
	showmessage1($str);
}
//关闭音频操作，删除数据库
if($close=='TheEnd') {
	$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
}
?>