2026-01-27 20:01:19 +08:00
< ? php
namespace app\api\model ;
use app\common\controller\Push ;
use think\Db ;
use think\Model ;
class Monster extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int' ;
// 定义时间戳字段名
protected $createTime = 'createtime' ;
protected $updateTime = 'updatetime' ;
public static function init ()
{
self :: beforeWrite ( function ( $row ) {
});
}
//选择金币列表
public function choice_gold_list (){
$list = db :: name ( 'monster_multiple' ) -> field ( 'multiple' ) -> select ();
$data = [];
$data [ 'list' ] = $list ;
return [ 'code' => 1 , 'msg' => '获取成功' , 'data' => $data ];
}
//游戏详情
public function get_monster_info ( $uid ){
$config = get_system_config ();
$user_info = db :: name ( 'user_wallet_coin' ) -> where ( 'user_id' , $uid ) -> field ( 'id,coin' ) -> find ();
if ( ! $user_info ){
return [ 'code' => 0 , 'msg' => '参数错误' , 'data' => null ];
}
$now_time = time ();
$data = [];
$data [ 'surplus_time' ] = 0 ;
$data [ 'is_finsh' ] = 3 ;
$data [ 'win_number' ] = 0 ;
// $data['list'] = [];
$data [ 'open_monster_price' ] = $config [ 'open_monster_price' ];
$data [ 'integral' ] = $user_info [ 'coin' ];
$data [ 'multiple_list' ] = db :: name ( 'monster' ) -> field ( 'id,type,type_name,multiple' ) -> order ( 'id asc' ) -> select ();
foreach ( $data [ 'multiple_list' ] as $a => & $b ){
$b [ 'num' ] = 0 ;
}
//当前是否有开放该活动
$monster_log = db :: name ( 'monster_log' ) -> order ( 'id desc' ) -> limit ( 1 ) -> find ();
if ( empty ( $monster_log )){
return [ 'code' => 0 , 'msg' => '获取成功' , 'data' => $data ];
} else {
if ( $monster_log [ 'end_time' ] <= $now_time ){
$is_finish = 1 ;
$data [ 'win_number' ] = $monster_log [ 'win_type' ];
} else {
$is_finish = 2 ;
$data [ 'surplus_time' ] = $monster_log [ 'end_time' ] - $now_time ;
}
$data [ 'is_finsh' ] = $is_finish ;
foreach ( $data [ 'multiple_list' ] as $a => & $b ){
$user_monster_feed_log = db :: name ( 'user_monster_log' ) -> where ( 'uid' , $uid ) -> where ( 'mid' , $monster_log [ 'id' ]) -> where ( 'type' , $b [ 'type' ]) -> find ();
if ( ! empty ( $user_monster_feed_log )){
$b [ 'num' ] = $user_monster_feed_log [ 'num' ];
}
}
}
return [ 'code' => 1 , 'msg' => '获取成功' , 'data' => $data ];
}
//生成当前国王游戏期数
public function create_monster_log (){
set_time_limit ( 0 );
ini_set ( 'memory_limit' , '1024M' );
$config = get_system_config ();
$now_time = time ();
$is_push = 5 ; //不推送
Db :: startTrans ();
2026-01-27 20:32:29 +08:00
try {
2026-01-27 20:01:19 +08:00
$map = [];
$map [ 'is_delete' ] = 1 ;
$monster_log = db :: name ( 'monster_log' ) -> where ( $map ) -> find ();
if ( $monster_log ){ //当前有未结束的国王游戏期数
if ( $now_time >= $monster_log [ 'end_time' ]){
$key_name = " api:monster:create_monster_log:id " . $monster_log [ 'id' ];
$redis_order_sn = redis_lock_exits ( $key_name , 1 , 7 );
if ( ! $redis_order_sn ){
return [ 'code' => 0 , 'msg' => '请重试1' , 'data' => null ];
}
$win_type = $monster_log [ 'win_type' ];
$in_amount = 0 ;
$win_type = $this -> check_win_type_is_loss ( $win_type , $monster_log );
$win_gift_info = db :: name ( 'vs_gift' ) -> alias ( 'a' ) -> join ( 'monster b' , 'a.gid = b.gid' ) -> where ( 'b.type' , $win_type ) -> field ( 'a.gid,a.gift_name,a.gift_price,a.base_image,b.multiple' ) -> find ();
//获取得奖用户ID
$map = [];
$map [ 'mid' ] = $monster_log [ 'id' ];
$part_user_array = db :: name ( 'user_monster_log' ) -> where ( $map ) -> group ( 'uid' ) -> column ( 'uid' );
$map [ 'type' ] = $win_type ;
$user_array = db :: name ( 'user_monster_log' ) -> where ( $map ) -> group ( 'uid' ) -> column ( 'uid' );
if ( ! empty ( $user_array )){
//有获奖用户
$insert_all = [];
foreach ( $user_array as $a => $b ){
$map = [];
$map [ 'mid' ] = $monster_log [ 'id' ];
$map [ 'uid' ] = $b ;
$map [ 'type' ] = $win_type ;
$user_monster_log = db :: name ( 'user_monster_log' ) -> where ( $map ) -> find ();
$wim_num = floor ( $user_monster_log [ 'price' ] * $win_gift_info [ 'multiple' ] / $win_gift_info [ 'gift_price' ]);
$in_amount += $win_gift_info [ 'gift_price' ] * $wim_num ;
//礼物送到背包
$reslut = model ( 'api/UserGiftPack' ) -> change_user_gift_pack ( $b , $win_gift_info [ 'gid' ], $wim_num , model ( 'UserGiftPack' ) :: LY_DRAW_GIFT_GET , '炼仙传说中奖获取' );
if ( $reslut [ 'code' ] == 1 ){
Db :: rollback ();
return [ 'code' => 0 , 'msg' => $reslut [ 'msg' ], 'data' => null ];
}
//用户中奖信息
$insert = [];
$insert [ 'uid' ] = $b ;
$insert [ 'rid' ] = $user_monster_log [ 'rid' ];
$insert [ 'mid' ] = $monster_log [ 'id' ];
$insert [ 'win_type' ] = $win_type ;
$insert [ 'win_gid' ] = $win_gift_info [ 'gid' ];
$insert [ 'price' ] = $user_monster_log [ 'price' ];
$insert [ 'num' ] = $wim_num ;
$insert [ 'createtime' ] = time ();
$insert_all [] = $insert ;
}
$reslut = db :: name ( 'user_monster_win_log' ) -> insertAll ( $insert_all );
if ( ! $reslut ){
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '请重试2' , 'data' => null ];
}
}
//结束当前抽卡期数
$update = [];
$update [ 'win_type' ] = $win_type ;
$update [ 'in_amount' ] = $in_amount ;
$update [ 'is_delete' ] = 2 ;
$update [ 'updatetime' ] = time ();
$reslut = db :: name ( 'monster_log' ) -> where ( 'id' , $monster_log [ 'id' ]) -> update ( $update );
if ( ! $reslut ){
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '请重试3' , 'data' => null ];
}
$is_push = 2 ;
}
} else { //当前未有国王游戏期数
if ( $config [ 'open_monster_status' ] != 1 ){
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '游戏已关闭' , 'data' => null ];
}
//距离上次多长时间
$map = [];
$map [ 'is_delete' ] = 2 ;
$last_monster_log = db :: name ( 'monster_log' ) -> where ( $map ) -> order ( 'id desc' ) -> limit ( 1 ) -> find ();
if ( $last_monster_log ){
// $interval_time = $last_monster_log['end_time'] + 10 + $config['end_interval_time'];
$interval_time = time () - $last_monster_log [ 'end_time' ];
if ( $interval_time < $config [ 'end_interval_time' ]){
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '距离上次结束尚未超过' . $config [ 'end_interval_time' ] . '秒' , 'data' => null ];
}
}
//当前国王游戏配置信息
$monster_list = db :: name ( 'monster' ) -> order ( 'id asc' ) -> select ();
$monster_data = '' ;
$total_num = 0 ;
$win_number_data = [];
foreach ( $monster_list as $a => $b ){
$da = $b [ 'id' ] . '-' . $b [ 'gid' ] . '-' . $b [ 'num' ] . ';' ;
$monster_data .= $da ;
$total_num += $b [ 'num' ];
for ( $i = 0 ; $i < $b [ 'num' ]; $i ++ ){
$win_number_data [] = $b [ 'id' ];
}
}
shuffle ( $win_number_data );
$win_key = array_rand ( $win_number_data );
//随机抽取中奖数字
$win_type = $win_number_data [ $win_key ];
//生成最新
$insert = [];
$insert [ 'win_type' ] = $win_type ;
$insert [ 'total_num' ] = $total_num ;
$insert [ 'config_text' ] = $monster_data ;
$insert [ 'createtime' ] = $now_time ;
$insert [ 'end_time' ] = $now_time + $config [ 'monster_count_down_time' ];
$reslut = db :: name ( 'monster_log' ) -> insert ( $insert );
if ( ! $reslut ){
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '生成失败' , 'data' => null ];
}
$is_push = 1 ; //开启推送
}
Db :: commit ();
if ( $is_push == 1 ){
$push_feed_monster_data = $this -> get_new_feed_monster_info ();
$push_data = [];
$push_data [ 'code' ] = 3031 ;
$push_data [ 'msg' ] = " 探索开始播报 " ;
$push_data [ 'data' ] = $push_feed_monster_data ;
// 推送
$push = new Push ( UID );
$push -> xianxuan ( $push_data );
} else if ( $is_push == 2 ){
$push_feed_monster_data = $this -> get_new_feed_monster_info ();
$push_feed_monster_data [ 'win_count' ] = count ( $user_array );
$push_feed_monster_data [ 'game_name' ] = '国王游戏' ;
$push_feed_monster_data [ 'win_type_name' ] = db :: name ( 'monster' ) -> where ( 'type' , $win_type ) -> value ( 'type_name' );
$push_feed_monster_data [ 'gift_name' ] = $win_gift_info [ 'gift_name' ];
$push_feed_monster_data [ 'num' ] = 0 ;
$push_feed_monster_data [ 'total_gift_price' ] = 0 ;
$push_feed_monster_data [ 'is_push_message' ] = 2 ;
if ( ! empty ( $part_user_array )){
foreach ( $part_user_array as $k => $v ){
$push_user_monster_data = [];
$push_user_monster_data [ 'win_type' ] = $win_type ;
$push_user_monster_data [ 'type_name' ] = db :: name ( 'monster' ) -> where ( 'type' , $win_type ) -> value ( 'type_name' );
//是否中奖
$map = [];
$map [ 'mid' ] = $monster_log [ 'id' ];
$map [ 'uid' ] = $v ;
$user_win_info = db :: name ( 'user_monster_win_log' ) -> where ( $map ) -> find ();
if ( $user_win_info ){ //中奖
$push_user_monster_data [ 'is_win' ] = 1 ;
$push_user_monster_data [ 'gid' ] = $win_gift_info [ 'gid' ];
$push_user_monster_data [ 'gift_name' ] = $win_gift_info [ 'gift_name' ];
$push_user_monster_data [ 'base_image' ] = localpath_to_netpath ( $win_gift_info [ 'base_image' ]);
$push_user_monster_data [ 'gift_price' ] = $win_gift_info [ 'gift_price' ];
$push_user_monster_data [ 'num' ] = $user_win_info [ 'num' ];
$push_user_monster_data [ 'total_gift_price' ] = $win_gift_info [ 'gift_price' ] * $user_win_info [ 'num' ];
$push_feed_monster_data [ 'num' ] += $user_win_info [ 'num' ];
$push_feed_monster_data [ 'total_gift_price' ] += $push_user_monster_data [ 'total_gift_price' ];
} else { //未中奖
$push_user_monster_data [ 'is_win' ] = 2 ;
$push_user_monster_data [ 'gid' ] = 0 ;
$push_user_monster_data [ 'gift_name' ] = '' ;
$push_user_monster_data [ 'base_image' ] = '' ;
$push_user_monster_data [ 'gift_price' ] = 0 ;
$push_user_monster_data [ 'num' ] = 0 ;
}
$push_data = [];
$push_data [ 'code' ] = 3032 ;
$push_data [ 'msg' ] = " 抽卡个人结束播报 " ;
$push_data [ 'data' ] = $push_user_monster_data ;
$push = new Push ( UID );
$push -> xianxuan ( $push_data );
// error_log(date('Y-m-d H:i:s').'_'.$v.'_'.$reslut."\r\n", 3, '1111.txt');
}
}
if ( $push_feed_monster_data [ 'num' ] > 0 ){
$push_feed_monster_data [ 'is_push_message' ] = 1 ;
}
$push_data = [];
$push_data [ 'code' ] = 3033 ;
$push_data [ 'msg' ] = " 探索结束播报 " ;
$push_data [ 'data' ] = $push_feed_monster_data ;
$push = new Push ( UID );
$push -> xianxuan ( $push_data );
// $close_data = $this->get_close_monster_win_log();
// $push_colse_data = [];
// $push_colse_data['code'] = 3034;
// $push_colse_data['msg'] = "国王游戏结束播报";
// $push_colse_data['data'] = $close_data;
// model('api/WebSocketPush')->send_to_all($push_colse_data);
}
return [ 'code' => 1 , 'msg' => '成功' , 'data' => null ];
2026-01-27 20:32:29 +08:00
} catch ( \Exception $e ) {
// 回滚事务
dump ( $e );
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '失败' , 'data' => null ];
}
2026-01-27 20:01:19 +08:00
}
//是否修改中奖类型
public function check_win_type_is_loss ( $win_type , $monster_log ){
$config = get_system_config ();
//当前亏损数值是否大于
$map = [];
$map [ 'is_delete' ] = 2 ;
$map [ 'out_amount' ] = [ '>' , 0 ];
$now_monster_out_amount = db :: name ( 'monster_log' ) -> where ( $map ) -> sum ( 'out_amount' ); //投入
$now_monster_in_amount = db :: name ( 'monster_log' ) -> where ( $map ) -> sum ( 'in_amount' ); //产出
//获取得奖用户ID
$map = [];
$map [ 'mid' ] = [ '=' , $monster_log [ 'id' ]];
$map [ 'type' ] = [ '=' , $win_type ];
$user_array = db :: name ( 'user_monster_log' ) -> where ( $map ) -> group ( 'uid' ) -> column ( 'uid' );
if ( ! empty ( $user_array )){
$wim_monster = db :: name ( 'monster' ) -> where ( 'type' , $win_type ) -> find ();
$in_amount = 0 ;
foreach ( $user_array as $a => $b ){
$map = [];
$map [ 'mid' ] = [ '=' , $monster_log [ 'id' ]];
$map [ 'uid' ] = [ '=' , $b ];
$map [ 'type' ] = [ '=' , $win_type ];
$user_monster_log = db :: name ( 'user_monster_log' ) -> where ( $map ) -> find ();
//中奖金额
$win_price = $user_monster_log [ 'price' ] * $wim_monster [ 'multiple' ];
$in_amount += $win_price ;
}
$profit = ( $now_monster_in_amount + $in_amount ) - ( $now_monster_out_amount + $monster_log [ 'out_amount' ]);
if ( $profit > $config [ 'monster_max_loss_price' ]){ //亏损大于3w
$type_price_1 = db :: name ( 'user_monster_log' ) -> where ( 'mid' , $monster_log [ 'id' ]) -> where ( 'type' , 1 ) -> sum ( 'price' );
$win_type_amount_1 = $type_price_1 * 2 ;
$type_price_2 = db :: name ( 'user_monster_log' ) -> where ( 'mid' , $monster_log [ 'id' ]) -> where ( 'type' , 2 ) -> sum ( 'price' );
$win_type_amount_2 = $type_price_2 * 4 ;
$type_price_3 = db :: name ( 'user_monster_log' ) -> where ( 'mid' , $monster_log [ 'id' ]) -> where ( 'type' , 3 ) -> sum ( 'price' );
$win_type_amount_3 = $type_price_3 * 6.5 ;
$type_price_4 = db :: name ( 'user_monster_log' ) -> where ( 'mid' , $monster_log [ 'id' ]) -> where ( 'type' , 4 ) -> sum ( 'price' );
$win_type_amount_4 = $type_price_4 * 10 ;
$type_price_5 = db :: name ( 'user_monster_log' ) -> where ( 'mid' , $monster_log [ 'id' ]) -> where ( 'type' , 5 ) -> sum ( 'price' );
$win_type_amount_5 = $type_price_5 * 15 ;
$type_price_6 = db :: name ( 'user_monster_log' ) -> where ( 'mid' , $monster_log [ 'id' ]) -> where ( 'type' , 6 ) -> sum ( 'price' );
$win_type_amount_6 = $type_price_6 * 20 ;
// $type_price_6 = db::name('user_monster_log')->where('mid', $monster_log['id'])->where('type', 6)->sum('price');
// $win_type_amount_6 = $type_price_6 * 50;
// $win_type_amount_arr = [$win_type_amount_1, $win_type_amount_2, $win_type_amount_3, $win_type_amount_4, $win_type_amount_5, $win_type_amount_6];
$win_type_amount_arr = [ $win_type_amount_1 , $win_type_amount_2 , $win_type_amount_3 , $win_type_amount_4 , $win_type_amount_5 , $win_type_amount_6 ];
// $win_type_amount_arr = [$win_type_amount_1, $win_type_amount_2, $win_type_amount_3, $win_type_amount_4];
$win_type_min = min ( $win_type_amount_arr );
// if($win_type_amount_4 == $win_type_min && $win_type_amount_3 == $win_type_min && $win_type_amount_2 == $win_type_min && $win_type_amount_1 == $win_type_min){
// $win_type = rand(1,4);
// }else
if ( $win_type_amount_1 == $win_type_min ){
$win_type = 1 ;
} else if ( $win_type_amount_2 == $win_type_min ){
$win_type = 2 ;
} else if ( $win_type_amount_3 == $win_type_min ){
$win_type = 3 ;
} else if ( $win_type_amount_4 == $win_type_min ){
$win_type = 4 ;
} else if ( $win_type_amount_5 == $win_type_min ){
$win_type = 5 ;
} else if ( $win_type_amount_6 == $win_type_min ){
$win_type = 6 ;
}
}
}
return $win_type ;
}
//当前最新探索信息
public function get_new_feed_monster_info (){
$now_time = time ();
$data = [];
$data [ 'surplus_time' ] = 0 ;
$data [ 'is_finsh' ] = 3 ;
$data [ 'win_number' ] = 0 ;
// $data['list'] = [];
$data [ 'multiple_list' ] = db :: name ( 'monster' ) -> field ( 'id,type,type_name,multiple' ) -> order ( 'id asc' ) -> select ();
//当前是否有开放该活动
$monster_log = db :: name ( 'monster_log' ) -> order ( 'id desc' ) -> limit ( 1 ) -> find ();
if ( empty ( $monster_log )){
return $data ;
} else {
if ( $monster_log [ 'end_time' ] <= $now_time ){
$is_finish = 1 ;
$data [ 'win_number' ] = $monster_log [ 'win_type' ];
} else {
$is_finish = 2 ;
$data [ 'surplus_time' ] = $monster_log [ 'end_time' ] - $now_time ;
}
$data [ 'is_finsh' ] = $is_finish ;
}
//当前参与用户探索列表
// $list = db::name('user_monster_feed_log')->alias('a')->join('yy_user b', 'a.uid = b.uid')->join('yy_monster c', 'a.type = c.type')->where('a.mid', $monster_log['id'])->field('a.id,b.uid,b.nick_name,b.base64_nick_name,b.head_pic,c.type_name,a.price')->order('a.id desc')->select();
// foreach ($list as $k => &$v){
// $v['nick_name'] = mb_convert_encoding(base64_decode($v['base64_nick_name']), 'UTF-8', 'UTF-8');
// $v['head_pic'] = localpath_to_netpath($v['head_pic']);
// }
// $data['list'] = $list;
return $data ;
}
//国王游戏
public function open_beat_monster ( $uid , $rid , $type , $num ){
$user_info = db :: name ( 'user_wallet_coin' ) -> where ( 'user_id' , $uid ) -> field ( 'id,coin' ) -> find ();
if ( ! $user_info ){
return [ 'code' => 0 , 'msg' => '参数错误' , 'data' => null ];
}
$config = get_system_config ();
$now_time = time ();
$monster = db :: name ( 'monster' ) -> where ( 'type' , $type ) -> find ();
if ( ! $monster ){
return [ 'code' => 0 , 'msg' => '当前类型不存在' , 'data' => null ];
}
//
$map = [];
$map [ 'end_time' ] = [ '>' , time ()];
$map [ 'is_delete' ] = 1 ;
$monster_log = db :: name ( 'monster_log' ) -> where ( $map ) -> find ();
if ( ! $monster_log ){
return [ 'code' => 0 , 'msg' => '请等待新一轮开启' , 'data' => null ];
} else {
$surplus_time = $monster_log [ 'end_time' ] - 5 ;
if ( $surplus_time < time ()){
return [ 'code' => 0 , 'msg' => '最后五秒内无法参与' , 'data' => null ];
}
}
if ( $num < 1 ){
return [ 'code' => 0 , 'msg' => '次数不能小于1' , 'data' => null ];
}
//该星途已探索多少次数
$map = [];
$map [ 'mid' ] = $monster_log [ 'id' ];
$map [ 'uid' ] = $uid ;
$user_type_num = db :: name ( 'user_monster_log' ) -> where ( $map ) -> column ( 'type' );
$map [ 'type' ] = $type ;
$user_monster_num = db :: name ( 'user_monster_log' ) -> where ( $map ) -> sum ( 'num' );
if ( $user_monster_num > 0 ){
if ( $user_monster_num + $num > $config [ 'open_monster_max_num' ]){
return [ 'code' => 0 , 'msg' => '该玩法次数已达上限' , 'data' => null ];
}
} else {
if ( $num > $config [ 'open_monster_max_num' ]){
return [ 'code' => 0 , 'msg' => '该玩法次数已达上限' , 'data' => null ];
}
}
$user_monster_log = db :: name ( 'user_monster_log' ) -> where ( $map ) -> find ();
if ( count ( $user_type_num ) >= 6 && ! in_array ( $type , $user_type_num )){
return [ 'code' => 0 , 'msg' => '玩法类型不能大于六个' , 'data' => null ];
}
// $open_monster_ticket = $config['open_monster_ticket'];
// $pay_ticket = $num * $open_monster_ticket;
// if($user_info['had_ticket_num'] < $pay_ticket){
// return ['code' => 201, 'msg' => '用户剩余门票数不足', 'data' => null];
// }
$money = $num * $config [ 'open_monster_price' ];
if ( $user_info [ 'coin' ] < $money ){
return [ 'code' => 0 , 'msg' => '金币不足' , 'data' => null ];
}
Db :: startTrans ();
try {
//扣除用户金币
$reslut = model ( 'common/UserWallet' ) -> change_user_money ( $uid , $money , 1 , model ( 'common/UserWallet' ) :: LINGXIAN_TRADITION_LOTTERY_COIN , model ( 'common/UserWallet' ) :: ChangeTypeLable ( model ( 'common/UserWallet' ) :: LINGXIAN_TRADITION_LOTTERY_COIN ));
if ( $reslut [ 'code' ] != 1 ) {
Db :: rollback ();
return [ 'code' => 0 , 'msg' => $reslut [ 'msg' ], 'data' => null ];
}
//是否探索过
if ( $user_monster_log ){ //已探索则继续
$update = [];
$update [ 'updatetime' ] = time ();
$reslut = db :: name ( 'user_monster_log' ) -> where ( 'id' , $user_monster_log [ 'id' ]) -> inc ( 'num' , $num ) -> inc ( 'price' , $money ) -> update ( $update );
if ( ! $reslut ){
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '请重试' , 'data' => null ];
}
} else { //未探索则探索
$insert = [];
$insert [ 'uid' ] = $uid ;
$insert [ 'rid' ] = $rid ;
$insert [ 'mid' ] = $monster_log [ 'id' ];
$insert [ 'type' ] = $type ;
$insert [ 'num' ] = $num ;
$insert [ 'price' ] = $money ;
$insert [ 'createtime' ] = time ();
$reslut = db :: name ( 'user_monster_log' ) -> insert ( $insert );
if ( ! $reslut ){
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '请重试' , 'data' => null ];
}
}
//用户探索记录表
$insert_data = [];
$insert_data [ 'uid' ] = $uid ;
$insert_data [ 'rid' ] = $rid ;
$insert_data [ 'mid' ] = $monster_log [ 'id' ];
$insert_data [ 'type' ] = $type ;
$insert_data [ 'num' ] = $num ;
$insert_data [ 'price' ] = $money ;
$insert_data [ 'createtime' ] = time ();
$fid = db :: name ( 'user_monster_feed_log' ) -> insertGetId ( $insert_data );
if ( ! $fid ){
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '请重试' , 'data' => null ];
}
//计入当期支出累消
$update = [];
$update [ 'updatetime' ] = time ();
$reslut = db :: name ( 'monster_log' ) -> where ( 'id' , $monster_log [ 'id' ]) -> inc ( 'out_amount' , $money ) -> update ( $update );
if ( ! $reslut ){
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '请重试' , 'data' => null ];
}
Db :: commit ();
$user_data = db :: name ( 'user' ) -> where ( 'id' , $uid ) -> find ();
$push_feed_monster_data = [];
$push_feed_monster_data [ 'id' ] = $fid ;
$push_feed_monster_data [ 'uid' ] = $uid ;
$push_feed_monster_data [ 'nick_name' ] = $user_data [ 'nickname' ] ? ? " " ;
$push_feed_monster_data [ 'head_pic' ] = $user_data [ 'avatar' ] ? ? " " ;
$push_feed_monster_data [ 'type' ] = $type ;
$push_feed_monster_data [ 'type_name' ] = db :: name ( 'monster' ) -> where ( 'type' , $type ) -> value ( 'type_name' );
$push_feed_monster_data [ 'price' ] = $money ;
$push_feed_monster_data [ 'num' ] = db :: name ( 'user_monster_log' ) -> where ( 'mid' , $monster_log [ 'id' ]) -> where ( 'uid' , $uid ) -> where ( 'type' , $type ) -> sum ( 'num' );
// $push_feed_monster_data['pay_ticket'] = $pay_ticket;
$push_feed_monster_data [ 'integral' ] = db :: name ( 'user_wallet_coin' ) -> where ( 'user_id' , $uid ) -> value ( 'coin' );
// $push_data = [];
// $push_data['code'] = 334;
// $push_data['msg'] = "探索公屏播报";
// $push_data['data'] = $push_feed_monster_data;
// model('api/WebSocketPush')->send_to_all($push_data);
return [ 'code' => 0 , 'msg' => '探索成功' , 'data' => $push_feed_monster_data ];
} catch ( \Exception $e ) {
// 回滚事务
// dump($e);
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '探索失败' , 'data' => null ];
}
}
//规则
public function get_monster_note (){
$config = get_system_config ();
$note = $config [ 'monster_feed_note' ];
return [ 'code' => 1 , 'msg' => '获取成功' , 'data' => $note ];
}
//奖池
public function get_monster_box_list (){
$monster_gift_list = db :: name ( 'monster' ) -> alias ( 'a' ) -> join ( 'vs_gift b' , 'a.gid = b.gid' ) -> field ( 'a.gid,b.base_image,b.gift_name,b.gift_price' ) -> order ( 'b.gift_price asc' ) -> select ();
foreach ( $monster_gift_list as $k => & $v ){
$v [ 'base_image' ] = localpath_to_netpath ( $v [ 'base_image' ]);
}
return [ 'code' => 1 , 'msg' => '获取成功' , 'data' => $monster_gift_list ];
}
//开奖记录
public function get_monster_log ( $uid , $page , $page_limit ){
$user_info = db :: name ( 'user' ) -> where ( 'id' , $uid ) -> find ();
if ( ! $user_info ){
return [ 'code' => 0 , 'msg' => '参数错误' , 'data' => null ];
}
$map = [];
$map [ 'a.is_delete' ] = [ '=' , 2 ];
$monster_list = db :: name ( 'monster_log' ) -> alias ( 'a' ) -> join ( 'monster b' , 'a.win_type = b.type' ) -> field ( 'a.id,a.win_type,b.type_name,a.createtime as add_time' ) -> where ( $map ) -> order ( 'a.id desc' ) -> page ( $page , $page_limit ) -> select ();
foreach ( $monster_list as $k => & $v ){
$v [ 'is_join' ] = 3 ;
$v [ 'gift_name' ] = '' ;
$v [ 'base_image' ] = '' ;
$v [ 'gift_price' ] = 0 ;
$v [ 'num' ] = 0 ;
$user_monster_log = db :: name ( 'user_monster_log' ) -> where ( 'uid' , $uid ) -> where ( 'mid' , $v [ 'id' ]) -> find ();
if ( $user_monster_log ){
$v [ 'is_join' ] = 2 ;
$user_monster_win_log = db :: name ( 'user_monster_win_log' ) -> where ( 'uid' , $uid ) -> where ( 'mid' , $v [ 'id' ]) -> find ();
if ( $user_monster_win_log ){
$v [ 'is_join' ] = 1 ;
$gift_info = db :: name ( 'gift' ) -> find ( $user_monster_win_log [ 'win_gid' ]);
$v [ 'gift_name' ] = $gift_info [ 'gift_name' ];
$v [ 'base_image' ] = localpath_to_netpath ( $gift_info [ 'base_image' ]);
$v [ 'gift_price' ] = $gift_info [ 'gift_price' ];
$v [ 'num' ] = $user_monster_win_log [ 'num' ];
}
}
}
return [ 'code' => 200 , 'msg' => '获取成功' , 'data' => $monster_list ];
}
//我的列表
2026-01-27 20:13:04 +08:00
public function get_user_monster_log ( $uid , $page , $page_limit ){
$user_info = db :: name ( 'user' ) -> where ( 'id' , $uid ) -> find ();
if ( ! $user_info ){
return [ 'code' => 201 , 'msg' => '参数错误' , 'data' => null ];
}
2026-01-27 20:01:19 +08:00
2026-01-27 20:13:04 +08:00
// $map = [];
// $map[] = ['a.uid', '=', $uid];
2026-01-27 20:16:42 +08:00
$user_monster_log = db :: name ( 'user_monster_win_log' )
-> alias ( 'a' )
-> join ( 'user b' , 'a.uid = b.id' )
-> join ( 'monster c' , 'a.win_type = c.type' )
-> join ( 'vs_gift d' , 'd.gid = a.win_gid' )
-> field ( 'a.id,a.mid,b.id as uid,b.nickname,b.avatar,a.win_type,c.type_name,d.gift_name,d.base_image,d.gift_price,a.num,a.createtime as add_time' ) -> order ( 'a.id desc' ) -> page ( $page , $page_limit ) -> select ();
2026-01-27 20:13:04 +08:00
foreach ( $user_monster_log as $k => & $v ){
2026-01-27 20:01:19 +08:00
$user_info = db :: name ( 'user' ) -> where ( 'id' , $v [ 'uid' ]) -> field ( 'id,nickname,avatar' ) -> find ();
$v [ 'nick_name' ] = $user_info [ 'nickname' ];
$v [ 'head_pic' ] = $user_info [ 'avatar' ];
2026-01-27 20:13:04 +08:00
2026-01-27 20:01:19 +08:00
}
2026-01-27 20:16:42 +08:00
return [ 'code' => 0 , 'msg' => '获取成功' , 'data' => $user_monster_log ];
2026-01-27 20:01:19 +08:00
}
//中奖次数榜单
public function get_monster_winner_num_list ( $uid , $time ){
if ( in_array ( $time , [ 1 , 2 , 3 ])){
}
$model = db :: name ( 'user_monster_win_log' ) -> field ( 'uid, count(id) as win_num' );
if ( $time == 1 ){
$model = $model -> whereTime ( 'add_time' , 'today' );
} else if ( $time == 2 ){
$model = $model -> whereTime ( 'add_time' , 'week' );
} else if ( $time == 3 ){
$model = $model -> whereTime ( 'add_time' , 'month' );
}
$list = $model -> group ( 'uid' ) -> order ( 'win_num desc, uid asc' ) -> limit ( 30 ) -> select ();
foreach ( $list as $k => & $v ){
$user_info = db :: name ( 'user' ) -> where ( 'id' , $v [ 'uid' ]) -> field ( 'id,nickname,avatar' ) -> find ();
$v [ 'nick_name' ] = $user_info [ 'nickname' ];
$v [ 'head_pic' ] = $user_info [ 'avatar' ];
}
return [ 'code' => 1 , 'msg' => '获取成功' , 'data' => $list ];
}
//设置房间期数开关
public function operate_room_monster_show_status ( $uid , $rid , $is_show_monster_push ){
$room_info = db :: name ( 'vs_room' ) -> where ( 'id' , $rid ) -> find ();
if ( ! $room_info ){
return [ 'code' => 0 , 'msg' => '该房间不存在' , 'data' => null ];
}
$room_admin_uid_arr = explode ( ',' , $room_info [ 'room_admin_uid' ]);
if ( $uid != $room_info [ 'room_owner_uid' ] && ! in_array ( $uid , $room_admin_uid_arr )) {
return [ 'code' => 0 , 'msg' => '无权限操作' , 'data' => null ];
}
Db :: startTrans ();
try {
$update = [];
$update [ 'is_show_monster_push' ] = $is_show_monster_push ;
$update [ 'update_time' ] = time ();
$reslut = db :: name ( 'room' ) -> where ( 'rid' , $rid ) -> update ( $update );
if ( ! $reslut ){
Db :: rollback ();
return [ 'code' => 0 , 'msg' => '修改失败' , 'data' => null ];
}
Db :: commit ();
$data = [];
$data [ 'rid' ] = $rid ;
$data [ 'is_show_monster_push' ] = $is_show_monster_push ;
//推送
$push_data = [];
$push_data [ 'code' ] = 3035 ;
$push_data [ 'msg' ] = " 修改房间期数开关 " ;
$push_data [ 'data' ] = $data ;
model ( 'api/WebSocketPush' ) -> send_to_group ( $rid , $push_data );
return [ 'code' => 200 , 'msg' => '修改成功' , 'data' => $data ];
} catch ( \Exception $e ) {
// 回滚事务
// dump($e);
Db :: rollback ();
return [ 'code' => 201 , 'msg' => '修改失败' , 'data' => null ];
}
}
}