<?php
namespace loyalsoft;
/* 
 * 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.
 */

include_once __DIR__ . '/../../main.php';

taskDeal();
//textInterface();
function taskDeal() {        
    $db = daoInst();
    $zoneid = 1;
    $index = 0;
    $size = 20;
    $page = 0;
    
    $userUidArr = array();
    while (true) {
        $page = $index * $size;
        $sql = "select * from tab_rolename WHERE zoneid= $zoneid limit %d,%d";             
        $queryStr = sprintf($sql, $page, $size);
        $result = $db->query($queryStr)->fetchAll();
        
        if(count($result) == 0){
            break;
        }
                       
        foreach ($result as $arr) {
            foreach ($arr as $k=>$item) {
                if($k == "userID" && !in_array($item, $userUidArr)){
                    $userUidArr[] = $item;
                }
             
            }
        }
        
//        if($index >= 10){//临时代码以后删除的
//            break;
//        }
        $index += 1;
    }
    
    startDeal($userUidArr);
       
}

/**
 * 测试接口 可以自己添加uid测试
 */
function textInterface() {
    $userUidArr = array();
    $userUidArr[] = "6347a46975d0a43de55f0cec";
    //$userUidArr[] = "EA74E58DCEB076ACC2D824684499CDA2";
    
    startDeal($userUidArr);
}

/**
 * 开始处理玩家任务信息
 * @param type $userUidArr
 */
function startDeal($userUidArr) {
    $mem = gMem();        
    foreach ($userUidArr as $userUid) {
        $userRedisKey = MemKey_User::Info_hash(1, $userUid);  
        if(!$mem->exists($userRedisKey)){          
            //$sFname = "savData/" ."noRedisExistUserUid.txt";
            $sFname = isOnline("otherData", "noRedisExistUserUid.txt");
            //$sFname = "../../../../../../ylsjTaskData/otherData/"."noRedisExistUserUid.txt";
            $fd = fopen($sFname, 'a');               
            fwrite($fd, "\r\n" . $userRedisKey);
            fclose($fd);
            continue;
        }
        
        if(!$mem->hexists($userRedisKey,'privateState')){
            //$sFname = "savData/" ."noPrivateState.txt";           
            //$sFname = "../../../../../../ylsjTaskData/otherData/"."noPrivateState.txt";
            $sFname = isOnline("otherData", "noPrivateState.txt");
            $fd = fopen($sFname, 'a');               
            fwrite($fd, "\r\n" . $userRedisKey);
            fclose($fd);
            continue;
        }
        if(!$mem->hexists($userRedisKey,'store')){
            //$sFname = "savData/" ."noStore.txt";
            //$sFname = "../../../../../../ylsjTaskData/otherData/"."noStore.txt";
            $sFname = isOnline("otherData", "noStore.txt");
            $fd = fopen($sFname, 'a');               
            fwrite($fd, "\r\n" . $userRedisKey);
            fclose($fd);
            continue;
        }
        
        if(!$mem->hexists($userRedisKey,'heros')){//
            //$sFname = "savData/" ."noHeros.txt";
            //$sFname = "../../../../../../ylsjTaskData/otherData/"."noHeros.txt";  
            $sFname = isOnline("otherData", "noHeros.txt");
            $fd = fopen($sFname, 'a');               
            fwrite($fd, "\r\n" . $userRedisKey);
            fclose($fd);
            continue;
        }
                
        $privateState = $mem->hget($userRedisKey, 'privateState');
        if(!StlUtil::dictHasProperty($privateState,'taskJianRong_index')){
            $privateState->taskJianRong_index = 0;
        }
        $startIndex = $privateState->taskJianRong_index;
        if($startIndex > 0){
            continue;                       
        }      
        $dic = GameConfig::taskJianRong();//任务兼容配置表
        $arr = (array)$dic;
	ksort($arr);         
           
        $store = $mem->hget($userRedisKey, 'store');
        $herosdic = $mem->hget($userRedisKey,'heros');
        $base = $mem->hget($userRedisKey, 'baseInfo');
        $taskcards = $store->taskcards;
        
        //处理前玩家数据保存
        taskSav_start($userRedisKey, $store, $privateState,$herosdic);
         
        foreach ($arr as $index =>$val) {       
            if($index <= $startIndex){
                continue;
            }
           
            $cmd = $val->cmd1;
            $tempUId = 999999; 
            $tag = true;
            switch ($cmd) {
                case 100://该任务存在即重置,不存在就算了 参数：任务id
                    $tag = false;
//                    $str = substr($base->name,0,2);
//                    if($str == "No."){
//                        $tag = true; 
//                    }
//                                 
                    break;
                case 200://多个武器拥有其中一个即不需要重置,都没有要重置 参数:多个武器id,号分开
                    $list = explode(',', $val->paras1);                    
                    $tag = true;
                    $equip = $store->equipment;
                    
                    foreach ($equip as $eUid => $eDic) {
                        if(in_array($eDic->typeId,$list)){
                            $tag = false;
                            break;
                        }                                           
                    }  
                                                          
                    break;
                case 201://多个武器任一一个等级到10级即不重置,都没有则重置
                    $list = explode(',', $val->paras1); 
                    $lv = $val->parasCtx1; 
                    $equip = $store->equipment;
                
                    foreach ($list as $eTypeId) {
                        foreach ($equip as $eUid => $eDic) {
                            if($eTypeId == $eDic->typeId && $eDic->level >= $lv){
                                $tag = false;
                                break 2; 
                            }
                        }
                    }
                                                                                                
                    break;
                case 202://多个武器任一一个突破到1星即不重置,都没有则重置
                    $list = explode(',', $val->paras1); 
                    $starLv = $val->parasCtx1; 
                    $equip = $store->equipment;
                 
                    foreach ($list as $eTypeId) {
                        foreach ($equip as $eUid => $eDic) {
                            if($eTypeId == $eDic->typeId && $eDic->starLevel >= $starLv){
                                $tag = false;
                                break 2; 
                            }
                        }
                    }
                                                         
                    break;
                case 300://唤灵师等级升到X级,不满足则重置***
                    $lv = $val->paras1;                                      
                    $heros = $herosdic->collectHeros;
                  
                    foreach ($heros as $heroUid => $v) {
                        if($v->level >= $lv){
                            $tag = false;
                            break;
                        }
                    }
                                   
                    break;
                case 301://唤灵师有过突破,不满足则重置
                    $sLv = $val->paras1;
                    $heros = $herosdic->collectHeros;
               
                    foreach ($heros as $heroUid => $v) {
                        if($v->curStar >= $sLv){
                            $tag = false;
                            break;
                        }
                    }
  
                    break;
                case 302://是否拥有某言灵,有其中一个则不重置,全没有则重置
                    $list = explode(',', $val->paras1);                    
                    $tag = true;
                    $yanling = $store->yanling;
                   
                    foreach ($yanling as $eUid => $eDic) {
                        if(in_array($eDic->typeId,$list)){
                            $tag = false;
                            break;
                        }                                           
                    }                                
                    
                    break;
                case 303://items 背包里是否有某道具(是否获得召唤卡)
                    $items = $store->items;
                    $itemId = $val->paras1;                 
                    if(StlUtil::dictHasProperty($items, $itemId)){
                        $tag = false;
                    }                                       
                    
                    break;
                case 304://任一言灵等级到20级,都没有则重置                    
                    $lv = $val->paras1; 
                    $yanling = $store->yanling;                               
                    
                    foreach ($yanling as $eUid => $eDic) {
                        if($eDic->level >= $lv){
                            $tag = false;
                            break; 
                        }
                    }

                    break;
                case 305://任一言灵突破到1星,都没有则重置                     
                    $slv = $val->paras1;
                    $yanling = $store->yanling;                                                          
                    foreach ($yanling as $eUid => $eDic) {
                        if($eDic->starLv >= $slv){
                            $tag = false;
                            break; 
                        }
                    }                        
                    
                    break;
                
                default:
                    break;
            }
                           
            $privateState->taskJianRong_index = $index;    
            if($tag){                     
                $k = 1;
                $tempUId = $privateState->taskCardTracing->$k;        
                if(StlUtil::dictHasProperty($taskcards,$tempUId)){
                    $task = new Ins_TaskCard($val->taskId_Modified);
                    $task->uid = $tempUId;
                    $task->state = 2;
                    $taskcards->$tempUId = $task;
                    
                    //福利--给50w金币
                    $base->gold += 500000;
                    if($cmd == 200 || $cmd == 201 || $cmd == 202){//给1010022武器
                        $cid = $privateState->currentId++;
                        $equip = new Ins_Weapon();
                        $equip->typeId = 1010022;
                        $store->equipment->$cid = $equip;
                    }
                    
                    if($cmd == 301){
                        $prizeitems = "2010001,10;3255001,3;3261001,15";
                        $list = explode(';', $prizeitems);
                        foreach ($list as $s) {
                            $slist = explode(',', $s);
                            $itemId = $slist[0];
                            $itemNum = $slist[1];
                            if(StlUtil::dictHasProperty($store->items,$itemId) ){
                                $store->items->$itemId += $itemNum;
                            } else {
                                $store->items->$itemId = $itemNum;
                            }
                        }
                        
                    }
                }
                break;                                                                    
            }
        }
        
        $store->taskcards = $taskcards;
             
        taskSav_end($userRedisKey, $store, $privateState,$base);
        
        //玩家数据回存
        gMem()->hset($userRedisKey, 'store', $store);
        gMem()->hset($userRedisKey, 'privateState', $privateState);
        gMem()->hset($userRedisKey, 'baseInfo', $base);
    }
    
}

/**
 * 保存数据地址 ----自己去方法里面设置 true或是false
 * @param type $g
 * @param type $name
 * @return string
 */
function isOnline($g,$name) {
    $Online = false; 
    $sFname = $name;
    if($Online){
        if($g == "start"){
            $sFname = "../../../../../../ylsjTaskData/sav_start/".$name; 
        } elseif ($g == "end") {
            $sFname = "../../../../../../ylsjTaskData/sav_last/".$name; 
        } else {
            $sFname = "../../../../../../ylsjTaskData/otherData/".$name; 
        }     
    } else {
        $sFname = "savData/". $name;
    }
    return $sFname;
}

/**
 * 处理完后数据回存
 * @param type $userUid
 * @param type $store
 * @param type $privateState
 * @param type $hero
 */
function taskSav_start($userUid,$store,$privateState,$hero) {         
    $name = isOnline("start", $userUid.".txt");
    $fd = fopen($name, 'a');       
    fwrite($fd, json_encode($store)."\r\n");
    fwrite($fd, "\r\n" . json_encode($privateState)."\r\n");
    fwrite($fd, "\r\n" . json_encode($hero)."\r\n");
    fclose($fd);
}

function taskSav_end($userUid,$store,$privateState,$base) {   
    //$name ="savData/". "$userUid"."-last.txt";
    //$name = "../../../../../../ylsjTaskData/sav_last/"."$userUid"."-last.txt";  
    $name = isOnline("end", $userUid."-last.txt");
    $fd = fopen($name, 'a');       
    fwrite($fd,"\r\n" . json_encode($store)."\r\n");
    fwrite($fd, "\r\n" . json_encode($privateState)."\r\n");
    fwrite($fd, "\r\n" . json_encode($base)."\r\n");
    fclose($fd);
}