Sign in to follow this  
beginnerdev

Implementing PvP for a simple, non-trivial, javascript mobile game app

Recommended Posts

I have a game we can sit down and play across the table on my tablet. How do I implement networked PvP? More specifically, how do I get the clients and website to communicate, how do I get the clients to interact, and what do you recommend as an online platform and database? [The game is abstract and extremely compact — no graphics, couple bytes per turn. However, I'd like to record games for future use as gamebooks. Maybe 256 bytes per game, which I think gets me ~4 million games per gigabyte. Since the method is non-trivial, I'd ideally want a database that can scale.]

Also, please assume I know nothing. I managed to learn enough javascript to code the game on my ipad, which only has wifi, so no plugins or libraries as yet. I do have a mac I can develop on, and Unity has been strongly recommended, but the game code is < 32 KB so I’m wondering if it’s overkill. (Beyond that code it’s mostly going to be AI, since most variation require a trivial amount of code.) My inclination is to create a kernel I can use to express an array of variants, but time is also of the essence, so I can make sacrifices in service of expedience.  The game itself seems to run the same in every HTML5 browser I've tried, although optimized for mobile touch.

 

Sorry if this question is broad, but I also I need general advice so I can take solid next steps.

Share this post


Link to post
Share on other sites

HTML5, or more specifically WebSockets does not allow a client web browser to host a server so regardless of if using Emscripten (via Unreal or Unity) or directly writing Javascript, it cannot work. You will need a server somewhere that can act as the game server for all your clients to connect to.

 

If you are interested in writing an app for phones or tablets, then these can of course host the server.

 

However, a slight hack you might be able to get away with if your game does not require much bandwidth to play: Use free Internet Relay Chat (IRC) servers to act as the "game server". Some of these support WebSockets or provide websockify proxies so that Web browser javascript can connect.

 

IRC such as irc.blitzed.org or freenode employ anti flood protection and limit clients to about 30 messages a minute. So if your game can get away with this, then it could work for you. I actually use this technique as a cheap and dirty list server for my multiplayer games.

 

If you need to scale up and host your own IRC servers one day (and remove the message cap) IRC is a standard (albeit pretty ancient though still popular) and you can find loads of very robust and tested open-source IRC servers.

 

As a rule of thumb, if your game is 2D, just plain Javascript and HTML5 canvas are often the easiest development platforms. If it is 3D then perhaps look at using an engine or perhaps Three.js (WebGL is a little too time consuming to use directly for most games). As for a database, then you are going to need a server anyway, perhaps use a RESTful type API in PHP and then SQLite? Loads of similar solutions here.

Edited by Karsten_

Share this post


Link to post
Share on other sites

Thank so much! Lot of good info here.

 

The games will be all be 2D for the first few generations, although the method extends quite seamlessly to a 3D model. (As you seem to indicate, a 3D version will be much more challenging to produce, not least from an interface standpoint, so that aspect will be a few years away.)

 

I'll definitely be implementing a server-based solution, both for matchmaking, player rankings, and recording games for future analysis.  Also, because the games are non-trivial, and can be expressed with many simple variants, I suspect mobile processors may be inadequate for a future "strong" AI.  (Although, based on extensive play-testing, a "weak" AI will certainly be sufficient for starters.  I'll actually more focused on getting humans to play humans to build community, even though the AI component is the most interesting part of the project.)  Ideally, I'll be looking to architect a solution that allows cross-platform play, which I suspect is feasible based on the amount of information transferred. 

 

That said, I definitely want a client that can be played on a tablet with no connectivity, as table-top play is certainly the most fun!  (This will have to including at least a weak AI, but shallow game trees and simple heuristics should suffice in the near term.)

 

It has been recommended I use Unity, node.js and MySQL. (I'll also look into the RESTful type API in PHP and then SQLite, since I have some familiarity with PHP, but let me know if you have any thoughts on the aforementioned tools.)

Edited by beginnerdev

Share this post


Link to post
Share on other sites

Instead of PHP (which is not a well-designed language to say the least), you may want to use Python. docs.python.org has a tutorial aimed at programmers that explains the language in a few hours of reading (although you probably want to read it again some time later for the more advanced parts).

 

Python is used a lot in web programming. For a bare bones approach, you typically start with a server implementation like bottle or flask (I assume there are more), and add page generation code for URLs that you serve. The Python code computes the values to display, a text template system then constructs the returned page.

SQLite is in the Python standard library.

 

 

One thing you may want to consider at this point too is your server provider. Assuming you don't run your own box, most providers support only a few languages/data bases, which means the choice you make now will affect the feasible set of server providers.

Share this post


Link to post
Share on other sites

True if you are already using a lot of Javascript in your game, node.js makes sense so you can share some code.

 

And yes, most languages are better than PHP, certainly Python I find to be nicer. However I always recommend PHP because there is a lot more free hosting services that provide PHP than any other scripting languages. I guess I am cheap like that haha.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this