Room.php & Team.php

<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once FCPATH . 'branch/fqb/D_Wxapp.php';

class Team extends D_Wxapp {

    private $wa;
    private $prefix;

    /**
     * 构造函数
    */
    
     
    public function __construct() {
        parent::__construct();
        $this->load->model('wxapp_model');
        $this->wa = $this->get_cache('wxapp-'.SITE_ID);
        $this->prefix = SITE_ID.'_wxapp';
    }
    
    // 创建房间页面
    public function add() {
        $this->verify_login();
        $team_category = $this->db->where('id', $this->req['catid'])->get('1_team_category')->row_array();
        
        // 房间类型
        $mapkind = $this->db->where('pid', 0)->order_by('id')->get('linkage_data_4')->result_array();
        
        // 地图
        if($this->req['id']) {
            $this->db->where('pid', $this->req['id']);
        } else {
            $this->db->where('pid', $mapkind['0']['id']);
        }
        $map = $this->db->get('linkage_data_4')->result_array();
        
        // 打手人数
        if($this->req['pid']) {
            $this->db->where('pid', $this->req['pid']);
        } else {
            $this->db->where('pid', $map['0']['id']);
        }
        $num = $this->db->get('linkage_data_4')->result_array();
        
        // 所拥有的角色
        $role = $this->db->where('uid', $this->uid)->get('member_role')->result_array();
        foreach ($role as $index => $item) {
            $name = $this->db->where('id', $item['region'])->get('linkage_data_6')->row_array()['name'];
            $role[$index]['role'] = $item['game'] .'-'.  $name;
        }
        if($mapkind) {
            $this->msg(self::CODE_SUCCESS, '', ['mapkind' => $mapkind, 'map' => $map, 'num' => $num, 'role' => $role, 'team_category' => $team_category]);
        } else {
            $this->msg(self::CODE_ERROR);
        }
    }
    
    // 创建房间
    public function add_room() {
        
        $this->verify_login();
        
        $team_category = $this->db->where('id', $this->req['catid'])->get('1_team_category')->row_array();
        // 地图类型
        $mapkind = $this->db->where('id', $this->req['did'])->get('linkage_data_4')->row_array();
        // 地图
        $map = $this->db->where('id', $this->req['tid'])->get('linkage_data_4')->row_array();
        //人数
        $num = $this->db->where('id', $this->req['nid'])->get('linkage_data_4')->row_array();
        // 角色
        $role = $this->db->where('id', $this->req['rid'])->get('member_role')->row_array();
        // $cid = $this->_order();
        // ($this->member['groupid'] == 3 && $cid) && exit_json(2, '请先完成上个订单', $cid);
        
        if(array_search(',', $this->req)) {
            $err = '请完善表单信息';
        } elseif(!$mapkind && !$map && !$num && !$team_category) {
            $err = '房间参数缺失';
        } elseif($this->member['groupid'] != 4) {
            $err = '角色不符';
        } else {
            // 添加房间
            $this->db->insert('1_team', array(
                'catid' => $team_category['id'],
                'uid' => $this->uid,
                'author' => $this->member['username'],
                'status' => 9,
                'inputtime' => SYS_TIME,
                'updatetime' => SYS_TIME,
                // 地图类型
                'title' => $mapkind['name'],
                'tid' => $mapkind['id'],
                // 地图
                'ditu' => $map['name'],
                'did' => $map['id'],
                // 最大人数
                'number' => $num['name'],
                'nid' => $num['id'],
                // 玩家集合
                'numbers' => '',
                // 房间状态
                'team' => 1,
                'thumb' => '',
                // 角色
                'rid' => $role['id'],
                'daqu' => $role['region'],
                // 金额
                'value' => $team_category['feiyong'],
                'second' => $team_category['second'],
                'max' => $team_category['number'],
                'starttime' => '',
            ));
            $this->msg(self::CODE_SUCCESS, '操作成功', $this->db->insert_id());
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    // 组队室详情
    public function show() {
        $this->verify_login();
        $data = $_POST;
        if(!$data) {
            $data = $this->req;
        }
        $team = $this->db->where('id', $data['id'])->get('1_team')->row_array();
        
        if(!$team) {
            $err = '该房间不存在';
        } elseif($team['team'] == 3) {
            $err = '该房间已关闭';
        } else {
            // 角色 大区
            $role = $this->db->where('id', $team['rid'])->get('member_role')->row_array();
            $team['role'] = $role['game'];
            $team['region'] = $this->db->where('id', $role['region'])->get('imt_linkage_data_6')->row_array();
            
            // 打手数 和 玩家总人数
            $count = $this->db->where('id', $team['nid'])->get('linkage_data_4')->row_array()['name'];
            $sum = [];
            for($i = 0; $i < (int)$team['max'] + (int)$count; $i++) {
                $sum[$i] = '';
            }
            $team['sum'] = $sum;
            $numbers = array_merge(array($team['uid']), explode(',', $team['numbers']));
            
            if($numbers) {
                foreach($numbers as $key => $item) {
                    if($item) {
                        if(dr_member_info($item)['groupid'] == 5) {
                            $thugs[$key]['username'] = dr_member_info($item)['username'];
                            $thugs[$key]['uid'] = dr_member_info($item)['uid'];
                        } elseif(dr_member_info($item)['groupid'] == 3) {
                            $wanjia = $item;
                        }
                        $users[$key]['avatar'] = dr_member_info($item)['avatar'];
                        $users[$key]['uid'] = $item;
                        $users[$key]['groupid'] = dr_member_info($item)['groupid'];
                        $users[$key]['username'] = dr_member_info($item)['username']; 
                    }
                }
            }
            $type = ['weixin', 'zhifu'];
            // 账号分配
            $fenpei = $this->db->where('cid', $team['id'])->where('uid', $wanjia)->where('status', 1)->where_in('type', $type)->get('member_paylog')->row_array();
            
            $team['numbers'] = $users;
            $team['thugs'] = array_values($thugs);
            if(count(explode(',', $team['numbers'])) == $team['max'] + $team['number']) {
                $status = 0;
            } else {
                $status = 1;
            }
            $this->msg(self::CODE_SUCCESS, '', ['status' => $status, 'team' => $team, 'fenpei' => $fenpei ? $fenpei : []]);
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    // 操作
    public function enter() {
        
        // $this->verify_login();
        $data = $_POST;
        $member = $this->member;
        // 大区
        $region = $this->db->where('id', $data['rid'])->get('member_role')->row_array()['region'];
        // $cid = $this->_order();
        // ($this->member['groupid'] == 3 && $cid) && exit_json(2, '请先完成上个订单', $cid);
        $team = $this->_search($data['tid'], $data['did'], $region);
        if(is_array($team)) {
            $numbers = $team['numbers'] ? $team['numbers'] .','. $this->uid : $this->uid;
            $this->db->set('numbers', implode(',', array_unique(explode(',', $numbers))))->where('id', $team['id'])->update('imt_1_team');
            $team = $this->db->where('id', $team['id'])->get('1_team')->row_array();
            $this->db->set('cid', $team['id'])->where('id', $data['oid'])->update('member_paylog');
            $this->msg(self::CODE_SUCCESS, '', $team['id']);exit();
        } else {
            $this->msg(self::CODE_ERROR, $team);exit(); 
        }
    }
    
    // 退出房间
    public function out_xiaopang() {
        $this->verify_login();
        $team = $this->db->where('id', $this->req['id'])->get('1_team')->row_array();
        if(!$team) {
            $err = '房间不存在';
        } elseif($team['team'] == 3) {
            $err = '房间已关闭';
        } elseif(!in_array($this->uid, explode(',', $team['numbers']))) {
            $err = '你已退出';
        } else {
            if($this->member['groupid'] == 3 || $this->member['groupid'] == 5) {
                $uids = array_merge(array_diff(explode(',', $numbers), array($this->uid)));
                $this->db->set('numbers', implode(',', $uids))->where('id',  $team['id'])->update('1_team');
            } elseif($this->member['groupid'] == 4) {
                $this->db->set('team', 3)->where('id',  $team['id'])->update('1_team');
                // 团长获得的钱
                $this->db->set('money', 'money' + $team['value'] * dr_var("zdstcfr"), false)->where('uid', $team['uid'])->update('member');
                $this->db->insert('member_paylog', array(
                    'uid' => $team['uid'],
                    'order_number' => SYS_TIME . rand(1000, 9999),
                    'value' => $team['value'] * dr_var("zdstcfr"),
                    'type' => '4',
                    'status' => 1,
                    'note' => $team.'获得'.$team['value'] * dr_var("zdstcfr").'元',
                    'module' => '',
                    'inputtime' => SYS_TIME
                ));
                $dashou = $this->db->where('dashou', $team['id'])->where('status', 1)->get('member_paylog')->row_array()['dashou'];
                // 打手获得的钱
                $this->db->set('money', 'money' + $team['value'] * dr_var("zdstcfr"), false)->where('uid', $dashou)->update('member');
            }
            $this->msg(self::CODE_SUCCESS, '退出成功');
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    
    // 退出房间
    public function out() {
        $this->verify_login();
        $team = $this->db->where('id', $this->req['id'])->get('1_team')->row_array();
        if(!$team) {
            $err = '房间不存在';
        } elseif($team['team'] == 3) {
            $err = '房间已关闭';
        } elseif(!in_array($this->uid, explode(',', $team['numbers']))) {
            $err = '你已退出';
        } else {
            if($this->member['groupid'] == 3 || $this->member['groupid'] == 5) {
                $uids = array_merge(array_diff(explode(',', $numbers), array($this->uid)));
                $this->db->set('numbers', implode(',', $uids))->where('id',  $team['id'])->update('1_team');
            } elseif($this->member['groupid'] == 4) {
                $this->db->set('team', 3)->where('id',  $team['id'])->update('1_team');
                // 团长获得的钱
                $this->db->set('money', 'money' + $team['value'] * dr_var("zdstcfr"), false)->where('uid', $team['uid'])->update('member');
                $this->db->insert('member_paylog', array(
                    'uid' => $team['uid'],
                    'order_number' => SYS_TIME . rand(1000, 9999),
                    'value' => $team['value'] * dr_var("zdstcfr"),
                    'type' => '4',
                    'status' => 1,
                    'note' => $team.'获得'.$team['value'] * dr_var("zdstcfr").'元',
                    'module' => '',
                    'inputtime' => SYS_TIME
                ));
                $dashou = $this->db->where('dashou', $team['id'])->where('status', 1)->get('member_paylog')->row_array()['dashou'];
                // 打手获得的钱
                $this->db->set('money', 'money' + $team['value'] * dr_var("zdstcfr"), false)->where('uid', $dashou)->update('member');
            }
            $this->msg(self::CODE_SUCCESS, '退出成功');
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    // 开始房间
    public function begin() {
        $this->verify_login();
        $team = $this->db->where('id', $_POST['id'])->get('1_team')->row_array();
        if(!$team) {
            $err = '房间不存在';
        } elseif($team['team'] == 3) {
            $err = '房间已关闭';
        } else {
            if($_POST['end'] == 1) {
                $this->db->set('team', 3);
            } else {
                $this->db->set('team', 2);
            }
            $this->db->where('id', $team['id'])->update('1_team');
            $team = $this->db->where('id', $_POST['id'])->get('1_team')->row_array();
            $this->msg(self::CODE_SUCCESS, '操作成功', $team);
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    // 售卖装备
    public function sell() {
        $this->verify_login();
        $team = $this->db->where('id', $_POST['cid'])->get('1_team')->row_array();
        if(!$team) {
            $err = '房间不存在';
        } elseif($team['team'] == 3) {
            $err = '房间已关闭';
        } elseif(!$_POST['equipment']) {
            $err = '装备必填';
        } elseif(!$_POST['price']) {
            $err = '价格必填';
        } else {
            $ins = $_POST;
            $ins['uid'] = $this->uid;
            $ins['inputtime'] = SYS_TIME;
            $ins['updatetime'] = SYS_TIME;
            $this->db->insert('1_equipment_sell', $ins);
            $this->msg(self::CODE_SUCCESS, '', $this->db->insert_id());
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    // 购买装备
    public function buy() {
        $this->verify_login();
        $equipment = $this->db->where('id', $this->req['id'])->get('1_equipment_sell')->row_array();
        if(!$equipment) {
            $err = '没有此装备';
        } elseif($equipment['status'] == 1) {
            $err = '此装备已经被购买';
        } elseif($this->member['money'] < $equipment['price']) {
            $err = '余额不足';
        } else {
            if($this->req['row'] == 1) {
                // 扣除金额
                $this->db->set('money', $this->member['money'] - $equipment['price'])->where('uid', $this->uid)->update('member');
                $this->db->insert('member_paylog', array(
                    'uid' => $this->uid,
                    'order_number' => SYS_TIME . rand(1000, 9999),
                    'value' => $equipment['price'],
                    // 'type' => '1',
                    'status' => 1,
                    'module' => 1,
                    'note' => '购买'.$equipment['equipment'].'装备支付'.$equipment['price'].'元',
                    'inputtime' => SYS_TIME,
                ));
                $this->db->set('gid', $this->uid)->set('status', 1)->where('id', $this->req['id'])->update('1_equipment_sell');
            }
            $this->msg(self::CODE_SUCCESS, '', $equipment);
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    // 举报玩家
    public function report() {
        $this->verify_login();
        $team = $this->db->where('id', $this->req['id'])->get('1_team')->row_array();
        // 总评论
        $result = $this->db->where('uid', $this->uid)->where('cid', $team['id'])->get('1_team_comment_data_0')->result_array();
        
        $numbers = $team['numbers'] ? explode(',', $team['numbers'] . ',' . $team['uid']) : [$team['uid']];
        
        if($result) {
            foreach($result as $key => $item) {
                $jubaowanjia[] = $item['jubaowanjia'];
            }
            $numbers = array_merge(array_diff($numbers, $jubaowanjia));
        }
        $numbers = array_merge(array_diff($numbers, array($this->uid)));
        if(!$team) {
            $err = '暂无该房间';
        } else {
            if($this->req['uid']) {
                // 判断是否评论过该玩家
                $row = $this->db->where('cid', $room['id'])
                                ->where('uid', $this->uid)
                                ->where('jubaowanjia', $this->req['uid'])
                                ->get('1_team_comment_data_0')->row_array();
                if(!in_array($this->req['uid'], $numbers)) {
                    $err = '该玩家不存在';
                } elseif($row) {
                    $err = '你已举报';
                } else {
                    if($this->req['content'] == 0) {
                        $content = '未按约定时间进入';
                    } elseif($this->req['content'] == 1) {
                        $content = '中途退出';
                    } elseif($this->req['content'] == 2) {
                        $content = '其他原因';
                    }
                    $this->db->insert('1_team_comment_data_0', array(
                        'cid' => $team['id'],
                        'uid' => $this->uid,
                        'author' => $this->member['username'],
                        'content' => $content,
                        'avgsort' => 0,
                        'reply' => 0,
                        'inputtime' => SYS_TIME,
                        'jubaowanjia' => $this->req['uid'],
                    ));
                    $this->msg(self::CODE_SUCCESS, '提交成功');exit();
                }
            } else {
                foreach($numbers as $key => $item) {
                    $users[$key]['username'] = dr_member_info($item)['username'];
                    $users[$key]['uid'] = $item;
                    $users[$key]['value'] = $key;
                }
                $this->msg(self::CODE_SUCCESS, '', $users);    
            }
        }
        if(isset($err)) {
           $this->msg(self::CODE_ERROR, $err); 
        }
    }
    
    // 下单
    public function order() {
        $this->verify_login();
        // $cid = $this->_order();
        if(array_search('', $this->req)) {
            $err = '请完善表单信息';
        } else {
            $ins = [
                'uid' => $this->uid,
                'order_number' => SYS_TIME . rand(1000, 9999),
                'phone' => $this->req['phone'] ? $this->req['phone'] : '',
                'cid' => '',
                'zhanghao' => $this->req['zhanghao'] ? $this->req['zhanghao'] : '',
                'password' => $this->req['password'] ? $this->req['password'] : '',
                'role' => $this->req['role'] ? $this->req['role'] : '',
                'value' => $this->req['money'],
                'order' => '',
                'module' => '',
                'note' => '',
                'leixing' => $this->req['Type'] ? $this->req['Type'] : '',
                'inputtime' => SYS_TIME
            ];
            $openid = $this->db->select('openid')->where('uid', $this->uid)->get(SITE_ID . '_wxapp_user')->row_array()['openid'];
            if (!$openid) {
                $this->msg(self::CODE_ERROR, '无法获取openid');
            } elseif($this->req['leixing'] == 2) {
                // 更新账户余额
                $this->db->set('money', $this->member['money'] - $this->req['money'])->where('uid', $this->uid)->update('member');
                $ins['status'] = 1;
                $ins['type'] = 'zhifu';
                $this->db->insert('member_paylog', $ins);
                $this->msg(self::CODE_two, '', ['id' => $this->db->insert_id()]);
            } elseif($this->req['leixing'] == 1) {
                $ins['status'] = 0;
                $ins['type'] = 'weixin';
             $this->db->insert('member_paylog', $ins);
                $pay_id = $this->db->insert_id();
                $pay = require WEBPATH . 'api/pay/weixin/config.php';
                define(APPID, $pay['appid']);
                define(MCHID, $pay['mchid']);
                define(KEY, $pay['key']);
                define(FC_PAY_ID, $pay_id);
                define(APPSECRET, $pay['appsecret']);
                require WEBPATH . 'api/pay/weixin/WxPay.Data.php';
                require WEBPATH . 'api/pay/weixin/WxPay.Api.php';
                require WEBPATH . 'api/pay/weixin/WxPay.JsApiPay.php';
                require WEBPATH . 'api/pay/weixin/WxPay.NativePay.php';
                $input = new WxPayUnifiedOrder();
                $input->SetBody('账户充值');
                $input->SetOut_trade_no("FC-{$pay_id}-{$this->uid}-ROOMS-{$pay_id}");
                $input->SetTotal_fee($this->req['money'] * 100);
                $input->SetTime_start(date('YmdHis', SYS_TIME));
                $input->SetTime_expire(date('YmdHis', SYS_TIME + 7200));
                $input->SetNotify_url(SITE_URL . 'api/pay/weixin/return_url.php');
                $input->SetTrade_type('JSAPI');
                $input->SetProduct_id($pay_id);
                $input->SetOpenid($openid);
                $order = WxPayApi::unifiedOrder($input);
                if (!$order) {
                    $this->msg(self::CODE_ERROR, '未获取到返回数据');
                } elseif (isset($order['code']) && $order['code'] == 0) {
                    $this->msg(self::CODE_ERROR, $order['msg']);
                } elseif ($order['err_code_des']) {
                    $this->msg(self::CODE_ERROR, $order['err_code_des']);
                } else {
                    $result = [
                        'appId' => APPID,
                        'timeStamp' => (string)SYS_TIME,
                        'nonceStr' => $order['nonce_str'],
                        'package' => 'prepay_id=' . $order['prepay_id'],
                        'signType' => 'MD5',
                    ];
                    ksort($result);
              $string = '';
              foreach ($result as $k => $v) {
                   $string .= ($string ? '&' : '') . $k . '=' . $v;
              }
              $result['paySign'] = strtoupper(md5($string . '&key=' . KEY));
              $result['str'] = KEY;
              unset($result['appid']);
                    $this->msg(self::CODE_SUCCESS, '', ['id' => $this->$pay_id, 'result' => $result]);
                }
            } else {
                
            }
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    // 打手接单
    public function dashou() {
        $this->verify_login();
        $data = $_POST;
        $order = $this->db->where('cid', $data['cid'])->where('status', 1)->get('member_paylog')->row_array();
        $this->db->set('dashou', $data['content'])->where('id', $order['id'])->update('member_paylog');
        $role = $this->db->where('id', $order['role'])->get('member_role')->row_array();
        if($order) {
            $this->msg(self::CODE_SUCCESS, '', ['order' => $order, 'role' => $role]);
        } else {
            $this->msg(self::CODE_SUCCESS, '暂无该账号');
        }
    }
    
    // 判断是否有正在进行的订单
    protected function _order() {
        $row = $this->db->where('team !=', 3)->get('1_team')->result_array();
        foreach($row as $key => $item) {
            if(in_array($this->uid, explode(',', $item['numbers'])) || $this->uid == $item['uid']) {
                return $item['id'];
            }
        }
    }
    
    /**
     *  $tid      地图id
     *  $did      地图类型id
     *  $region   大区
     *  $team     状态  1未开启 2已开启 3已关闭
     **/
    
    protected function _search($tid, $did, $region) {
        
        $team = ['1', '2'];
        // $data = $this->db->where('uid !=', $this->uid)->where('did', $tid)->where('tid', $did)->where('daqu', $region)->where_in('team', $team)->order_by('id')->get('imt_1_team')->result_array();
        $data = $this->db->where('did', $tid)->where('tid', $did)->where('daqu', $region)->where_in('team', $team)->order_by('id')->get('imt_1_team')->result_array();
        // return $this->db->last_query();
        if($data) {
            foreach($data as $key => $item) {
                $bully = [];
                $wanjia = [];
                if($item['numbers']) {
                    $users = $this->db->where_in('uid', explode(',', $item['numbers']))->get('member')->result_array();
                    foreach($users as $value) {
                        if($value['groupid'] == 5) {
                            $bully[] = $value;
                        } elseif($value['groupid'] == 3) {
                            $wanjia[] = $value;
                        }
                    }
                }
                if(!in_array($this->uid, $bully) 
                        && $this->member['groupid'] == 5 
                        && count($bully) <= $item['number']) {
                    return $item;
                } elseif(!in_array($this->uid, $wanjia)
                        && $this->member['groupid'] == 3 
                        && count($wanjia) <= $item['max']) {
                    return $item;
                } elseif($this->uid != $team['uid'] && $this->member['groupid'] == 4) {
                    return '角色不符';
                } else {
                    return '暂无符合的房间';
                }
            }
        } else {
            return '暂无符合的房间';
        }
        
    }

}
<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once FCPATH . 'branch/fqb/D_Wxapp.php';

class Room extends D_Wxapp {

    private $wa;
    private $prefix;

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
        $this->load->model('wxapp_model');
        $this->wa = $this->get_cache('wxapp-'.SITE_ID);
        $this->prefix = SITE_ID.'_wxapp';
        
    }
    
    // 预组队房间
    public function index() {
        $this->verify_login();
        $catid = $this->db->where('id', 1)->get('1_room_category')->row_array()['id'];
        if($this->req['search']) {
            $this->db->where('id', $this->req['search']);
        }
        // 房间状态
        $fjzt = ['0', '1', '2'];
        // 所有房间
        $room = $this->db->where('catid', $catid)->where_in('fjzt', $fjzt)->where('starttime <', SYS_TIME)->order_by('id desc')->get('1_room')->result_array();
        if(!$catid || !$room) {
            $this->msg(self::CODE_ERROR, '暂无房间', $catid);
        }else{
            // 自己在房间的状态
            foreach($room as $key => $item) {
                if(in_array($this->uid, explode(',', $item['uids']))) {
                    $room[$key]['user_status'] = 1; 
                } elseif($this->uid == $item['uid']) {
                    $room[$key]['user_status'] = 4;
                } else {
                    $room[$key]['user_status'] = 3;
                }
                $room[$key]['thumb'] = dr_thumb($item['thumb']);
                $room[$key]['starttime'] = date('Y-m-d H:i', $item['starttime']);
            }
            $this->msg(self::CODE_SUCCESS, '', ['room' => $room, 'catid' => $catid]);
        }
    }
    
    // 创建房间页面
    public function add() {
        $this->verify_login();
        $room_category = $this->db->where('id', $this->req['catid'])->get('1_room_category')->row_array();
        
        // 房间类型
        $mapkind = $this->db->where('pid', 0)->order_by('id')->get('linkage_data_3')->result_array();
        // 地图
        if($this->req['id']) {
            $this->db->where('pid', $this->req['id']);
        } else {
            $this->db->where('pid', $mapkind['0']['id']);
        }
        $map = $this->db->get('linkage_data_3')->result_array();
        
        // 人数
        if($this->req['pid']) {
            $this->db->where('pid', $this->req['pid']);
        } else {
            $this->db->where('pid', $map['0']['id']);
        }
        $num = $this->db->get('linkage_data_3')->result_array();
        
        // 所拥有的角色
        $role = $this->db->where('uid', $this->uid)->get('member_role')->result_array();
        foreach ($role as $index => $item) {
            $name = $this->db->where('id', $item['region'])->get('linkage_data_6')->row_array()['name'];
            $role[$index]['role'] = $item['game'] .'-'.  $name;
        }
        
        if($mapkind) {
            $this->msg(self::CODE_SUCCESS, '', ['mapkind' => $mapkind, 'map' => $map, 'num' => $num, 'role' => $role, 'room_category' => $room_category]);
        } else {
            $this->msg(self::CODE_ERROR);
        }
    }
    
    
    // 创建房间页面
    public function add_xiaopang() {
        $this->verify_login();
        $room_category = $this->db->where('id', $this->req['catid'])->get('1_room_category')->row_array();
        
        // 房间类型
        $mapkind = $this->db->where('pid', 0)->order_by('id')->get('linkage_data_3')->result_array();
        // 地图
        if($this->req['id']) {
            $this->db->where('pid', $this->req['id']);
        } else {
            $this->db->where('pid', $mapkind['0']['id']);
        }
        $map = $this->db->get('linkage_data_3')->result_array();
        
        // 人数
        if($this->req['pid']) {
            $this->db->where('pid', $this->req['pid']);
        } else {
            $this->db->where('pid', $map['0']['id']);
        }
        $num = $this->db->get('linkage_data_3')->result_array();
        
        // 所拥有的角色
        $role = $this->db->where('uid', $this->uid)->get('member_role')->result_array();
        foreach ($role as $index => $item) {
            $name = $this->db->where('id', $item['region'])->get('linkage_data_6')->row_array()['name'];
            $role[$index]['role'] = $item['game'] .'-'.  $name;
        }
        
        // $array = array('mapkind' => $mapkind, 'map' => $map, 'num' => $num, 'role' => $role, 'room_category' => $room_category);
        // var_dump($array);
        // die;
        
        if($mapkind) {
            $this->msg(self::CODE_SUCCESS, '', ['mapkind' => $mapkind, 'map' => $map, 'num' => $num, 'role' => $role, 'room_category' => $room_category]);
        } else {
            $this->msg(self::CODE_ERROR);
        }
    }
    
    // 创建房间
    public function add_room() {
        $this->verify_login();
        $room_category = $this->db->where('id', $this->req['catid'])->get('1_room_category')->row_array();
        // 地图类型
        $mapkind = $this->db->where('id', $this->req['did'])->get('linkage_data_3')->row_array();
        // 地图
        $map = $this->db->where('id', $this->req['tid'])->get('linkage_data_3')->row_array();
        //人数
        $num = $this->db->where('id', $this->req['rid'])->get('linkage_data_3')->row_array();
        $user = dr_member_info($this->uid);
        if(array_search(',', $this->req)) {
            $err = '请完善表单信息';
        } elseif($user['money'] - $room_category['feiyong'] < 0) {
            $err = '账户余额不足';
        } elseif(!$mapkind && !$map && !$num && !$room_category) {
            $err = '房间参数缺失';
        } else {
            $fjzt = strtotime($this->req['start']) < SYS_TIME ? 0 : 1;
            // 创建房间,扣除金额
            $this->db->set('money', $user['money'] - $room_category['feiyong'])->where('uid', $this->uid)->update('member');
            
            // 添加房间
            $this->db->insert('1_room', array(
                'catid' => $room_category['id'],
                'uid' => $this->uid,
                'author' => $user['username'],
                'status' => 9,
                'inputtime' => SYS_TIME,
                'updatetime' => SYS_TIME,
                // 地图类型
                'title' => $mapkind['name'],
                'did' => $mapkind['id'],
                // 地图
                'dtmc' => $map['name'],
                'tid' => $map['id'],
                // 最大人数
                'wanjiarenshu' => $num['name'],
                'rid' => $num['id'],
                // 玩家
                'uids' => '',
                // 房间状态
                'fjzt' => $fjzt,
                'thumb' => '',
                // 角色
                'role' => $this->req['role'],
                // 金额
                'fangjianjine' => $room_category['feiyong'],
                'starttime' => strtotime($this->req['start']),
                'endtime' => '',
            ));
            
            // 加入记录流水
            $this->db->insert('member_paylog', array(
                'uid' => $this->uid,
                'order_number' => SYS_TIME . rand(1000, 9999),
                'value' => '-'.$room_category['feiyong'],
                'type' => '1',
                'status' => 1,
                'module' => 1,
                'note' => '创建'.$this->db->insert_id().'预组队房间支付'.$room_category['feiyong'].'元',
                'inputtime' => SYS_TIME,
            ));
            
            $this->msg(self::CODE_SUCCESS, '创建成功');
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    // 创建房间
    public function add_room_xiaopang() {
        $this->verify_login();
        $room_category = $this->db->where('id', $this->req['catid'])->get('1_room_category')->row_array();
        // 地图类型
        $mapkind = $this->db->where('id', $this->req['did'])->get('linkage_data_3')->row_array();
        // 地图
        $map = $this->db->where('id', $this->req['tid'])->get('linkage_data_3')->row_array();
        //人数
        $num = $this->db->where('id', $this->req['rid'])->get('linkage_data_3')->row_array();
        $user = dr_member_info($this->uid);
        if(array_search(',', $this->req)) {
            $err = '请完善表单信息';
        } elseif($user['money'] - $room_category['feiyong'] < 0) {
            $err = '账户余额不足';
        } elseif(!$mapkind && !$map && !$num && !$room_category) {
            $err = '房间参数缺失';
        } else {
            $fjzt = strtotime($this->req['start']) < SYS_TIME ? 0 : 1;
            // 创建房间,扣除金额
            $this->db->set('money', $user['money'] - $room_category['feiyong'])->where('uid', $this->uid)->update('member');
            
            // 添加房间
            $this->db->insert('1_room', array(
                'catid' => $room_category['id'],
                'uid' => $this->uid,
                'author' => $user['username'],
                'status' => 9,
                'inputtime' => SYS_TIME,
                'updatetime' => SYS_TIME,
                // 地图类型
                'title' => $mapkind['name'],
                'did' => $mapkind['id'],
                // 地图
                'dtmc' => $map['name'],
                'tid' => $map['id'],
                // 最大人数
                'wanjiarenshu' => $num['name'],
                'rid' => $num['id'],
                // 玩家
                'uids' => '',
                // 房间状态
                'fjzt' => $fjzt,
                'thumb' => '',
                // 角色
                'role' => $this->req['role'],
                // 金额
                'fangjianjine' => $room_category['feiyong'],
                'starttime' => strtotime($this->req['start']),
                'endtime' => '',
            ));
            
            // 加入记录流水
            $this->db->insert('member_paylog', array(
                'uid' => $this->uid,
                'order_number' => SYS_TIME . rand(1000, 9999),
                'value' => '-'.$room_category['feiyong'],
                'type' => '1',
                'status' => 1,
                'module' => 1,
                'note' => '创建'.$this->db->insert_id().'预组队房间支付'.$room_category['feiyong'].'元',
                'inputtime' => SYS_TIME,
            ));
            
            $this->msg(self::CODE_SUCCESS, '创建成功');
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    // 房间详情
    public function show() {
        $this->verify_login();
        $room = $this->db->where('id', $this->req['id'])->get('1_room')->row_array();
        $uids = $room['uids'] ? explode(',', $room['uid'] . ',' . $room['uids']) : [$room['uid']];
        if(!$room) {
            $err = '暂无该房间';
        } elseif($room['fjzt'] == 3) {
            $err = '该房间已关闭';
        } else {
            if($this->uid == $room['uid']) {
                $room['user_status'] = 1;
            } elseif(in_array($this->uid, explode(',', $room['uids']))) {
                $room['user_status'] = 2;   
            }
            foreach($uids as $key => $item) {
                $users[$key]['username'] = dr_member_info($item)['username'];
                $users[$key]['avatar'] = dr_member_info($item)['avatar'];
                $users[$key]['uid'] = $item;
            }
            $room['users'] = $users;
            $room['starttime'] = date('Y-m-d H:i', $room['starttime']);
            // 所在大区
            $role = $this->db->where('id', $room['role'])->get('member_role')->row_array();
            $room['region'] = $this->db->where('id', $role['region'])->get('linkage_data_6')->row_array()['name'];
            $this->msg(self::CODE_SUCCESS, '', $room);
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    // 加入退出房间
    public function operation() {
        $this->verify_login();
        $room = $this->db->where('id', $this->req['id'])->get('1_room')->row_array();
        if(!$room) {
            $err = '暂无该房间';
        } elseif($room['fjzt'] == 3) {
            $err = '该房间已关闭';
        } else {
            if($this->req['out']) {
                // 退出房间操作
                if($this->uid == $room['uid']) {
                    $this->db->set('fjzt', 3)->set('endtime', SYS_TIME)->where('id', $this->req['id'])->update('1_room');
                } else {
                    $uids = array_merge(array_diff(explode(',', $room['uids']), array($this->uid)));
                    $this->db->set('uids', implode(',', $uids))->where('id', $this->req['id'])->update('1_room');
                }
                $this->msg(self::CODE_SUCCESS, '操作成功', $room['id']);
            } else {
                if($room['fjzt'] == 1 || $room['starttime'] < SYS_TIME) {
                    $err = '该房间已开启';
                } elseif(count(array_merge(explode(',', $room['uids']), array($room['uid']))) > $room['wanjiarenshu']) {
                    $err = '该房间已满';
                } elseif($this->member['money'] < $room['fangjianjine']) {
                    $err = '账户余额不足';
                } else {
                    $this->db->set('money', $this->member['money'] - $room['fangjianjine'])->where('uid', $this->uid)->update('member');
                    // 记录财务流水
                    $this->db->insert('member_paylog', array(
                        'uid' => $this->uid,
                        'order_number' => SYS_TIME . rand(1000, 9999),
                        'value' => '-'.$room['fangjianjine'],
                        'type' => '2',
                        'status' => 1,
                        'module' => 1,
                        'note' => '加入'.$room['id'].'预组队房间支付'.$room['fangjianjine'].'元',
                        'inputtime' => SYS_TIME,
                    ));
                    $uids = $room['uids'] ? $room['uids'] .','. $this->uid : $this->uid;
                    // $sqzdwj = $room['sqzdwj'] ? $room['sqzdwj'] .','. $this->uid : $this->uid;
                    
                    // $this->db->set('sqzdwj', $sqzdwj)->where('id', $this->req['id'])->update('1_room');
                    $this->db->set('uids', $uids)->where('id', $this->req['id'])->update('1_room');
                    $this->msg(self::CODE_SUCCESS, '加入成功');
                }
            }
        }
        if(isset($err)) {
            $this->msg(self::CODE_ERROR, $err);
        }
    }
    
    // 举报玩家
    public function report() {
        $this->verify_login();
        $room = $this->db->where('id', $this->req['id'])->get('1_room')->row_array();
        // 总评论
        $result = $this->db->where('uid', $this->uid)->where('cid', $room['id'])->get('1_room_comment_data_0')->result_array();
        $uids = $room['uids'] ? explode(',', $room['uids'] . ',' . $room['uid']) : [$room['uid']];
        if($result) {
            foreach($result as $key => $item) {
                $jubaowanjia[] = $item['jubaowanjia'];
            }
            $uids = array_merge(array_diff($uids, $jubaowanjia));
        }
        $uids = array_merge(array_diff($uids, array($this->uid)));
        
        if(!$room) {
            $err = '暂无该房间';
        } else {
            if($this->req['uid']) {
                // 判断是否评论过该玩家
                $row = $this->db->where('cid', $room['id'])
                                ->where('uid', $this->uid)
                                ->where('jubaowanjia', $this->req['uid'])
                                ->get('1_room_comment_data_0')->row_array();
                if(!in_array($this->req['uid'], $uids)) {
                    $err = '该玩家不存在';
                } elseif($row) {
                    $err = '你已评论';
                } else {
                    if($this->req['content'] == 0) {
                        $content = '未按约定时间进入';
                    } elseif($this->req['content'] == 1) {
                        $content = '中途退出';
                    } elseif($this->req['content'] == 2) {
                        $content = '其他原因';
                    }
                    $this->db->insert('1_room_comment_data_0', array(
                        'cid' => $room['id'],
                        'uid' => $this->uid,
                        'author' => $this->member['username'],
                        'content' => $content,
                        'avgsort' => 0,
                        'reply' => 0,
                        'inputtime' => SYS_TIME,
                        'jubaowanjia' => $this->req['uid'],
                    ));
                    $this->msg(self::CODE_SUCCESS, '提交成功');die();
                }
            } else {
                foreach($uids as $key => $item) {
                    $users[$key]['username'] = dr_member_info($item)['username'];
                    $users[$key]['uid'] = $item;
                    $users[$key]['value'] = $key;
                }
                
                $this->msg(self::CODE_SUCCESS, '', $users);    
            }
        }
        if(isset($err)) {
           $this->msg(self::CODE_ERROR, $err); 
        }
    }
    
    // 结束后分润
    public function branch() {
        $this->verify_login();
        $room = $this->db->where('id', $this->req['id'])->get('1_room')->row_array();
        $data = $this->db->select("count('jubaowanjia') as count, jubaowanjia")->group_by('jubaowanjia')->where('cid', $room['id'])->get('1_room_comment_data_0')->result_array();
        if($data) {
            foreach($data as $key => $item) {
                if($intem['count'] >= (int)($room['wanjiarenshu'] / 2)) {
                    $uids[] = $item['jubaowanjia'];
                }
            }
            $money = $room['wanjiarenshu'] * $room['fangjianjine'] - ($room['wanjiarenshu'] * $room['fangjianjine']) * dr_var("yzdptfl");
            $uids = array_diff(explode(',', $room['uids']), $uids);
            foreach($uids as $key => $item) {
                $this->db->set('money', 'money' + number_format($money / count($uids), 2), 'false')->where('uid', $item)->update('member');
                $this->db->insert('member_paylog', array(
                    'uid' => $item,
                    'order_number' => SYS_TIME . rand(1000, 9999),
                    'value' => number_format($money / count($uids), 2),
                    'type' => '1',
                    'status' => 1,
                    'note' => '预组队'.$room['id'].'结束,获得'.number_format($money / count($uids), 2).'元',
                    'module' => '',
                    'inputtime' => SYS_TIME
                ));
            }
        }
        
    }
}

 

有帮助(- 没帮助(-