根据用户排名而不是得分进行排名 [英] Ranking based on users placement instead of score

查看:68
本文介绍了根据用户排名而不是得分进行排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,我无法缠住我的头.

I have a issue that I cannot wrap my head around.

我正在使用Laravel框架.

I am using the Laravel Framework.

我正在尝试根据展示位置创建一个排名表(表示用户没有任何SCORE,他们只有展示位置)

I am trying to make a ranking table based on placement (Meaning the user does not have any SCORE, they just have placements)

我希望它的工作方式如下:

How I want it to work is the following way:

用户A =展示位置:1

用户B =展示位置:10

User B = Placement: 10

用户B 赢得了用户A ,然后用户B 被放置为 1号用户A 被放置为数字2 ,然后我希望它相应地更新所有其他用户.

User B wins over User A, then User B gets placed as number 1 and User A gets placed as number 2, and then I want it to update all the other users accordingly.

我似乎找不到可靠的方法.

I can't seem to find a reliable way of doing this.

推荐答案

我认为这不是Laravel的挑战,而是SQL的挑战.解决起来可能很简单:基本上,您将询问被击败者的实际职位,如果该职位大于获胜者,则您什么都不做,否则,将失败者的职位分配给新获胜者并更新表格的其余部分在位置列中为+1.

I don't think this is a Laravel challenge but an SQL one. And it may be simple to solve: basically, you will ask for the actual position of the defeated person, if the position is greater than the winner, you do nothing, otherwise you will assign the position of the loser to the new winner and update the rest of the table with a +1 in the position column.

在代码中将是这样的:

$winner_player = User::where('id', userA->id)->first();
$loser_player = User::where('id', userB->id)->first();

if($winner_player->position < $loser_player->position) {
     //Update the rest of the users. 
     //We add 2 because we need space for the new winner and for 
     //the loser that is still above of the rest of the players. 
     DB::table('users')
        ->where('position', '>', $loser_player->position)
        ->update(DB::raw('position+2'));

     //Set the winner with the actual position of the loser.
     $winner_player->position = $loser_player->position;
     $winner_player->save();

     //Set the looser with the new position (+1 of his actual).
     $loser_player->position = $loser_player->position + 1; 
     $loser_player->save();
}

更新后的逻辑 正如分类指出的那样,它会四处移动行,但操作不正确,因此我正在更新逻辑以使其按预期工作,这也将更加简单.

UPDATED LOGIC As Classified pointed out, it moves the rows around but doesn't do it correctly, so I'm updating the logic to make it work as it is supposed to, and it will be a little simpler too.

$winner_player = User::where('id', userA->id)->first();
$loser_player = User::where('id', userB->id)->first();

if($winner_player->position < $loser_player->position) {
     //Set the winner with the actual position of the loser.
     $winner_player->position = $loser_player->position;

     //Update the users between the swap. There is no need to update 
     //the whole table, we only update the records between the swap.
     DB::table('users')
        ->where([['position', '<', $winner_player->position],
                 ['position', '>=', $loser_player->position]])
        ->update(DB::raw('position+1'));

     //Save the value of the winner AFTER updating the positions 
     //between winner and loser.
     $winner_player->save();
}

这篇关于根据用户排名而不是得分进行排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆