模型层
/*
* 进房间数据处理
*
*/
public function joinRoom_deal($user,$numb)
{
// 房间信息
if($room = Db::name('room')->where('number',$numb)->field('id,zhuang,users,ready,lives,times,number,status,create_time,update_time')->find()){
// 坐下玩家组
$users = json_decode($room['users'],true);
// 空位组
$list_e = [];
// 玩家详情信息
$list_u = [];
// 计数是否首个进房
$num = 0;
$lives_all = Gateway::getUidListByGroup(config('prefix').$room['id']);
// 遍历拿到玩家详情以及空位的key
foreach ($users as $key => $val) {
if($val){
if(!$room['status']){
if(in_array($val,$lives_all)){
$list_u[$key] = Db::name('member')
->where('uid',$val)
->field('uid,wechat_openid,username,nickname,sex,userpic,is_agent,pid,agent_level,cost,total_bonus,islock')
->find();
}else{
$users[$key] = 0;
$list_u[$key] = [];
$list_e[] = $key;
$num++;
}
}else{
$list_u[$key] = Db::name('member')
->where('uid',$val)
->field('uid,wechat_openid,username,nickname,sex,userpic,is_agent,pid,agent_level,cost,total_bonus,islock')
->find();
}
}else{
$list_u[$key] = [];
$list_e[] = $key;
$num++;
}
}
// 有空位则加入
if(!empty($list_e) && !in_array($user['uid'],$users)){
// 打乱
shuffle($list_e);
// 弹出
$k = array_shift($list_e);
// 进详情组
$list_u[$k] = Db::name('member')->where('uid',$user['uid'])
->field('uid,wechat_openid,username,nickname,sex,userpic,is_agent,pid,agent_level,cost,total_bonus,islock')
->find();
// 进房间users组
$users[$k] = $user['uid'];
// 排序
ksort($users);
// 这里需要给所有人发送
$arr_1 = ['type'=>'joinRoom_all','code'=>200,'data'=>$list_u];
Gateway::sendToGroup(config('prefix').$room['id'],json_encode($arr_1));
}
if(in_array($user['uid'],$users)){
$k = array_search($user['uid'],$users);
$k++;
}else{
$k = 0;
}
// 首个进房
if($num==6){
$room['zhuang'] = $k - 1;//轮6次换一次庄家
}
// 准备组
$ready = json_decode($room['ready'],true);
// 排序
ksort($ready);
// 检查是否已进过房间
if($user_r = Db::name('roomman')->where('uid',$user['uid'])->where('rid',$room['id'])->find()){
Db::startTrans();
try{
$user_r['status'] = 1; //是否离开 0是/1否
$user_r['update_time'] = time();
Db::name('roomman')->update($user_r);
$room['users'] = json_encode($users); // uid用户组
$room['lives'] = count($lives_all)+1; // 在线玩家
$res = Db::name('room')->update($room);
$room['list_u'] = $list_u; // 玩家详情信息
$room['list_r'] = $ready; // 准备的uid用户组
$room['is_sit'] = $k; // 加入房间后 你的座位号
$arr = $this->GameStatus($room);
// 提交事务
Db::commit();
}catch(\Exception $e){
// 回滚
Db::rollback();
$arr = ['type'=>'joinRoom','code'=>400,'msg'=>'系统繁忙'];
}
}else{
Db::startTrans();
try{
$roomman['uid'] = $user['uid'];
$roomman['rid'] = $room['id'];
$roomman['create_time'] = $roomman['update_time'] = time();
$res = Db::name('roomman')->insert($roomman);
$room['users'] = json_encode($users);
$room['lives'] = count($lives_all)+1;
$ress = Db::name('room')->update($room);
$room['list_u'] = $list_u;
$room['list_r'] = $ready;
$room['is_sit'] = $k;//加入房间后 你的座位号
$arr = $this->GameStatus($room);
// 提交事务
Db::commit();
}catch(\Exception $e){
// 回滚
Db::rollback();
$arr = ['type'=>'joinRoom','code'=>400,'msg'=>'系统繁忙'];
}
}
}else{
$arr = ['type'=>'joinRoom','code'=>400,'msg'=>'房间不存在'];
}
return $arr;
}
/*
* 游戏状态
*
*/
protected function GameStatus($room)
{
// 正在下注中
if($room['status']){
// 游戏状态标识
if($room['update_time'] - time() <= 5 && $room['update_time'] - time() > 0){
// 准备中
$room['sign'] = 3;
$room['waittime'] = $room['update_time'] - time();
}else{
$room['sign'] = 1;
// 期号信息
$room['end'] = Db::name('result')->where('status',0)->field('id,rid,uid,users,status,create_time')->order('id desc')->find();
// 剩余下注秒数,20s
$room['waittime'] = $room['end']['create_time'] - time();
}
}else{
// 开奖信息
if($end = Db::name('result')->where('status',1)->field('id,rid,uid,users,result,status,create_time,update_time')->order('id desc')->find()){
// 开牌时间,20s
if(time() - $end['update_time'] <= 20){
// 游戏状态标识,2开牌中
$room['sign'] = 2;
// 结果集
$end['result'] = json_decode($end['result'],true);
// 剩余开牌时间
$room['waittime'] = $end['update_time'] + 20 - time();
// 开奖信息
$room['end'] = $end;
}else{
// 等待着准备
$room['sign'] = 4;
$room['waittime'] = 0;
}
}else{
$room['sign'] = 4;
$room['waittime'] = 0;
}
}
$room['qihao'] = Db::name('result')->where('rid',$room['id'])->order('id desc')->value('id');//最近开奖期号
$arr = ['type'=>'joinRoom','code'=>200,'data'=>$room];
return $arr;
}
控制器层
/*
* 加入房间,1是下注中,2是开牌中,3是准备中,4是等待准备
*
*/
public function joinRoom($client_id,$data)
{
// 玩家信息
$user = $this->userInfo;
// 验证
$result = $this->validate($data,'Jroom');
if($result!==true){
$arr = ['type'=>'joinRoom','code'=>400,'msg'=>$result];
Gateway::sendToUid($user['uid'],json_encode($arr));
return;
}
// 房间id
$numb = $data['numb'];
$model = osc_model('push','game');
$result = $model->joinRoom_deal($user,$numb);
if($result['code'] == 200){
Gateway::joinGroup($client_id,config('prefix').$result['data']['id']);
}
// 返回
Gateway::sendToUid($user['uid'],json_encode($result));
}
涉及4张表:room:房间表;roomman:房间关联表;member:会员表;result:开奖信息表。
CREATE TABLE `sg_room` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`rate` int(11) NOT NULL DEFAULT '0' COMMENT '倍率',
`remainder` int(11) NOT NULL DEFAULT '0' COMMENT '牌数',
`z_max` int(11) NOT NULL DEFAULT '0' COMMENT '上庄限额',
`z_min` int(11) NOT NULL DEFAULT '0' COMMENT '下庄限额',
`join_min` int(11) NOT NULL DEFAULT '0' COMMENT '进房额度',
`keep` int(11) NOT NULL DEFAULT '0' COMMENT '最大连庄数',
`desc` varchar(10) NOT NULL DEFAULT '' COMMENT '描述',
`users` varchar(256) NOT NULL DEFAULT '[0,0,0,0,0,0]' COMMENT '玩家数组',
`ready` varchar(256) DEFAULT '[0,0,0,0,0,0]' COMMENT '准备人数',
`zhuang` int(11) NOT NULL DEFAULT '0' COMMENT '1-10代表位置1-10',
`times` int(11) NOT NULL DEFAULT '0' COMMENT '坐庄次数',
`type` int(11) NOT NULL DEFAULT '0' COMMENT '底分',
`status` int(11) NOT NULL COMMENT '是否结束',
`lives` int(11) NOT NULL DEFAULT '0' COMMENT '在线人数',
`number` int(11) NOT NULL DEFAULT '0' COMMENT '加庄,默认0不加庄',
`create_time` int(11) NOT NULL,
`update_time` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `number` (`number`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `sg_roomman` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL COMMENT '玩家id',
`rid` int(11) NOT NULL COMMENT '房间id',
`times` int(11) DEFAULT '0' COMMENT '坐庄次数',
`sort` int(11) DEFAULT '0' COMMENT '上庄队列',
`status` int(11) NOT NULL DEFAULT '1' COMMENT '是否已离开',
`create_time` int(11) NOT NULL COMMENT '首次时间',
`update_time` int(11) NOT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `sg_member` (
`uid` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户id',
`reg_type` varchar(20) DEFAULT NULL,
`wechat_openid` varchar(128) DEFAULT NULL,
`username` char(20) DEFAULT NULL COMMENT '用户名',
`password` char(128) DEFAULT NULL COMMENT '密码',
`checked` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否审核',
`address_id` int(8) NOT NULL DEFAULT '0',
`nickname` char(20) DEFAULT NULL COMMENT '昵称',
`sex` tinyint(2) NOT NULL DEFAULT '0',
`userpic` varchar(255) DEFAULT NULL COMMENT '会员头像',
`is_agent` tinyint(2) NOT NULL DEFAULT '0' COMMENT '是否是代理商',
`pid` mediumint(8) NOT NULL DEFAULT '0' COMMENT '上级id',
`agent_level` mediumint(8) NOT NULL DEFAULT '0' COMMENT '代理级别',
`cost` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '账号余额',
`total_bonus` decimal(9,3) NOT NULL DEFAULT '0.000' COMMENT '代理商奖金',
`points` mediumint(8) NOT NULL DEFAULT '0' COMMENT '积分',
`cash_points` mediumint(8) NOT NULL DEFAULT '0' COMMENT '已经兑换积分',
`wish` smallint(5) NOT NULL DEFAULT '0' COMMENT '收藏的数量',
`regdate` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '注册时间',
`lastdate` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最后登录时间',
`regip` char(15) DEFAULT NULL COMMENT '注册ip',
`lastip` char(15) DEFAULT NULL COMMENT '上次登录ip',
`loginnum` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '登陆次数',
`email` char(32) DEFAULT NULL COMMENT '电子邮箱',
`telephone` varchar(20) DEFAULT NULL,
`groupid` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '用户组id',
`areaid` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '地区id',
`message` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否有短消息',
`islock` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否锁定',
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='会员表';
CREATE TABLE `sg_result` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`rid` int(11) NOT NULL COMMENT '房间id',
`uid` int(11) NOT NULL COMMENT '庄家id',
`status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否结束',
`users` varchar(256) NOT NULL COMMENT '该局玩家id组',
`result` text NOT NULL COMMENT '结果集',
`num` text NOT NULL COMMENT '牛1-牛9结果集',
`create_time` int(11) NOT NULL COMMENT '创建时间',
`update_time` int(11) NOT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1DEFAULT CHARSET=utf8;