<?php

namespace Media\Controller;

use Think\Controller;
use Common\Api\GameApi;
use Admin\Model\GameModel;

class SelfbuiltController extends BaseController
{
    private $model_name = 'selfbuit';

    public function __construct()
    {
        parent::__construct();
        if($_REQUEST['gid']!=""){
            $_REQUEST['game_id']=$_REQUEST['gid'];
        }elseif($_REQUEST['game_id']!=""){
            $_REQUEST['gid']=$_REQUEST['game_id'];
        }
		$game = new GameModel();
        $selftbuilt = M("Selfbuilt")->where(['gameid' => $_REQUEST['gid']])->order('id desc')->find();
        if (empty($selftbuilt)) $this->redirect('Index/index');

        $selftbuilt = M("Selfbuilt")->where(['gameid' => $_REQUEST['gid']])->order('id desc')->find();
        $selftbuilt['gpic'] = explode(',', $selftbuilt['screenshots']);
        $selftbuilt['gshuffling'] = explode(',', $selftbuilt['shuffling']);
        
        $info=$game->detail($_REQUEST['gid']);
        if($selftbuilt['wei_url']==0)$selftbuilt['wei_url']=$info['wei_url'];
        $this->assign("selftbuilt", $selftbuilt);
        $this->assign('data', $info);

        $map['game_id']=$info['and_id'];
        $and_server=M('server','tab_')->field('id,server_name,start_time')->where($map)->order('id desc')->select();
        $map['game_id']=$info['ios_id'];
        $ios_server=M('server','tab_')->field('id,server_name,start_time')->where($map)->order('id desc')->select();
        $this->assign('and_server',$and_server);
        $this->assign('ios_server',$ios_server);
        
	    $this->assign('gamesou',get_game_source($info['and_id'],$info['ios_id']));
	    
	    $hot_game_and = M('game','tab_')->field('id,relation_game_name')->where(array('game_status'=>1,'recommend_status'=>2,'sdk_version'=>1))->order('id desc')->limit(6)->select();
	    $hot_game_ios = M('game','tab_')->field('id,relation_game_name')->where(array('game_status'=>1,'recommend_status'=>1,'sdk_version'=>1))->order('id desc')->limit(6)->select();
	    $this->assign('hot_game_and', $hot_game_and);
	    $this->assign('hot_game_ios', $hot_game_ios);
    }



    /*游戏管理*/
    public function game($game_id = 0)
    {
		$doc = D('Document');
		$map['game_id']=$game_id;
		$server=M('server','tab_')->field('id,server_name,server_status,game_id,server_name,server_status,start_time')->where($map)->order('id desc')->find();
		$this->assign('serverss',$server);
		$self_id = get_self_id($game_id);
		$this->assign('grole', $doc->game_index_doc('game_int', $self_id, 6));//角色
		$this->assign('newest', $doc->game_index_doc('game_guide', $self_id, 6));//游戏攻略/最新
		$this->assign('news', $doc->game_index_doc('game_news', $self_id, 6));//游戏公告/新闻
		$this->assign('active', $doc->game_index_doc('game_act', $self_id, 6));//游戏活动
		$this->assign('novice_guide', $doc->game_index_doc('game_novice', $self_id, 6));//新手
		$this->assign('master', $doc->game_index_doc('game_master', $self_id, 6));//高手进价
		$this->assign('features', $doc->game_index_doc('game_character', $self_id, 6));//特色系统
        $this->display();
    }

    public function weiduan($game_id){
        $map['sys_document.display'] = 1;
        $map['sys_document.status'] = 1;
        $map['category_id']=51;
        $map['self_id']=get_self_id($game_id);
        $data = M('document')
            ->field('sys_document.id,sys_category.id as cid,sys_document.title,sys_category.title as ctitle')
            ->join('sys_category  on sys_category.id = sys_document.category_id')
            ->join('sys_document_special  on sys_document.id = sys_document_special.id')
            ->where($map)
            ->order('sys_document.create_time,level desc')
            ->limit($limit)
            ->select();
         $this->assign('doc',$data);
         $this->assign('game_idd',$game_id);//斗破
         $this->assign('user_id',session('user_auth.uid'));
        $this->display();
    }
    
    public function servers($game_id){
        $map['game_id']=$game_id;
        $area=M('server','tab_')->where($map)->order('start_time desc')->select();
        $maps['a.game_id']=$game_id;
        $maps['user_id']=session('user_auth.user_id');
        $login_rec=M('user_login_record','tab_')
                   ->field('a.server_name,a.docking_server_id,a.id,tab_user_login_record.*')
                   ->join('tab_server as a on a.id=tab_user_login_record.server_id')
                   ->where($maps)
                   ->order('a.start_time desc')
                    ->group('a.id')
                  ->limit(2)
                  ->select();
        $this->assign('area',$area);
        $this->assign('login_rec',$login_rec);
        $this->display();   
    }



    public function getlists($name, $self_id, $p, $order = "d.id desc")
    {
        $time = time();
        $model['model'] = "Document";
        $model['table'] = "__DOCUMENT__ as d";
        $model['field'] = "d.id,d.title,d.link_id,d.cover_id,d.name,d.create_time,ds.self_id,ds.content,c.title as ctitle,c.id as pid,c.name as cname";
        $model['join'] = "__DOCUMENT_SPECIAL__ as ds on (d.id = ds.id and ds.self_id=$self_id )";
        $model['joinnum'] = "right";
        $model['join1'] = "__CATEGORY__ as c on (c.id=d.category_id and c.name like '$name' )";
        $model['joinnum1'] = "right";
        $model['where'] = "d.status>0 and d.display=1 and d.create_time<$time and (d.deadline =0 or d.deadline > $time) ";
        $model['order'] = $order;
        parent::glists($model, $p);
    }

    public function lists($cid=1, $game_id, $p = 1){
        /*  1>新闻公告    2>攻略    3>新手指南    4>游戏资料    5>高手进阶    6>特色    7>活动    8>角色 */
        $cat = ['1'=>['name'=>'game_news','desc'=>'新闻'],
            '2'=>['name'=>'game_guide','desc'=>'攻略'],
            '3'=>['name'=>'game_novice','desc'=>'新手'],
            '4'=>['name'=>'game_data','desc'=>'资料'],
            '5'=>['name'=>'game_master','desc'=>'进阶'],
            '6'=>['name'=>'game_character','desc'=>'特色'],
            '7'=>['name'=>'game_act','desc'=>'活动'],
            '8'=>['name'=>'game_int','desc'=>'角色'],
        ];
        $cat_name = $cat[$cid]['name'];
        $this->assign('cid',$cid);
        $this->assign('cat',$cat);
        $category = M("category")->where(['name'=>$cat_name])->find();
        $map['category_id'] = $category['id'];
        $map['self_id']=get_self_id($game_id);
        $map['display']=1;
        $map['status']=1;
        $mode = [
            'table' => 'document',
            'pre'=>'sys_',
            'map'=>$map,
            'join' => 'sys_document_special on sys_document_special.id=sys_document.id',
            'order'=>'create_time desc,level desc',
            'tep'=>'lists'
        ];
        $this->lists_page($mode,$p);
    }

    public function detail($id, $game_id = 0)
    {

        $cat = ['game_news'=>['cid'=>'1','desc'=>'新闻'],
            'game_guide'=>['cid'=>'2','desc'=>'攻略'],
            'game_novice'=>['cid'=>'3','desc'=>'新手'],
            'game_data'=>['cid'=>'4','desc'=>'资料'],
            'game_master'=>['cid'=>'5','desc'=>'进阶'],
            'game_character'=>['cid'=>'6','desc'=>'特色'],
            'game_act'=>['cid'=>'7','desc'=>'活动'],
            'game_int'=>['cid'=>'8','desc'=>'角色'],
        ];
        
        $doc = D('Document');
        $detail = $doc->game_detail($id);
        $this->assign('detail', $detail);
        
        $map['category_id'] = $detail['category_id'];
        $map['self_id']=$detail['self_id'];
        $map['display']=1;
        $map['status']=1;
        $map['sys_document.id']=['gt',$id];
        $data = D('Document')
        ->field('sys_document.id,sys_document.title')
        ->join('sys_document_special  on sys_document.id = sys_document_special.id')
        ->where($map)
        ->order('create_time desc,level desc')
        ->find();
        $this->assign('next', $data);
        

        $cate_name = M("category")->field('name,title')->find($detail['category_id']);
        $this->assign('cate_name', $cate_name['title']);
        $this->assign('cid', $cat[$cate_name['name']]['cid']);
        $this->assign('game_id', $game_id);
        $this->display();
    }

    public function server($game_id, $p = 1)
    {
        $map['game_id']=$game_id;
        $map['show_status']=1;
        $map['stop_status']=1;
        $mode = [
            'table' => 'Server',
            'pre'=>'tab_',
            'map'=>$map,
            'order'=>'start_time desc',
            'tep'=>'server'
        ];
        $map['recommend_status']=1;
        $lists=M('Server','tab_')->where($map)->order('start_time desc')->limit(9)->select();
        $this->assign('tui',$lists);
        $this->lists_page($mode,$p);
    }

    public function error()
    {
        $game_id = $_REQUEST['game_id'];
        $area_id = $_REQUEST['area_id'];
        $time = time();
        $area = D('Server');
        $result = $area->table("__SERVER__ as a")
            ->field('a.*')
            ->limit(1)
            ->where("a.game_id=$game_id and a.id=$area_id")
            ->order("a.start_time desc")->select();
        $data = $result[0];
        $this->assign('data', $data);
        $result = $area->table("__SERVER__ as a")
            ->field('a.*')
            ->limit(4)
            ->where("a.game_id=$game_id and a.start_time<$time")
            ->order("a.start_time desc")->select();
        $this->assign('area', $result);
        $this->display();
    }
}

?>