I'm making a PHP battle system, and need to sort out the ranking.
How I want it to work is say there's 100 players, and you're rank 90. You can challenge players 5 ranks above, so say you challenge and beat the player in rank 85, then YOU become 85 and he becomes 86.
I'm not sure how to do this with a database though. How to set it out, or store rank. Obviously I can't save a field with your 'rank' since if you win it'd have to edit every single players rank below you in the database, wouldn't it?
Any ideas would be helpful.
I recommend you do keep a rank field and relax the bottleneck in the queries:
In your example use something like
UPDATE players
SET rank=IF(playerid=<id-of-challengin-player>,rank-5,rank+1)
WHERE rank>=85 AND rank<=90
This will change ranks
old new
85 86
86 87
87 88
88 89
89 90
90 85
which looks like what you want, but uses a single query, that can leverage an index and touches only 6 rows.
Edit
To facilitate maintenance (fix rank numbers, if there are holes etc.), run
CREATE VIEW players_by_rank AS
SELECT * FROM players ORDER BY rank
once, then use
UPDATE (SELECT @newrank:=0) AS init, players_by_rank
SET score=(@newrank:=@newrank+1)
to close gaps in the table. Mind that this touches all rows!
你确实可以在数据库中有一个排名栏并按以下方式更新: update yourtable set playerRank=playerRank+1 where playerRank>85
然后将获胜玩家更新为playerRank 85.这可能会击中大量记录,但它这不会是数据库的沉重代价。
Since the 'rank' you describe is not based on any other rules, such as points.. something that can be calculated, you must store this rank everywhere.
And yea, that does imply that you need to update a whole bunch of records every time; but since you mention you can only challenge up to 5 positions ahead of you, this also implies that you only ever have to update 6 records every time.. (no big deal)
90 -> 85
89 -> 90
88 -> 89
87 -> 88
86 -> 87
85 -> 86
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.