<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 宠物配置
 */
class Pet extends MY_Controller
{
    public $postData = array('frombg' => 1, 'lid' => 1, 'sid' => 1);
    public $url;
    public function __construct()
    {
        date_default_timezone_set("Asia/Shanghai");
        parent::__construct();
        $this->load->helper('cookie');
        $this->load->library('curl');
        $this->env = get_cookie($this->_wenenv);
        $this->url = $this->config->item('officialUrl');
    }

    public function index(){
        $posts = $this->postData;
        $posts['mod'] = 'Pet';
        $posts['act'] = 'getPetConfig';
        $petConfig = $this->curl->req($this->url, $posts);
        $data = [
            "petConfig"   => json_decode($petConfig,true),
            "petExpConfig"=> [],
        ];
        $this->load->view('pet/index', $data);
    }

    public function config($petId){
        $tb  = otable::PetConfig();
        $petInfo = oo::commonOprDb('slavedb')->getOne("SELECT * FROM {$tb} WHERE id={$petId} LIMIT 1",MYSQLI_ASSOC);
        $petInfo['addExp']    = json_decode($petInfo['addExp'],true);
        $petInfo['skillBase'] = json_decode($petInfo['skillBase'],true);
        $tb  = otable::PetExpConfig2();
        $ret = oo::commonOprDb('slavedb')->getAll("SELECT * FROM {$tb} WHERE petId={$petId}",MYSQLI_ASSOC);
        $this->load->view('pet/config', ['list'=>$ret,'petId'=>$petId,"petInfo"=>$petInfo]);
    }

    public function actionConfig2(){
        $input = $this->input->post('config');
        $petId = $this->input->post('petId');
        $input = array_values($input);
        $tb  = otable::PetExpConfig2();
        $maxLv = 0;
        foreach ($input as $row){
            $maxLv = max($row['lv'], $maxLv);
            $ret = oo::commonOprDb('games')->query("UPDATE {$tb} SET `exp`='{$row['exp']}',`skill`='{$row['skill']}' WHERE lv='{$row['lv']}' AND petId={$petId}");
        }
        if($ret == 0){
            return $this->response(1, '操作失败');
        }else{
            oo::commonOprRedis('games')->hset(okeys::PetMaxLv(), $petId, $maxLv);
            oo::commonOprRedis('games')->delete(okeys::PetLevelConfig($petId));
            return $this->response(0, '操作成功');
        }
    }

    public function actionConfig3(){
        $input = $this->input->post('add');
        $petId = $this->input->post('petId');
        $value = [];
        foreach ($input['minLv'] as $key=>$row){
            $temp = [
                'minLv'      => $row,
                'feedAddExp' => $input['feedAddExp'][$key],
            ];
            if($petId == 1){
                $temp['stealAddExp'] = $input['stealAddExp'][$key];
            }else if($petId == 2){
                $temp['defenseAddExp'] = $input['defenseAddExp'][$key];
            }else if($petId == 3){
                $temp['attackAddExp']  = $input['attackAddExp'][$key];
                $temp['defenseAddExp'] = $input['defenseAddExp'][$key];
            }
            $value[] = $temp;
        }
        array_multisort(array_column($value,'minLv'),SORT_ASC,$value);
        $value = addslashes(json_encode($value,JSON_NUMERIC_CHECK));
        $tb  = otable::PetConfig();
        $ret = oo::commonOprDb('games')->query("UPDATE {$tb} SET `addExp`='{$value}' WHERE id={$petId}");
        if($ret == 0){
            return $this->response(1, '操作失败');
        }else{
            oo::commonOprRedis('games')->delete(okeys::PetConfig("",true));
            $fileName = "PetConfig".time().".json";
            oo::commonOprModel('json')->setStaticConfig("PetConfig",$fileName);
            self::makeJson($fileName);
            return $this->response(0, '操作成功');
        }
    }

    public function actionConfig4(){
        $input = $this->input->post('add');
        $petId = $this->input->post('petId');
        $value = [];
        foreach ($input['minLv'] as $key=>$row){
            $temp = [
                'minLv'      => $row,
                'sucAttack'  => $input['sucAttack'][$key],
                'failAttack' => $input['failAttack'][$key],
            ];
            $value[] = $temp;
        }
        array_multisort(array_column($value,'minLv'),SORT_ASC,$value);
        $value = addslashes(json_encode($value,JSON_NUMERIC_CHECK));
        $tb  = otable::PetConfig();
        $ret = oo::commonOprDb('games')->query("UPDATE {$tb} SET `skillBase`='{$value}' WHERE id={$petId}");
        if($ret == 0){
            return $this->response(1, '操作失败');
        }else{
            oo::commonOprRedis('games')->delete(okeys::PetConfig("",true));
            $fileName = "PetConfig".time().".json";
            oo::commonOprModel('json')->setStaticConfig("PetConfig",$fileName);
            self::makeJson($fileName);
            return $this->response(0, '操作成功');
        }
    }

    public function actionConfig(){
        $input = $this->input->post();
        $posts = $this->postData;
        $posts['mod'] = 'Pet';
        if($input['type'] == "expConfig"){
            $posts['act'] = 'savePetExpConfig';
            $data = [];
            foreach ($input['lv'] as $key => $value) {
                $data[$value] = $input['exp'][$key];
            }
            $posts['data']= json_encode($data);
        }else if($input['type'] == "petConfig"){
            $fileName = "PetConfig".time().".json";
            $posts['filename'] = $fileName;
            $posts['act'] = 'savePetConfig';
            $posts['data']= json_encode($input['pet']);
        }
        $retData = $this->curl->req($this->url, $posts);
        if($retData == 0){
            return $this->response(1, '操作失败');
        }else{
            if($input['type'] == "petConfig"){
               self::makeJson($fileName);
            }
            return $this->response(0, '操作成功');
        }
    }

    public function makeJson($fileName){
        $posts = $this->postData;
        $posts['mod'] = 'Pet';
        $posts['act'] = 'getPetConfig';
        $petConfig = $this->curl->req($this->url, $posts);
        self::SaveJson(json_decode($petConfig,true),$fileName);
    }

}