<?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;
                }
             
            }
        }
        
        $index += 1;
    }
    
    startDeal($userUidArr);
       
}

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

/**
 * 开始处理玩家任务信息
 * @param type $userUidArr
 */
function startDeal($userUidArr) {
    $mem = gMem();        
    $weaponConf = GameConfig::weaponextra_level();
    
    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;
        }            
                
        $privateState = $mem->hget($userRedisKey, 'privateState');
        if(!StlUtil::dictHasProperty($privateState,'weapon_index')){
            $privateState->weapon_index = 0;
        }
        $startIndex = $privateState->weapon_index;
        if($startIndex > 0){
            continue;                       
        }                               
        $store = $mem->hget($userRedisKey, 'store');      
        $equipment = $store->equipment;
        
        //处理前玩家数据保存
        taskSav_start($userRedisKey, $store, $privateState);
        
        $tempDic = new \stdClass();
        $index = 0;
        foreach ($equipment as $k => $item) {
            $typeId = $item->typeId;
            
            if(!StlUtil::dictHasProperty($weaponConf, $typeId)){
                $sFname = isOnline("otherData", "noConf.txt");
                $fd = fopen($sFname, 'a');               
                fwrite($fd, "\r\n" . $typeId);
                fclose($fd);
                continue;
            }
            
            $lv = $item->level;
            $dic = $weaponConf->$typeId;
            $arr = (array)$dic;
            ksort($arr);  
            foreach ($arr as $star => $val) {
                if($lv >= $val->starlimitLv){
                    $item->starLevel = $val->starLv;
                }
            }
            $index += 1;
            $tempDic->$k = $item;
            $privateState->weapon_index = $index;   
        }
               
        $store->equipment = $tempDic;
             
        taskSav_end($userRedisKey, $store, $privateState);
        
        //玩家数据回存
        gMem()->hset($userRedisKey, 'store', $store);
        gMem()->hset($userRedisKey, 'privateState', $privateState);  
    }
    
}

/**
 * 保存数据地址 ----自己去方法里面设置 true或是false
 * @param type $g
 * @param type $name
 * @return string
 */
function isOnline($g,$name) {
    $Online = false; 
    $sFname = $name;
    if($Online){
        if($g == "start"){
            $sFname = "../../../../../../ylsjWeaponData/sav_start/".$name; 
        } elseif ($g == "end") {
            $sFname = "../../../../../../ylsjWeaponData/sav_last/".$name; 
        } else {
            $sFname = "../../../../../../ylsjWeaponData/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) {         
    $name = isOnline("start", $userUid."-pre.txt");
    $fd = fopen($name, 'a');       
    fwrite($fd, json_encode($store)."\r\n");
    fwrite($fd, "\r\n" . json_encode($privateState)."\r\n");
    fclose($fd);
}

function taskSav_end($userUid,$store,$privateState) {   
    //$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");
    
    fclose($fd);
}