15

I will probably use javascript to develop an online board/card game. My approach will be to have a client that will be able to work in standalone mode, so it must enforce rules. That means for example, if a player can't play a card, he or she shouldn't even be able to play it. This is to enhance the user experience.

The idea here is to add hooks to send and receive events to and from the server, and share the code that implements the game rules between the server and the client. I don't see the point of writing them twice.

So, if I play in "server" mode, the client will update the server with my actions (verifying them as well), and the server will send me updates about the rest of the players.

Is there any framework to leverage this work?

For the server side, my options seem to be Node.js (unstable, but everything would be JS and that's neat), Erlang + erlang_js, and maybe some of those weird frameworks that "compile into javascript", that I'm no really fond of.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Papipo
  • 2,799
  • 2
  • 23
  • 28
  • 1
    I'm in a similar situation myself. How do you keep a system like this DRY? The client should be able to conduct the game standalone, but the server needs to validate everything eventually. If rules change, it would be preferable not to have to change both client and server code. – brainjam Jul 23 '10 at 15:54
  • 1
    Have you looked into the GWT at all? Java code that compiles to Javascript and can be used to write the Client and the Server sides. – g.d.d.c Jul 23 '10 at 15:54
  • @g.d, GWT had crossed my mind (and may be suitable for OP), but I've already committed to Python/jQuery as my back/front. – brainjam Jul 23 '10 at 15:58
  • check out http://stackoverflow.com/questions/3225251/how-can-i-share-code-between-node-js-and-the-browser/3225341#3225341 – Dagg Nabbit Jul 23 '10 at 15:59
  • @g.d.d.c it seems more natural to me that if I am going to code the server and the client in the same language, it should be javascript. And I don't like the static nature of java at all, I am too used to scripting languages that support closures. @no I want the client to load a .js file and have all the logic there, not to send code from the server, that's a different approach. I want my client to be snappy :-) – Papipo Jul 23 '10 at 16:14
  • @codecaster that's pretty much what the page I linked was about... how to share js between node.js and the client. Check my answer there, it's relevant. You can use a single gameloop and preprocess a few things with `//#ifdef SERVER`, `//#ifdef CLIENT` wherever the checks need to be done, then have calls to separate server.js and client.js scripts that talk to each other (I used WebSockets, I'm really hoping that becomes a standard). – Dagg Nabbit Jul 23 '10 at 16:50

2 Answers2

3

Sounds like Ape fits your requirements rather closely. It supports javascript modules so you can run the same code on the server as you use for clients and sending messages between different users is well supported (check out the mmorpg and chat demos).

phyrex1an
  • 246
  • 2
  • 2
  • Very nice! I didn't knew about Ape. I don't know if I am going to use it but I think this leads me in the right direction, thanks a lot! – Papipo Jul 25 '10 at 14:30
  • Unfortunately APE (which is neat) is only for pushing stuff for clients. – Papipo Jul 27 '10 at 20:53
1

I've been using RingoJs http://ringojs.org for exactly this - client&server code sharing - for a while.

For writing networked browser games easier i'm developing a thin layer on top of that: http://gamejs.org

oberhamsi
  • 1,303
  • 8
  • 18