Random assignment of players to groups?
G
Gavin Anderson 🇬🇧 (gavin67)
Hi Alfie - could you confirm if the automatic assignment of players to groups for any particular tournament is random? A few players at our club are wondering whether it's driven by some non-random factor (eg perhaps the order in which people sign up for the tournament) as there seems to be a regularity to the same people being in the same group. It may all just be our imagination though...
A
Alfie Kirkpatrick [Developer]
Gavin Anderson 🇬🇧 (gavin67), yes they are sorted randomly. Here is the code... 🙂
const players = (await this.db.listByBucket<TournamentEntry>(tournamentId, "tournament-entry", true, false))
.filter(player => player.checkedIn)
.filter(player => !player.disabled)
// sort by sign up date
.sort((a, b) => a.created - b.created)
// expand double entries
.flatMap<TournamentEntry>(player => (player.de ? [player, player] : [player]))
// cut at max players
.filter((_, index) => !tournament.mp || index < tournament.mp)
// de-dupe
.filter((value, index, self) => self.findIndex(v => v.itemId === value.itemId) === index)
// add random sort key and sort
.map(value => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
// reduce back to value only
.map(({ value }) => value)
// add double entries back in
.flatMap<TournamentEntry>(player => (player.de ? [player, player] : [player]))
G
Gavin Anderson 🇬🇧 (gavin67)
Alfie Kirkpatrick [Developer] Brilliant. Cheers Alfie.