作者 何书鹏
1 个管道 的构建 通过 耗费 27 秒

玩游戏优化

... ... @@ -294,6 +294,7 @@ class Game extends Api
"id": 201, //ID
"name": "鞋盒", //名称
"image": "" //图片
"is_right": 1 //是否正确答案:0=否,1=是
}],
"result_id": "1" //游戏记录ID
}
... ... @@ -321,13 +322,13 @@ class Game extends Api
$right_list = Rubbish::where('type',$barrier['rubbish_type'])
->where('difficulty',$barrier['difficulty'])
->where('image','neq','')
->field('id,name,image')
->field('id,name,image,1 is_right')
->orderRaw('rand()')
->limit($barrier['question_num'])
->select();
$wrong_list = Rubbish::where('type','neq',$barrier['rubbish_type'])
->where('image','neq','')
->field('id,name,image')
->field('id,name,image,0 is_right')
->orderRaw('rand()')
->limit($barrier['mixed_num'])
->select();
... ... @@ -488,6 +489,114 @@ class Game extends Api
}
}
/**
* @ApiWeigh (82)
* @ApiTitle (过关)
* @ApiSummary (过关)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="rubbish_ids", type="integer", required=true, description="垃圾ID集合")
* @ApiParams (name="result_id", type="integer", required=true, description="记录ID")
* @ApiReturn ({
"code": 1, //状态码:0=报错,1=过关
"msg": "答对",
"time": "1623376045",
"data": {
"grade": 0, //得分
"achievement_list": [] //获得的成就
}
})
*/
public function barrierSuccess()
{
$user = $this->auth->getUser();
$rubbish_ids = $this->request->param('rubbish_ids');
$result_id = $this->request->param('result_id');
(empty($rubbish_ids) || empty($result_id)) && $this->error('缺少必须参数');
$result = GameBarrierResult::where('id',$result_id)->find();
empty($result) && $this->error('记录不存在');
$barrier = GameBarrier::where('id',$result['barrier_id'])->find();
empty($barrier) && $this->error('关卡不存在');
$result['status'] == '1' && $this->error('已过关');
$right_num = Rubbish::where('id','in',$rubbish_ids)->where('type',$barrier['rubbish_type'])->group('id')->count();
$right_num < $barrier['question_num'] && $this->error('未全部答对');
$time = time();
$take_time = $time - $result['createtime'];
$take_time > $barrier['countdown'] && $this->error('已超时');
Db::startTrans();
try{
// 时间奖励:剩余时间秒数除以5,然后取整数
$take_time_score = ceil(($barrier['countdown'] - $take_time) / 5);
$grade = $barrier['barrier_score'] + $take_time_score;
// 分数可按一定比例兑换积分
$score = ceil($grade/$barrier['score_ratio']);
if ($score > 0) {
// 把未同步的积分同步给小程序
$miniprogram_user = User::where('mobile',$user['mobile'])->where('type','1')->field('id,score')->find();
if(!empty($miniprogram_user) && $user['score'] > 0){
User::score(-$user['score'], $user['id'], '同步积分到小程序');
User::score($user['score'], $miniprogram_user['id'], '从小游戏同步积分');
}
// 玩游戏积分,小程序用户是否有相同的手机号,如果有就同步,如果没有就不同步
$get_score_user_id = !empty($miniprogram_user) ? $miniprogram_user['id'] : $user['id'];
User::score($score, $get_score_user_id, '小游戏第'.$barrier['barrier_sn'].'关分数兑换积分');
}
// 更新累计分数排名
$grade_rank = GameGradeRank::where('user_id',$user['id'])->find();
if(empty($grade_rank)){
GameGradeRank::create([
'user_id' => $user['id'],
'grade' => $grade
]);
}else{
// 取最高分
$max_grade = GameBarrierResult::where('status','1')->where('barrier_id',$result['barrier_id'])->max('grade');
if($grade > $max_grade){
$grade_rank->setInc('grade',$grade-$max_grade);
}
}
// 记录得分
$result->isUpdate(true)->save([
'status' => '1',
'grade' => $grade
]);
$this->updateGradeRank();
// 更新关卡排名
$barrier_rank = GameBarrierRank::where('user_id',$user['id'])->find();
if(empty($barrier_rank)){
GameBarrierRank::create([
'user_id' => $user['id'],
'barrier_sn' => $barrier['barrier_sn']
]);
}else{
if($barrier_rank['barrier_sn'] < $barrier['barrier_sn']){
$barrier_rank->isUpdate(true)->save([
'barrier_sn' => $barrier['barrier_sn']
]);
}
}
$this->updateBarrierRank();
// 获得成就
$achievement_list = $this->getAchievement();
// 删除超过两个小时没有过关的记录
GameBarrierResult::where('status','0')->where('createtime','<',time()-7200)->delete();
Db::commit();
}catch (PDOException $e){
Db::rollback();
$this->error($e->getMessage());
}catch (Exception $e){
Db::rollback();
$this->error($e->getMessage());
}
$this->success('过关',compact('grade','achievement_list'));
}
// 更新总得分排名
private function updateGradeRank(){
$list = GameGradeRank::order('grade desc')
... ...
此 diff 太大无法显示。