foin137 / werwolfonline.eu

www.werwolfonline.eu
http://www.werwolfonline.eu/info
GNU General Public License v3.0
20 stars 11 forks source link

Double Player ID in a single game #11

Open SecurID opened 4 years ago

SecurID commented 4 years ago

Encoutered the problem that in a game two players had the same id. I think the problem is that the table ist not created with a unique id field. Maybe that would fix the problem. Will have a look into it in a few days. But maybe someone fixed it already

foin137 commented 4 years ago

I think these problems may occur if two people reload the page at the same time, when the server handles two requests simultaneously and one script fetches data from the database while the other one changes its values. The table should probably be locked somehow to prevent this.

What I mean is: Two requests are made simultaneously: A and B. A reads from the database the number of players, e.g. 5. B reads from the database the number of players: still 5 (because A has not yet added a new entry) A creates a new entry in the database and sets the playerID to the next number 5 (if we start at 0) B creates a new entry in the database and sets the playerID to what it belives is the next number 5 (though in fact, during the time B retrieved data from the table and the time it adds this entry, the other request A has added already a new entry)

I guess this can happen at multiple stages in the program and should be somehow fixed.

However, I am not completely sure how do properly deal with it. I would think of adding a column "table_locked" or something, and if it is set to 1, values shouldn't be changed, but I'm not sure if that's the best way.

Anyway solving this would probably include going through the whole code and identifying sections where data in the database might get changed and somehow "lock" the database before it.

SecurID commented 4 years ago

Yeah i just looked into the log and the users created themselves at the same time. Isn't a good fix if the players table is created with the ID field as UNIQUE and auto_increment?

foin137 commented 4 years ago

Might work for this, it's worth a try I guess!

If you want to have a look into it, feel free to make a pull request. I'm not sure if I find time to fix it in the next weeks myself, unfortunately.

PS: I think the same problem also occurs at other parts in the code, though (changing some entry twice or at the same time). But this would probably require something else (like table locking) and could also be fixed later.

SecurID commented 4 years ago

Yeah I will have some time this week to fix this :)