The main point of this thread to get some answers about JavaScript/PHP interaction. Most importantly, how to attach a JavaScript game to a PHP database, then assign each player an area of the game so that no player can control another. Then feed the results of the match back to the PHP database to update the player's statistics.
I would assume object instances in the game state via JavaScript would do the trick based on what the MySQL database gives it.
There are really two separate questions here:
- state initialization
- ongoing communication
For the second point, Javascript & PHP themselves are irrelevant: like in any other networked game, you need to communicate with the server over some protocol. JSON messages over HTTPS connections or websockets is the popular and obvious option.
The first point is more interesting. As you suggest, you
can use PHP to generate some or all of the Javascript that is being served on the page.
However, this has some consequences:
- security: if you're directly generating Javascript code in PHP, you could end up opening yourself up to script injection and other nasties caused by malicious user input, and
- poor separation of code: your client-side code becomes tightly coupled to the backend, which makes it harder to test, move, refactor, etc.
The more modern option - which has become increasingly popular with the rise of bundling tools like Webpack - is to serve your Javascript code as a
library on your own page that has an explicitly constructor / initialization pattern / entry point. You still might need to get some initial user state onto the page to pass into your JS code - such as a user ID or an auth token or whatever else - but that can then just be a plain JSON object, which you can sfely serialize onto the page from a native object in your backend code.
It's a little bit more work initially to get it set up, but it's well worth it for the strong logical separation you get out of it, and makes it easier to add nice stuff like offline storage on the browser side, to swap out the backend, etc.