Commit 2b1d67e7 authored by hisuinohoshi's avatar hisuinohoshi

update roommng2

新增:
- 创建/解散房间功能;
- 房主/房间创建者:有权限解散非进行中、或已经没有幸存者的自己创建的房间;房主退出房间且房间内尚有其他玩家时,将房主权限移交给其他玩家;

变化:
- 房间列表只会显示已被创建的房间;

修正:
- 后台玩家管理功能在php8+报错的问题;
parent aade25b9
......@@ -164,7 +164,7 @@ $no_self_sponsored = 0;
$rsgame_bots = 4;
//房间数量上限
$max_rooms = 3;
$max_rooms = 15;
//重登陆时是否自动退出房间(1:自动退出 0:不自动退出)
$login_exit_room = 0;
......
......@@ -64,6 +64,7 @@ CREATE TABLE `acbra2_game` (
`gamestate` tinyint(3) unsigned NOT NULL DEFAULT '0',
`groomid` tinyint(3) unsigned NOT NULL DEFAULT '0',
`groomnums` tinyint(3) unsigned NOT NULL DEFAULT '0',
`groomownid` char(15) NOT NULL default '',
`starttime` int(10) unsigned NOT NULL DEFAULT '0',
`winmode` tinyint(3) unsigned NOT NULL DEFAULT '0',
`winner` char(15) NOT NULL DEFAULT '',
......
......@@ -74,7 +74,7 @@ if($command == 'kill' || $command == 'live' || $command == 'del') {
if($operlist || $operlist2 || $dfaillist || $gfaillist){
if($command == 'kill'){
$operword = '被杀死';
$qryword = "UPDATE {$tablepre}players SET hp='0',state='15',bid='0', ";
$qryword = "UPDATE {$tablepre}players SET hp='0',state='15',bid='0' ";
}elseif($command == 'live'){
$operword = '被复活';
$qryword = "UPDATE {$tablepre}players SET hp=mhp,state='0' ";
......
......@@ -27,6 +27,12 @@ function roommng_verify_db_game_structure()
$db->query("ALTER TABLE {$gtablepre}game ADD groomnums tinyint(3) unsigned NOT NULL DEFAULT '0' AFTER groomid");
echo "向game表中添加了字段groomnums<br>";
}
$result = $db->query("DESCRIBE {$gtablepre}game groomownid");
if(!$db->num_rows($result))
{
$db->query("ALTER TABLE {$gtablepre}game ADD groomownid char(15) NOT NULL default '' AFTER groomnums");
echo "向game表中添加了字段groomownid<br>";
}
$result = $db->query("SHOW INDEX FROM {$gtablepre}game");
$gr = $db->fetch_array($result);
......@@ -39,7 +45,156 @@ function roommng_verify_db_game_structure()
return;
}
function roommng_close_room($rkey,$log_print=0)
# 创建一个新房间
function roommng_create_new_room(&$udata)
{
global $db,$gtablepre,$now;
global $startmin,$max_rooms;
if(!empty($udata['roomid']))
{
echo "你已经在房间里了。要创建新房间必须退出当前房间。<br>";
return;
}
# 统计当前已新建房间数量
$result = $db->query("SELECT * FROM {$gtablepre}game WHERE groomid>0 ");
$now_room_nums = $db->num_rows($result);
if($now_room_nums >= $max_rooms)
{
echo "房间数量已达上限,无法新建房间。<br>";
return;
}
# 新建并初始化房间状态
$now_room_nums++;
$starttime = $now + $startmin*5;
$db->query("INSERT INTO {$gtablepre}game (gamenum,groomid,groomownid,gamestate,starttime) VALUES ('1','$now_room_nums','{$udata['username']}','0','$starttime')");
# 加入房间
roommng_join_room($now_room_nums,$udata);
return;
}
# 加入一个房间
function roommng_join_room($rkey,&$udata)
{
global $db,$gtablepre;
if(!empty($udata['roomid']))
{
echo "你已经在房间里了。要加入房间必须退出当前房间。<br>";
return;
}
$result = $db->query("SELECT * FROM {$gtablepre}game WHERE groomid='$rkey'");
if($db->num_rows($result))
{
$gdata = $db->fetch_array($result);
$gdata['groomnums']++;
# 更新房间内玩家数量
$db->query("UPDATE {$gtablepre}game SET groomnums={$gdata['groomnums']} WHERE groomid={$rkey}");
# 加入房间
$db->query("UPDATE {$gtablepre}users SET roomid={$rkey} WHERE username='{$udata['username']}'");
}
else
{
# 要加入的房间号不存在时,尝试新建一个
roommng_create_new_room($udata);
}
return;
}
# 离开当前房间
function roommng_exit_room(&$udata)
{
global $db,$gtablepre;
if(empty($udata['roomid']))
{
echo "你不在任何房间里。<br>";
return;
}
echo "已退出房间{$udata['roomid']}<br>";
# 退出房间时更新房间状态
$result = $db->query("SELECT * FROM {$gtablepre}game WHERE groomid={$udata['roomid']}");
if($db->num_rows($result))
{
$gdata = $db->fetch_array($result);
$gdata['groomnums']--;
# 检查解散房间还是更新房间状态
if($gdata['groomnums'] > 0)
{
# 房主退出房间时,将房主权限移交给房间内其他人
if(!empty($gdata['groomownid']) && $gdata['groomownid'] == $udata['username'])
{
$result2 = $db->query("SELECT * FROM {$gtablepre}users WHERE roomid={$udata['roomid']} AND username!='{$udata['username']}'");
if($db->num_rows($result2))
{
$udata2 = $db->fetch_array($result2);
$new_ownid = $udata2['uid'];
echo "将房主权限移交给了{$udata2['username']}<br>";
}
}
if(isset($new_ownid))
{
$db->query("UPDATE {$gtablepre}game SET groomnums={$gdata['groomnums']},groomownid={$new_ownid} WHERE groomid={$udata['roomid']}");
}
else
{
$db->query("UPDATE {$gtablepre}game SET groomnums={$gdata['groomnums']} WHERE groomid={$udata['roomid']}");
}
}
else
{
roommng_close_room($udata['roomid']);
}
}
# 更新用户状态
$db->query("UPDATE {$gtablepre}users SET roomid = 0 WHERE username='{$udata['username']}'");
return;
}
# 房主解散自己所在的房间
function roommng_close_own_room(&$udata)
{
global $db,$gtablepre;
if(empty($udata['roomid']))
{
echo "你不在任何房间里。<br>";
return;
}
$result = $db->query("SELECT * FROM {$gtablepre}game WHERE groomid={$udata['roomid']}");
if($db->num_rows($result))
{
$gdata = $db->fetch_array($result);
# 不能解散没有房主的房间
if(empty($gdata['groomownid']) || (!empty($gdata['groomownid']) && $gdata['groomownid'] != $udata['username']))
{
echo "你没有权限解散房间{$udata['roomid']}<br>";
return;
}
# 不能解散正在游戏中的房间
if($gdata['gamestate'] > 10 && $gdata['alivenum'])
{
echo "不能解散游戏正在进行且尚有幸存者的房间!<br>";
return;
}
# 解散房间
roommng_close_room($udata['roomid']);
}
# 更新用户状态
$db->query("UPDATE {$gtablepre}users SET roomid = 0 WHERE username='{$udata['username']}'");
return;
}
# 强制解散指定房间
function roommng_close_room($rkey)
{
global $db,$gtablepre;
......@@ -54,15 +209,16 @@ function roommng_close_room($rkey,$log_print=0)
{
$gdata = $db->fetch_array($result);
# 清空房间内玩家
if($gdata['groomnums']) $db->query("UPDATE {$gtablepre}users SET roomid = 0 WHERE roomid= {$rkey}");
if($gdata['groomnums']) $db->query("UPDATE {$gtablepre}users SET roomid=0 WHERE roomid={$rkey}");
# 关闭房间
$db->query("DELETE FROM {$gtablepre}game WHERE groomid = {$rkey}");
if($log_print) echo "已关闭房间 {$rkey} 号<br>";
$db->query("DELETE FROM {$gtablepre}game WHERE groomid={$rkey}");
echo "已关闭房间 {$rkey} 号<br>";
}
else
{
if($log_print) echo "房间 {$rkey} 未开启,或房间不存在!<br>";
echo "房间 {$rkey} 未开启,或房间不存在!<br>";
}
return;
}
......
......@@ -29,48 +29,24 @@ if(!empty($roomact))
if($udata['password'] != $cpass) { gexit($_ERROR['wrong_pw'], __file__, __line__); }
if($udata['groupid'] <= 0) { gexit($_ERROR['user_ban'], __file__, __line__); }
if(strpos($roomact,'join') !== false)
if($roomact == 'create')
{
roommng_create_new_room($udata);
}
elseif(strpos($roomact,'join') !== false)
{
if(!empty($udata['roomid'])) gexit('你已经在房间里了,想加入其它房间要先从当前房间退出。', __file__, __line__);
$join_id = (int)str_replace("join","",$roomact);
if(in_array($join_id,range(1,$max_rooms)))
{
$db->query("UPDATE {$gtablepre}users SET roomid = {$join_id} WHERE username='$cuser'");
$result = $db->query("SELECT groomnums FROM {$gtablepre}game WHERE groomid = {$join_id}");
if($db->num_rows($result))
{
$join_nums = $db->result($result, 0);
$join_nums++;
$db->query("UPDATE {$gtablepre}game SET groomnums = {$join_nums} WHERE groomid = {$join_id}");
}
}
else
{
gexit('要加入的房间不存在!', __file__, __line__);
}
unset($roomact);
roommng_join_room($join_id,$udata);
}
elseif($roomact == 'exit')
{
if(empty($udata['roomid'])) gexit('你没有在任何房间里!', __file__, __line__);
$result = $db->query("SELECT groomnums FROM {$gtablepre}game WHERE groomid = {$udata['roomid']}");
if($db->num_rows($result))
{
$join_nums = $db->result($result, 0);
$join_nums--;
# 退出房间时,检查是关闭房间还是减少房间内人数
if($join_nums > 0)
{
$db->query("UPDATE {$gtablepre}game SET groomnums = {$join_nums} WHERE groomid = {$udata['roomid']}");
}
else
{
roommng_close_room($udata['roomid']);
}
}
$db->query("UPDATE {$gtablepre}users SET roomid = 0 WHERE username='$cuser'");
unset($roomact);
roommng_exit_room($udata);
}
elseif($roomact == 'close')
{
roommng_close_own_room($udata);
}
unset($roomact);
}
else
{
......
......@@ -9,6 +9,7 @@ CREATE TABLE bra_game (
`gamestate` tinyint(3) unsigned NOT NULL DEFAULT '0',
`groomid` tinyint(3) unsigned NOT NULL DEFAULT '0',
`groomnums` tinyint(3) unsigned NOT NULL DEFAULT '0',
`groomownid` char(15) NOT NULL default '',
`starttime` int(10) unsigned NOT NULL DEFAULT '0',
`winmode` tinyint(3) unsigned NOT NULL DEFAULT '0',
`winner` char(15) NOT NULL DEFAULT '',
......
<dialog id="roomlist" style="max-width: 90%;max-height: 80%; text-align: left;">
<span class="evergreen">当前房间列表:<br><span class="grey">房间状态更新可能存在延迟<br>如果发现房间状态不对请刷新几次</span></span><br>
<div style="height:5px;">&nbsp;</div>
<form method="post" name="roomact" onsubmit="return false;" >
<input type="hidden" name="roomact" id="roomact" value="">
<table CellSpacing=0 CellPadding=0 class="infotable">
<tr>
<td class="b1" width="60px">编号</td>
<!--<td class="b1" width="80px">游戏模式</td>-->
<td class="b1" width="60px">状态</td>
<!--<td class="b1" width="120px">创建者/房主</td>-->
<td class="b1" width="60px">人数</td>
<!--<td class="b1" width="120px">运行时间</td>-->
<td class="b1" width="80px">操作</td>
</tr>
<!--{loop range(1,$max_rooms) $rkey}-->
<tr>
<td class="b3" width="60px" height="30px">
$rkey
</td>
<!--<td class="b3" width="80px">游戏模式</td>-->
<td class="b3" width="60px">
<!--{if !empty($roomlist[$rkey]['gamestate'])}-->
$gstate[$roomlist[$rkey]['gamestate']]
<!--{else}-->
<span class="grey">未开启</span>
<!--{/if}-->
</td>
<!--<td class="b3" width="120px">
房主
</td>-->
<td class="b3" width="60px">
<!--{if !empty($roomlist[$rkey]['groomnums'])}-->
<span class="yellow">$roomlist[$rkey]['groomnums']</span>
<!--{else}-->
<span class="grey">-</span>
<!--{/if}-->
</td>
<!--<td class="b3" width="120px">运行时间</td>-->
<td class="b3" width="80px">
<!--{if !empty($rid)}-->
<!--{if $rid == $rkey}-->
<input type="button" value="退出房间" onclick="$('roomact').value='exit';postCmd('roomact','index.php');setTimeout(function(){window.location.href='index.php';},300);disabled=true;" >
<!--{eval $now_rooms = !empty($roomlist) ? count($roomlist) : 0;}-->
<span class="evergreen">当前房间列表({$now_rooms}/{$max_rooms})
<!--{if $now_rooms < $max_rooms && !$groomid}-->
<input type="button" value="点击创建一个新的房间" onclick="$('roomact').value='create';postCmd('roomact','index.php');setTimeout(function(){window.location.href='index.php';},300);disabled=true;" >
<!--{/if}-->
<br>
<div style="height:5px;">&nbsp;</div>
<table CellSpacing=0 CellPadding=0 class="infotable">
<tr>
<td class="b1" width="60px">编号</td>
<!--<td class="b1" width="80px">游戏模式</td>-->
<td class="b1" width="60px">状态</td>
<td class="b1" width="120px">创建者/房主</td>
<td class="b1" width="60px">人数</td>
<!--<td class="b1" width="120px">运行时间</td>-->
<td class="b1" width="80px">操作</td>
</tr>
<!--{if !empty($now_rooms)}-->
<!--{loop $roomlist $rkey $rinfo}-->
<tr>
<td class="b3" width="60px" height="30px">
$rkey
</td>
<!--<td class="b3" width="80px">游戏模式</td>-->
<td class="b3" width="60px">
$gstate[$rinfo['gamestate']]
</td>
<td class="b3" width="120px">
$rinfo['groomownid']
</td>
<td class="b3" width="60px">
$rinfo['groomnums']
</td>
<!--<td class="b3" width="120px">运行时间</td>-->
<td class="b3" width="80px">
<!--{if !empty($rid)}-->
<!--{if $rid == $rkey}-->
<span tooltip2="退出房间后,如房间内没有其他玩家,则房间会解散;房主退出房间会将房主权限转移给房间内其他玩家;">
<input type="button" value="退出" onclick="$('roomact').value='exit';postCmd('roomact','index.php');setTimeout(function(){window.location.href='index.php';},300);disabled=true;" >
</span>
<!--{if !empty($rinfo['groomownid']) && $rinfo['groomownid'] == $cuser}-->
<span tooltip2="不能解散正在进行游戏且尚有其他玩家存活的房间">
<input type="button" value="解散" onclick="$('roomact').value='close';postCmd('roomact','index.php');setTimeout(function(){window.location.href='index.php';},300);disabled=true;" >
</span>
<!--{/if}-->
<!--{else}-->
<span class="grey">-</span>
<!--{/if}-->
<!--{else}-->
<span class="grey">-</span>
<input type="button" value="加入" onclick="$('roomact').value='join{$rkey}';postCmd('roomact','index.php');setTimeout(function(){window.location.href='index.php';},300);disabled=true;" >
<!--{/if}-->
<!--{else}-->
<input type="button" value="加入房间" onclick="$('roomact').value='join{$rkey}';postCmd('roomact','index.php');setTimeout(function(){window.location.href='index.php';},300);disabled=true;" >
<!--{/if}-->
</td>
</tr>
<!--{/loop}-->
</table>
</td>
</tr>
<!--{/loop}-->
<!--{/if}-->
</table>
</form>
<img class="dialog-background" src="img/profile.gif" onclick="closeDialog($('roomlist'))">
</dialog>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment