Question

Think about a game, players are trying to solve problems and every problem is actually means "Level".

Players will see realtime ranking when playing.

RedisIO has sorted set feature, I'll use it.

But I don't know how to score players:

PlayerA at 7 level, total game time 80  seconds
PlayerB at 7 level, total game time 65  seconds
PlayerC at 5 level, total game time 40  seconds
PlayerD at 1 level, total game time 200 seconds

ranking that I want is just like this

1) PlayerB - because level 7 and 65  seconds
2) PlayerA - because level 7 and 80  seconds
3) PlayerC - because level 5 and 40  seconds
4) PlayerD - because level 1 and 200 seconds

I tried

(timeSpent/level)

calculation but it didn't work well when somebody is at lesser level and lesser spent time than other players.

Était-ce utile?

La solution

Short answer: you can have the following function:

score = (level * HUGE_NUMBER) - timeSpent

For HUGE_NUMBER, you can select a value that is slightly larger than the maximum allowed time to finish a level.

While that might be enough for most cases, I would rather use sorting for this problem to avoid any potential unseen bugs in the ranking algorithm.

Assuming that the level of a player is the dominant factor in the ranking, I'd sort all players by level in descending order. This may give you something like that (note that it's not the final ranking yet):

1) PlayerA - because level 7 and 80  seconds
2) PlayerB - because level 7 and 65  seconds
3) PlayerC - because level 5 and 40  seconds
4) PlayerD - because level 1 and 200 seconds

Following that, I'd create sublists of players in each level and sort them by time in ascending order. In the above example, the second sorting would give you the final correct ranking.

1) PlayerB - because level 7 and 65  seconds
2) PlayerA - because level 7 and 80  seconds
3) PlayerC - because level 5 and 40  seconds
4) PlayerD - because level 1 and 200 seconds
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top