小时榜礼物配置

This commit is contained in:
2025-09-30 21:10:48 +08:00
parent 38d81d3f47
commit 80fe3afca6
7 changed files with 464 additions and 189 deletions

View File

@@ -8,7 +8,7 @@ class RoomHourRanking
{
public static function withdraw_status (){
return [
[25 => '全时段'],
// [25 => '全时段'],
[0 => '00:00-01:00'],
[1 => '01:00-02:00'],
[2 => '02:00-03:00'],
@@ -64,6 +64,8 @@ class RoomHourRanking
if($v['user_id']){
$v['nickname'] = db::name('user')->where(['id'=>$v['user_id']])->value('nickname');
}
$v['stime'] = date('Y-m-d H:i', $v['stime']);
$v['etime'] = date('Y-m-d H:i', $v['etime']);
}
}
@@ -79,7 +81,11 @@ class RoomHourRanking
//房间小时榜配置
public function room_hour_ranking_config()
{
$list = db::name('vs_hour_ranking_config')->order('id', 'desc')->find();
$list = db::name('vs_hour_ranking_config')->where('id', '1')->find();
if($list){
//open_time 字段转化为2025-09-30 07:00 的格式
$list['open_time'] = $list['open_time'] ? date('Y-m-d H:i', $list['open_time']) : 0;
}
return V(1,"成功", $list);
}
@@ -92,14 +98,97 @@ class RoomHourRanking
$data['broadcast_times'] = input('broadcast_times');
$data['is_open_xlh'] = input('is_open_xlh');
$data['min_price'] = input('min_price');
// $data['introd'] = input('introd');
$open_time = input('open_time');
$data['open_time'] = $open_time ? strtotime($open_time) : 0;
$data['createtime'] = time();
$data['updatetime'] = time();
$timeSlots = json_decode(input('timeJson'), true);
$timePeriods = $this->withdraw_status();
// 构建正确的时间映射关系
$timeMap = [];
foreach($timePeriods as $period) {
foreach($period as $key => $value) {
$timeMap[$key] = $value;
}
}
//$timeMap 里面 键值互换
$timeMap = array_flip($timeMap);
//开启事务
db::startTrans();
if($timeSlots){
$insertData = [];
foreach ($timeSlots as $timeSlot) {
// $timeRange = ;
$timeRange = $timeMap[$timeSlot['time']] ?? '99';
foreach ($timeSlot['reward'] as $rewardItem) {
$hasReward = !empty($rewardItem['content']);
if ($hasReward) {
// 有奖励内容:为每个奖励内容创建一条记录
foreach ($rewardItem['content'] as $rewardContent) {
if($rewardContent['type'] == 0){
$coin = $rewardContent['value'];
$gift_id = 0;
$gift_name = '';
}else{
$coin = 0;
$gift_id = $rewardContent['value'];
$gift_name = $rewardContent['name'];
}
$insertData[] = [
'time_id' => $timeRange,
'ranking' => $rewardItem['index'],
// 'rank_name' => $rewardItem['name'],
'gift_type' => $rewardContent['type'],
'gift_id' => $gift_id,
'coin' => $coin,
'name' => $gift_name,
'createtime' => time()
];
}
} else {
// 无奖励内容:插入一条空奖励记录
$insertData[] = [
'time_id' => $timeRange,
'ranking' => $rewardItem['index'],
// 'rank_name' => $rewardItem['name'],
'gift_type' => 0,
'gift_id' => 0,
'coin' => 0,
'name' => '',
'createtime' => time(),
];
}
}
}
// 批量插入
if (!empty($insertData)) {
$del = db::name('vs_hour_ranking_gift_config')->where('id','>',0)->delete();
$ins = db::name('vs_hour_ranking_gift_config')->insertAll($insertData);
}else{
$ins = false;
$del = false;
}
}else{
$del = true;
$ins = true;
}
// $res = db::name('vs_hour_ranking_config')->where('id', $id)->update($data);
$res = db::name('vs_hour_ranking_config')->where('id', $id)->update($data);
if ($res) {
// if ($del && $ins && $res) {
if ($ins) {
db::commit();
return V(1, "成功");
} else {
db::rollback();
return V(0, "失败");
}
}
@@ -133,73 +222,6 @@ class RoomHourRanking
//核心配置列表
public function core_config_list()
{
$list = db::name('vs_hour_ranking_gift_config')->select();
$data = [];
if($list){
// 获取时间段对应关系
$timePeriods = $this->withdraw_status();
$timeMap = [];
foreach($timePeriods as $period) {
foreach($period as $key => $value) {
$timeMap[$key] = $value;
}
}
// 按时间段和排名分组数据
$groupedData = [];
foreach ($list as $item) {
$timeId = $item['time_id'];
$ranking = $item['ranking'];
if (!isset($groupedData[$timeId])) {
$groupedData[$timeId] = [];
}
if (!isset($groupedData[$timeId][$ranking])) {
$groupedData[$timeId][$ranking] = [];
}
// 根据礼物类型确定值
if($item['gift_type'] == 1) {
$value = $item['coin'];
} else {
$value = $item['gift_id'];
}
$groupedData[$timeId][$ranking][] = [
'type' => $item['gift_type'],
'value' => $value
];
}
// 构建最终数据结构
foreach ($groupedData as $timeId => $rankings) {
$timeKey = $timeMap[$timeId] ?? "未知时间段";
$data[$timeKey] = [];
// 按排名排序
ksort($rankings);
foreach ($rankings as $ranking => $contents) {
$data[$timeKey][] = [
'index' => $ranking,
'content' => $contents
];
}
}
}
return V(1, "成功", $data);
}
public function core_config_lists()
{
$list = db::name('vs_hour_ranking_gift_config')->select();
$data = [];
// 按 time_id 和 ranking 重组数据
$reorganizedData = [];
$timePeriods = $this->withdraw_status();
// 构建正确的时间映射关系
@@ -210,41 +232,118 @@ class RoomHourRanking
}
}
foreach ($list as $item) {
// $timeId = $item['time_id'];
$timeId = $timeMap[$item['time_id']] ?? '未知时间段';
$ranking = $item['ranking'];
// 先按时间段和排名索引分组查询
$timeRanges = db::name('vs_hour_ranking_gift_config')->distinct(true)
->order('time_id')
->column('time_id');
if (!isset($reorganizedData[$timeId])) {
$reorganizedData[$timeId] = [];
}
if (!isset($reorganizedData[$timeId][$ranking])) {
$reorganizedData[$timeId][$ranking] = [];
}
if($item['gift_id']){
if($item['gift_type'] == 2){
$gift_name = db::name('vs_gift')->where(['gid'=>$item['gift_id']])->value('gift_name');
}
if($item['gift_type'] == 3 || $item['gift_type'] == 4){
$gift_name = db::name('vs_decorate')->where(['did'=>$item['gift_id']])->value('title');
$result = [];
foreach ($timeRanges as $timeRange) {
// 查询该时间段的所有数据
$rewards = db::name('vs_hour_ranking_gift_config')->where('time_id', $timeRange)
->field('ranking, gift_type, gift_id,coin,name')
->order('ranking')
->select();
$rewardMap = [];
foreach ($rewards as $reward) {
$rankIndex = $reward['ranking'];
if (!isset($rewardMap[$rankIndex])) {
$rewardMap[$rankIndex] = [
'index' => $rankIndex,
// 'name' => $reward['rank_name'],
'content' => []
];
}
}else{
$gift_name = '';
// 添加奖励内容到content数组
if ($reward['gift_id'] != 0 || $reward['coin'] != 0) {
if($reward['gift_id'] != 0){
$rewardMap[$rankIndex]['content'][] = [
'type' => $reward['gift_type'],
'value' => $reward['gift_id'],
// 'coin' => $reward['coin'],
'name' => $reward['name'],
];
}
if($reward['coin'] != 0){
$rewardMap[$rankIndex]['content'][] = [
'type' => $reward['gift_type'],
'value' => $reward['coin'],
'name' => $reward['name'],
];
}
}
}
$reorganizedData[$timeId][$ranking][] = [
'gift_type' => $item['gift_type'],
'gift_id' => $item['gift_id'],
'gift_name' => $gift_name,
'coin' => $item['coin']
// 按index排序
ksort($rewardMap);
$result[] = [
'time' => $timeMap[$timeRange],
'reward' => array_values($rewardMap)
];
}
// 输出重组后的数据
print_r($reorganizedData);
return V(1, "成功", $data);
return V(1, "成功", $result);
}
// public function core_config_lists()
// {
// $list = db::name('vs_hour_ranking_gift_config')->select();
// $data = [];
//
// // 按 time_id 和 ranking 重组数据
// $reorganizedData = [];
// $timePeriods = $this->withdraw_status();
//
// // 构建正确的时间映射关系
// $timeMap = [];
// foreach($timePeriods as $period) {
// foreach($period as $key => $value) {
// $timeMap[$key] = $value;
// }
// }
//
// foreach ($list as $item) {
//// $timeId = $item['time_id'];
// $timeId = $timeMap[$item['time_id']] ?? '未知时间段';
// $ranking = $item['ranking'];
//
// $gift_name = '';
// if (!isset($reorganizedData[$timeId])) {
// $reorganizedData[$timeId] = [];
// }
// if (!isset($reorganizedData[$timeId][$ranking])) {
// $reorganizedData[$timeId][$ranking] = [];
// }
// if($item['gift_id']){
// if($item['gift_type'] == 2){
// $gift_name = db::name('vs_gift')->where(['gid'=>$item['gift_id']])->value('gift_name');
// }
// if($item['gift_type'] == 3 || $item['gift_type'] == 4){
// $gift_name = db::name('vs_decorate')->where(['did'=>$item['gift_id']])->value('title');
// }
//
// }else{
// $gift_name = '';
// }
// $reorganizedData[$timeId][$ranking][] = [
// 'gift_type' => $item['gift_type'],
// 'gift_id' => $item['gift_id'],
// 'gift_name' => $gift_name,
// 'coin' => $item['coin']
// ];
// }
//
// // 输出重组后的数据
// print_r($reorganizedData);
// return V(1, "成功", $data);
// }
}

View File

@@ -691,7 +691,7 @@ class Room extends Model
if (!$is_join) {
db::name('vs_room_visitor')->insert(['room_id' => $room_id, 'user_id' => $user_id, 'createtime' => time()]);
}
// db::name('user_data')->where('user_id', $user_id)->update(['room_id' => $room_id]);
db::name('user_data')->where('user_id', $user_id)->update(['room_id' => $room_id]);
$room['is_use_code'] = 0;
$liang = model('api/Decorate')->user_decorate_detail($room['id'],7);
@@ -850,7 +850,7 @@ class Room extends Model
}
$xlh_info['xlh_info'] = model('BlindBoxTurntableGift')->get_user_xlh_info($room_id);
$open_time = db::name('vs_hour_ranking_config')->order('id', 'desc')->value('open_time');
$open_time = db::name('vs_hour_ranking_config')->where('id', 1)->value('open_time');
if($open_time > 0){
if($open_time <= time()){
$hour_open = 1;//开启
@@ -2555,27 +2555,34 @@ class Room extends Model
public function user_reconnect($user_id,$room_id)
{
if(!$room_id){
$room_id = Db::name('vs_room_visitor')->where('user_id', $user_id)->order('id desc')->value('room_id');
$room_id = Db::name('user_data')->where('user_id', $user_id)->value('room_id');
}
if(!$room_id){
return ['code' => 0, 'msg' => '参数错误', 'data' => null];
}
//查询用户在几个房间
$room = db::name('vs_room_visitor')->where('user_id', $user_id)->select();
if($room){
$room_list = [];
$userinroom = 0;
foreach ($room as $v){
$room_list[] = $v['room_id'];
if($v['room_id'] == $room_id){
$userinroom = $room_id;
break;
}
}
$count = count($room_list);
if($count == 1 && $room_id == $room_list[0]){
$room_info = db::name('vs_room_visitor')->where(['id' => $room[0]['id']])->update(['room_id' => $room_id, 'user_id' => $user_id, 'updatetime' => time(),'remarks' =>'断线后重连']);
if($userinroom == $room_id){
$room_info = db::name('vs_room_visitor')->where(['id' => $room_id])->update(['room_id' => $room_id, 'user_id' => $user_id, 'updatetime' => time(),'remarks' =>'断线后重连']);
if(!$room_info){
return ['code' => 0, 'msg' => '更新加入房间失败', 'data' => null];
}
}elseif ($count > 1)
}else
{
//退出除过最后一个房间
for ($i = 0; $i < $count - 1; $i++){
db::name('vs_room_visitor')->where('user_id', $user_id)->where('room_id', $room_list[$i])->delete();
$room_info = db::name('vs_room_visitor')->insert(['room_id' => $room_id, 'user_id' => $user_id, 'createtime' => time(),'remarks' =>'断线后重连']);
if(!$room_info){
return ['code' => 0, 'msg' => '加入房间失败', 'data' => null];
}
}
}else{
$room_info = db::name('vs_room_visitor')->insert(['room_id' => $room_id, 'user_id' => $user_id, 'createtime' => time(),'remarks' =>'断线后重连']);
@@ -2595,7 +2602,7 @@ class Room extends Model
return ['code' => 0, 'msg' => '参数错误', 'data' => null];
}
$room_t = db::name('vs_room')->field('type_id,label_id')->where('id',$room_id)->find();
if(!(($room_t == 1 || $room_t == 8) && $room_t['label_id'] == 1)){
if(!(($room_t['type_id'] == 1 || $room_t['type_id'] == 8) && $room_t['label_id'] == 1)){
if($type == 1){//1-切后台2-切前台
$is_bg = db::name('vs_user_in_room_bg')->where(['user_id' => $user_id,'room_id' => $room_id])->find();
if($is_bg){

View File

@@ -21,15 +21,15 @@ class RoomHourRanking extends Model
}
//判断是否开启
$open_time = db::name('vs_hour_ranking_config')->order('id', 'desc')->value('open_time');
$open_time = db::name('vs_hour_ranking_config')->where('id', 1)->value('open_time');
if ($open_time == 0) {
// return ['code' => 0, 'msg' => '排行榜暂未开启', 'data' => null];
}
//是否开启巡乐会
$is_open_xlh = db::name('vs_hour_ranking_config')->order('id', 'desc')->value('is_open_xlh');
$is_open_xlh = db::name('vs_hour_ranking_config')->where('id', 1)->value('is_open_xlh');
//是否开启红包(下面的不要删掉,以后要用)
// $is_open_red_pack = db::name('vs_hour_ranking_config')->order('id', 'desc')->value('is_open_red_pack');
// $is_open_red_pack = db::name('vs_hour_ranking_config')->where('id', 1)->value('is_open_red_pack');
$is_open_red_pack = 0 ;
$profit = db::name('vs_room')->alias('a')

View File

@@ -922,11 +922,13 @@ class Tencent extends Model
if($action == 'Disconnect') {
Db::name('user')->where('id', $uid)->update(['is_online' => 0, 'updatetime' => time()]);
$quit_room = Db::name('vs_room_visitor')->where('user_id', $uid)->select();
Log::record("腾讯离线推送,房间信息".json_encode($quit_room),"infos");
if($quit_room) {
foreach ($quit_room as $v){
foreach ($quit_room as &$v){
$room_type = db::name('vs_room')->where(['id' => $v['room_id']])->field('step,type_id,label_id')->find();
Log::record("腾讯离线推送,房间类型".json_encode($room_type),"infos");
if($room_type['label_id'] == 1 && ($room_type['type_id'] == 1 || $room_type['type_id'] == 8)){
Log::record("腾讯离线推送,我是二卡八","infos");
Db::name('user')->where('id', $uid)->update(['is_online' => 1, 'updatetime' => time()]);
}else{
if(isset($room_type) && ($room_type['type_id'] == 7 && ($room_type['step'] == 2 ||$room_type['step'] == 3)) || $room_type == 2){
@@ -938,10 +940,12 @@ class Tencent extends Model
//查询是否有切后台的操作
$bg_room = db::name('vs_user_in_room_bg')->where(['room_id' => $v['room_id'],'user_id' => $v['user_id']])->find();
if(!$bg_room){
Log::record("腾讯离线推送,要踢你","infos");
model('Room')->quit_room($v['user_id'], $v['room_id'],$v['user_id'],2);
}
}
}
$room_type = null;
}
}

View File

@@ -103,6 +103,7 @@ class UserGiftPack extends Model
self::FIRST_CHARGE,
self::DRAW_GIFT,
self::XLH_DRAW_GIFT_GET
,self::HOUR_RANK_GET
];
//获取背包日志列表
$log_model = Db::name('vs_user_gift_pack_log')->where(['user_id'=>$uid])->whereIn('type', $type)

View File

@@ -53,7 +53,7 @@ class UserWallet extends Model
//27.小时榜获得
if($gift_type == 1){ //1金币2收益钻石
if($in_out_type == 1){//1收入
$in_out_types = [2,5,6,8,13,14,15,16,22,23,26];
$in_out_types = [2,5,6,8,13,14,15,16,22,23,26,27];
}elseif($in_out_type == 2){//2支出
$in_out_types = [4,7,10,17,20,24,25];
}

View File

@@ -24,13 +24,21 @@ class RoomHourRanking
//获取上一个小时的开始时间和结束时间
$start_time = strtotime(date('Y-m-d H:00:00', strtotime('-1 hour')));
$end_time = strtotime(date('Y-m-d H:00:00'));
echo "开始时间:" .$start_time."\n";
echo "结束时间:" .$end_time."\n";
//当前小时的前一个小时24小时计时法0-23
$pre_hour = date('H', strtotime('-1 hour'));
echo "上个时间段:" .$pre_hour."\n";
$is_open_time = db::name('vs_hour_ranking_config')->where('id', 1)->value('open_time');
if ($is_open_time == 0) {
echo "未开启时间段:" .$is_open_time."\n";
return;
}
//是否全局飘瓶
$is_public_server = db::name('vs_hour_ranking_config')->order('id', 'desc')->value('is_public_server');
$is_public_server = db::name('vs_hour_ranking_config')->where('id', 1)->value('is_public_server');
if ($is_public_server == 1) {
//全局飘瓶时间段
$xlh_time_range = db::name('vs_hour_ranking_config')->order('id', 'desc')->value('broadcast_times');
$xlh_time_range = db::name('vs_hour_ranking_config')->where('id', 1)->value('broadcast_times');
if($xlh_time_range){
if($xlh_time_range == 25){
$is_piao = 1;
@@ -51,15 +59,25 @@ class RoomHourRanking
}
//获取上一个时间段的配置
$gift_list = db::name('vs_hour_ranking_gift_config')->where('time_id',$pre_hour)->group('ranking')->order('id', 'desc')->select();
// $gift_list = db::name('vs_hour_ranking_gift_config')->where('time_id',$pre_hour)->group('ranking')->order('id', 'desc')->select();
$gift_list = $this->get_hour_ranking($pre_hour);
// echo "上个时间段的配置:" .json_encode($gift_list)."\n";
// 提取所有有奖励的内容
$allRewards = $this->extractAllRewards($gift_list);
// 按index分组
$groupedRewards = $this->groupRewardsByIndex($allRewards);
// 按名次顺序分配奖励
$distributionResult = $this->distributeByRank($groupedRewards);
//获取上个数组的个数,从而获取配置了多少个名次
$count = count($gift_list);
// echo "上个时间段的配置总数:" .$count."\n";
//获取前一个小时的 前$count名房间排行
$room_list = model('api/RoomHourRanking')->room_hour_ranking(1, $count, $start_time, $end_time);
$room_owner = [];
if ($room_list['code'] == 1) {
//获取房间排行奖励最低值
$min_price = db::name('vs_hour_ranking_config')->order('id', 'desc')->value('min_price');
$min_price = db::name('vs_hour_ranking_config')->where('id', 1)->value('min_price');
if ($room_list['data']['lists']) {
$i = 0;
foreach ($room_list['data']['lists'] as $k => $v){
@@ -74,44 +92,50 @@ class RoomHourRanking
}
}
if ($gift_list && $room_owner) {
if ($distributionResult && $room_owner) {
$text_list_new = [];
foreach ($gift_list as $k => $v) {
// echo "礼物数:" .json_encode($distributionResult)."\n";
foreach ($distributionResult as $k => $value) {
//礼物全部给他偷偷放在装扮表及金额 中
//有几个用户就发几个
if(count($room_owner) > $k){
if($v['gift_type'] == 1){//1金币2礼物3头像4坐骑
$res = $this->add_coin($v['coin'], $room_owner[$k]['user_id'],$k + 1,$room_owner[$k]['room_id'],$room_owner[$k]['total_price'],$is_piao);
}elseif ($v['gift_type'] == 2){
$res = $this->add_gift($v['gift_id'], $room_owner[$k]['user_id'],$k + 1,$room_owner[$k]['room_id'],$room_owner[$k]['total_price'],$is_piao);
}elseif ($v['gift_type'] == 3){
$res = $this->add_decorate($v['gift_id'], $room_owner[$k]['user_id'],$k + 1,$room_owner[$k]['room_id'],$room_owner[$k]['total_price'],$is_piao,3);
}elseif ($v['gift_type'] == 4){
$res = $this->add_decorate($v['gift_id'], $room_owner[$k]['user_id'],$k + 1,$room_owner[$k]['room_id'],$room_owner[$k]['total_price'],$is_piao,4);
}
if(isset($res) && $res){
if($is_piao == 1){
$room_name = $room_owner[$k]['room_name'];
//推送礼物横幅
if($k == 0){
$text = '新科状元!【'.$room_name.'】独占鳌头!';
}
elseif($k == 1){
$text = '金榜榜眼!【'.$room_name.'】才气逼人';
}
elseif($k == 2){
$text = '风采探花!【'.$room_name.'】实力绽放';
}
foreach ($value['rewards'] as $v){
if($v['type'] == 0){//1金币2礼物3头像4坐骑
echo "发金币:" .$v['value'].'==>'.$room_owner[$k]['user_id']."\n";
$res = $this->add_coin($v['value'], $room_owner[$k]['user_id'],$k + 1,$room_owner[$k]['room_id'],$room_owner[$k]['total_price'],$is_piao);
}elseif ($v['type'] == 1){
echo "发礼物:" .$v['value'].'==>'.$room_owner[$k]['user_id']."\n";
$res = $this->add_gift($v['value'], $room_owner[$k]['user_id'],$k + 1,$room_owner[$k]['room_id'],$room_owner[$k]['total_price'],$is_piao);
}elseif ($v['type'] == 2){
$res = $this->add_decorate($v['value'], $room_owner[$k]['user_id'],$k + 1,$room_owner[$k]['room_id'],$room_owner[$k]['total_price'],$is_piao,3);
}elseif ($v['type'] == 3){
$res = $this->add_decorate($v['value'], $room_owner[$k]['user_id'],$k + 1,$room_owner[$k]['room_id'],$room_owner[$k]['total_price'],$is_piao,4);
}
if(isset($res) && $res){
if($is_piao == 1){
$room_name = $room_owner[$k]['room_name'];
//推送礼物横幅
if($k == 0){
$text = '新科状元!【'.$room_name.'】独占鳌头';
}
elseif($k == 1){
$text = '金榜榜眼!【'.$room_name.'】才气逼人';
}
elseif($k == 2){
$text = '风采探花!【'.$room_name.'】实力绽放!';
}
$text_list_new[] = [
'text' => $text ?? '恭喜【'.$room_name.'】获得礼物!',
'room_id' => $room_owner[$k]['room_id'],
'room_name' => $room_name,
'rank_number' => $k + 1,
];
$text_list_new[] = [
'text' => $text ?? '恭喜【'.$room_name.'】获得礼物!',
'room_id' => $room_owner[$k]['room_id'],
'room_name' => $room_name,
'rank_number' => $k + 1,
];
}
}
}
}
}
if(!empty($text_list_new)){
@@ -124,49 +148,52 @@ class RoomHourRanking
//添加金币到钱包
public function add_coin($coin,$user_id,$ranking,$room_id,$total_price,$is_piao){
$data = [
'user_id' => $user_id,
'change_value' => $coin,
if($coin > 0){
$data = [
'user_id' => $user_id,
'change_value' => $coin,
// 'room_id' => $room_ids,
'money_type' => 1,
'change_type' => 27,
'from_id' => 0,
'remarks' => '小时榜获得',
'createtime' => time()
];
'money_type' => 1,
'change_type' => 27,
'from_id' => 0,
'remarks' => '小时榜获得',
'createtime' => time()
];
//开启事务
Db::startTrans();
$res = Db::name('vs_user_money_log')->insert($data);
if(!$res){
Db::rollback();
//开启事务
Db::startTrans();
$res = Db::name('vs_user_money_log')->insert($data);
if(!$res){
Db::rollback();
}
//增加用户金币
$res1 = Db::name('user_wallet')->where(['user_id'=>$user_id])->setInc('coin',$coin);
if(!$res1){
Db::rollback();
}
//添加到排行表
$start_time = strtotime(date('Y-m-d H:00:00', strtotime('-1 hour')));
$end_time = strtotime(date('Y-m-d H:00:00')) - 1;
$res2 = db::name('vs_hour_ranking')->insert([
'ranking' => $ranking,
'room_id' => $room_id,
'flowing_water' => $total_price,
'coin' => $coin,
'time_id' => date('H', strtotime('-1 hour')),
'stime' => $start_time,
'etime' => $end_time,
'createtime' => time(),
'updatetime' => time(),
'is_public_server' => $is_piao
]);
if(!$res2){
Db::rollback();
}
Db::commit();
}
//增加用户金币
$res1 = Db::name('user_wallet')->where(['user_id'=>$user_id])->setInc('coin',$coin);
if(!$res1){
Db::rollback();
}
//添加到排行表
$start_time = strtotime(date('Y-m-d H:00:00', strtotime('-1 hour')));
$end_time = strtotime(date('Y-m-d H:00:00')) - 1;
$res2 = db::name('vs_hour_ranking')->insert([
'ranking' => $ranking,
'room_id' => $room_id,
'flowing_water' => $total_price,
'coin' => $coin,
'time_id' => date('H', strtotime('-1 hour')),
'stime' => $start_time,
'etime' => $end_time,
'createtime' => time(),
'updatetime' => time(),
'is_public_server' => $is_piao
]);
if(!$res2){
Db::rollback();
}
Db::commit();
return true;
}
@@ -232,7 +259,144 @@ class RoomHourRanking
}
/**
* 提取所有有奖励的内容
*/
private function extractAllRewards($responseData)
{
$allRewards = [];
foreach ($responseData as $timeSlot) {
foreach ($timeSlot['reward'] as $rewardItem) {
$index = $rewardItem['index'];
$content = $rewardItem['content'];
// 只处理有奖励内容的数据
if (!empty($content)) {
foreach ($content as $rewardContent) {
$allRewards[] = [
'index' => $index,
'type' => $rewardContent['type'],
'value' => $rewardContent['value'],
'name' => $rewardContent['name'] ?? ''
];
}
}
}
}
return $allRewards;
}
/**
* 按index分组奖励
*/
private function groupRewardsByIndex($allRewards)
{
$grouped = [];
foreach ($allRewards as $reward) {
$index = $reward['index'];
if (!isset($grouped[$index])) {
$grouped[$index] = [];
}
$grouped[$index][] = $reward;
}
// 按index排序
ksort($grouped);
return $grouped;
}
/**
* 按名次顺序分配奖励
*/
private function distributeByRank($groupedRewards)
{
$distribution = [];
$currentRank = 0; // 从第1名开始
// 按index顺序分配index 0 = 第1名index 1 = 第2名以此类推
foreach ($groupedRewards as $index => $rewards) {
// 确保名次连续,如果有空缺则填充空名次
while ($currentRank < $index) {
$distribution[] = [
'rank' => $currentRank + 1,
'rewards' => []
];
$currentRank++;
}
// 分配当前名次的奖励
$distribution[] = [
'rank' => $currentRank + 1,
'rewards' => $rewards
];
$currentRank++;
}
return $distribution;
}
public function get_hour_ranking($time){
// 先按时间段和排名索引分组查询
$timeRanges = db::name('vs_hour_ranking_gift_config')->distinct(true)
->where('time_id', '=', $time)
->order('time_id')
->column('time_id');
$result = [];
foreach ($timeRanges as $timeRange) {
// 查询该时间段的所有数据
$rewards = db::name('vs_hour_ranking_gift_config')->where('time_id', $timeRange)
->field('ranking, gift_type, gift_id,coin,name')
->order('ranking')
->select();
$rewardMap = [];
foreach ($rewards as $reward) {
$rankIndex = $reward['ranking'];
if (!isset($rewardMap[$rankIndex])) {
$rewardMap[$rankIndex] = [
'index' => $rankIndex,
// 'name' => $reward['rank_name'],
'content' => []
];
}
// 添加奖励内容到content数组
if ($reward['gift_id'] != 0 || $reward['coin'] != 0) {
if($reward['gift_id'] != 0){
$rewardMap[$rankIndex]['content'][] = [
'type' => $reward['gift_type'],
'value' => $reward['gift_id'],
// 'coin' => $reward['coin'],
'name' => $reward['name'],
];
}
if($reward['coin'] != 0){
$rewardMap[$rankIndex]['content'][] = [
'type' => $reward['gift_type'],
'value' => $reward['coin'],
'name' => $reward['name'],
];
}
}
}
// 按index排序
ksort($rewardMap);
$result[] = [
'time' => $timeRange,
'reward' => array_values($rewardMap)
];
}
return $result;
}
}