<?php
/**
 * Created by PhpStorm.
 * User: WolfDong
 * Date: 2018/5/23
 * Time: 15:33
 */
namespace Home\Controller;
use Think\Controller;
class GameController extends Controller
{

    public function TestThisNamedContains()
    {
        $Named=I('Named');

        //$playerdata = M('Playerdata') ->where("UserName = '".$Named."'")->find();
        $playerdata = M('Playerdata') ->field('UserName')->select();

        foreach ($playerdata as $userName)
        {
            $str=explode("|",$userName['username']);
            if($str[0]==$Named){
                print_r('0');
                return;
            }
        }
        print_r('1');

    }
    public function TestThisIDContains()
    {
        $ID=I('ID');
        $userData = M('Users') ->where("username = '".$ID."'")->find();
        if($userData==null){
            print_r("1");
        }else{
            print_r("0");
        }
    }


    /**
     * 保存信息
     */
    public function storage()
    {

        $data = I('data');
        $userName=I('username');
        //去除左右两边的逗号
        $playerdata = trim($data,"/(\n)|(\s)|(\t)|(\')|(')/");
        //数组分割
        $arrname   = explode("#",$playerdata);
        $count     = count($arrname);
        //分割出金币钻石等数据
        $resources = explode(",",$arrname[0]);
        //查询用户ID
        $userID = M('Users') ->where("username = '".$userName."'")->field('id')->find();
        $useridNum=$userID['id'];
        //清除用户名
        unset($resources[0]);

        $add['UID'] = $userID['id'];
        $add['UserName'] = $resources[1]; //玩家昵称
        $add['GuildId'] = $resources[2];
        $add['UserIcon'] = $resources[3]; //玩家头像
        $add['Gold'] = $resources[4]; //金币数量
        $add['Crystal'] = $resources[5]; //水晶数量
        $add['LV'] = $resources[6]; //等级
        $add['EXP_Max'] = $resources[7]; //经验值上限
        $add['EXP'] = $resources[8]; //现有的经验值
        $add['EXPAddition'] = $resources[9]; //经验加成
        $add['EXPAdditionDataTime'] = date("Y-m-d H:i:s");
        $add['EXPAdditionHasTime'] = $resources[16]; //经验加成多长时间
        $add['Power']  = $resources[17];     //战斗力
        $add['CityOpenBit']  = $resources[18];     //可以攻打城池的等级
        $add['LotteryGeneralCount'] = $resources[19]; //武将抽奖计数
        $add['LotteryGeneralWeaponCount']= $resources[20]; //稀有装备抽奖计数
        $add['LotteryGeneralTimeLimitCount']= $resources[21]; //限时抽奖计数
        $add['LotteryGeneralFreeCharce']=$resources[22]; //是否拥有免费抽奖的机会
        $add['PlayerFirstTime']         = $resources[23];     //是否是第一次登陆
        $add['EG']         = $resources[24];     //出战武将列表

        $addPlayerdata = M('Playerdata')->where("UID = '".$userID['id']."'")->save($add);



        //$addPlayerdata = M('Playerdata')->where("pid = '68'")->save($add);
        //添加道具数据
        $propdata = M('propdata')->where("UID = '".$userID['id']."'")->delete();
        $dataadd['UID'] = $userID['id'];
        $dataadd['Num'] = $arrname[1];
        $addpropdata = M('propdata')->add($dataadd);


        //判断是否修改成功
        if($addPlayerdata !==false and $addpropdata !==false)
        {
            $res = 1;
        }
        else
        {
            $res = 0;
        }

        print_r($res);

    }
    public function AddGeneralData()
    {
        $generalData = I('data');
        $arr=explode(',',$generalData);
        $userName=I('username');
        $userID=M('Users') ->where("username = '".$userName."'")->field('id')->find();
        $generaldataStr = M('Generaldata')->where("NameStr = '".$arr[0]."'")->delete();
        $gd['UID']       = $userID['id'];
        $gd['NameStr']  = $arr['0'];
        $gd['Lv']      = $arr['1'];
        $gd['Stratum']      = $arr['2'];
        $gd['AttackRange']      = $arr['3'];
        $gd['GrowthRateAttackRange'] = $arr['4'];
        $gd['SkillRange']     = $arr['5'];
        $gd['GrowthRateSkillRange']     = $arr['6'];
        $gd['Speed']    = $arr['7'];
        $gd['GrowthRateSpeed']     = $arr['8'];
        $gd['Dodge'] = $arr['9'];
        $gd['HP_Max']  = $arr['10'];
        $gd['GrowthRateHP_Max']      = $arr['11'];
        $gd['HP']      = $arr['12'];
        $gd['HPReple']      = $arr['13'];
        $gd['GrowthRateHPReple'] = $arr['14'];
        $gd['Energy_Max']     = $arr['15'];
        $gd['Energy']     = $arr['16'];
        $gd['GrowthRateEnergy']    = $arr['17'];
        $gd['EnergyReple']     = $arr['18'];
        $gd['GrowthRateEnergyReple'] = $arr['19'];
        $gd['Armor']  = $arr['20'];
        $gd['GrowthRateArmor']      = $arr['21'];
        $gd['PA']      = $arr['22'];
        $gd['GrowthRatePA']      = $arr['23'];
        $gd['MA'] = $arr['24'];
        $gd['GrowthRateMA']     = $arr['25'];
        $gd['LifeSteal']     = $arr['26'];
        $gd['GrowthRateLifeSteal']    = $arr['27'];
        $gd['EXP_Max']     = $arr['29'];
        $gd['EXP'] = $arr['28'];
        $gd['EXPAddition']  = $arr['30'];
        $gd['G_Force']      = $arr['31'];
        $gd['GrowthRateForce']      = $arr['32'];
        $gd['Wit']      = $arr['33'];
        $gd['GrowthRateWit'] = $arr['34'];
        $gd['Agility']     = $arr['35'];
        $gd['GrowthRateAgility']     = $arr['36'];
        $gd['CommandPresence']    = $arr['37'];
        $gd['Battles']      = $arr['38'];
        $gd['VictoryCount']      = $arr['39'];
        $gd['PropDatas'] = $arr['40'];
        $gd['SkillName']     = $arr['41'];
        $gd['lastPropDatas']     = $arr['42'];
        $gd['GeneralHasTime']     = mktime($resources['43'],$resources['44'],$resources['45'],$resources['46'],$resources['47'],$resources['48']);
        $gd['GeneralHasTimeValue']     = $arr['49'];
        $addMechdata = M('Generaldata')->add($gd);
        if($addPlayerdata !==false and $addpropdata !==false)
        {
            $res = 1;
        }
        else
        {
            $res = 0;
        }

        print_r($res);
    }
    public function AddPropData()
    {
        $propData = I('data');
        $userName=I('username');
        $userID=M('Users') ->where("username = '".$userName."'")->field('id')->find();

        $propdata = M('propdata')->where("UID = '".$userID['id']."'")->delete();
        $dataadd['UID'] = $userID['id'];
        $dataadd['Num'] = $propData;
        $addpropdata = M('propdata')->add($dataadd);

        //$propdata = M('Propdata')->where("UID = '".$userID['id']."'")->delete();
        //$dataadd['UID'] = $userID['id'];
        //$dataadd['Num'] = $propData;
        //$addpropdata = M('propdata')->add($dataadd);
        if($addpropdata !==false)
        {
            $res = 1;
        }
        else
        {
            $res = 0;
        }

        print_r($res);

    }
    public function ChangePlayerData()
    {
        $data = I('data');
        $username=I('username');
        $resources=explode(",",$data);
        $userID = M('Users') ->where("username = '".$username."'")->field('id')->find();
        $useridNum=$userID['id'];
        //清除用户名
        unset($resources[0]);

        $add['UID'] = $userID['id'];
        $add['UserName'] = $resources[1]; //玩家昵称
        $add['asd'] = "我";
        $add['UserIcon'] = $resources[2]; //玩家头像
        $add['Gold'] = $resources[3]; //金币数量
        $add['Crystal'] = $resources[4]; //水晶数量
        $add['LV'] = $resources[5]; //等级
        $add['EXP_Max'] = $resources[6]; //经验值上限
        $add['EXP'] = $resources[7]; //现有的经验值
        $add['EXPAddition'] = $resources[8]; //经验加成
        $add['EXPAdditionDataTime'] = mktime($resources['9'],$resources['10'],$resources['11'],$resources['12'],$resources['13'],$resources['14']);
        $add['EXPAdditionHasTime'] = $resources[15]; //经验加成多长时间
        $add['Power']  = $resources[16];     //战斗力
        $add['CityOpenBit']  = $resources[17];     //可以攻打城池的等级
        $add['LotteryGeneralCount'] = $resources[18]; //武将抽奖计数
        $add['LotteryGeneralWeaponCount']= $resources[19]; //稀有装备抽奖计数
        $add['LotteryGeneralTimeLimitCount']= $resources[20]; //限时抽奖计数
        $add['LotteryGeneralFreeCharce']=$resources[21]; //是否拥有免费抽奖的机会
        $add['PlayerFirstTime']         = $resources[22];     //是否是第一次登陆

        $addPlayerdata = M('Playerdata')->where("UID = '".$userID['id']."'")->save($add);
    }

    public function GetUserData()
    {
        $username = I('username');
        $userID = M('Users') ->where("username = '".$username."'")->field('id')->find();
        $res=$userID['id'];
        print_r($res);
    }
    public function GetAllPlayerData()
    {
        $res="SELECT*FROM playerdata";
        print_r($res);
    }
    public function GetPlayerData()
    {
        $username = I('username');
        $userID = M('Users') ->where("username = '".$username."'")->field('id')->find();
        $useridNum=$userID['id'];
        $playerdata = M('Playerdata') ->where("UID = '".$useridNum."'")->find();
        //$playername= explode("|",$playerdata['UserName']) ;
        //$playerdata=$playername[0];
        //$playerdata['EG']=$playername[1];
        //$playerdata['EXPAdditionDataTime']=date("Y-m-d H:i:s",$playerdata['EXPAdditionDataTime']);
        $res=implode(',',$playerdata);
        print_r($res);
    }
    public function GetPlayerExpAddTime()
    {
        $username = I('username');
        $userID = M('Users') ->where("username = '".$username."'")->field('id')->find();
        $useridNum=$userID['id'];
        $playerdata = M('playeraddexptime') ->where("UID = '".$userID['id']."'")->find();
        //$playerdata['EXPAdditionDataTime']=date("Y-m-d H:i:s",$playerdata['EXPAdditionDataTime']);

        $res=implode(',',$playerdata);
		$newTime['UID']=$userID['id'];
		$newTime['Time']=date("Y-m-d H:i:s");
        $addPlayerdata = M('playeraddexptime')->where("UID = '".$userID['id']."'")->save($newTime);
        print_r($res);
    }
    public function GetPropData(){
        $username = I('username');
        $userID = M('Users') ->where("username = '".$username."'")->field('id')->find();
        $useridNum=$userID['id'];
        $propdata = M('Propdata') ->where("UID = '".$useridNum."'")->field('Num')->find();
        $res=implode(',',$propdata);
        print_r($res);
    }
    public function GetGeneralData(){

        $username = I('username');
        $data = M('Users')
        ->where("username = '".$username."'")
        ->join("sg_generaldata ON sg_users.id = sg_generaldata.UID")
        ->field('NameStr,Lv,Stratum,AttackRange,GrowthRateAttackRange,SkillRange,GrowthRateSkillRange,Speed,GrowthRateSpeed,Dodge,HP_Max,GrowthRateHP_Max,HP,HPReple,GrowthRateHPReple,Energy_Max,Energy,GrowthRateEnergy,EnergyReple,GrowthRateEnergyReple,Armor,GrowthRateArmor,PA,GrowthRatePA,MA,GrowthRateMA,LifeSteal,GrowthRateLifeSteal,EXP_Max,EXP,EXPAddition,G_Force,GrowthRateForce,Wit,GrowthRateWit,Agility,GrowthRateAgility,CommandPresence,Battles,VictoryCount,PropDatas,SkillName,lastPropDatas,GeneralHasTime,GeneralHasTimeValue')
        ->order('Stratum')
        ->select();

        //数据的转换
        $arr = '';
        foreach($data as $val)
        {
            $val['GeneralHasTime']=date($val['GeneralHasTime']);
             time()-$val['GeneralHasTime'];
            $da = implode(',',$val);
            $abc[] = $da;
        }

        foreach($abc as $v)
        {
            $arr.=$v."_";
        }
        //数据拼接
        $res = $generaldata;
        print_r($arr);
    }
    public function rankingExp()
    {
        $user = I('username');
        $rankType=I('rankType');
        if(empty($user)&&empty($rankType))
        {
            $res = "0";
        }
        else
        {

            if($rankType=="0")
            {
                //$sql = "SELECT COUNT( * ) +1 AS mc FROM `sg_playerdatas` WHERE (EXP > (SELECT EXP FROM `sg_playerdatas` JOIN sg_users ON id=UID WHERE username = '".$user."'))";
                //$data = M()->query($sql);
                $ranking = M('Playerdata')
                ->order('LV desc')
                ->field("UID,UserName,UserIcon,CityOpenBit,LV,EXP,EXP_Max,Power,athleticsIntegral,EG")
				->limit(50)
                ->select();
            }else if($rankType=="1")
            {
                //$sql = "SELECT COUNT( * ) +1 AS mc FROM `sg_playerdatas` WHERE (Power > (SELECT Power FROM `sg_playerdatas` JOIN sg_users ON id=UID WHERE username = '".$user."'))";
                //$data = M()->query($sql);
                $ranking = M('Playerdata')
                ->order('Power desc')
                ->field("UID,UserName,UserIcon,CityOpenBit,LV,EXP,EXP_Max,Power,athleticsIntegral,EG")
				->limit(50)
                ->select();
            }else if($rankType=="2")
            {
                //$sql = "SELECT COUNT( * ) +1 AS mc FROM `sg_playerdatas` WHERE (Power > (SELECT Power FROM `sg_playerdatas` JOIN sg_users ON id=UID WHERE username = '".$user."'))";
                //$data = M()->query($sql);
                $ranking = M('Playerdata')
                ->order('athleticsIntegral desc')
                ->field("UID,UserName,UserIcon,CityOpenBit,LV,EXP,EXP_Max,Power,athleticsIntegral,EG")
				->limit(50)
                ->select();
            }

            $res = '';

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

            $res=substr($res,0,-1);
            //$res=$res."#".$data[0]['mc'];
        }
        print_r($res);
    }
    public function RandomSelectMorePeople(){
        $username = I("username");
        $playernames = M('users')->where("username != '".$username."'")->field("username")->select();
        $selectCount = count($playernames) > 30 ? 30 : count($playernames);
        $selectKey = array_rand($playernames, $selectCount);
        $res = '';
        for ($i=0; $i < $selectCount; $i++) {
            $playernameArr[] = $playernames[$selectKey[$i]]['username'];
        }
        foreach($playernameArr as $v){

            $playerNamesInfo.=$v."#";
        }
        $res = rtrim($playerNamesInfo, '#');
        print_r($res);
    }
    public function GetLotteryData()
    {
        //M('Lotterydatas')->where("Time='2'")->field('Lottery')->find();
        $Lottery = M('Lotterydatas')
        //->order('Time desc')
        ->field("Time,Lottery")
        ->select();
        foreach ($Lottery as $v)
        {
            $v = join(",",$v); //可以用implode将一维数组转换为用逗号连接的字符串，join是别名
            $temp[] = $v;
        }
        $res = '';
        foreach($temp as $v)
        {
            $res.=$v."$";
        }
        //$res=substr($res,0,-1);
        //$timeCount=1;
        //$contents=M('lotterydatas')->find();
        //$res=$contents['lottery'];
        print_r($res);
    }

    public function GetServerTime()
    {
        print_r(date("Y-m-d H:i:s"));
    }
    public function TestTime()
    {
        $username = I('username');
        $userID = M('Users') ->where("username = '".$username."'")->field('id')->find();
        $propdata = M('playeraddexptime')->where("UID = '".$userID['id']."'")->delete();

        $dataadd['UID'] = $userID['id'];
        $dataadd['Time'] = date("Y-m-d H:i:s");
        $addpropdata = M('playeraddexptime')->add($dataadd);
    }
    public function SaveFriendInfo()
    {
        $names     = I("namesdata");
        $arrname   = explode("#",$names);
        $username  = $arrname[0];
        $friendName = $arrname[1];
        $userID     = M('users') ->where("username = '".$username."'")->field('id')->find();
        $friendID   = M('users') ->where("username = '".$friendName."'")->field('id')->find();
        $friendIDArr     = M('friendtable') ->where("uid = '".$userID['id']."'")->field('fuid')->select();
        $res = '';
        foreach($friendIDArr as $val)
        {
            if ($val['fuid'] == $friendID['id']) {
                $res = 2;
                print_r($res);
                return;
            }

        }
        $dataadd['UID'] = $userID['id'];
        $dataadd['FUID'] = $friendID['id'];
        $dataadd['FName'] = $friendName;
        $addFriendtable = M('friendtable')->add($dataadd);
        if($addFriendtable !==false)
        {
            $res = 1;
        }
        else
        {
            $res = 0;
        }
        print_r($res);
    }
    public function SelectFriendRelation()
    {
        $names     = I("namesdata");
        $arrname   = explode("#",$names);
        $username  = $arrname[0];
        $friendName = $arrname[1];
        $userID     = M('users') ->where("username = '".$username."'")->field('id')->find();
        $friendID   = M('playerdata') ->where("UserName = '".$friendName."'")->field('uid')->find();
        $friendOfFriendsIDArr     = M('friendtable') ->where("uid = '".$userID['id']."'")->field('fuid')->select();
        $res = '';
        foreach($friendOfFriendsIDArr as $val)
        {
            if ($val['fuid'] == $friendID['uid']) {
                $res = 1;
                print_r($res);
                return;
            }
        }
        $res = 0;
        print_r($res);
    }
    public function SetFriendInfo()
    {
        $username = I('username');
        $data = M('Users')
            ->where("username = '".$username."'")
             ->join("sg_friendtable ON sg_users.id = sg_friendtable.uid")
            ->field('fuid,fname')
            ->order('fuid')
            ->select();
        //数据的转换
        $arr = '';
        foreach($data as $val)
        {
            $da = implode(',',$val);
            $abc[] = $da;
        }
        foreach($abc as $v){

            $arr.=$v."#";
        }

        //数据拼接
        $res = rtrim($arr,'#');
        print_r($res);
    }

    public function DeleteFriendInfo()
    {
        $names     = I("namesdata");
        $arrname   = explode("#",$names);
        $username  = $arrname[0];
        $friendName = $arrname[1];
        $userID     = M('users') ->where("username = '".$username."'")->field('id')->find();
        $friendID   = M('users') ->where("username = '".$friendName."'")->field('id')->find();
        $friendIDArr1     = M('friendtable') ->where("uid = '".$userID['id']."'")->select();
        $friendIDArr2     = M('friendtable') ->where("uid = '".$friendID['id']."'")->select();

        foreach ($friendIDArr1 as $val)
        {
            if($val['fuid']==$friendID['id'])
            {
                $this1FID=$val['fid'];
            }
        }

        foreach ($friendIDArr2 as $val)
        {
            if($val['fuid']==$userID['id'])
            {
                $this2FID=$val['fid'];
            }
        }
        $friendIDDelete1     = M('friendtable') ->where("fid = '".$this1FID."'")->delete();
        $friendIDDelete2     = M('friendtable') ->where("fid = '".$this2FID."'")->delete();
        $res = '';
        if($friendIDDelete1!==false || $friendIDDelete2!==false)
        {
            $res = 0;
            print_r($res);
            return;
        }
        $res = 1;
        print_r($res);
    }
    public function GetSelectAthleticsGeneralData()
    {
        $names     = I("AthleticsName");

        $userData = M('playerdata') ->where("username = '".$names."'")->field('uid,eg')->find();
        $GeneralNames=explode("+",$userData['eg']);
        echo $userData['uid'];
        foreach ($GeneralNames as $var)
        {
            $generaldata=M('generaldata') ->where("UID = '".$userData['uid']."'" AND"NameStr='".$var."'")->find();
            $generalAllData=implode(",",$generaldata);
            $generalDatas.=$generalAllData."#";
        }
        print_r($generalDatas);
    }

}

