<?php
class YouxiAction extends AdmAction{
	// 游戏管理
	public function index(){
		// 获取游戏
		$Data = M("Yx");
		$yxList = $Data->field("yx_id id,yx_idx idx,yx_jian jian,yx_name name,yx_tags tags,yx_desp desp")->order("yx_idx DESC")->select();
		F("yxList",$yxList);
		$this->assign("yxList",$yxList);
		
		// 开服计划
		$Data = M("Qf");
		$qfList = $Data->field("yx_name,qf_id,qf_open")->join("yeyou_yx ON yx_id = qf_yx")->where("qf_kf = 0")->order("qf_open DESC")->select();
		$this->assign("qfList",$qfList);
		
		// 显示页面
		$this->display();
	}
	// 确定开服
	public function kaifu(){
		// 获取参数
		$qufu = INT("qufu");
		$type = INT("type");
		$name = GET("name");
		// 判断参数
		if(!$qufu || !$name){
			echo(json_encode("参数错误或为空!"));
			exit;
		}
		// 确定参数
		$kfData["qufu"] = $qufu;
		$kfData["name"] = $name;
		// 申请开服
		import("@.iGuoji.Webgame");
		$Webgame = new Webgame();
		$rel = $Webgame->kaifu($kfData);
		// 判断结果
		if(!$rel[0]){
			echo(json_encode($rel[1]));
			exit;
		}
		// 修改区服
		$Data = M("Qf");
		$qfData["qf_kf"] = $rel[1];
		$qfData["qf_type"] = $type?1:0;
		$qfData["qf_name"] = $name;
		$rel = $Data->where("qf_id = $qufu")->save($qfData);
		if($rel === false){
			echo(json_encode("修改开服信息失败!"));
			exit;
		}
		// 操作成功
		echo(json_encode("success"));
		exit;
	}
	
	// 区服管理
	public function qufu(){
		// 导入分页
		import("@.iGuoji.Page");
		// 区服总数
		$Data = M("Qf");
		$qfSum = $Data->where("qf_kf != 0")->count("qf_id");
		// 分页对象
		$page = new Page($qfSum,20);
		// 区服列表
		$Data = M("Qf");
		$qfList = $Data->field("qf_id,qf_kf,qf_type,yx_name,qf_name,qf_open")->join("yeyou_yx ON yx_id = qf_yx")
			->where("qf_kf != 0")->order("qf_open DESC")->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign("qfList",$qfList);
		
		// 更新开服表
		$Data = M("Qf");
		$qfList = $Data->field("qf_open open,yx_name game,yx_jian jian,qf_name name")->join("yeyou_yx ON yx_id = qf_yx")
			->where("qf_type = 1 AND qf_kf > 0 AND qf_open > ".(time()-86500*2))->order("qf_open ASC")->select();
		if(count($qfList) < 10){
			$qfList = $Data->field("qf_open open,yx_name game,yx_jian jian,qf_name name")->join("yeyou_yx ON yx_id = qf_yx")
				->where("qf_type = 1 AND qf_kf > 0")->order("qf_open DESC")->limit(10)->select();
			$qfList = array_reverse($qfList);
		}
		F("qfList",$qfList);
		
		// 显示页面
		$this->assign("code",$page->show());
		$this->display();
	}
}
?>