<?php
class YouxiAction extends Action {
	// 网站首页
    public function index(){
		// 获取游戏
		$yxList = F("yxList");
		$this->assign("yxList",$yxList);
		// 显示页面
        $this->display();
    }
	// 游戏专区
    public function section(){
		// 获取参数
		$jian = ABC("jian");
		if(!$jian){
			redirect("/Youxi/index.html");
			exit;
		}
		// 查询游戏
		$Data = M("Yx");
		$yxObj = $Data->where("yx_jian = '$jian'")->find();
		if(!$yxObj){
			redirect("/Youxi/index.html?jian=".$jian);
			exit;
		}
		$this->assign("yxObj",$yxObj);
		// 查询区服
		$Data = M("Qf");
		$qfList = $Data->where("qf_type = 1 AND qf_kf > 0 AND qf_yx = ".$yxObj["yx_id"])->order("qf_open DESC")->select();
		$this->assign("qfList",$qfList);
		// 显示页面
		$this->assign("time",time());
        $this->display();
    }
	// 开始游戏
	public function play(){
		// 没有登录
		if(!$_SESSION["ID"] || !$_SESSION["ZH"]){
			$this->error("您还没有登录!","/Account/login.html");
			exit;
		}
		// 获取参数
		$id = ABC("id");
		$jian = ABC("jian");
		if(!$id || !$jian){
			redirect("/Youxi/index.html");
			exit;
		}
		// 查询游戏
		$Data = M("Yx");
		$yxObj = $Data->where("yx_jian = '$jian'")->find();
		if(!$yxObj){
			redirect("/Youxi/index.html?jian=$jian");
			exit;
		}
		$this->assign("yxObj",$yxObj);
		// 查询区服
		$Data = M("Qf");
		$qfObj = $Data->where("qf_id = $id")->find();
		if(!$qfObj || $qfObj["qf_yx"] != $yxObj["yx_id"]){
			redirect("/Youxi/index.html?jian=$jian&qf=$id");
			exit;
		}
		// 还未开放
		if($qfObj["qf_open"] > time() || !$qfObj["qf_kf"]){
			$this->error("该服将于".date("Y-m-d H:i:s",$qfObj["qf_open"])."开放!");
			exit;
		}
		$this->assign("qfObj",$qfObj);
		
		// 玩家日志
		$Data = M("YxRz");
		$rzObj = $Data->where("rz_zh = ".$_SESSION["ID"]." AND rz_qf = ".$id)->find();
		if($rzObj){
			// 修改最后登录时间
			$Data = M("YxRz");
			$rzData["rz_ip"] = IP();
			$rzData["rz_date"] = time();
			$rel = $Data->where("rz_zh = ".$_SESSION["ID"]." AND rz_qf = ".$id)->save($rzData);
			if($rel === false){
				$this->error("请再次尝试登录! -2");
				exit;
			}
		}else{
			// 记录玩家登录详情
			$Data = M("YxRz");
			$rzData["rz_zh"] = $_SESSION["ID"];
			$rzData["rz_yx"] = $qfObj["qf_yx"];
			$rzData["rz_qf"] = $qfObj["qf_id"];
			$rzData["rz_ip"] = IP();
			$rzData["rz_date"] = time();
			$rel = $Data->add($rzData);
			if($rel === false){
				$this->error("请再次尝试登录! -1");
				exit;
			}
		}
		
		// 游戏链接
		import("@.iGuoji.Webgame");
		$Webgame = new Webgame();
		$path = $Webgame->login($qfObj["qf_kf"],$_SESSION["ZH"]);
		$this->assign("path",$path);
		
		// 显示页面
		$this->display();
	}
}