php斗地主判断大小,地主规则引入
<?php
//是不是单
function isDan($temp) {
if (count($temp) != 1) {
return false;
} else {
return '{"type":"dan","value":"'.$temp[0].'"}';
}
}
//是不是对
function isDui($temp) {
if (count($temp) != 2 || $temp[0] != $temp[1]) {
return false;
} else {
return '{"type":"dui","value":"'.$temp[0]*$temp[1].'"}';
}
}
//是不是火箭
function isHuojian($temp) {
if (count($temp) == 2 && (($temp[0] == 100 && $temp[1] == 99) || ($temp[0] == 99 || $temp[1] == 100))) {
return '{"type":"huojian","value":"99999999"}';
} else {
return false;
}
}
//是不是三个一样的
function isSan($temp) {
if (count($temp) != 3 || ($temp[0] != $temp[1] || $temp[1] != $temp[2])) {
return false;
} else {
return '{"type":"san","value":"'.$temp[0] * $temp[1] * $temp[2].'"}';
}
}
//是不是炸弹
function isZha($temp) {
if (count($temp) != 4 || ($temp[0] != $temp[1] || $temp[1] != $temp[2] || $temp[2] != $temp[3])) {
return false;
} else {
return '{"type":"zhadan","value":"'.$temp[0] * $temp[1] * $temp[2] * $temp[3].'"}';
}
}
//是不是三带一
function isSandaiyi($temp) {
sort($temp);
if (count($temp) == 4) {
if ($temp[0] == $temp[1] && $temp[1] == $temp[2] && $temp[2] != $temp[3]) {
$value = $temp[0] * $temp[1] * $temp[2];
return '{"type":"sandaiyi","value":"'.$value.'"}';
} else if ($temp[0] != $temp[1] && $temp[1] == $temp[2] && $temp[2] == $temp[3]) {
$value = $temp[1] * $temp[2] * $temp[3];
return '{"type":"sandaiyi","value":"'.$value.'"}';
} else {
return false;
}
} else {
return false;
}
}
//是不是三带二
function isSandaiEr($temp) {
sort($temp);
if (count($temp) == 5) {
if ($temp[0] == $temp[1] && $temp[1] == $temp[2] && $temp[2] != $temp[3] && $temp[3] == $temp[4]) {
$value = $temp[0] * $temp[1] * $temp[2];
return '{"type":"sandaier","value":"'.$value.'"}';
} else if ($temp[0] == $temp[1] && $temp[1] != $temp[2] && $temp[2] == $temp[3] && $temp[3] == $temp[4]) {
$value = $temp[2] * $temp[3] * $temp[4];
return '{"type":"sandaier","value":"'.$value.'"}';
} else {
return false;
}
} else {
return false;
}
}
//是不是顺子
function isShunzi($temp) {
if (count($temp) < 5) {
return false;
}
sort($temp);
$value = 0;
for ($i = 0; $i < count($temp) - 1; $i++) {
if ($temp[$i+1] - $temp[$i] != 1) {
return false;
}
$value = $value + $temp[$i];
}
return '{"type":"shunzi","value":"'.$value.'"}';
}
//是不是连对
function isLiandui($temp) {
sort($temp);
if (count($temp) < 6 || count($temp) % 2 != 0) {
return false;
}
for ($i = 0; $i < count($temp) - 1; $i = $i + 2) {
if ($temp[$i] != $temp[$i+1]) {
return false;
}
if ($i < count($temp) - 2) {
if ($temp[$i] - $temp[$i+2] != -1) {
return false;
}
}
}
$value = 0;
for ($i = count($temp) - 1; $i >= 0; $i--) {
$value = $temp[$i] + $value;
}
return '{"type":"liandui","value":"'.$value.'"}';
}
//是不是飞机带单
function isFeijiDaiDan($temp) {
if (count($temp) < 8) {
return false;
}
$arrs = array_count_values($temp);
$list = [];
foreach ($arrs as $k => $v) {
if ($v == 3) {
$list[] = $k;
}
}
sort($list);
$value = array_product($list);
for ($i=0; $i < count($list) - 1; $i++) {
if ($list[$i] - $list[$i + 1] != -1) {
return false;
}
}
$pokers = count($list) * 3 + count($list);
if ($pokers != count($temp)) {
return false;
}
return '{"type":"feijidaidan","value":"'.$value.'"}';
}
//是不是飞机带双
function isFeijiDaiShuang($temp) {
if (count($temp) < 8) {
return false;
}
$arrs = array_count_values($temp);
$list = [];
foreach ($arrs as $k => $v) {
if ($v == 3) {
$list[] = $k;
}
}
sort($list);
$value = array_product($list);
for ($i=0; $i < count($list) - 1; $i++) {
if ($list[$i] - $list[$i + 1] != -1) {
return false;
}
}
$pokers = count($list) * 3 + count($list) * 2;
if ($pokers != count($temp)) {
return false;
}
$list = [];
foreach ($arrs as $k => $v) {
if ($v == 2) {
$list[] = $k;
}
}
$pokers = count($list);
if (count($list) != $pokers) {
return false;
}
return '{"type":"feijidaishuang","value":"'.$value.'"}';
}
//是不是飞机不带
function isFeijiBuDai($temp) {
sort($temp);
if (count($temp) % 3 != 0 || count($temp) < 3) {
return false;
} else {
$value = 0;
$index = [];
for ($i = 0; $i + 2 < count($temp); $i = $i + 3) {
if ($i != count($temp)) {
if (!isSan([$temp[$i],$temp[$i+1],$temp[$i+2]])) {
return false;
}
$value = $value + $temp[$i] + $temp[$i + 1] + $temp[$i+2];
$index[] = $temp[$i];
}
}
sort($index);
for ($i=0; $i < count($index) - 1; $i++) {
if ($index[$i] - $index[$i + 1] != -1) {
return false;
}
}
return '{"type":"feijibudai","value":"'.$value.'"}';
}
}
//是不是四带一
function isSiDaiYi($temp) {
sort($temp);
if (count($temp) == 5 && $temp[0] == $temp[1] && $temp[1] == $temp[2] && $temp[2] == $temp[3]) {
$value = $temp[0] + $temp[1] + $temp[2] + $temp[3];
return '{"type":"sidaiyi","value":"'.$value.'"}';
} else if (count($temp) == 5 && $temp[1] == $temp[2] && $temp[2] == $temp[3] && $temp[3] == $temp[4]) {
$value = $temp[1] + $temp[2] + $temp[3] + $temp[4];
return '{"type":"sidaiyi","value":"'.$value.'"}';
} else {
return false;
}
}
//是不是四带二
function isSiDaiEr($temp) {
sort($temp);
if (count($temp) == 6 && $temp[0] == $temp[1] && $temp[1] == $temp[2] && $temp[2] == $temp[3]) {
$value = $temp[0] + $temp[1] + $temp[2] + $temp[3];
return '{"type":"sidaier","value":"'.$value.'"}';
} else if (count($temp) == 6 && $temp[1] == $temp[2] && $temp[2] == $temp[3] && $temp[3] == $temp[4]) {
$value = $temp[2] + $temp[3] + $temp[4] + $temp[5];
return '{"type":"sidaier","value":"'.$value.'"}';
} else {
return false;
}
}
//检测牌型
function checkpokers($temp) {
if (empty($temp)) {
return false;
}
if (isDan($temp)) {
return isDan($temp);
} else if (isDui($temp)) {
return isDui($temp);
} else if (isHuojian($temp)) {
return isHuojian($temp);
} else if (isSan($temp)) {
return isSan($temp);
} else if (isZha($temp)) {
return isZha($temp);
} else if (isSandaiyi($temp)) {
return isSandaiyi($temp);
} else if (isShunzi($temp)) {
return isShunzi($temp);
} else if (isLiandui($temp)) {
return isLiandui($temp);
} else if (isFeijiBuDai($temp)) {
return isFeijiBuDai($temp);
} else if (isSiDaiYi($temp)) {
return isSiDaiYi($temp);
} else if (isSiDaiEr($temp)) {
return isSiDaiEr($temp);
} else if (isSandaiEr($temp)) {
return isSandaiEr($temp);
} else if (isFeijiDaiDan($temp)) {
return isFeijiDaiDan($temp);
} else if (isFeijiDaiShuang($temp)){
return isFeijiDaiShuang($temp);
} else {
return false;
}
}
//检测牌的大小
function checkdaxiao($round_type, $res)
{
if ($round_type->type == 'huojian') {
return true;
}
if ($res->type == 'zhadan') {
if ($round_type->type == 'huojian') {
return false;
} else {
return true;
}
}
if ($round_type->type == $res->type && $res->value > $round_type->value) {
return true;
}
return false;
}
斗地主数据结构, 大厅 & 房间 & 游戏 汇总信息
<?php
/**
* 大厅 & 房间 & 游戏汇总信息
*/
//房间信息数组
$GLOBALS['rooms'] = [];
//大厅座位详情数组 请不要在意这些细节
$GLOBALS['hall'] = [
'DZ-1A' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-1B' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-1C' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-1D' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-1E' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-2A' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-2B' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-2C' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-2D' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-2E' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-3A' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-3B' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-3C' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-3D' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-3E' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-4A' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-4B' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-4C' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-4D' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
'DZ-4E' => [
'user_a'=>'off','user_b'=>'off','user_c'=>'off','status'=>'off','pokers'=>[],
'whosturn'=>'','dizhu'=>'','whospoker' => 'off','game_double'=>1,'round_type'=>'off',
'user_a_id'=>'off','user_b_id'=>'off','user_c_id'=>'off',
'user_a_status'=>'空闲','user_b_status'=>'空闲','user_c_status'=>'空闲',
'user_a_pokers'=>'','user_b_pokers'=>'','user_c_pokers'=>'',
'user_a_nick'=>'off','user_b_nick'=>'off','user_c_nick'=>'off',
'user_a_identity'=>'off','user_b_identity'=>'off','user_c_identity'=>'off'],
];
/**
* 扑克牌数组 后缀:1代表黑桃 2代表红桃 3代表梅花 4代表方块
*/
$GLOBALS['poker_list'] = [
'Joker1' => 100,
'Joker2' => 99,
'TWO_1' => 20,
'TWO_2' => 20,
'TWO_3' => 20,
'TWO_4' => 20,
'A_1' => 14,
'A_2' => 14,
'A_3' => 14,
'A_4' => 14,
'K_1' => 13,
'K_2' => 13,
'K_3' => 13,
'K_4' => 13,
'Q_1' => 12,
'Q_2' => 12,
'Q_3' => 12,
'Q_4' => 12,
'J_1' => 11,
'J_2' => 11,
'J_3' => 11,
'J_4' => 11,
'TEN_1' => 10,
'TEN_2' => 10,
'TEN_3' => 10,
'TEN_4' => 10,
'NINE_1' => 9,
'NINE_2' => 9,
'NINE_3' => 9,
'NINE_4' => 9,
'EIGHT_1' => 8,
'EIGHT_2' => 8,
'EIGHT_3' => 8,
'EIGHT_4' => 8,
'SEVEN_1' => 7,
'SEVEN_2' => 7,
'SEVEN_3' => 7,
'SEVEN_4' => 7,
'SIX_1' => 6,
'SIX_2' => 6,
'SIX_3' => 6,
'SIX_4' => 6,
'FIVE_1' => 5,
'FIVE_2' => 5,
'FIVE_3' => 5,
'FIVE_4' => 5,
'FOUR_1' => 4,
'FOUR_2' => 4,
'FOUR_3' => 4,
'FOUR_4' => 4,
'THREE_1' => 3,
'THREE_2' => 3,
'THREE_3' => 3,
'THREE_4' => 3,
];
分数结算 房间重置方法
/**
* 分数结算 房间重置方法
*/
//用户赢了加场数
function updateuserwin($uid)
{
$mysql = new mysqlDb();
$mysql->query("update user_games set user_ddzwin = user_ddzwin + 1 where uid = $uid");
}
//用户输了加场数
function updateuserlose($uid)
{
$mysql = new mysqlDb();
$mysql->query("update user_games set user_ddzlose = user_ddzlose + 1 where uid = $uid");
}
//获取用户胜率
function getuserwinlose($uid)
{
$mysql = new mysqlDb();
$sql = "select * from user_games where uid = $uid";
$res = $mysql->getRow($sql);
if ($res['user_ddzwin'] + $res['user_ddzlose'] != 0) {
$res = $res['user_ddzwin'] / ($res['user_ddzwin'] + $res['user_ddzlose']);
$res = round($res, 2) * 100;
} else {
$res = 0;
}
return $res;
}
/**
* 房间重置方法
* @param [string] $roomid [传入房间ID]
*/
function resetRoom($roomid)
{
$GLOBALS['hall'][$roomid]['pokers'] = [];
$GLOBALS['hall'][$roomid]['status'] = 'off';
$GLOBALS['hall'][$roomid]['whosturn'] = '';
$GLOBALS['hall'][$roomid]['game_double'] = 1;
$GLOBALS['hall'][$roomid]['whospoker'] = '';
$GLOBALS['hall'][$roomid]['round_type'] = 'off';
$GLOBALS['hall'][$roomid]['user_a_pokers'] = [];
$GLOBALS['hall'][$roomid]['user_b_pokers'] = [];
$GLOBALS['hall'][$roomid]['user_c_pokers'] = [];
$GLOBALS['hall'][$roomid]['user_a_status'] = '空闲';
$GLOBALS['hall'][$roomid]['user_b_status'] = '空闲';
$GLOBALS['hall'][$roomid]['user_c_status'] = '空闲';
$GLOBALS['hall'][$roomid]['user_a_identity'] = 'off';
$GLOBALS['hall'][$roomid]['user_b_identity'] = 'off';
$GLOBALS['hall'][$roomid]['user_c_identity'] = 'off';
}
/**
* [游戏结束 结算方法]
* @param [string] $roomid [房间的ID]
* @param [string] $identity [传入出完牌玩家的身份 ]
* @param [string] $type [可选参数 是否有人逃跑 ]
* @return [array] [返回输赢数组]
*/
function gameOver($roomid, $identity, $type = [])
{
$win = []; $lose = [];
//如果是有人逃跑 直接返回结果
if (isset($type['ranout'])) {
resetRoom($roomid);
return ['win'=>$win,'lose'=>[$GLOBALS['hall'][$roomid]['user_a_nick']], 'ranout'=>$type['ranout']];
}
if ($identity == '地主') {
if ($GLOBALS['hall'][$roomid]['user_a_identity'] == '地主') {
$win[] = $GLOBALS['hall'][$roomid]['user_a_nick'];
updateuserwin($GLOBALS['hall'][$roomid]['user_a_id']);
}
if ($GLOBALS['hall'][$roomid]['user_b_identity'] == '地主') {
$win[] = $GLOBALS['hall'][$roomid]['user_b_nick'];
updateuserwin($GLOBALS['hall'][$roomid]['user_b_id']);
}
if ($GLOBALS['hall'][$roomid]['user_c_identity'] == '地主') {
$win[] = $GLOBALS['hall'][$roomid]['user_c_nick'];
updateuserwin($GLOBALS['hall'][$roomid]['user_c_id']);
}
if ($GLOBALS['hall'][$roomid]['user_a_identity'] == '农民') {
$lose[] = [$GLOBALS['hall'][$roomid]['user_a_nick'], $GLOBALS['hall'][$roomid]['user_a_pokers']];
updateuserlose($GLOBALS['hall'][$roomid]['user_a_id']);
}
if ($GLOBALS['hall'][$roomid]['user_b_identity'] == '农民') {
$lose[] = [$GLOBALS['hall'][$roomid]['user_b_nick'], $GLOBALS['hall'][$roomid]['user_b_pokers']];
updateuserlose($GLOBALS['hall'][$roomid]['user_b_id']);
}
if ($GLOBALS['hall'][$roomid]['user_c_identity'] == '农民') {
$lose[] = [$GLOBALS['hall'][$roomid]['user_c_nick'], $GLOBALS['hall'][$roomid]['user_c_pokers']];
updateuserlose($GLOBALS['hall'][$roomid]['user_c_id']);
}
} else if ($identity == '农民') {
if ($GLOBALS['hall'][$roomid]['user_a_identity'] == '地主') {
$lose[] = [$GLOBALS['hall'][$roomid]['user_a_nick'], $GLOBALS['hall'][$roomid]['user_a_pokers']];
updateuserlose($GLOBALS['hall'][$roomid]['user_a_id']);
}
if ($GLOBALS['hall'][$roomid]['user_b_identity'] == '地主') {
$lose[] = [$GLOBALS['hall'][$roomid]['user_b_nick'], $GLOBALS['hall'][$roomid]['user_b_pokers']];
updateuserlose($GLOBALS['hall'][$roomid]['user_b_id']);
}
if ($GLOBALS['hall'][$roomid]['user_c_identity'] == '地主') {
$lose[] = [$GLOBALS['hall'][$roomid]['user_c_nick'], $GLOBALS['hall'][$roomid]['user_c_pokers']];
updateuserlose($GLOBALS['hall'][$roomid]['user_c_id']);
}
if ($GLOBALS['hall'][$roomid]['user_a_identity'] == '农民') {
$win[] = $GLOBALS['hall'][$roomid]['user_a_nick'];
updateuserwin($GLOBALS['hall'][$roomid]['user_a_id']);
}
if ($GLOBALS['hall'][$roomid]['user_b_identity'] == '农民') {
$win[] = $GLOBALS['hall'][$roomid]['user_b_nick'];
updateuserwin($GLOBALS['hall'][$roomid]['user_b_id']);
}
if ($GLOBALS['hall'][$roomid]['user_c_identity'] == '农民') {
$win[] = $GLOBALS['hall'][$roomid]['user_c_nick'];
updateuserwin($GLOBALS['hall'][$roomid]['user_c_id']);
}
}
$pokers = [
'user_a_pokers' => $GLOBALS['hall'][$roomid]['user_a_pokers'],
'user_b_pokers' => $GLOBALS['hall'][$roomid]['user_b_pokers'],
'user_c_pokers' => $GLOBALS['hall'][$roomid]['user_c_pokers'],
];
//重置房间所有属性
resetRoom($roomid);
//返回胜负数据给玩家
return ['win'=>$win,'lose'=>$lose,'pokers'=>$pokers];
}
监听用户连接大厅事件
/**
* 监听用户连接大厅事件
*/
$serv->on('Open', function($server, $req) {
$id = $req->fd;
$GLOBALS['fds'][] = $id;
});
服务器监听用户动作
<?php
/**
* 服务器监听用户动作
*/
$serv->on('Message', function($server, $frame) {
//推送用户在线列表
if ($frame->data == 'getonlines') {
foreach ($GLOBALS['fds'] as $v) {
$server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
}
//新进用户加入列表
} else if (substr($frame->data, 0, 8) == 'mystatus') {
$info = json_decode(substr($frame->data, 8));
$GLOBALS['userlist'][$frame->fd] = $info;
//推送大厅座位情况
} else if (substr($frame->data, 0, 10) == 'tableinfos') {
foreach ($GLOBALS['fds'] as $v) {
$server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
}
//推送是否允许坐下
} else if (substr($frame->data, 0, 8) == 'seatdown') {
$info = json_decode(substr($frame->data, 8));
if ($GLOBALS['hall'][$info->table][$info->seat] == 'off') {
$server->push($frame->fd, 'seatdown'.'ok');
} else {
$server->push($frame->fd, 'seatdown'.'no');
}
//将加入房间的玩家信息更新到数组
} else if (substr($frame->data, 0, 11) == 'getroominfo') {
//写入用户状态信息
$info = json_decode(substr($frame->data, 11));
$GLOBALS['hall'][$info->roomid][$info->seatid] = $frame->fd;
$GLOBALS['hall'][$info->roomid][$info->seatid.'_nick'] = $info->user_nick;
$GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] = $info->mystatus;
$GLOBALS['hall'][$info->roomid][$info->seatid.'_id'] = $info->uid;
$GLOBALS['hall'][$info->roomid][$info->seatid.'_point'] = getuserwinlose($info->uid);
//如果房间数组不存在,初始化
if (!isset($GLOBALS['rooms'][$info->roomid])) {
$GLOBALS['rooms'][$info->roomid] = [];
}
//判断是重新开局还是新进玩家
$isreopen = false;
foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
if ($v['fd'] == $frame->fd) {
$isreopen = true;
break;
}
}
//判断加入房间的是第几个 并加入游戏顺序
if ($isreopen == false) {
if (count($GLOBALS['rooms'][$info->roomid]) == 0) {
$GLOBALS['rooms'][$info->roomid][0] = ['fd'=>$frame->fd,'nick'=>$info->user_nick];
} else if (count($GLOBALS['rooms'][$info->roomid]) == 1) {
$GLOBALS['rooms'][$info->roomid][1] = ['fd'=>$frame->fd,'nick'=>$info->user_nick];
} else if (count($GLOBALS['rooms'][$info->roomid]) == 2) {
$GLOBALS['rooms'][$info->roomid][2] = ['fd'=>$frame->fd,'nick'=>$info->user_nick];
}
}
//广播到该房间所有的用户 广播到全局
$fd1 = $GLOBALS['hall'][$info->roomid]['user_a'];
$fd2 = $GLOBALS['hall'][$info->roomid]['user_b'];
$fd3 = $GLOBALS['hall'][$info->roomid]['user_c'];
foreach ($GLOBALS['fds'] as $v) {
$server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
if ($v == $fd3 || $v == $fd1 || $v == $fd2) {
$server->push($v, 'freshorder'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
}
}
$server->push($frame->fd, 'roomchat'.'{"nick":"系统消息","msg":"欢迎来到世界上最Low的在线斗地主!"}');
//改变准备状态
} else if (substr($frame->data, 0, 15) == 'pushreadystatus') {
// 等待 || 准备
if (substr($frame->data, 15, 2) == 'ok') {
$info = json_decode(substr($frame->data, 17));
$GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] = '准备';
$GLOBALS['userlist'][$frame->fd]->status = '准备';
} else if (substr($frame->data, 15, 2) == 'no') {
$info = json_decode(substr($frame->data, 17));
$GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] = '等待';
$GLOBALS['userlist'][$frame->fd]->status = '等待';
$GLOBALS['hall'][$info->roomid]['status'] = 'off';
}
//刷新房间内信息变动
if (isset($info)) {
$fd1 = $GLOBALS['hall'][$info->roomid]['user_a'];
$fd2 = $GLOBALS['hall'][$info->roomid]['user_b'];
$fd3 = $GLOBALS['hall'][$info->roomid]['user_c'];
//如果三个人状态都在准备中 推送开始游戏消息
if ($GLOBALS['hall'][$info->roomid]['user_a_status'] == '准备' &&
$GLOBALS['hall'][$info->roomid]['user_b_status'] == '准备' &&
$GLOBALS['hall'][$info->roomid]['user_c_status'] == '准备') {
//初始化房间数据
resetRoom($info->roomid);
//修改游戏状态 并且推送
$GLOBALS['hall'][$info->roomid]['user_a_status'] = '游戏中';
$GLOBALS['hall'][$info->roomid]['user_b_status'] = '游戏中';
$GLOBALS['hall'][$info->roomid]['user_c_status'] = '游戏中';
//更新游戏胜率
$GLOBALS['userlist'][$fd1]->point = getuserwinlose($GLOBALS['userlist'][$fd1]->uid);
$GLOBALS['userlist'][$fd2]->point = getuserwinlose($GLOBALS['userlist'][$fd2]->uid);
$GLOBALS['userlist'][$fd3]->point = getuserwinlose($GLOBALS['userlist'][$fd3]->uid);
//更新游戏状态
$GLOBALS['userlist'][$fd1]->status = '游戏中';
$GLOBALS['userlist'][$fd2]->status = '游戏中';
$GLOBALS['userlist'][$fd3]->status = '游戏中';
$GLOBALS['hall'][$info->roomid]['status'] = 'on';
$server->push($fd1, 'gostartgame'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
$server->push($fd2, 'gostartgame'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
$server->push($fd3, 'gostartgame'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
}
//大厅座位信息广播到全局 用户列表刷新
foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
$server->push($v['fd'], 'freshorder'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
}
}
//广播房间准备状态到全局
foreach ($GLOBALS['fds'] as $v) {
$server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
$server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
}
//广播房间内的消息
} else if (substr($frame->data, 0, 8) == 'roomchat') {
$info = json_decode(substr($frame->data, 8));
foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
$server->push($v['fd'], 'roomchat'.substr($frame->data, 8));
}
//广播全局聊天
} else if (substr($frame->data, 0, 9) == 'publicmsg') {
foreach ($GLOBALS['fds'] as $v) {
$server->push($v, $frame->data);
}
//接受确认游戏的信息并推送对局初始化
} else if (substr($frame->data, 0,8) == 'letsplay') {
$info = json_decode(substr($frame->data, 8));
if (count($GLOBALS['hall'][$info->roomid]['pokers']) == 0) {
$GLOBALS['hall'][$info->roomid]['pokers'] = $GLOBALS['poker_list'];
}
//发牌
$pokers = array_rand($GLOBALS['hall'][$info->roomid]['pokers'], 17);
foreach ($pokers as $k => $v) {
foreach ($GLOBALS['hall'][$info->roomid]['pokers'] as $a => $s) {
if ($v == $a) {
unset($GLOBALS['hall'][$info->roomid]['pokers'][$a]);
}
}
}
$GLOBALS['hall'][$info->roomid][$info->seatid.'_pokers'] = $pokers;
$temp = [];
foreach ($pokers as $a => $s) {
$temp[] = ['poker'=>$s, 'value'=>$GLOBALS['poker_list'][$s]];
}
$server->push($GLOBALS['hall'][$info->roomid][$info->seatid], 'fapaile'.json_encode($temp));
//第一顺位玩家初始化顺位
if ($GLOBALS['rooms'][$info->roomid][0]['fd'] == $frame->fd) {
//初始化顺位
$GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
$waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][0]['nick'].'"}';
//让顺位0用户开始叫地主
$server->push($GLOBALS['rooms'][$info->roomid][0]['fd'], 'jiaobujiao');
//其它两个用户开始等待
$server->push($GLOBALS['rooms'][$info->roomid][1]['fd'], $waiting);
$server->push($GLOBALS['rooms'][$info->roomid][2]['fd'], $waiting);
}
//用户放弃叫地主事件
} else if (substr($frame->data, 0,8) == 'wobujiao') {
$info = json_decode(substr($frame->data, 8));
$GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] = '不叫';
//如果三个人都不叫 则重新发牌
if ($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫' &&
$GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫' &&
$GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') {
//重置房间
resetRoom($info->roomid);
//循环广播重开游戏事件
foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
$server->push($v['fd'], 'nobodystandup');
}
//否则开始下一位叫牌
} else {
//刷新出牌次序
if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
$GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
}
//拿到下一位
$turn = $GLOBALS['hall'][$info->roomid]['whosturn'];
$waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][$turn]['nick'].'"}';
//推送叫牌给下一位
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
if ($turn == $k) {
$server->push($v['fd'], 'jiaobujiao');
}
$server->push($v['fd'], 'tabujiao'.$info->nick);
$server->push($v['fd'], $waiting);
}
}
//有人叫地主
} else if (substr($frame->data, 0,6) == 'wojiao') {
$info = json_decode(substr($frame->data, 6));
$GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] = '叫地主';
//如果已经有两个人不叫了 他叫他就是地主
if ($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫' &&
$GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫') {
$GLOBALS['hall'][$info->roomid]['user_c_identity'] = '地主';
$GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
$GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';
//通知地主他是地主 通知对手等待他出牌
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
$server->push($v['fd'], 'dizhuis'.$info->nick);
$server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
if ($v['fd'] != $frame->fd) {
$server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_c_nick'].'"}');
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
}
}
} else if ($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫' &&
$GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') {
$GLOBALS['hall'][$info->roomid]['user_a_identity'] = '地主';
$GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';
$GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';
//通知地主他是地主 通知对手等待他出牌
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
$server->push($v['fd'], 'dizhuis'.$info->nick);
$server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
if ($v['fd'] != $frame->fd) {
$server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_a_nick'].'"}');
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
}
}
} else if ($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫' &&
$GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫'){
$GLOBALS['hall'][$info->roomid]['user_b_identity'] = '地主';
$GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
$GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';
//通知地主他是地主 通知对手等待他出牌
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
$server->push($v['fd'], 'dizhuis'.$info->nick);
$server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
if ($v['fd'] != $frame->fd) {
$server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_b_nick'].'"}');
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
}
}
//开始抢地主
} else {
//刷新叫次序
if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
$GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
}
//拿到下一位
$turn = $GLOBALS['hall'][$info->roomid]['whosturn'];
$waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][$turn]['nick'].'"}';
//推送抢地主给下一位
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
if ($turn == $k) {
$server->push($v['fd'], 'qiangbuqiang');
}
//告诉所有人他叫了,等待下一位
$server->push($v['fd'], 'tajiaole'.$info->nick);
$server->push($v['fd'], $waiting);
}
}
//不抢地主
} else if (substr($frame->data, 0,9) == 'wobuqiang') {
$info = json_decode(substr($frame->data, 9));
//如果叫地主的人不抢了 第一个抢地主的人就是地主
if ($GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] == '叫地主') {
//找出第一个叫地主的人
foreach ($GLOBALS['hall'][$info->roomid] as $k => $v) {
if ($v === '抢地主') {
//取得地主昵称
$nick = $GLOBALS['hall'][$info->roomid][substr($k, 0, 6).'_nick'];
//通知地主他是地主 通知对手等待他出牌
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
$server->push($v['fd'], 'dizhuis'.$nick);
$server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
if ($v['nick'] != $nick) {
$server->push($v['fd'], 'waiting'.'{"nick":"'.$nick.'"}');
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
}
}
break;
}
}
//继续判断
} else {
$GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] = '不抢';
//如果已经有两个人不抢也不叫地主了 那么剩下一个人就是地主
if (($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '叫地主' ||
$GLOBALS['hall'][$info->roomid]['user_c_identity'] == '抢地主')) {
$GLOBALS['hall'][$info->roomid]['user_c_identity'] = '地主';
$GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
$GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';
//通知地主他是地主 通知对手等待他出牌
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
$server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_c_nick']);
$server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
if ($v['fd'] != $frame->fd) {
$server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_c_nick'].'"}');
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
}
}
} else if (($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '叫地主' ||
$GLOBALS['hall'][$info->roomid]['user_a_identity'] == '抢地主')) {
$GLOBALS['hall'][$info->roomid]['user_a_identity'] = '地主';
$GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';
$GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';
//通知地主他是地主 通知对手等待他出牌
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
$server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_a_nick']);
$server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
if ($v['fd'] != $frame->fd) {
$server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_a_nick'].'"}');
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
}
}
} else if (($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '叫地主' ||
$GLOBALS['hall'][$info->roomid]['user_b_identity'] == '抢地主')){
$GLOBALS['hall'][$info->roomid]['user_b_identity'] = '地主';
$GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
$GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';
//通知地主他是地主 通知对手等待他出牌
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
$server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_b_nick']);
$server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
if ($v['fd'] != $frame->fd) {
$server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_b_nick'].'"}');
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
}
}
} else {
//刷新抢次序
if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
$GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
}
$turn = $GLOBALS['hall'][$info->roomid]['whosturn'];
$waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][$turn]['nick'].'"}';
//推送抢地主给下一位
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
if ($turn == $k) {
$server->push($v['fd'], 'qiangbuqiang');
}
//告诉所有人他抢了,等待下一位
$server->push($v['fd'], 'tabuqiang'.$info->nick);
$server->push($v['fd'], $waiting);
}
}
}
//抢地主
} else if (substr($frame->data, 0,7) == 'woqiang') {
$info = json_decode(substr($frame->data, 7));
$GLOBALS['hall'][$info->roomid]['game_double'] = $GLOBALS['hall'][$info->roomid]['game_double'] * 2;
//如果叫地主的人在抢地主了 那么地主就是他
if ($GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] == '叫地主') {
$GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
$GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';
$GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';
$GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] = '地主';
//通知地主他是地主 通知对手等待他出牌
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
$server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid][$info->seatid.'_nick']);
$server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
if ($v['fd'] != $frame->fd) {
$server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid][$info->seatid].'"}');
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
}
}
//继续判断
} else {
$GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] = '抢地主';
//如果已经有两个人不抢也不叫了 谁抢谁就是地主
if (($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '叫地主' ||
$GLOBALS['hall'][$info->roomid]['user_c_identity'] == '抢地主')) {
$GLOBALS['hall'][$info->roomid]['user_c_identity'] = '地主';
$GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
$GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';
//通知地主他是地主 通知对手等待他出牌
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
$server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_c_nick']);
$server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
if ($v['fd'] != $frame->fd) {
$server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_c_nick'].'"}');
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
}
}
} else if (($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '叫地主' ||
$GLOBALS['hall'][$info->roomid]['user_a_identity'] == '抢地主')) {
$GLOBALS['hall'][$info->roomid]['user_a_identity'] = '地主';
$GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';
$GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';
//通知地主他是地主 通知对手等待他出牌
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
$server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_a_nick']);
$server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
if ($v['fd'] != $frame->fd) {
$server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_a_nick'].'"}');
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
}
}
} else if (($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不抢' ||
$GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') &&
($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '叫地主' ||
$GLOBALS['hall'][$info->roomid]['user_b_identity'] == '抢地主')){
$GLOBALS['hall'][$info->roomid]['user_b_identity'] = '地主';
$GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
$GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';
//通知地主他是地主 通知对手等待他出牌
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
$server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_b_nick']);
$server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
if ($v['fd'] != $frame->fd) {
$server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_b_nick'].'"}');
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
}
}
} else {
//刷新抢次序
if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
$GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
}
//拿到下一位次序
$turn = $GLOBALS['hall'][$info->roomid]['whosturn'];
//拿到下一位昵称
$next_nick = $GLOBALS['rooms'][$info->roomid][$turn]['nick'];
//拼接等待字符串
$waiting = 'waiting'.'{"nick":"'.$next_nick.'"}';
//推送抢地主给下一位
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
if ($turn == $k) {
$server->push($v['fd'], 'qiangbuqiang');
}
//告诉所有人他抢了,等待下一位
$server->push($v['fd'], 'taqiangle'.$info->nick);
$server->push($v['fd'], $waiting);
}
}
}
//我出牌
} else if (substr($frame->data, 0,8) == 'wochupai') {
$info = json_decode(substr($frame->data, 8), true);
//拿到扑克数组
$pokers = $info['pokers'][0];
//取得完整扑克键值对
$temp = [];
//取得值
$numbers = [];
foreach ($pokers as $k => $v) {
$temp[] = ['poker'=>$v,'value'=>$GLOBALS['poker_list'][$v]];
$numbers[] = $GLOBALS['poker_list'][$v];
}
//判断是什么牌型
$res = checkpokers($numbers);
//如果牌型正确 继续操作
if ($res != false) {
$res = json_decode($res);
$round_type = $GLOBALS['hall'][$info['roomid']]['round_type'];
if ($GLOBALS['hall'][$info['roomid']]['round_type'] == 'off' || checkdaxiao($round_type, $res) ||
$GLOBALS['hall'][$info['roomid']]['whospoker'] == $info['nick'] ||
$res->type == 'huojian') {
$server->push($frame->fd, 'chupaiok'.json_encode($pokers));
//标记最后出牌的人是谁
$GLOBALS['hall'][$info['roomid']]['whospoker'] = $info['nick'];
//保存这一手的类型
$GLOBALS['hall'][$info['roomid']]['round_type'] = $res;
foreach ($pokers as $r) {
foreach ($GLOBALS['hall'][$info['roomid']][$info['seatid'].'_pokers'] as $k => $v) {
if ($v == $r) {
array_splice($GLOBALS['hall'][$info['roomid']][$info['seatid'].'_pokers'], $k, 1);
}
}
}
//如果玩家还有牌 游戏继续
if (count($GLOBALS['hall'][$info['roomid']][$info['seatid'].'_pokers']) > 0) {
//刷新出牌次序
if ($GLOBALS['hall'][$info['roomid']]['whosturn'] == count($GLOBALS['rooms'][$info['roomid']]) - 1) {
$GLOBALS['hall'][$info['roomid']]['whosturn'] = 0;
} else {
$GLOBALS['hall'][$info['roomid']]['whosturn'] = $GLOBALS['hall'][$info['roomid']]['whosturn'] + 1;
}
//拿到下一位次序
$turn = $GLOBALS['hall'][$info['roomid']]['whosturn'];
//拿到下一位昵称
$next_nick = $GLOBALS['rooms'][$info['roomid']][$turn]['nick'];
//拼接等待字符串
$waiting = 'waiting'.'{"nick":"'.$next_nick.'"}';
foreach ($GLOBALS['rooms'][$info['roomid']] as $k => $v) {
//广播是谁出的
$server->push($v['fd'], 'lasthand'.$info['nick']);
//告诉所有人他出了 广播牌队列
$server->push($v['fd'], 'chulepai'.json_encode($temp));
//等待下一位
$server->push($v['fd'], $waiting);
}
//推送出牌给下一位
foreach ($GLOBALS['rooms'][$info['roomid']] as $k => $v) {
if ($turn == $k) {
$server->push($v['fd'], 'chupaile');
}
}
//如果玩家没有牌了 结束游戏
} else {
//调用计分结算方法 返回win和lose数组
$res = gameOver($info['roomid'], $GLOBALS['hall'][$info['roomid']][$info['seatid'].'_identity']);
//广播结算到整个房间 修改玩家状态
foreach($GLOBALS['rooms'][$info['roomid']] as $v) {
$GLOBALS['userlist'][$v['fd']]->status = '等待';
$server->push($v['fd'], 'gameover'.json_encode($res));
$server->push($v['fd'],'freshorder'.json_encode([$GLOBALS['rooms'][$info['roomid']],$GLOBALS['hall'][$info['roomid']]]));
}
//广播房间准备状态到全局
foreach ($GLOBALS['fds'] as $v) {
$server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
$server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
}
}
}
}
//我不出牌
} else if (substr($frame->data, 0, 7) == 'wobuchu') {
$info = json_decode(substr($frame->data, 7));
//刷新出次序
if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
$GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
} else {
$GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
}
//拿到下一位次序
$turn = $GLOBALS['hall'][$info->roomid]['whosturn'];
//拿到下一位昵称
$next_nick = $GLOBALS['rooms'][$info->roomid][$turn]['nick'];
//拼接等待字符串
$waiting = 'waiting'.'{"nick":"'.$next_nick.'"}';
//推送不出牌给下一位
foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
if ($turn == $k) {
$server->push($v['fd'], 'chupaile');
}
//告诉所有人他不出,等待下一位
$server->push($v['fd'], 'tabuchu'.$info->nick);
$server->push($v['fd'], $waiting);
}
} else if (substr($frame->data, 0, 7) == '') {
$info = json_decode(substr($frame->data, 7));
}
});
监听用户断开连接事件
<?php
/**
* 监听用户断开连接事件
*/
$serv->on('Close', function($server, $fd) {
//将用户从在线列表移除
unset($GLOBALS['userlist'][$fd]);
//将用户从连接池移除
foreach ($GLOBALS['fds'] as $k => $v) {
if ($v == $fd) {
unset($GLOBALS['fds'][$k]);
shuffle($GLOBALS['fds']);
}
}
//将用户从房间游戏顺序列表移除 顺便获取房间ID
foreach ($GLOBALS['rooms'] as $k => $v) {
foreach ($v as $u => $y) {
if ($y['fd'] == $fd) {
unset($GLOBALS['rooms'][$k][$u]);
shuffle($GLOBALS['rooms'][$k]);
//如果座位A是我
if ($GLOBALS['hall'][$k]['user_a'] == $fd) {
//如果在游戏时间逃跑 马上结算游戏
if ($GLOBALS['hall'][$k]['status'] == 'on') {
$res = gameOver($k, $GLOBALS['hall'][$k]['user_a_identity'], ['ranout'=>$GLOBALS['hall'][$k]['user_a_nick']]);
//游戏结束消息广播到整个房间 更新用户状态
foreach ($GLOBALS['rooms'][$k] as $q) {
$GLOBALS['userlist'][$q['fd']]->status = '等待';
$server->push($q['fd'], 'gameover'.json_encode($res));
}
}
//座位信息初始化
$GLOBALS['hall'][$k]['user_a'] = 'off';
$GLOBALS['hall'][$k]['user_a_nick'] = 'off';
$GLOBALS['hall'][$k]['user_a_status'] = 'off';
$GLOBALS['hall'][$k]['user_a_identity'] = 'off';
$GLOBALS['hall'][$k]['user_a_pokers'] = '';
$GLOBALS['hall'][$k]['round_type'] = 'off';
//更新座位信息到全局
foreach ($GLOBALS['rooms'][$k] as $e) {
$server->push($e['fd'], 'freshorder'.json_encode([$GLOBALS['rooms'][$k],$GLOBALS['hall'][$k]]));
}
//如果座位B是我
} else if ($GLOBALS['hall'][$k]['user_b'] == $fd) {
//如果在游戏时间逃跑 马上结算游戏
if ($GLOBALS['hall'][$k]['status'] == 'on') {
$res = gameOver($k, $GLOBALS['hall'][$k]['user_b_identity'], ['ranout'=>$GLOBALS['hall'][$k]['user_b_nick']]);
//游戏结束消息广播到整个房间 更新用户状态
foreach ($GLOBALS['rooms'][$k] as $q) {
$GLOBALS['userlist'][$q['fd']]->status = '等待';
$server->push($q['fd'], 'gameover'.json_encode($res));
}
}
$GLOBALS['hall'][$k]['user_b'] = 'off';
$GLOBALS['hall'][$k]['user_b_nick'] = 'off';
$GLOBALS['hall'][$k]['user_b_status'] = 'off';
$GLOBALS['hall'][$k]['user_b_identity'] = 'off';
$GLOBALS['hall'][$k]['user_b_pokers'] = '';
$GLOBALS['hall'][$k]['round_type'] = 'off';
foreach ($GLOBALS['rooms'][$k] as $e) {
$server->push($e['fd'], 'freshorder'.json_encode([$GLOBALS['rooms'][$k],$GLOBALS['hall'][$k]]));
}
//如果座位C是我
} else if ($GLOBALS['hall'][$k]['user_c'] == $fd) {
//如果在游戏时间逃跑 马上结算游戏
if ($GLOBALS['hall'][$k]['status'] == 'on') {
$res = gameOver($k, $GLOBALS['hall'][$k]['user_c_identity'], ['ranout'=>$GLOBALS['hall'][$k]['user_c_nick']]);
//游戏结束消息广播到整个房间 更新用户状态
foreach ($GLOBALS['rooms'][$k] as $q) {
$GLOBALS['userlist'][$q['fd']]->status = '等待';
$server->push($q['fd'], 'gameover'.json_encode($res));
}
}
$GLOBALS['hall'][$k]['user_c'] = 'off';
$GLOBALS['hall'][$k]['user_c_nick'] = 'off';
$GLOBALS['hall'][$k]['user_c_status'] = 'off';
$GLOBALS['hall'][$k]['user_c_identity'] = 'off';
$GLOBALS['hall'][$k]['user_c_pokers'] = '';
$GLOBALS['hall'][$k]['round_type'] = 'off';
foreach ($GLOBALS['rooms'][$k] as $e) {
$server->push($e['fd'], 'freshorder'.json_encode([$GLOBALS['rooms'][$k],$GLOBALS['hall'][$k]]));
}
}
}
}
}
//向大厅的所有用户推送最新的在线人数 和 大厅座位情况
foreach ($GLOBALS['fds'] as $v) {
//推送玩家列表更新
$server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
//推送大厅信息更新
$server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
}
});