Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Manage List of Users with JSON

    I have multiple players. They each have a string id and various attributes such as x, y angle.

    I need a loop updating their attributes. I also need to be able to access directly someone's attributes with his id.

    What would be the best way to do that?

    If I want to cycle throught all players to update them, I need to put them all into an array and the id of the player would then be the index (number) but that could change if I remove or add new players.

    Or the id of player could be an attribute string but if I want to change the value of a specific player, I'd need to cycle through all my list everytime and check if the playerList[i].id == indexWanted.

    Any advice would be welcome

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    From what I read you have a turn based game with a number of players which could change at any time. You believe you have to loop through all the players to update a single player. No, you don't.

    You find a player in the player-turns table by his id number or his user name Not by the index number of the player-turns table. No need to cycle through all the players.

    If this is turn based and you have to wait until all players make a move have a column called PLAYED and set it to 1 after you update. Then check that column to see if all have completed their turn, If not, you wait. If they all have taken their turn you take the games turn because you have already processed them.
    Evolution - The non-random survival of random variants.

  • #3
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    From what I read you have a turn based game with a number of players which could change at any time. You believe you have to loop through all the players to update a single player. No, you don't.

    You find a player in the player-turns table by his id number or his user name Not by the index number of the player-turns table. No need to cycle through all the players.

    If this is turn based and you have to wait until all players make a move have a column called PLAYED and set it to 1 after you update. Then check that column to see if all have completed their turn, If not, you wait. If they all have taken their turn you take the games turn because you have already processed them.
    My game is in real time. =/ But yeah, I guess I should use a DB and query players info that way.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Even if you did it without a DB (but then how will you communicate changes made by one player to all the other players???), using the index number and JSON encoding is not the way to go.

    If your JSON object is something like:
    Code:
    var players = {
        "ann" : { "x": 73, "y": 101, "speed": 47.331 },
        "bob" : { "x": -4, "y": 811, "speed" : 12.5 },
        ...
    };
    Then you look a player up by name (or any other id you want to use). Or you can iterate through all of them:
    Code:
    for ( var player in players )
    {
        var info = players[player];
        info.x += info.speed;
        info.y += info.speed;
    }
    Okay, that's pretty unlikely actual game coding, but you get the idea. You can easily iterate through all of them and/or find any one of them, as needed.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    IdkWhatsRc (06-17-2013)

  • #5
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Even if you did it without a DB (but then how will you communicate changes made by one player to all the other players???), using the index number and JSON encoding is not the way to go.

    If your JSON object is something like:
    Code:
    var players = {
        "ann" : { "x": 73, "y": 101, "speed": 47.331 },
        "bob" : { "x": -4, "y": 811, "speed" : 12.5 },
        ...
    };
    Then you look a player up by name (or any other id you want to use). Or you can iterate through all of them:
    Code:
    for ( var player in players )
    {
        var info = players[player];
        info.x += info.speed;
        info.y += info.speed;
    }
    Okay, that's pretty unlikely actual game coding, but you get the idea. You can easily iterate through all of them and/or find any one of them, as needed.
    That looks promising. But what is it "conventional" way to update every player?

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Well, for a "real time" game, the truly correct answer is "don't use a browser."

    It's really not suitable for such things as fast-moving first-person shoot-em-up games.

    But assuming you are doing something a little less "real time" than that...

    The big question is: How will you notify player "B" when player "A" has made a move?

    The conventional browser based answer: Using AJAX, every NN seconds the browser makes another request of the server, and the server tells the browser what moves each person has made.

    Major issue: How *few* NN can be there--and how responsive your "real time" game can be--depends on having not too many players, a reasonable fast server, and reasonably fast connections by all players. If you are trying to run this on, say, an el cheapo shared hosting server, you might well find that you will get "hiccups" when the shared server is loaded and your users suddenly find that "real time" means "in the last 30 seconds" or so.

    So...you have to keep the number of players down, keep the amount of work done by the server down, write *tight* (a.k.a. fast) server code, and hope. Or buy a dedicated server. $$$$.

    There are other solutions, aside from AJAX-based. Java applets (*NOT* JavaScript) are one choice: Java is capable of "push" communications from the server, so you aren't dependent of constant polling every NN seconds. Node.js is a server-side system that is also capable of this. So the questions become: How much new stuff do you want to learn? How much do you want to spend on this?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Well, for a "real time" game, the truly correct answer is "don't use a browser."

    It's really not suitable for such things as fast-moving first-person shoot-em-up games.

    But assuming you are doing something a little less "real time" than that...

    The big question is: How will you notify player "B" when player "A" has made a move?

    The conventional browser based answer: Using AJAX, every NN seconds the browser makes another request of the server, and the server tells the browser what moves each person has made.

    Major issue: How *few* NN can be there--and how responsive your "real time" game can be--depends on having not too many players, a reasonable fast server, and reasonably fast connections by all players. If you are trying to run this on, say, an el cheapo shared hosting server, you might well find that you will get "hiccups" when the shared server is loaded and your users suddenly find that "real time" means "in the last 30 seconds" or so.

    So...you have to keep the number of players down, keep the amount of work done by the server down, write *tight* (a.k.a. fast) server code, and hope. Or buy a dedicated server. $$$$.

    There are other solutions, aside from AJAX-based. Java applets (*NOT* JavaScript) are one choice: Java is capable of "push" communications from the server, so you aren't dependent of constant polling every NN seconds. Node.js is a server-side system that is also capable of this. So the questions become: How much new stuff do you want to learn? How much do you want to spend on this?
    I'm currently using Nodejs + Websocket + Socket.io. It's quite efficient and make a decent real-time game is possible with that.
    Ex: https://hacks.mozilla.org/2012/03/browserquest/

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    *SIGH* Why didn't you say so.

    But I *DID* say you could use that:[quote]...Node.js is a server-side system that is also capable of this....[/code]

    So if you knew the answer, why'd you ask?

    Now the question: Who is going to host Nodejs for you?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    [QUOTE=Old Pedant;1341631]*SIGH* Why didn't you say so.

    But I *DID* say you could use that:
    ...Node.js is a server-side system that is also capable of this....[/code]

    So if you knew the answer, why'd you ask?

    Now the question: Who is going to host Nodejs for you?
    Well, my question was more about how to manage players at the same time rather than what technology to use.

    Right now, I'm basically importing all the player data on the server, updating their attributes then putting everything back to the DB but I guess that isn't really the proper way to do it...

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    why mess around with a DB is you have socket.IO? That's like putting a diving board on your bathtub instead of your giant pool in the backyard...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #11
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    why mess around with a DB is you have socket.IO? That's like putting a diving board on your bathtub instead of your giant pool in the backyard...
    You think I should not use a DB? How would I save data if my server is down or whatever?

    Do you think I should import everything on the server and do stuff with them. Then, like every minute, I save-copy everything in the DB and I could also save player info when he logs out?

  • #12
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by IdkWhatsRc View Post
    You think I should not use a DB? How would I save data if my server is down or whatever?

    Do you think I should import everything on the server and do stuff with them. Then, like every minute, I save-copy everything in the DB and I could also save player info when he logs out?

    even if you used a db, how you can you save to the db if the server is down?

    i don't think you should save every minute, i think you should save when something changes.

    my main point is that if you have socket.io, there is no reason to save every move to a DB just so other players can see the result like you would in say, a php solution to the problem.

    if you are using socket.io then you already have the event bindings needed to trigger state saves. you simply JSON.stringify your App's state object(s) and save it to disk. use JSON.parse on the data file(s) when the server boots. to roll the socket.io state to how it was before restarting the server.

    There may be long-term or voluminous data that indeed belongs in a DB, but that's not the same things as the turn-by-turn minutia used to sync players in the game with each other....
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •