heroiclabs / nakama

Distributed server for social and realtime games and apps.

Home Page:https://heroiclabs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TournamentRecordWrite adds to leaderboard cache even if there isnt't a new score

robtraynor opened this issue · comments

Description

I was getting an error where an incorrect Rank was being returned by TournamentRecordsList, and I think I've tracked it do to
TournamentRecordWrite, updating the rank cache even if the score isn't used.

In the case where the score isnt updated and the leaderboard is doesnt require a join rankCache.Insert is always called:

	record.Rank = rankCache.Insert(leaderboard.Id, leaderboard.SortOrder, record.Score, record.Subscore, oldScore, oldSubscore, expiryUnix, ownerId)

in core_tournament.go.

In this case oldScore and oldSubscore are zero, since they are set from the result of the database update which is empty in the case where there is no update. Meaning that when the rankCache looks to delete the old entry there are no matches and instead it is added.

I can create pull request to fix if you want by adding logic similar to core_leaderboard.go and calling rankCache.Get, if there hasn't been an update, but im not sure if this logic would need to be mirrored in the requiredJoin branch for cosistency.

Steps to Reproduce

  1. Set up a tournament without required join and operator set to best
  2. Use any client to call TournamentRecordWrite
  3. Call TournamentRecordWrite again with a lower score

Expected Result

The rankCache for the leaderboard should contain one entry

Actual Result

The rankCache for the leaderboard contains two entries

@robtraynor Nice find, this makes sense. The rank cache entries are definitely intended to be unique so if there's a situation that adds an entry without removing the previous one, that's not intended.

A PR would be awesome! 🙏

@zyro After digging into it some more I think the actual issue is that the database doesn't get updated if the score is not an improvement over the previous score, meaning num_scores donest get incremented. Is this intended? i.e. is the max_num_scores the number of improvements of over your original score? in which case it is correct as is. Or is it the max number of entries into the tournament? In which case it will need to be chaged.

Off the top of my head I think max_num_scores is meant to be the submitted count full stop, improved or not. I need to look into it to be sure. 👀

That sounds like a separate issue though, right? If memory serves the number of scores doesn't affect rank and/or rank caching.

I think if the score is not an improvement, the values passed to the rank cache for the old values will always be 0, since WHERE clause prevents the query from completing, therefore it cant find it to remove it from the cache and instead adds a new one. Also since the query doesnt complete the num_scores value never actually gets incremented. So I think they are two issues caused by the same thing.

EDIT: Sorry something got messed up in the formatting of this message, I've fixed it now

I've create PR which should fix this, let me know if it is correct. #1125

@zyro Hey just wondering if there is an update?

Sorry, I closed this by mistake.