Jump to content
  • Advertisement
Sign in to follow this  

How is persistence managed by browser games?

This topic is 1121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering,


when you play browser games you never seem to witness a page refresh, even if there is data being stored on the server. So how are databases handled by browser games.


In my opinion there are several possible scenarios:


1. Data is being modified during your absence. Let's say in FarmVille your plants will grow while you are not using the application. So is this data being constantly updated on the server or does the application calculate everything that might have happened in a fast forward when you launch it?


2. Let's say you are currently playing in the browser and you completed a mission and get a coin reward or something. Is this data-update instantly written to a database or is it somehow stored in some kind of session tracker (cookies?) and stored on the server once you leave the application?



              2.1 If it is written to the server instantly how is this done without a page refresh? I mean if there is some kind of client/server communication there must be a page refresh right?


              2.2 If the data is send to the server once you leave the application, how to handle a case where the user simply kills the browser process by simply closing it? Can this scenario be catched by code so the data can still be written?

Edited by Prot

Share this post

Link to post
Share on other sites

I'm not 100% sure on how the session or state is managed while the player is away but I would think basically when you drop a crop down on the map the game stores the crop type, amount of time to grow, and timestamp of when it was placed on the farm.  This would then allow your simulation game to then properly draw the right sprite on the screen based on how much time has elapsed since the player place a crop on their farm.


As for the sending and receiving data from the server it all comes down to a simple technology called AJAX (Asynchronous JavaScript and XML).  jQuery is a good example of how to post and receive message from your server without having to load a full page.  You will probably need an event handler on the browser that manages all your GUI controls and then posts data to the server for processing.  Also you might want an update thread that connect to the server that checks for different kinds of events.  There are a bunch of different techniques but I think the real answer is work with one you understand and exploit it to the best you can.


As for running the simulation on the server while the player is away isn't a good idea.  The amount of horse power you would need to run your game would exponentially grow based on the amount of players you have.  It would be better to record markers and time stamps in the database so the simulation could predict their next progression in the game when players return.  Web servers need to keep it as light as possible so they can server as many clients as they can without bogging down the computer.

Share this post

Link to post
Share on other sites

1a. How is persistence managed?


Probably a combination of HTML5's Local Storage, cookies, and server data.


The localStorage system is one of many features added to HTML5 focused on web applications.  Others are things like built-in drag-and drop, the application cache, the graphical canvas.


Programs where all the data is lost when you change browsers will likely use local storage and cookies. Programs where everything persists no matter where you log in are stored on the server.


1b. How do things change over time?


Some of that is done with persistent data on the back end, but most is done when you log in.  The game client knows a certain time has advanced and passes on rules for that time.  It is not so much a fast forward -- rapid simulation of tiny time steps -- but more of a big event sent to everybody that the player has logged in.  Every item gets the time passed to it and it knows what to do.  


All the stuff gets loaded as data, then everything gets a notice that 17 hours have passed, so a plant says 'I need to advance 17 hours, I have fruit now.'


2.0. When are rewards given to players?


As part of design, you need to figure out when key events are saved.


For online games many major events are triggers for persistence.  Buying/selling objects between players, events triggered at login, achievements, these are all situations that need to be saved or persisted. 


In your example about an award, something happens that triggers the change, and the game notifies the player. The game client notifies the server that the player has been informed, and the server saves the update.  Often today's web games do significant processing at load.  The game client has persisted what the last-used version was (e.g. version 92 on 9/7/2015) and what the current version is (e.g. version 96 on 10/11/2015). Then the client will have rules about what to show. If they were gone a long time the game may offer to discard all the expired challenge messages and clean up everything, or they may show every event and a bunch of "what's new today" messages.  


2.1. How do they communicate?


Probably AJAX, although there are additional options for persistent connections and longer term communications through many methods.  Looks like lede already covered that one a bit.


2.2. When is the game saved?


As mentioned in 2.0 above, the game design needs to figure out when to save data.


Some events require an immediate save.  Anything that is big to the player needs to be communicated to the server and saved immediately.  


Otherwise, the game can choose some time period for periodic saves based on the scope of the game.  It might trigger a save every 60 seconds, or every 4 minutes, or some other value. It needs to be enough that if the app crashed or the player left or the system disconnected from the Internet, the game could be reloaded and continue from that point.  Often there are different pieces of data that are saved at different times, and there are some things that are not saved at all. The details are part of the game's design.


When learning it is probably easiest to just save all the state information needed to restart the game, rather than to only save partial data or to use reconstruction methods to figure out details based on minimal data.  Just save it all and load it all.  Over time you'll learn that you can get away with saving far less than everything, but that takes practice.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!