MattGibney / DarkThrone

Lovingly re-creating the DarkThrone experience.
https://darkthronereborn.com
1 stars 0 forks source link

Calculate player rank upon creation #37

Closed fpopa closed 4 months ago

fpopa commented 4 months ago

Previously we did not specify a rank, which made it default to 0. Which messed up the overall ranking.

What we do now is try to calculate the rank based on the number of players by fetching a count.

I'm very unfamiliar with this ORM, I tried using a raw query initially but got it to work this way. Very open to suggestions.


It's not perfect but it solves our case while not handling an edge case -> race condition when multiple users create players at the same time and count results are not correct -> but this gets solved when the ranking gets recalculated.

On a second thought:

This is still not very nice because we assume that other players will always have better ranks than a new player which might not be the case 🤔. I'm fine with leaving the fix like this because we'll not break the top rankings, but when we do store scores in the database and sort by that when fetching users this problem will not be a problem anymore.

image

new player getting lowest rank:

dark_throne=# select * from players order by overall_rank asc
dark_throne-# ;
               id               |            user_id             |  display_name   | race  |  class  | avatar_url |          created_at           |  gold  | attack_turns | experience | overall_rank
--------------------------------+--------------------------------+-----------------+-------+---------+------------+-------------------------------+--------+--------------+------------+--------------
 PLR-01HQH08DQV8402052QJ6MZXG3X | USR-01HQGXA57SH11ZRW0R9945WCA7 | humanfighter    | human | fighter |            | 2024-02-25 20:45:43.55209+00  | 155150 |         1002 |        145 |            1
 PLR-01HQXZ5H2G44K429K53A9V7XK8 | USR-01HQP15VB5VGJY5H9R25CFKF5D | new321          | human | fighter |            | 2024-03-01 21:36:47.697884+00 |  10000 |         1000 |        102 |            2
 PLR-01HQY12RJ4M11F4ZVNNC94615X | USR-01HQP15VB5VGJY5H9R25CFKF5D | newplayerfail   | human | fighter |            | 2024-03-01 22:10:14.21785+00  |  10000 |         1000 |          0 |            3
 PLR-01HQY0RTXKSEXHPAQTJ8V1KCPN | USR-01HQP15VB5VGJY5H9R25CFKF5D | test_new_p      | human | thief   |            | 2024-03-01 22:04:48.958393+00 |  10000 |         1000 |          0 |            4
 PLR-01HQP168EHS3BPK2KJ1JMTMJTW | USR-01HQP15VB5VGJY5H9R25CFKF5D | 123             | human | fighter |            | 2024-02-27 19:38:13.33023+00  |  10000 |         1000 |          0 |            5
 PLR-01HQY1KV2T8FMWHXHZT9D6VRQ5 | USR-01HQP15VB5VGJY5H9R25CFKF5D | new_player_test | elf   | cleric  |            | 2024-03-01 22:19:33.855791+00 |  10000 |         1000 |          0 |            6
(6 rows)
image