simon987 / Much-Assembly-Required

Assembly programming game
https://muchassemblyrequired.com
GNU General Public License v3.0
926 stars 88 forks source link

Game objectId de-associating ? #162

Closed simon987 closed 6 years ago

simon987 commented 6 years ago

I imagine that could be due to getNextObjectId() not being thread safe? Maybe it would be wiser to rely on mongodb ids.

Stack strace:

Exception in thread "Thread-10" java.lang.ClassCastException: net.simon987.npcplugin.ElectricBox cannot be cast to net.simon987.npcplugin.NonPlayerCharacter
    at net.simon987.npcplugin.Factory.update(Factory.java:78)
    at net.simon987.server.game.world.World.update(World.java:183)
    at net.simon987.server.GameServer.tick(GameServer.java:192)
    at net.simon987.server.GameServer.run(GameServer.java:144)
    at java.lang.Thread.run(Thread.java:748)
simon987 commented 6 years ago

Okay I think I figured it out: The current nextObjectId is 10, and it is saved in the database as 7 from the last save
The server restarts while saving the game - only game objects and/or users are saved but not the nextObjectId
It restores 7 from the database, the next 3 objects overwrites existing objects.

I think the only option is to use mongodb's ObjectId()

mike2ykme commented 6 years ago

I’m going to suggest something but I’m not entirely sure what you’re looking at because I just started looking at your project. However What about using an atomic Integer and just getting the next int or something similar?

simon987 commented 6 years ago

Are you referring to AtomicInteger ? I'm not convinced that it's a thread safety issue anymore (see last comment), but changing to AtomicNumber would certainly be a good idea, just to be sure. thanks !

simon987 commented 6 years ago

I think that I'll switch to ObjectId

simon987 commented 6 years ago

Couldn't reproduce the problem at all after switching to BSON ObjectId