Zemllia / RPGram

A text RPG for Telegram
GNU Affero General Public License v3.0
3 stars 1 forks source link

Saving & loading game state on bot restart #13

Open artyom-poptsov opened 4 years ago

artyom-poptsov commented 4 years ago

When bot is restarted and a user tries to interact with it, the bot produces the following error:

14:20:08.851 [cadropod_bot Telegram Executor] FATAL org.telegram.telegrambots.updatesreceivers.DefaultBotSession - null
java.lang.NullPointerException: null
    at rpgram.RPGram.onUpdateReceived(RPGram.java:131) ~[classes/:?]
    at java.util.ArrayList.forEach(ArrayList.java:1540) ~[?:?]
    at org.telegram.telegrambots.meta.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27) ~[telegrambots-meta-4.4.0.1.jar:?]
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:307) [telegrambots-4.4.0.1.jar:?]

If the user issues /start command after restart, the error does not occur.

F1uctus commented 4 years ago

There is a break statement on that line

https://github.com/Zemllia/RPGram/blob/d439c7a0fd78718bf07e39dce19d00d149002738/src/main/java/rpgram/RPGram.java#L128-L134

Edit: OK. In #12 there is that line: answer = curPlayer.getMap().viewMapArea(curPlayer.getPos(), curPlayer.getFieldOfView());

I think, when bot is restarted, curPlayer becomes null, so we need to save players list somewhere, or even implement registration system?

artyom-poptsov commented 4 years ago

We should save game state somewhere (e.g. to a database) periodically or keep the actual state here altogether.

A more fast solution could be that we should notify the player that the bot was restarted and ask his/her to re-start game by issuing /start command again.

F1uctus commented 4 years ago

Saving a game state using a manually written logic is a hell of a job. After each addition to the game (e.g. new player property, new thing, etc) manual serialization logic should be updated too.

Recently I looked at https://github.com/EsotericSoftware/kryo, but was not able to integrate it to my branch yet. Seems like Map is a too complex object to be processed by Kryo without many modifications, and I have a little experience with Java serialization libraries.

The https://github.com/kostaskougios/cloning library is powerful enough to clone the entire game state automagically, but it is not purposed for serialization, only for cloning.

I will continue the investigation, but later. If no serialization library will be found, manual serializing may be considered as a last resort...