<?php

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

class IAchiServer{
    const ACHI_PROTO_MONEY_EXP_CHANGED  = 0x3002;
    const ACHI_PHP_REPORT               = 0x3003;
    const ACHI_PHP_GET                  = 0x3004;
    const ACHI_PHP_SET_SHOW             = 0x3006;
    const ACHI_PHP_SET_AUTO_SHOW_TITLE  = 0x3007;
    const ACHI_PHP_IS_AUTO_SHOW_TITLE   = 0x3008;
    const ACHI_PHP_DELETE               = 0x3012;

    public function __construct(){

    }

    /*{{{ moneyChange */
    /**
     * 通知成就server 金钱变化
     * @param  uid          游戏uid
     * @param  money        变化的钱数
     */
    public function moneyExpChange( $uid, $money=0, $exp=0 )
    {
		return 0;

        $money      = "{$money}";
        $exp        = "{$exp}";
        $packet     = new ESPacket();
        $packet->WriteBegin(self::ACHI_PROTO_MONEY_EXP_CHANGED);
        $packet->WriteInt($uid);
        $packet->WriteString($exp);
        $packet->WriteString($money);
        $packet->WriteEnd();
        $request    =   $packet->GetPacketBuffer();
        $response = NetUtil::tcpLongCmd( oo::$config['AchiServer'][0], oo::$config['AchiServer'][1], $request, 2, 2, 0 , "\0", false );
        return 0;
    }

    /*{{{ reportAchiTitle */
    /**
     * 上报成就server 用户获得成就或称号
     * @param  uid          游戏uid
     * @param  type         1成就，2称号
     * @param  id                       成就or称号id
     */
    public function reportAchiTitle( $uid, $type, $id )
    {
		return 0;

        if($uid <= 0 || $type > 2 || $type < 1 || $id <= 0)
        {
                return -1;
        }

        $packet = new ESPacket();
        $packet->WriteBegin(self::ACHI_PHP_REPORT);
        $packet->WriteInt($uid);
        $packet->WriteInt($type);
        $packet->WriteInt($id);
        $packet->WriteEnd();
        $request = $packet->GetPacketBuffer();
        $response = NetUtil::tcpLongCmd( oo::$config['AchiServer'][0], oo::$config['AchiServer'][1], $request, 2,2,0, "\0", false);
        return 0;
    }

    /*{{{ delete */
    /**
     * 删除成就或称号
     * @param  uid          游戏uid
     * @param  type         1成就，2称号
     * @param  id             成就or称号id
     */
    public function delete ( $uid, $id, $type = 2 )
    {
        return 0;

        if($uid <= 0 || $type > 2 || $type < 1 || $id <= 0)
        {
                return -1;
        }

        $packet = new ESPacket();
        $packet->WriteBegin(self::ACHI_PHP_DELETE);
        $packet->WriteInt($uid);
        $packet->WriteInt($type);
        $packet->WriteInt($id);
        $packet->WriteEnd();
        $request = $packet->GetPacketBuffer();
        $response = NetUtil::tcpLongCmd( oo::$config['AchiServer'][0], oo::$config['AchiServer'][1], $request, 2,2,0, "\0", false);
        return 0;
    }

    /*{{{ getAchiTitleList */
    /**
     * 获取用户成就或称号列表
     * @param  uid          游戏uid
     * @param  type         1成就，2称号
     *
     */
    public function getAchiTitleList ( $uid, $type)
    {
        if($uid <= 0 || $type > 2 || $type < 1 )
        {
        $array['ret'] =    -1001;
        return $array;
        }

        $packet = new ESPacket();
        $packet->WriteBegin(self::ACHI_PHP_GET);
        $packet->WriteInt($uid);
        $packet->WriteInt($type);
        $packet->WriteEnd();
        $request = $packet->GetPacketBuffer();
        $response = NetUtil::tcpCmd( oo::$config['AchiServer'][0], oo::$config['AchiServer'][1], $request, 2, 2 );

        if ( empty( $response ) )
        {
            $array['ret'] =    -1001;
            return $array;
        }
        preg_match( '/ret:(\d),uid:(\d{1,}),type:(\d{1,}),defShow:(\d{0,}),count:(\d{0,}),atList:([^,]+)/', $response, $match );

        $array['ret'] = isset( $match[1] ) ? $match[1] : -11;
        $array['uid'] = isset( $match[2] ) ? $match[2] : 0;
        $array['type'] = isset ($match[3]) ? $match[3] : 0;
        $array['defShow'] = isset( $match[4]) ? $match[4] : 0;
        $array['count'] = isset( $match[5])?  $match[5] : 0;
        $array['atList'] = isset($match[6])?  explode("|",$match[6]) : array();
        return $array;
    }

    /*{{{ setShow */
    /**
     * 设置成就或称号的显示
     * @param  uid          游戏uid
     * @param  type         1成就，2称号
     * @param  id                       成就or称号id
     */
    public function setShow ( $uid, $type, $id )
    {
        if($uid <= 0 || $type > 2 || $type < 1 || $id <= 0)
        {
                return -1;
        }

        $packet = new ESPacket();
        $packet->WriteBegin(self::ACHI_PHP_SET_SHOW);
        $packet->WriteInt($uid);
        $packet->WriteInt($type);
        $packet->WriteInt($id);
        $packet->WriteEnd();
        $request = $packet->GetPacketBuffer();

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

    /*{{{ setAutoShowTitle */
    /**
     * 设置成就或称号的显示
     * @param  uid          游戏uid
     * @param  status       0不自动显示称号，1自动显示称号
     */
    public function setAutoShowTitle ( $uid, $status )
    {
        if($uid <= 0 || $status > 1 || $status < 0 )
        {
            return -1;
        }

        $packet = new ESPacket();
        $packet->WriteBegin(self::ACHI_PHP_SET_AUTO_SHOW_TITLE);
        $packet->WriteInt($uid);
        $packet->WriteInt($status);
        $packet->WriteEnd();
        $request = $packet->GetPacketBuffer();

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

    /*{{{ isAutoShowTitle */
    /**
     * 是否己设置自动显示称号
     * @param  uid          游戏uid
     */
    public function isAutoShowTitle ( $uid)
    {
        $array = array();
        if($uid <= 0)
        {
            return -1;
        }

        $packet = new ESPacket();
        $packet->WriteBegin(self::ACHI_PHP_IS_AUTO_SHOW_TITLE);
        $packet->WriteInt($uid);
        $packet->WriteEnd();
        $request = $packet->GetPacketBuffer();

        $response = NetUtil::tcpCmd( oo::$config['AchiServer'][0], oo::$config['AchiServer'][1], $request, 2, 2 );

        if ( empty( $response ) )
        {
            $array['ret'] = -1001;
            return $array;
        }
        preg_match( '/ret:(\d),uid:(\d{1,}),isAutoShowTitle:(\d{0,1})/', $response, $match );

        $array['ret'] = isset( $match[1] ) ? $match[1] : -11;
        $array['uid'] = isset( $match[2] ) ? $match[2] : 0;
        $array['status'] = isset ($match[3]) ? $match[3] : 0;
        return $array;
    }
}
