1

I'm working on a simple boardgame and I want to provide a web interface (currently trying flask, but django would also be an option). Users will send HTTP requests (or ajax) for the game actions, which will be processed using view functions. These game actions (view functions) will alter the game state data that is maintained on the server.

Now my question is, how should I implement the game state data? I could store this in a database and access (get/set) the database in any view function call (game action). But this may be heavy on the database? I searched online and learned that I can't use global variables because they aren't shared between multiple server threads/instances. Using session storage also won't work because the game state is shared between multiple users (sessions).

davidism
  • 121,510
  • 29
  • 395
  • 339
PieterV
  • 816
  • 10
  • 23
  • You have to have a database. A fast one like Redis is great for your need. – Siyu Dec 13 '18 at 13:45
  • But if I have a Game object with state and logic, that means I have to serialize and deserialize that object for every request? Is there no way where I can just access that object whenever I want? – PieterV Dec 13 '18 at 13:51
  • You store only the state, not logic, serialization is a necessary cost. When it comes to sharing data between threads, you have to put the data somewhere else than these threads, which is effectively a database. – Siyu Dec 13 '18 at 13:55
  • Fair enough. I'm well aware that my case is so simple that it doesn't matter at all. But just out of curiosity, what if I had a more taxing online game, like an FPS? I assume that, for example, the locations of characters on the map isn't stored in a database? Otherwise there would be continues database accesses while moving characters around? – PieterV Dec 13 '18 at 14:01
  • First in memory database can be really fast, and second as far as I know, online FPS does not necessary store everything. The server only needs verify if a move is "eligible". – Siyu Dec 13 '18 at 14:07
  • And there is no way where I can just keep a python object alive over multiple view function calls? – PieterV Dec 13 '18 at 15:10
  • you can do that with a global variable but it's only readable from a single django/flask instance. – Siyu Dec 13 '18 at 15:19

2 Answers2

0

just put your game at the same folder as the views.py call it by

from yourfilename import *

then use the functions or classes as you like

pickle:

import pickle
pickleout = open('w.pickle','ab')
    pickle.dump(board,pickleout)
    pickleout.close()

and to call it use:

picklein=open('w.pickle','rb')
    arr=pickle.load(picklein)
wafi
  • 78
  • 9
  • This indeed explains how to get access to the game state and logic in my view functions. But I wanted to know how to access running (instantiated) state. For example, which cards have already been drawn from a deck and which haven't. This is continuously updated via view functions. – PieterV Dec 13 '18 at 14:07
  • you can constantly print the cards in the html.. – wafi Dec 13 '18 at 14:52
  • please elaborate – wafi Dec 13 '18 at 14:53
  • Printing the html just shows the user which cards are taken, but the server also needs to remember this. – PieterV Dec 13 '18 at 15:07
  • u can constantly put it in array .. define arr=[] ... then use "arr.append(the_card)" – wafi Dec 13 '18 at 15:24
  • Yes, but where do I store the array? As in variable within a function, it will be delete when the function ends. As a global variable, it will only be available within that web server instance. A database is a possibility, but I was looking for alternatives to that. – PieterV Dec 13 '18 at 15:47
  • use pickle,,,i will edit my answer so i can right some code,, look up – wafi Dec 13 '18 at 15:55
0

IMHO you should:

  1. Keep running (instantiated) state in some kind of in memory database (Memcache, Redis, etc).
  2. Save state to actual (disk) database asynchronously.
  3. Only send and receive state changes (partial updates).

You game state data could probably be a a dictionary (faster than serializing a custom object I think).

You can see an example of synchronizing state between players in a multiplayer game here (sorry for the Erlang)

yorodm
  • 4,359
  • 24
  • 32