<?php

namespace loyalsoft;

/**
 * Description of StatisticsProc
 *
 * @author cyzhao
 */
class StatisticsProc {

    /**
     * 已经完成新手引导的玩家记录
     * @param type $uid
     * @param type $index
     */
    static function NewbieGuideRecord($uid, $index) {
        if ($index == 2) {//现在游戏新手引导就2步
            $key = "NewbieGuideRecord-" . req()->zoneid;
            gMem()->lpush($key, $uid);
        }
    }

    /**
     * 玩家自己任务完成统计-----每日任务进度变化--主线，学院，日程，悬赏的任务全记录在这
     * @param type $id
     */
    static function SelfTaskDataCollect($taskId) {
        $task = $taskId . "-" . time();

        $arr = array();
        $arr[] = $task;

        $num = gMem()->rpush("SelfTaskDataCollect-" . req()->uid . "-" . req()->zoneid, $arr);
    }

    /**
     * 全区：记录uid-主线任务id,玩家做任务更新id,以获取最高任务--------主线，学院，日程，悬赏的任务全记录在这
     * @param type $taskId
     */
    static function TaskDataCollect($taskId) {
        $taskMo = GameConfig::item_taskcard_getItem($taskId);
        if ($taskMo->type == 1) {//主线
            $key = "TaskDataCollect-" . req()->zoneid;
            gMem()->hset($key, req()->uid, $taskId);
        } else {//主线任务外其他的
            $key = "TaskDataCollectOther-" . req()->zoneid;
            gMem()->hset($key, req()->uid, $taskId);
        }
    }

    /**
     * 全区--玩家解锁课程进度:uid-courseId
     * @param type $courseId
     */
    static function UserCourseIngRecord($courseId) {
        $key = "UserCourseIngRecord-" . req()->zoneid;
        gMem()->hset($key, req()->uid, $courseId);
    }

    /*
     * 统计全区------玩家最新解锁建筑
     */

    static function unlockBuild($buildId) {
        $key = "unlockBuild-" . req()->zoneid;
        gMem()->hset($key, req()->uid, $buildId);
    }

    /**
     * 统计全区------玩家最新解锁关卡
     * @param type $gateId
     */
    static function unlockGate($gateId) {
        $key = "unlockGate-" . req()->zoneid;
        gMem()->hset($key, req()->uid, $gateId);
    }

    /*
     * 某言灵的拥有人数
     */

    static function yanling_UserNumRecord($yanlingId) {
        $key = "yanling_UserNumRecord-" . req()->zoneid;
        $arr = array();
        if (gMem()->hexists($key, $yanlingId)) {
            $arr = gMem()->hget($key, $yanlingId);
        }
        if (!in_array(req()->uid, $arr)) {
            $arr[] = req()->uid;
        }

        gMem()->hset($key, $yanlingId, $arr);
    }

    /**
     * $yanlingId
     * @param type $yanlingId
     * @param type $type 1 代表装备(替换)//0代表卸载
     */
    static function yanlingEquipRecord($yanlingId, $type) {
        $key = "yanlingEquipRecord-" . req()->zoneid;
        $arr = array();
        if (gMem()->hexists($key, $yanlingId)) {
            $arr = gMem()->hget($key, $yanlingId);
        }
        if ($type == 1) {
            $arr[] = req()->uid;
            gMem()->hset($key, $yanlingId, req()->uid);
        } else {
            if (in_array(req()->uid, $arr)) {
                $arrNew = array();
                foreach ($arr as $uid) {
                    if ($uid == req()->uid) {
                        continue;
                    }
                    $arrNew[] = $uid;
                }
                gMem()->hset($key, $yanlingId, req()->uid);
            }
        }
    }

    static function dailyTaskInit() {
        $list = GameConfig::item_taskcard_type_getItemArray(2);
        if ($list == null || count($list) == 0) {
            return;
        }
        $key = "TargetStatistics-" . req()->zoneid;
        foreach ($list as $value) {
            $str = "dailyTaskId_ComUserNum-" . $value->typeId;
            if (gMem()->hexists($key, $str)) {
                break;
            }
            gMem()->hset($key, $str, 0);
        }
    }

    /**
     * 
     * @param type $type
     * @param type $id--id
     * @param type $ctx 当前指标
     * @param type $lastCtx 前一个指标 需要减一
     */
    static function TargetStatistics($type, $id = 0, $ctx = "0", $lastCtx = 0) {
        $key = "TargetStatistics-" . req()->zoneid;

        $specialArr = array();
        switch ($type) {
            case Enum_TargetStatistics::registerUserNum:
                $field = "registerUserNum";
                break;
            case Enum_TargetStatistics::comNewGuide_UserNum:
                $field = "comNewGuideUserNum";
                break;
            case Enum_TargetStatistics::unlockbuidId:
                $field = "unlockbuidId-" . $id;
                break;
            case Enum_TargetStatistics::mainTaskId_ComUserNum:
                $field = "mainTaskId_ComUserNum-" . $id;
                break;
            case Enum_TargetStatistics::shopTaskId_ComUserNum:
                $field = "shopTaskId_ComUserNum-" . $id;
                break;
            case Enum_TargetStatistics::shopTaskId_BuyUserNum:
                $field = "shopTaskId_BuyUserNum-" . $id;
                break;
            case Enum_TargetStatistics::collegeTaskId_ComUserNum:
                $field = "collegeTaskId_ComUserNum-" . $id;
                break;
            case Enum_TargetStatistics::collegeCouseId_UnlockUserNum:
                $field = "collegeCouseId_UnlockUserNum-" . $id;
                break;

            case Enum_TargetStatistics::dailyTaskId_ComUserNum:
                $field = "dailyTaskId_ComUserNum-" . $id;
                break;

            case Enum_TargetStatistics::storyGateId_ComUserNum:
                $field = "storyGateId_ComUserNum-" . $id;
                break;
            case Enum_TargetStatistics::storyGateId_BattleNum:
                $field = "storyGateId_BattleNum-" . $id;
                $specialArr[] = Enum_TargetStatistics::storyGateId_BattleNum;
                break;
            case Enum_TargetStatistics::HuanLingShiGateId_ComUserNum:
                $field = "HuanLingShiGateId_ComUserNum-" . $id;
                break;
            case Enum_TargetStatistics::HuanLingShiGateId_BattleNum:
                $field = "HuanLingShiGateId_BattleNum-" . $id;
                $specialArr[] = Enum_TargetStatistics::HuanLingShiGateId_BattleNum;
                break;

            case Enum_TargetStatistics::WeaponGateId_ComUserNum:
                $field = "WeaponGateId_ComUserNum-" . $id;
                break;
            case Enum_TargetStatistics::WeaponGateId_BattleNum:
                $field = "WeaponGateId_BattleNum-" . $id;
                $specialArr[] = Enum_TargetStatistics::WeaponGateId_BattleNum;
                break;
            case Enum_TargetStatistics::YanLingGateId_ComUserNum:
                $field = "YanLingGateId_ComUserNum-" . $id;
                break;
            case Enum_TargetStatistics::YanLingGateId_BattleNum:
                $field = "YanLingGateId_BattleNum-" . $id;
                $specialArr[] = Enum_TargetStatistics::YanLingGateId_BattleNum;
                break;

            case Enum_TargetStatistics::userlevel:
                $field = "userlevel-" . $id;
                $fieldNew = "userlevel-" . $lastCtx;
                break;

            case Enum_TargetStatistics::newUserLotteryDraw_one:
                $field = "newUserLotteryDraw_one";
                break;
            case Enum_TargetStatistics::newUserLotteryDraw_ten:
                $field = "newUserLotteryDraw_ten";
                break;

            case Enum_TargetStatistics::yanLingLotteryDraw_one:
                $field = "yanLingLotteryDraw_one";
                break;

            case Enum_TargetStatistics::yanLingLotteryDraw_ten:
                $field = "yanLingLotteryDraw_ten";
                break;
            case Enum_TargetStatistics::weaponLotteryDraw_one:
                $field = "weaponLotteryDraw_one";
                break;

            case Enum_TargetStatistics::weaponLotteryDraw_ten:
                $field = "weaponLotteryDraw_ten";
                break;

            case Enum_TargetStatistics::commonLotteryDraw_one:
                $field = "commonLotteryDraw_one";
                break;

            case Enum_TargetStatistics::commonLotteryDraw_ten:
                $field = "commonLotteryDraw_ten";
                break;
            case Enum_TargetStatistics::HuanLingShiLevel_UserNum:
                $field = "HuanLingShiLevel_UserNum-" . $id . "-level" . $ctx;
                $fieldNew = "HuanLingShiLevel_UserNum-" . $id . "-level" . $lastCtx;
                break;
            case Enum_TargetStatistics::HuanLingShiId_UserNum:
                $field = "HuanLingShiId_UserNum-" . $id;
                break;
            case Enum_TargetStatistics::YanLingId_UserNum:
                $field = "YanLingId_UserNum-" . $id;
                break;
            case Enum_TargetStatistics::YanLingIdLevel_UserNum:
                $field = "YanLingIdLevel_UserNum-" . $id . "-level" . $ctx;
                $fieldNew = "YanLingIdLevel_UserNum-" . $id . "-level" . $lastCtx;
                break;
            case Enum_TargetStatistics::WeaponId_UserNum:
                $field = "WeaponId_UserNum-" . $id;
                break;
            case Enum_TargetStatistics::WeaponIdLevel_UserNum:
                $field = "WeaponIdLevel_UserNum-" . $id . "-level" . $ctx;
                $fieldNew = "WeaponIdLevel_UserNum-" . $id . "-level" . $lastCtx;
                break;
            default:
                break;
        }

        if (in_array($type, $specialArr)) {
            $val = gMem()->hget($key, $field);
            if ($val == null) {
                $val = $ctx;
            } else {
                $vlist = explode('/', $val);
                $clist = explode('/', $ctx);

                $n1 = $vlist[0] + $clist[0];
                $n2 = $vlist[1] + $clist[1];
                $val = $n1 . "/" . $n2;
            }
            gMem()->hset($key, $field, $val);
        } else {
            if ($lastCtx != null) {
                $count = gMem()->hget($key, $fieldNew);
                if ($count != null) {
                    $count -= 1;
                    if ($count <= 0) {
                        $count = 0;
                    }
                    gMem()->hset($key, $field, $count);
                }
            }

//            $count = gMem()->hget($key,$field);
//            if($count == null){
//                $count = 0;
//            }  
//            gMem()->hset($key,$field,$count+1); 
            gMem()->hincrby($key, $field, 1);
        }
    }

}
