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