#Getting a GM or GM Proxying
1 messages · Page 1 of 1 (latest)
so it does seem like the order is guaranteed, I'm guessing based on insertion order. But I'm curious if there is a chance for skew.
I recently ask whether there is a guaranteed order of documents in a collection. The answer was no, so I assume it's the case here as well.
Sorting the users by user id and then selecting the first GM is probably safer
The id is randomly generated tho, so that seems pointless.
But yeah, there's no guarantees on the order of users.
Sorting by ID would be guaranteed to be the same order across multiple clients, at least
I would suggest filtering and then sorting, rather than sorting and then filtering, but it’s small data so not a big deal
thanks all
in case someone looks at this archive in the future I guess it would be something like:
game.users.filter(u => u.isGM && u.active).sort((a, b) => a.id.localeCompare(b.id)).shift()
localeCompare is dependent on the selected locale of the client, so it could be different in different clients. I’m not sure if this happens with just regular alphanumeric characters, which the ids consist of, but it’s something to be aware of
it's just my defacto for strings. unless you are comparing across locales i think it almost always makes sense to sort using the locale.
Setting second parameter of localeCompare to something specific would get more predictable results.
There was recently a bug report about combat tracker combatants sorting differently between clients because some were on english and others on russian. Similar situations are not terribly unlikely.
is the combat tracker sort using localeCompare?
I believe it is.
It has been sorting by name with localeCompare as a tie breaker, but I think it’s not anymore for this reason.
This probably the safe solution
Yeah, the issue was closed https://gitlab.com/foundrynet/foundryvtt/-/issues/6231
But it was still caused by localeCompare sorting
This is how I ensure only one GM acts, but it sounds like this may be flawed if the list isn't guaranteed to be the same.
let GMs = game.users.filter(user => user.isGM && user.active);
if (GMs[0].id != game.user.id){
return;
}```
Yes, it’s recommended to sort by something that is consistent across clients first, e.g. using localeCompare with a fixed locale
I guess if I throw a sort in as was being discussed, that would guarantee it
Although I'd expect the list of GMs to be consistent by order of login time anyway
The login time has nothing to do with that, as far as I understand.