Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Javascript Multiplayer Game - Server Side Validation.

Javascript Multiplayer Game - Server Side Validation.

Problem

I'm starting my hand at creating a multi-player game using HTML 5 / Javascript.

I'm currently doing some prototypes on different ideas on how to stop people from cheating. Now I know I need to do everything Server side and just have the client sending "actions".

My issue is I can't workout the best way to store the game state for each player.

So just looking at something basic; Two players running round a empty map.

Currently my idea is

Both clients(sockets.io) send their actions to a Node.JS server that then responds with a X/Y coord. This is fine. But obviously both clients need to know where the other player is.

I thought about doing this by creating a new database Table for each game and having the game state stored in there so the two node.js connections can talk to eachother.

My question is, is this the best way to interact between two node.js connections, would it be fast enough? Or is there a design patten for this specific task that I'm missing?

thanks

Problem courtesy of: james

Solution

Creating a new table per game is generally considered a Terrible Idea.

If you don't need persistence -- ie. in the case your Node.js server croaks, a game may be lost -- you can simply store the games in memory, in an object, let's say games, which might contain an object that has an array of players, each of which might contain x and y coordinates, etc.

var games = {
  123019240: {
    players: {
      123: {x: 1, y: 1, name: 'Joe'},
      456: {x: 2, y: 2, name: 'Jimbob'}
    }
  }
};

If you do need persistence, though, you really should probably look into some other databases than SQL -- for instance Redis might be a good choice for the task.

In any case, SQL feels like the wrong tool, and creating new tables on demand even more so. (If you are using SQL, though, consider a table layout with game_id, player_id, x and y and be sure to have an index on game_id :) )

Solution courtesy of: AKX

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Javascript Multiplayer Game - Server Side Validation.

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×