-1

I've been learning Pygame for a quiet long time. Now, I'm managing to let players create accounts for the game and connect to them if they're playing online.

I've seen some game developers storing players' data in text files, yet that's a very classic way and it's unsecure ! So I thought about databases ...

However, I think I'm misunderstanding how databases work in general. In fact, I was thinking of them just as servers but after doing some Google researches, I realized they're not. Let's talk about the SQLite as an example. (as i just learned the SQLite3 python module's basics on the fly).

I thought it stores data in a server or in 'clouds' (like Apple iCloud) but then I realized it just stores it on the computer disk, and this won't enable players to connect to their accounts if they're using the game from a computer that's different from the one they used to sign up for example and that's a possible case and I want to cover max cases that can happen while connecting to the game in order to ensure a good player experience with the game.

So, is there any way to ensure a good connection to the game with any device ? And do you think I have to use the socket module ?

NOTE :

Keep in mind that the game itself isn't multiplayer and doesn't need Internet connection. However, I want the players to be able to connect to their accounts from any device just to be able to save their progress in the game.

Developeeer
  • 110
  • 1
  • 1
  • 10
  • 2
    You will need some kind of server, e.g. a computer that is accessible via the internet. On there, the Database (e.g. a file) will be stored and access will be provided to different players. – MegaIng Jun 12 '21 at 17:53
  • 1
    Note that you could use a cloud as this kind of server: But be aware that that might violate the corresponding ToS. Better would be something like AWS. – MegaIng Jun 12 '21 at 18:02
  • 1
    Did you mean you want something that supports `Play Offline`, where a user can play without internet connection, but still update their progress on a remote database once they go online? – Brian Destura Jul 18 '21 at 11:04
  • @bdbd yeah something like that – Developeeer Jul 18 '21 at 12:40
  • If you want to have a really simple solution, save the progress in a file and let him/her choose location to save that file. They may keep it in DropBox etc, and sync themselves between devices. – hans Jul 19 '21 at 13:42

2 Answers2

3

There's a lot of different approaches to this. As Rahul Hindocha pointed out, you need some kind of backend.

Let me list the ones I can think of. Note that my answer will be slightly Google flavored as I have most experience there.

  1. A Flask/Django/Falcon app running in the Cloud. This backend could use either local storage or a SQL implementation to store the data. You need to take care of data persistance when using Cloud services though, as fe. Google App Engine and Heroku Dyno's both have ephemeral filesystems, which means all data stored on the local instance gets deleted regulary when the instance gets reset. A Google Compute Engine (GCE) instance would work though, as the filesystem on this is persistent.
  2. Use something like Google CloudSQL or a GCE instance running SQL and connect to that from your app.
  3. Use another Cloud storage option. There's tons of companies that offer online storage and python libraries to connect to them. There's also free tiers. Think Amazon AWS (S3), Google Cloud (GCS/Firestore), MS Azure, Heroku, etc...
  4. Google Drive to store login information. I've done this in the past before I switched to using GCS buckets. You need to encrypt/decrypt the data in your app though, as storing plain text is just plain stupid. I did this by putting the data in a JSON string, then encrypt the complete JSON string using PyCrypto and upload. Download and decrypt when you need it. See this SO answer for a short explanation. Google offers a library for this.
  5. Google Sheets to store login information. Put the relevant information into columns, with a row for each user. You can also use the encrypt/decrypt here to make sure SPI/PII is encrypted in transit and at rest. Google offers a library for this, and there's gspread which makes it a bit easier still.
  6. Raspberry PI running the app mentioned in number 1 above. Note that you'll need to expose this to the internet, and keep it up and running. This is not something I would do lightly (the exposing part).

As for the play offline, you can make it a background thread that just retries every x minutes, and have the main thread run your game logic, storing whatever needs to be uploaded on the local filesystem to cache it for upload:

  1. Main game thread saves data into fe. JSON file to_be_uploaded.json
  2. Background upload process checks for internet connection
  3. Then checks if to_be_uploaded.json has content
  4. If 2&3 == True, remove contents from to_be_uploaded.json and store in pending_upload.json
  5. Try upload.
  6. If successfull, remove pending_upload.json and goto #2. If not goto #5

The reason for #4 above is for data contention, where main game thread tries to add to to_be_uploaded.json while the background thread already has the object loaded and then uploads it. If it deletes the contents of the file to_be_uploaded.json it would miss the write from the main thread. Putting the data in a secondary cache file pending_upload.json removes this problem, unless we're talking about very frequent writes to to_be_uploaded.json. In this case you would need to look into file locking methods.

There's other ways to go about this, but this is what springs to mind after thinking for a couple of minutes...

Edo Akse
  • 4,051
  • 2
  • 10
  • 21
2

You would need to create a backend for your game. You can use Flask or Django framework for doing this. You can send the player data to the server so that it would store that in the database. And when the user would sign-in to their account, the server would send the data to the game so that it can load the user's progress.

Rahul Hindocha
  • 170
  • 2
  • 14