<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

    /**
     * 登陆
     */
    public function login()
    {
        //接受数据
        $username = I('username');
        $userpwd = I('password');
        $password = md5($userpwd);

        $where['username'] = $username;
        $userInfo = M('users')->where($where)->find();
        //判断用户是否存在
        if($userInfo)
        {
            if($password == $userInfo['password'])
            {

                $playerdata = M('Playerdata')->where("uid = '".$userInfo['id']."'")->find();
                if(!$playerdata)
                {
                   $add['uid'] = $userInfo['id'];
                   M('Playerdata') -> add($add);
                }

               //连续登陆添加300金币

                $today = mktime(0,0,0,date('m'),date('d'),date('Y'));   //今天0点
                $yesterday  = mktime(0,0,0,date('m'),date('d')-1,date('Y')); //昨天0点

                if ($userInfo['endlogintime'] >= $today) {

                }
                if($userInfo['endlogintime'] >= $yesterday  and  $userInfo['endlogintime'] <= $today)
                {
                    M('Playerdata')->where("uid = '".$userInfo['id']."'")->setInc('gold',300);
                    $res = 2;
                }
                else
                {
                $res = 1;
                }
                $save['endlogintime']    = time();
                M('Users')->where("id = '".$userInfo['id']."'")->save($save);

            }
            else
            {
                $res = "0";   //输入的密码有误
            }
        }
        else
        {
            $res = "0";   //该用户不存在
        }

        //结果输出
        print_r($res);
    }

    /**
     * 用户注册
     */
    public function register()
    {
        //接受数据
        $username = I('username');
        $password = md5(I('password'));
        $SecurityCode = md5(I('SecurityCode'));

        //根据用户名查询信息
        $existence['username'] = $username;
        $userInfo = M('Users')->where($existence)->find();

        //判断用户名是否存在
        if($userInfo)
        {
            $res = "0";  //该用户名已注册
        }
        else
        {
            $add['username'] = $username;
            $add['password'] = $password;
            $add['SecurityCode'] = $SecurityCode;
            $add['registertime'] = time();
            echo date('Y-m-d H:i:s',mktime(15,15,20,3,18,2019));
            $judge = M('Users') -> add($add);
            echo "  judge  ".$judge."  ";
            //判断是否注册成功
            if($judge)
            {
                $res = "1";
                $userID = M('Users') ->where("username = '".$username."'")->field('id')->find();
                $playerdataadd['UID']=$userID['id'];
                $playerdataadd['EXPAdditionDataTime']=time();
                M('Playerdata') -> add($playerdataadd);

				$dataadd['UID'] = $userID['id'];
				$dataadd['Time'] = date("Y-m-d H:i:s");
				$addpropdata = M('playeraddexptime')->add($dataadd);
            }
            else
            {
                $res = "0";  //注册失败
            }
        }

        //结果输出
        print_r($res);
    }
    public function SaveAllPropData()
    {
        $propdata = I('propdata');
        $arrname   = explode("_",$propdata);
        $prop['id']=$arrname[0];
        $prop['name']=$arrname[1];
        $prop['icon']=$arrname[2];
        $prop['propType']=$arrname[3];
        $prop['attackType']=$arrname[4];
        $prop['functionObject']=$arrname[5];
        $prop['glodPrice']=$arrname[6];
        $prop['crystalPrice']=$arrname[7];
        $prop['num']=$arrname[8];
        $prop['force']=$arrname[9];
        $prop['wit']=$arrname[10];
        $prop['agility']=$arrname[11];
        $prop['hp']=$arrname[12];
        $prop['hpReple']=$arrname[13];
        $prop['energy']=$arrname[14];
        $prop['energyReple']=$arrname[15];

        $prop['attack']=$arrname[16];
        $prop['range']=$arrname[17];
        $prop['lifeSteal']=$arrname[18];
        $prop['speed']=$arrname[19];
        $prop['dodge']=$arrname[20];
        $prop['armor']=$arrname[21];
        $prop['exp']=$arrname[22];
        $prop['expAddition']=$arrname[23];
        $prop['skillHarmAddition']=$arrname[24];
        $prop['attackHarmAddition']=$arrname[25];
        $prop['timeOfDuration']=$arrname[26];
        $prop['introduce']=$arrname[27];
        $prop['skillPropID']=$arrname[28];
        $prop['exclusive']=$arrname[29];
        $addMechdata = M('proptable')->add($prop);
        //echo $prop;
        //if($addMechdata !==false){
            //$res=1;
        //}else{
            //$res=0;
        //}
        print_r($prop);
    }
    public function GetAllPropData()
    {
        $allpropstr = M('proptable')
        ->select();

        foreach ($allpropstr as $v)
        {
            $v = join("|",$v); //可以用implode将一维数组转换为用逗号连接的字符串，join是别名
            $temp[] = $v;
        }
        $res = '';
        foreach($temp as $v)
        {
            $res.=$v."$";
        }
        print_r($res);
    }
    public function SaveAllGeneralData()
    {
        $generaldata = I('generaldata');
        $arrname   = explode("_",$generaldata);
        $general['id']=$arrname[0];
        $general['name']=$arrname[1];
        $general['index']=$arrname[2];
        $general['icon']=$arrname[3];
        $general['lv']=$arrname[4];
        $general['stratum']=$arrname[5];
        $general['prefabPath']=$arrname[6];
        $general['stateType']=$arrname[7];
        $general['attack']=$arrname[8];
        $general['talent']=$arrname[9];
        $general['range']=$arrname[10];
        $general['growthRateAttackRange']=$arrname[11];
        $general['speed']=$arrname[12];
        $general['growthRateSpeed']=$arrname[13];
        $general['dodge']=$arrname[14];
        $general['skillRange']=$arrname[15];
        $general['growthRateSkillRange']=$arrname[16];
        $general['hp_Max']=$arrname[17];
        $general['growthRateHP_Max']=$arrname[18];
        $general['hp']=$arrname[19];
        $general['hpReple']=$arrname[20];
        $general['growthRateHPReple']=$arrname[21];
        $general['energy_Max']=$arrname[22];
        $general['energy']=$arrname[23];
        $general['growthRateEnergy']=$arrname[24];
        $general['energyReple']=$arrname[25];
        $general['growthRateEnergyReple']=$arrname[26];
        $general['armor']=$arrname[27];
        $general['growthRateArmor']=$arrname[28];
        $general['pa']=$arrname[29];
        $general['growthRatePa']=$arrname[30];
        $general['ma']=$arrname[31];
        $general['growthRateMa']=$arrname[32];
        $general['lifeSteal']=$arrname[33];
        $general['growthRateLifeSteal']=$arrname[34];
        $general['exp_Max']=$arrname[35];
        $general['exp']=$arrname[36];
        $general['expAddition']=$arrname[37];
        $general['force']=$arrname[38];
        $general['growthRateForce']=$arrname[39];
        $general['wit']=$arrname[40];
        $general['growthRateWit']=$arrname[41];
        $general['agility']=$arrname[42];
        $general['growthRateAgility']=$arrname[43];
        $general['commandPresence']=$arrname[44];
        $general['skillName']=$arrname[49];
        $general['skillHarmAddition']=$arrname[50];
        $general['attackHarmAddition']=$arrname[51];
        $general['generalSuiPianPrice']=$arrname[55];
        $general['generalCrystalPrice']=$arrname[56];
        $general['generialTalent']=$arrname[64];
        $addMechdata = M('generaltable')->add($general);
print_r($general);
    }
    public function GetAllGeneralData()
    {
        $allgeneralstr = M('generaltable')
        ->select();

        foreach ($allgeneralstr as $v)
        {
            $v = join("|",$v); //可以用implode将一维数组转换为用逗号连接的字符串，join是别名
            $temp[] = $v;
        }
        $res = '';
        foreach($temp as $v)
        {
            $res.=$v."$";
        }
        print_r($res);
    }
}