Jump to content

Some questions regarding good practice uses of Phaser.


Aristy
 Share

Recommended Posts

Hey,

I'm here once again with some more questions. :)

-. I want to design my socket.io code to have authentication mechanism. Should I attach a "Auth" middleware on all endpoints or is there are better way (e.g session) to handle this? Are there any examples about this?

- Imagine the following code:

class GameState
{
    constructor() {
        ...
        this.createPlayer();
    }

    createPlayer() {
        this.player = new Player(this).getPlayer();
    }
}

class Player extends AbstractPlayer
{
    constructor(state) {
        super(state);

        this.state = state;
        this.player = this.state.game.add.sprite(x, y, 'player');
    }

    getPlayer() {
        return this.player;
    }
}

This code requires me to pass "state" value into Player object so I can reference to game object using "this.state.game". Can it be done in a better approach? I don't like passing state objects into my Player object. It feels like it breaks some of the solid principles, and overall looks bad.

- Is it possible to pass backend parameters directly to our game without having to rely on a seperate script tag on HTML pages? Like:

// backend
    res.render(path.join(__dirname + '/build/game.html'), {
        hello: "world"
    });

// game.html
    <body>
        <script type="text/javascript"> 
            window.hello = "{{ hello }}";
        </script>
        <script src="phaser.min.js"></script>
        <script src="game.js"></script>
    </body>

// game.js
    this.game.add.text(this.x, this.y, window.hello); // prints "world"

- What is the trick to measure latency? Send a timestamp from backend, compare it to current timestamp on client and show the difference as latency?

Link to comment
Share on other sites

I think you're going to end up sending Auth with each request and checking it on each request. A Session is basically that, except I wouldn't store a bunch of data server-side in a session vs. retrieving it each time from your persisted store or getting the client to send it each time and making sure none of the values changed (like with a checksum, or JWT, or something).

What does that "state" object have in it besides the game? What is its role? Does it need to be passed in or can the sprites go grab it from somewhere?

You'll have to just add identifiers to track objects. I like using GUIDs, like this package: https://github.com/broofa/node-uuid You're much more likely to make a unique one then with an integer.

You're probably better off passing the ID and some state instead of the entire sprite object. But I hesitate to make a hard-and-fast rule without knowing more about what the game does.

Broadly, latency is the time it takes to roundtrip a packet from one end to another. Mostly. :) So I'd measure how long it takes for a value sent from the server/client to reach the client/server.

I know next to nothing about this side of things. Check out PhoneGap, though? I think it's got CLI-based build methods vs. a more encompassing IDE approach.

 

Link to comment
Share on other sites

Updated my questions.

On 02.05.2016 at 5:56 PM, drhayes said:

I think you're going to end up sending Auth with each request and checking it on each request. A Session is basically that, except I wouldn't store a bunch of data server-side in a session vs. retrieving it each time from your persisted store or getting the client to send it each time and making sure none of the values changed (like with a checksum, or JWT, or something).

Just like cookies that store session fingerprints? Am I supposed to attach them every single time I make a socket call and verify with a middleware?
 

On 02.05.2016 at 5:56 PM, drhayes said:

What does that "state" object have in it besides the game? What is its role? Does it need to be passed in or can the sprites go grab it from somewhere?

Some game state specific attributes such as this.players, this.enemies, this.score etc. I need to pass it in order to access "game" in class scope. I could also pass "game" to constructor, but then I wouldn't be able to access state properties such as score.

On 02.05.2016 at 5:56 PM, drhayes said:

You'll have to just add identifiers to track objects. I like using GUIDs, like this package: https://github.com/broofa/node-uuid You're much more likely to make a unique one then with an integer.

Thought so. I'll most likely use this instead of creating my own.

On 02.05.2016 at 5:56 PM, drhayes said:

You're probably better off passing the ID and some state instead of the entire sprite object. But I hesitate to make a hard-and-fast rule without knowing more about what the game does.

I think you're right. Sprites has alot attributes in their object. I will make sure I pass whatever I need to keep data small.

Can I encode them to be smaller though so I don't pass them as plain JS objects? (e.g something that crypts the object and makes it smaller and backend may decrypt it to get object back)

On 02.05.2016 at 5:56 PM, drhayes said:

Broadly, latency is the time it takes to roundtrip a packet from one end to another. Mostly. :) So I'd measure how long it takes for a value sent from the server/client to reach the client/server.

Yes, I was wondering the algorithm actually. Does the client generate a timestamp, send it to server, make server compare it and return the difference? Or server sends the timestamp and client compares? The algorithms I created usually breaks when server goes down and have spikes. (80 ping goes up to 800 then goes back to 80. Not because my network is bad but rather my algorithm is crappy.)

On 02.05.2016 at 5:56 PM, drhayes said:

I know next to nothing about this side of things. Check out PhoneGap, though? I think it's got CLI-based build methods vs. a more encompassing IDE approach.

Solved it. Looks like they have alot of dependencies. Cordova depends on Android SDK which depends on Java, had to install different packages and API runtimes. Took like 7 hours with my slow DSL connection but I finally got it to work. :)

Link to comment
Share on other sites

I'd send the session identifier (whatever you end up using) on every request, I think. But, again: you should end up measuring things and making decisions based on engineering trade-offs rather than listening to me.

Encode: Probably not. You're talking serialize to string and marshall to the server vs. serialize to string, compress, then marshall to the server. CPU-vs-space probably means space wins 'cuz you need that CPU, but depends on the game and how much you end up passing back and forth.

Latency: exactly that. And you're guaranteed to get wild spikes, so you have to do something to smooth them if you want to measure it meaningfully over time. And I'd say it's 'cuz the ping is bad, not 'cuz the algorithm is crappy. The internet can really suck.

Link to comment
Share on other sites

On 4/30/2016 at 10:53 PM, Aristy said:

- Is it possible to pass backend parameters directly to our game without having to rely on a seperate script tag on HTML pages?

You can always pass them later, you're setting up a socket anyway so whenever the back end is ready (if it is before the connection then you'll have to wait, which is likely) just pass whatever you need down the pipe. Whilst it might not even be applicable to you this approach also helps to decouple the front-end from the back-end so if you change the housing you don't have to worry about grabbing all the variables, just make sure they remain available at an endpoint.

You could use REST instead just to set stuff up if it was easier and you dont mind another roundtrip to get config/init stuff.

The downside of course is possibly a slight delay for the user as you do another roundtrip to get initialisation stuff, although the user is probably waiting for the socket anyway and sending it down a socket rather than opening another request will probably be super quick. Likely sending another request isnt going to hurt too bad if you went that route, most games need a lot of stuff anyway so a short initial delay is usually tolerated better than 'standard' pages/apps.

Up to you if you consider this over-engineering though. For server rendered projects I normally ignore the ugliness of spamming globals and go that way, depends mostly on if there is any sensitive data or not.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...