<?php

/**
 * Notes:从DB恢复到缓存
 * User: wsc
 * Time: 2020/6/6 10:39
 * Class ModelRecover
 */
class ModelRecoverdata{
    /**
     * Notes:恢复用户属性(金币money，体力spans，盾牌shield)
     * User: wsc
     * Time: 2020/6/4 17:30
     * @param $uid
     */
    public function recoverAttr($uid,$needRecover=[]){
        $res =false;
        $key = okeys::userAttr($uid);
        $tb = otable::gh_gameserver($uid);
        $field = implode(",",$needRecover);
        $sql = "SELECT {$field} FROM {$tb} WHERE mid={$uid}";
        $data = oo::commonOprDb("ghgames")->getOne($sql,1);
        if(empty($data)){
            oo::logs()->debug3(["time"=>date("H:i:s"),"uid"=>$uid],'gameinfo_err.log');//未找到用户
            $res2 = $this->writeUserGameInfo($uid);//写新的
            if($res2){
                $data = oo::commonOprDb("ghgames")->getOne($sql,1);
            }
        }
        if($data){
            if($data){//非新用户恢复记录
                if((isset($data['money'])&&$data['money']==60000)&&(isset($data['star'])&&$data['star']==0)){//新用户不记录

                }else{
                    oo::logs()->debug3(["time"=>date("H:i:s"),"uid"=>$uid,"data"=>$data],'gameinfo_recover.log');
                }
            }
            foreach ($data as $k=>$v){
                oo::commonOprRedis('user')->hSet($key,$k,$v);
            }
            $res = true;
        }
        return $res;
    }

    public function writeUserGameInfo($uid,$data=[]){
        $ghtable = otable::gh_gameserver($uid);
        if(empty($data)){
            $csSpins = intval(oo::commonOprModel('readconfig')->getCon('basic','initialSpins')); //初始体力值
            $baseStar = intval(oo::commonOprModel('readconfig')->getCon('basic','initialStar')); //初始星星
            $baseMoney = intval(oo::commonOprModel('readconfig')->getCon('basic','repairMoney')); //初始金幣;
            $baseLevel = intval(oo::commonOprModel('readconfig')->getCon('basic','initialLevel'));  //初始等级
            $diamond   =0;
            $shield = 0;
        }else{
            $csSpins = intval($data['spins']);
            $baseStar = intval($data['star']);
            $baseMoney = intval($data['money']);
            $baseLevel = intval($data['levelId']);
            $diamond = intval($data['diamond']);
            $shield =intval($data['shield']);
        }
        $insertSrv = " INSERT INTO {$ghtable} (mid, appid, star,levelId,money,spins,diamond,shield) VALUES ({$uid}, 1, {$baseStar}, {$baseLevel},{$baseMoney},{$csSpins},{$diamond},{$shield}) ";
        $res = oo::commonOprDb('ghgames')->query($insertSrv);
        return $res;
    }

    /**
     * Notes:恢复用户基本统计（获取钱money，打伤了demage，被打伤数demaged，打破次数destory，被打破次数destoryed，打获取的总钱coinbya
     *                      偷次数stealnum，完美偷次allstealnum，偷到总钱stealcoin，被偷总钱数stealed，防护次数defense，）
     * User: wsc
     * Time: 2020/6/6 10:40
     * @param $uid
     * @return bool
     */
    public function recoverUserBasicsStatistics($uid,$limit=0){
        $res =false;
        $key =  okeys::userBasicsStatistics($uid);
        $tb  = otable::statistics();
        $sql = "SELECT * FROM {$tb} WHERE uid={$uid} LIMIT 1";
        $data = oo::commonOprDb('common')->getOne($sql,1);
        if(!empty($data)){
            oo::logs()->debug3($data,'recover_user_statistics.log');//写恢复日志
            unset($data['uid']);
            unset($data['id']);
            oo::commonOprRedis('userinfo')->hMset($key,$data);
            oo::commonOprRedis('userinfo')->expire($key,oo::redisRandomExpire(86400*7));
        }else{
            $sql2 = " INSERT INTO {$tb} (uid) VALUES ({$uid}) ";
            oo::commonOprDb('common')->query($sql2,1);
            if($limit==0){
                $this->recoverUserBasicsStatistics($uid,1);
            }
        }
        return $res;
    }


}
