<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace loyalsoft;

/**
 *   约定好的消息类型
 */
class SysMessageType extends Enum {
       
    /**
     * 恭喜玩家X在第10关五庄观中中，获得了传奇宝石！
     */
    const GetGem_GreaterOrangeQual_MainGate = 1;
    
    /**
     * 恭喜玩家X通过扫荡第10关五庄观中，获得了传奇宝石！
     */
    const GetGem_GreaterOrangeQual_SweepMainGateIndex = 2;
    
    /**
     * 恭喜玩家X在活动中获得了传奇宝石！
     */
    const GetGem_GreaterOrangeQual_Active = 3;
    
    /**
     * 恭喜玩家X通过璀璨宝箱获得了传奇宝石！
     */
    const GetGem_GreaterOrangeQual_ShopBox = 4;
    
    /**
     * 恭喜玩家X通过合成获得了传奇宝石！
     */
    const GetGem_GreaterOrangeQual_GemCompose = 5;
}

/**
 * 系统消息对象
 */
class SysMessageModel extends Object_ext {

    /** @var SysMessageType */
    public $type;

    /** @var array 消息数据 */
    public $datas;

    /** @var int */
    public $ts;

}

/**
 * Description of SystemProc
 *
 * @author c'y'zhao
 */
class SystemProc {

    /**
     * 系统消息的过期时间(秒)
     * @var int
     */
    const SYS_MSG_OUTTS = 3600;

    /**
     * 主逻辑分发
     * @param type $req
     * @return type
     */
    static function procMain($req) {
        switch (req()->cmd) {           
            case CmdCode::cmd_system_clienttick:                                # 6901 客户端心跳包
                return self::clientTick();
            case CmdCode::cmd_system_getsysmessage:                             # 6902 拉取系统消息
                return self::getSysMessage();          
            default :
                return Resp::err(ErrCode::cmd_err);
        }
    }
    
    /**
     * [6901] 客户端心跳包
     */
    static function clientTick() {
        $timespan = now() - ctx()->base()->lastSaveTs;
  
        if ($timespan < 300) {                                                  # 间隔超过5分钟的不算
            if ($timespan < 30) {                                               # 心跳包固定30秒1次
                $timespan = 30;
            }
                  
            UserProc::updateUserInfo();
        }
        return Resp::ok();
    }  
    
    /**
     * [6902]获取系统广播消息列表
     */
    static function getSysMessage() {
        //客户端参数解析
        $ts = req()->paras[0];                                                   # 消息列表有效时间戳
        $defaultwaitcont = 60;                                                  # 暂时使用60秒作为频率,若是有压力就调大些. 若是没有问题,可以调小些.
        $itemDisplayTime = 7;                                                   # 7秒显示完一条消息

        $sysMessage = self::mem_getSysMsg_zset(req()->zoneid, $ts);

        $count = count($sysMessage) * $itemDisplayTime;                         # 计算下次取消息的时间
        if ($count < $defaultwaitcont) {                                        # 最低刷新时间
            $count = $defaultwaitcont;
        }
        $ret = array(
            'sysMessage' => $sysMessage,
            'nextFetchTime' => $count
        );

        return Resp::ok($ret);                                                  # 返回信息
    }
    
    /**
     * 取数据
     * @param int $zoneid
     * @return array
     */
    private static function mem_getSysMsg_zset($zoneid, $ts) {
        $ret = gMem()->zrangebyscore(MemKey_GameRun::Game_SysMsgByZone_zset($zoneid), $ts, now(+1));
        //todo:目前是在这里手动解码的, 应该移动到CRedisUtil里面去.
        $arr = ArrayInit();
        if ($ret) {
            foreach ($ret as $msg) {
                $arr[] = JsonUtil::decode($msg);
            }
        }
        return $arr;
    }

    /**
     * 回写数据
     * @param int $zoneid
     * @param SysMessageModel $value
     */
    private static function mem_addSysMsg_zset($zoneid, $value) {
        $ts = $value->ts;
        $key = MemKey_GameRun::Game_SysMsgByZone_zset($zoneid);
        gMem()->zadd($key, array(json_encode($value) => $ts));                  # 插入消息
        gMem()->zremrangebyscore($key, 0, $ts - self::SYS_MSG_OUTTS);           # 清理过期数据
    }
    
    /**
     * 推送系统消息
     * @param int $zoneid 分区
     * @param SysMessageType $type enum
     * @param array $datas
     */
    static function pushSysMessage($zoneid, $type, $datas) {
        $newMsg = new SysMessageModel(array(#                                   # 添加新系统消息
            'type' => $type,
            'datas' => $datas,
            'ts' => now()
        ));
        self::mem_addSysMsg_zset($zoneid, $newMsg);                             # 写入mem
    }
    
    //----下面是广播信息
      
    /**
     * 恭喜玩家X在第10关五庄观中中，获得了传奇宝石！
     * @param type $zoneid
     * @param type $name
     * @param type $gemId
     */
    static function GetGem_GreaterOrangeQual_MainGate($zoneid, $name,$gateIndex,$gateName,$gemId) {
        $mo = GameConfig::gem_getItem($gemId);
        if ($mo->qual >= 5) {            
            $arr = explode(';', glc()->GemQualName); 
            $gemName = "";
            foreach ($arr as $val) {
                $strList = explode(':', $val);              
                if($strList[0] == $mo->qual){
                    $gemName = $strList[1];
                    break;
                }
            }
                        
            $desc = "在第".$gateIndex."关".$gateName."中";
            self::pushSysMessage($zoneid, SysMessageType::GetGem_GreaterOrangeQual_MainGate, array(
                $name,$desc,$gemName,$mo->qual
            ));
        }        
    }
    
    /**
     * 恭喜玩家X通过扫荡第 [10关五庄观] 中，获得了传奇宝石！
     * @param type $zoneid
     * @param type $name
     * @param type $gemId
     */
    static function GetGem_GreaterOrangeQual_SweepMainGateIndex($zoneid, $name,$gateIndex,$gateName, $gemId) {
        $mo = GameConfig::gem_getItem($gemId);
        if ($mo->qual >= 5) {            
            $arr = explode(';', glc()->GemQualName); 
            $gemName = "";
            foreach ($arr as $val) {
                $strList = explode(':', $val);              
                if($strList[0] == $mo->qual){
                    $gemName = $strList[1];
                    break;
                }
            }
            $desc = "通过扫荡第".$gateIndex."关".$gateName;            
            self::pushSysMessage($zoneid, SysMessageType::GetGem_GreaterOrangeQual_SweepMainGateIndex, array(
                $name,$desc,$gemName,$mo->qual
            ));
        } 
                      
    }
    
    /**
     * 恭喜玩家X在活动中获得了传奇宝石！
     * @param type $zoneid
     * @param type $name
     * @param type $gemId
     */
    static function GetGem_GreaterOrangeQual_Active($zoneid, $name, $gemId) {
        $mo = GameConfig::gem_getItem($gemId);
        if ($mo->qual >= 5) {
            $arr = explode(';', glc()->GemQualName); 
            $gemName = "";
            foreach ($arr as $val) {
                $strList = explode(':', $val);              
                if($strList[0] == $mo->qual){
                    $gemName = $strList[1];
                    break;
                }
            }
                        
            self::pushSysMessage($zoneid, SysMessageType::GetGem_GreaterOrangeQual_Active, array(
                $name,$gemName,$mo->qual
            ));
        }        
    }
    
    /**
     * 恭喜玩家X通过璀璨宝箱获得了传奇宝石！
     * @param type $zoneid
     * @param type $name
     * @param type $gemId
     */
    static function GetGem_GreaterOrangeQual_ShopBox($zoneid, $name, $gemId) {
        $mo = GameConfig::gem_getItem($gemId);
        if ($mo->qual >= 5) {
            $arr = explode(';', glc()->GemQualName); 
            $gemName = "";
            foreach ($arr as $val) {
                $strList = explode(':', $val);              
                if($strList[0] == $mo->qual){
                    $gemName = $strList[1];
                    break;
                }
            }
            self::pushSysMessage($zoneid, SysMessageType::GetGem_GreaterOrangeQual_ShopBox, array(
                $name,$gemName,$mo->qual
            ));
        }        
    }
    
    /**
     * 恭喜玩家X通过合成获得了传奇宝石！
     * @param type $zoneid
     * @param type $name
     * @param type $gemId
     */
    static function GetGem_GreaterOrangeQual_GemCompose($zoneid, $name, $gemId) {
        $mo = GameConfig::gem_getItem($gemId);
        $qual = $mo->qual;
        if ($qual >= 5) {
            $arr = explode(';', glc()->GemQualName); 
            $gemName = "";
            foreach ($arr as $val) {
                $strList = explode(':', $val);              
                if($strList[0] == $qual){
                    $gemName = $strList[1];
                    break;
                }
            }
            self::pushSysMessage($zoneid, SysMessageType::GetGem_GreaterOrangeQual_GemCompose, array(
                $name,$gemName,$mo->qual
            ));
        }        
    }
    
    
//    /**
//     * 得到S级角色
//     * @param type $zoneid
//     * @param type $name
//     * @param type $weaponTypeId
//     */
//    static function GetHero_S($zoneid, $name, $heroId) {
//        self::pushSysMessage($zoneid, SysMessageType::GetSpecialQualityWeapon, array(
//                $name, $color, $mo->name
//            ));
//    }

}
