I am building a website/app using node.js, express and nowjs. Once user log into my website, they can move a sprite around using the arrow keys (in addition to other services). The position of the i-th user's sprite is kept server side.
user[i] = { x : 0; y : 0 }
Arrow keypresses are sent to the server to update the sprite position.
E.g. user[5].x += 5.
I do not want two users to share the same position ("collision"). So, when the server receive a keypress, a server-side function checks if the resulting coordinates is equal to some coordinates in the "user" array, and ignore the move if it does.
Question: How do I handle the case when two users choose to move onto the coordinates at exactly the same time? If I understand correctly, the server-side function will be called at the same time, and both users will end up on the same coordinates because at the time of checking, no one has that sets of coordinates yet?
Or, am I safe because if 2 clients call a server-side function (e.g. using nowjs) at the same time, one function will be processed after another always?
node.js is single-threaded, nothing happens "at the same time". You will always have one move before the other.
The javascript code in node.js is executed on a single thread so you should be safe. If you were using an asynchronous library to calculate collisions on a separate thread, you would be at risk, but it doesn't sound like that's going on.