I'm making a programming game to teach myself Clojure. Basically it is a server that polls clients for actions with JSON over HTTP.
Currently I have whole game state as a vector atom that contains individual player state maps. The server updates game changes into these player states and polls clients for their desired next action.
I've read some stuff on atoms and agents and others but I haven't quite figured them out yet.
My question is: How should I change the data structure or its storing mechanism and how should I do the polling and other updating so that they would not interfere with each other or run cleanly at the same time?
I figured out that the polling should probably be done with agents (am I right?). And maybe I should add a watcher that updates returned value to the player state which is managed in server.
I've also thought that to enable this I should change the game state into a map so that I can easily access individual player states.
(def game-state {"player1" {:stuff {...} :action a1}
"player2" {:stuff {...} :action a3}})
Should the game state be comprised of player states as atoms, like this:
{"player1" atom
"player2" atom}
... or something else?
On top of this all there is a visualiser HTML/JavaScript(AngularJS) page that regularly polls the game state from the server.
Currently as I don't have threaded polling everything else is stuck while a slow client thinks its next action (test case).
Any opinions and advice on how to do this correctly in Clojure is appreciated.
ps. I haven't included a database because I felt that storing data over game sessions was not needed, but if using a DB makes this easier or more correct I could probably use some in-memory DB.
core.async
for something like this, and plain vanilla atoms for managing state. And use as few atoms as possible; in this case, you need only one:def game-state (atom {:player1 ... :player2 ...})
– Charles Duffy