Tuesday 28 August 2018

Temporary Session Storage using Node.js?

I have a bot that I have coded in node.js and users interact with this bot. You can talk with it and use commands but everything is pretty come and go. There is no storage of persistent data (except for a MySQL database to keep track of certain things like how much gold a person has, which they obtain by interacting with the bot)​I was working on creating some games with the bot that users can gamble the gold with. And the game I coded was a blackjack game. (Simple 21 against the bot)​The user is dealt two cards to start and can either hit or stay and the values are added up. The hard part of this though, is that only one person can seem to play at a time because of conflicting function calls. So I was wondering if there was a way using node.js to have temporary storage of persistent asynchronous data.So I could code something like:function playBlackjack(userid, betamount){ session_start(); var unique_id = session['id']; { ... } //all the code for the various things like hitMe(), stay(), start(), endGame() etc... giveReward(session['id']); //for if they win/lose it'll get removed/added to their mysql user session_end(); //this cleanly ends the players session and if they were to run the blackjack command again it would start a fresh session, giving their userid a new session variable } So that way the user can walk away from the bot, come back tomorrow and since the session never ended they can pick up where they left off and continue playing as long as their session hasnt ended cleanly and other users data for their unique sessions are tracked individually.I generally dont want to use a JSON file to do this because I dont want to clog the server up with JSON files (sure I could have them set to auto delete after X amount of time, but if there is a more elegant solution I would love to explore that)​Next, I want to try to code a DND mini game where the bot generates random scenarios (this parts already done) but I need persistent sessions so that when the bot prompts the user to roll a die and awaits their response, they enter !roll d20 and the value thats retrieved from the roll, can then be used in the bot for the progression of the story telling. I wonder what is the cleanest way to handle this also. I dont think MySQL would work here, nor would a JSON file unless i create a JSON file that has username, date/time, story (other data like gold, stats etc) to keep the story telling persistent..​Any ideas? If the JSON route is the best, I assume I would have to create files like:DND_userid.jsonBLACKJACK_userid.json​which would have multiple arrays with session_idsI reckon it would look something like this{ "user_id" : "mrsodasexy", "mode": "DND", "sessions": [ { "id" : "845skjjd93jf94f", "data": [ { "date_start" : "08/27/2018 15:11:11", "date_end" : "08/27/2018 16:30:32", "story" : " ... ", //as HTML string "last_sentence" : "you rested in the inn", "stats" : [ ... ], "inventory" : [ ... ] } ] }, { "id" : "167sjd93j9woiwiw", "data": [ { "date_start" : "08/28/2018 16:14:16", "date_end" : "", "story" : " ... ", //as HTML string "last_sentence" : "and you found 20 pieces of gold", "stats" : [ ... ], "inventory" : [ ... ] } ] } ] } And I would just look for each session where date_end is empty and continue from that session?Is that the best way to do this?​

Submitted August 28, 2018 at 07:11PM by mrsodasexy

No comments:

Post a Comment