How to create a 2 player game in ember


#1

I like to create a game in ember.It is easy to create a oneplayer game .But I wonder how to create a 2 player game For example like battleship


#2

So you’ll need some pieces which at a high level are:

  1. a way to identify a user, probably traditional authentication of some sort
  2. a way to synchronize data across clients, probably a backend API of some sort
  3. some mechanism for updating data (e.g. from the other player) and responding to those updates

If you don’t want to write your own backend I’d highly recommend Firebase which would cover all three of the above. It’s more or less a “dumb backend” with some light layers around a database and it uses websockets so the data updating bits (#3) are taken care of for you for free. There is an ember addon called emberfire which provides all of the adapters and client API code for firebase. Firebase also provides incredibly easy to configure backend authentication for google/facebook/github/email-password/twitter and more.

If you want to use firebase you’ll need to do the following at a high level:

  1. create your ember app
  2. install emberfire
  3. create a firebase project in the firebase console (use the realtime database instead of the “cloud firestore” because I don’t think the latter is supported by emberfire yet)
  4. configure emberfire to use the project you set up in your config/environment.js
  5. install torii (or ember simple auth, torii was a little easier to get started with for firebase in particular)
  6. set up one of the authentication methods in the firebase console and then in your torii auth code
  7. create some ember data models (e.g. player, ship, board, etc.)
  8. start writing data!

There are some more concerns about database structure and security rules and such but if you are just experimenting you could worry about those later.

If you want an example of a working ember app backed by firebase you can take a look at the source for this silly app I made called bracketize. It uses google authentication only (but others would be similar to configure), basic “secure” routes, data management, etc.

And of course if you run into any issues or questions feel free to ask!


#3

Thats is the point where I am stuck.Can I use websockets here??


#4

You can definitely use websockets (the main alternative would probably be polling). I’d look into ember-websockets and figure out how you want to handle the updates (push them to the store? do something else?).

If you want your websocket data to be encapsulated in ember data models you could also look at some of the data update code in emberfire to see if they’re doing anything fancy as far as auto-updating models and such.