Jump to content
  • Advertisement
Sign in to follow this  

[web] Pointers for PHP or Ruby backend for HTML/Javascript game

This topic is 2764 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'm starting to work on an HTML/Javascript game for Facebook with a friend. The game requires a database as it's essentially a non-realtime MMO where all the players are participating in one world and can interact. We plan to use PHP or Ruby as our backend for all database interaction and actual game logic, so I'm trying to get a feel for whether there are libraries out there that already solve some of this or patterns we should be following while implementing the system ourselves.

Currently I've done a little investigation and not found any existing libraries. I started writing my own system, but I'm not sure it's the right approach. Essentially I have a single file that can have data posted to it from Javascript. For example, if I wanted to buy an item, the Javascript would look like this:

$.post('http://mydomain.com/transact.php', { method: 'BuyItem', user: 1, item: 3, quantity: 1 }, function(data) { /* handle response */ });

The "method" is matched with a PHP class so that I can instantiate the class and pass it the rest of the data to handle the response, in this case ensuring the user can afford the item and if so purchasing the item. In this system I would then continue to add new classes that represent each action or set of actions such as selling items, traveling, and so on.

Does that sound like a sane approach for keeping all game logic on the server? Is there a more "standard" way to handle such interactions between Javascript and a server side language like PHP or Ruby? Is there an existing solution out there that will save me a lot of time and frustration?

Thanks in advance.

Share this post

Link to post
Share on other sites
I'd need to see your overall design, but.....

If you're going to use Javascript to pass information to the server, then I'd recommend either jQuery or Moo Tools. jQuery is my favorite, but either will do the job.

However, javascript is not required to pass data to the server. If this is a text-based (with static images) game, then you can simply use an HTML <form> tag to pass information back and forth. This will be much easier and save you much more time than writing unnecessary Javascript.

There are some libraries for PHP frameworks, but I was always more comfortable rolling my own. Probably because I spent too much time developing my own sites before many of the frameworks existed.

I hope this was helpful. I'd love to hear more about your project as I could probably offer more advise if I knew the bigger picture.

Share this post

Link to post
Share on other sites
We are using jQuery, sorry. The code above is using jQuery to post the data. We aren't using forms simply because the entire game is to be played without refreshing the page at all (i.e. all with AJAX requests and dynamically loading in new HTML as needed).

At its core we're trying to build an economy sim where you can buy, sell, and trade goods. We know we want all game logic on the server as a limiter for cheating. We'll probably also do some type of key signing as well, but it's not going to be mission critical since the data passed from client to server is merely intent and is always subject to server approval.

So the big part we're trying to figure out now is what the server code should look like to both allow for agile/iterative development and hopefully scale well as we add new actions and items to the game beyond the core economy bits and ramp up in user counts (we're always questioning the architecture as we go up to 100 users, 1,000 users, 10,000 users, 100,000 users, and 1,000,000 users since that's obviously the goal smile.gif).

Right now in PHP we have something like this as our core bit:

$t = new $_POST['method']; // creates a class using the name passed to us from the client
$user = $_POST['user'];
$data = $_POST['data'];
echo $t.Transact($mysqli, $user, $data);

We then create a bunch of PHP classes for each action, e.g.

class BuyItem {
public function Transact($mysqli, $user, &$data) { /* stuff goes here */ }

We use glob in our main transact file so that it includes all PHP files in a subdirectory set up to hold our transaction classes. This lets us simply drop in a new PHP file with the class, add an AJAX call with that class name as our method, and assuming the AJAX and PHP agree on input/output format things should just work.

The question is whether or not there are existing frameworks that make this simpler and/or whether we're going to hit some fundamental issues with this route. So far it seems logical and is working in our proof of concept, but we currently only have two actions.

I'd love any insight from others that have built web games similar so that I can avoid any potential pitfalls.

Share this post

Link to post
Share on other sites
Well sorry to say I had to make a new account to post this. My old unknownhero account i lost the password and i don't have the old email anymore so this will have to do.

Since I think the web development forum could use some freshing up with some good technical stuff. I'll post a bit about what I acheived for my game using php and js.

I'm making an online real time rpg in jquery/php with a php daemon on the backend. Mysql is the database

I don't use classes in php on the web end because I need it to be fast and it doesn't make sense to make classes on every request a client makes. The web front end is just a bunch of php scripts that are called from an input php script when a client makes a request to the game. Think procedural code.

The input script is just one big switch statement directing client's requests to the php files. The game either return html or json response depending on what the client is doing.

The game's main php script is a file with just a single function that retrieves all the data a client would need to display in json. Like what map he is in and how many npcs or items are around him.

Depending on the user's latency this script can average 5 updates a second by polling. Plenty fast. Latency affects update speed. Higher latency pushes it up to 2 to 3 updates a second.

On the daemon end that runs 24/7 I use php instead of previoiusly python because its faster to code new features and not deal with python's issues. I uses classes because the script/code runs nonstop and controls npc's and items. Since the script doesn't end, it wouldn't take up much cpu to use classes but memory can grow over time. last time it took about 40 MB to load the entire 19,000 map tiles from mysql in the game. Not too bad.

In another test I ran 50 simple npcs whlie one player was in the game. It took about 120ms per a game tick. Plenty to scale. Not all npcs have to be active at the same time. Its just a matter of code.

All in all its mainly a technical feat that taught me alot about coding. Its fast, it runs and it can scale depending on the hardware. THe first big limit would be mysql. Hardware is cheap but scaling mysql will be the main challenge.

Your non-realtime game would probably hit the same issue of the database as far as scaling goes but at a much higher user count.

To those who are interested in doing a js/php game. Its very possible with browsers and cpus getting faster and faster. Real time can work, just don't expect it to be fast/accurate/huge at the same time. Latency is still an issue and the DOM is probably the biggest limit now. Thats subject to change when browsers update to new versions. Javascript is plenty fast. PHP will get even faster.

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!