<?php

require_once( PATH_LIB . 'cserver/ESPacket.php' );
require_once( PATH_LIB . 'cserver/NetUtil.php' );

class IBroadCastServer {

	/** 广播协议命令字 START **/
	const NEW_BROADSERVER_TO_All = 0x7852;		//php进行全服广播 ( PHP->B )
	const NEW_BROADSERVER_TO_USER = 0x7600;		//php进行特定用户单播( PHP->B )
	const MONEYCHANGE = 0x1007;					//用户属性相关
	const PROPSCHANGE = 0x1008;					//活动道具相关
	const NEWSCHANGE  = 0x1009;					//消息相关
	const TASKACHCHANGE	= 0x1010;   			//任务成就
	const INVITEFRIPLAYING	= 0x1011;   		//好友邀请打牌
	const BROAD_CAST_PAY_SUCC = 0x100D;   		//支付成功
	const MATCH_USERREWARD = 0x2001;			//比赛场: 玩家领奖通知
	const BROADSERVER_TO_ALL = 0x2006;			//大喇叭
	const BROADWCUPPOOLCHIPS_TO_ALL = 0x2007;	//世界杯奖金池
	const BROADPOKER_RWD = 0x1013;	            //玩牌领奖通知
	const BROADUSER_HEADIMG = 0x1014;	            //修改头像通知
	/** 广播协议命令字 END **/

	public function __construct() {
		
	}

	/** php进行特定用户单播( PHP->B )
	 * uid  : int(用户ID)
	 * 	info : array(用户个人数据)
	 * 	新广播server接口
	 * */
	static function newBroadToUser($uid, $type, $info = array()) {
		if (empty($type)) { // 空广播不发
			return true;
		}

		$packet = new ESPacket();
		$packet->WriteBegin(self::NEW_BROADSERVER_TO_USER);
		$packet->WriteInt($uid);
		$packet->WriteInt($type);
		$packet->WriteString(json_encode($info));
		$packet->WriteEnd();
		$request = $packet->GetPacketBuffer();
		!PRODUCTION_SERVER && oo::logs()->debug2($uid.'|'.oo::$config['BroadServer'][0].'|'. oo::$config['BroadServer'][1] . "|" .$type.'|'. json_encode($info), 'newBroadToUser.log');

		NetUtil::tcpLongCmd(oo::$config['BroadServer'][0], oo::$config['BroadServer'][1], $request, 2, 2, 0, "\0", false);
		return 0;
	}


	/** php进行特定用户单播( PHP->B )
	 * uid  : int(用户ID)
	 * 	info : array(用户个人数据)
	 * 	新广播server接口
	 * */
	static function broadToUser($uid, $type, $info = '') {
		if (empty($type)) { // 空广播不发
			return true;
		}
		$packet = new ESPacket();
		$packet->WriteBegin(self::NEW_BROADSERVER_TO_USER);
		$packet->WriteInt($uid);
		$packet->WriteInt($type);
		$packet->WriteString($info);
		$packet->WriteEnd();
		$request = $packet->GetPacketBuffer();
		!PRODUCTION_SERVER && oo::logs()->debug2($uid.'|'.oo::$config['BroadServer'][0].'|'. oo::$config['BroadServer'][1] . "|" .$type.'|'. json_encode($info), 'newBroadToUser.log');

		NetUtil::tcpLongCmd(oo::$config['BroadServer'][0], oo::$config['BroadServer'][1], $request, 2, 2, 0, "\0", false);
		return 0;
	}

	/** 
	*   php进行全服广播 ( PHP->B )
	* 	msg : string 内容
	*/
	static function newBroadToAll($msg) {
		if (empty($msg)) { // 空广播不发
			return true;
		}
		$packet = new ESPacket();
		$packet->WriteBegin(self::NEW_BROADSERVER_TO_All);
		$packet->WriteString("");
		$packet->WriteString($msg);
		$packet->WriteString("");
		$packet->WriteString("");
		$packet->WriteEnd();
		$request = $packet->GetPacketBuffer();
		NetUtil::tcpLongCmd(oo::$config['BroadServer'][0], oo::$config['BroadServer'][1], $request, 2, 2, 0, "\0", false);
		return 0;
	}

	/* notifySeatExp */

	/**
	 * 通知用户坐下加经验
	 * @param  uid          游戏uid
	 * @param  experience   需要增加的经验 > 0
	 */
	public function notifySeatExp($uid, $exp = 0) {
		
		$type = 0x5001; //通知用户坐下加经验
		$arr['exp'] = $exp;
		self::newBroadToUser($uid, $type, $arr);
		return 0;
	}

	/**
	*	用户属性变化, 包括金币,经验,钻石等
	*	该方法目前只通知金币变化的广播通知
	*/
	static public function notifyPropertyChange($uid, $array = array() ) {
		self::newBroadToUser($uid, self::MONEYCHANGE, $array);
		return 0;
	}

	/**
	*	玩家道具变化通知
	*	array( id => count ) 道具ID => 改变的数量
	*/
	static public function notifyPropsChange($uid, $array = array() )
	{
		self::newBroadToUser($uid, self::PROPSCHANGE, $array);
		return 0;
	}

	/**
	*	大转盘
	*/
	static public function notifyBigwheel($uid, $array = array() )
	{
		
	}

	/**
	*	支付成功
	*/
	static public function notifyPaysucc($uid, $array = array() )
	{
		self::newBroadToUser($uid, self::BROAD_CAST_PAY_SUCC, $array);
		return 0;
	}

	/**
	*	消息相关
	*/
	static public function notifyNews($uid, $array = array())
	{
		self::newBroadToUser($uid, self::NEWSCHANGE, $array);
		return 0;
	}

	/**
	*	成就和任务相关
	*/
	static public function notifyTaskAch($uid, $array = array())
	{
		self::newBroadToUser($uid, self::TASKACHCHANGE, $array);
		return 0;
	}


	/**
	*	比赛场 玩家领奖
	*/
	static public function notifyMatchUserReward($uid, $array = array())
	{
		self::newBroadToUser($uid, self::MATCH_USERREWARD, $array);
		return 0;
	}
	
	/**
	*	喇叭
	*/
	static public function sendSpeaker($msg, $uid = 0, $nick = 'System')
	{
		$data['type'] = self::BROADSERVER_TO_ALL;
        $data['uid'] = $uid;
        $data['nick'] = $nick;
		$data['msg'] = $msg;
		$json = json_encode($data);
		self::newBroadToAll($json);
		return 0;
	}

	/**
	*	世界杯活动奖金池通知
	*/
	static public function noticeWCupPoolChip($list = [])
	{
	    if(empty($list) || !is_array($list)) {
	        return false;
        }
		$data['type'] = self::BROADWCUPPOOLCHIPS_TO_ALL;
        $data['data'] = $list;
		$json = json_encode($data);
		self::newBroadToAll($json);
		return true;
	}


	/**
	*	玩牌活动领奖通知
	*/
	static public function noticePokerRWD($uid, $array = [])
	{
        self::newBroadToUser($uid, self::BROADPOKER_RWD, $array);
        return 0;
	}

	/**
	*	玩牌活动领奖通知
	*/
	static public function noticeUserHeadImg($uid, $array = [])
	{
	    if(empty($array)){
	        return false;
        }
        self::newBroadToUser($uid, self::BROADUSER_HEADIMG, $array);
        return 0;
	}



	/**
	*	邀请好友打牌广播
	*/
	static public function notifyInviteFriPlaying($uid, $str)
	{
		self::broadToUser($uid, self::INVITEFRIPLAYING, $str);
		return 0;
	}

}
