Reputation: 13
I'm making call to game server every 30 seconds and getting this response:
{
name: 'test',
map: 'pool',
maxplayers: 12,
players:
[ { deaths: 0,
kills: '0',
ping: 50,
name: 'test',
score: 0
} ],
}
How can I console.log something like this: "${name} joined a server!" then array from last call changes to this for example:
players:
[ { deaths: 0,
kills: '0',
ping: 50,
name: 'test',
score: 0
},
{ deaths: 0,
kills: '0',
ping: 20,
name: 'test2',
score: 0
}
]
I was thinking using database and saving response to it, and then compare to new array from most recent call but I feel there should be a better way to do it.
EDIT:
I didn't explain my self correct, I'm using "gamedig" npm module to get stats from battlefield game server, I don't have access to actual server, all I know is ip.
Upvotes: 1
Views: 140
Reputation: 36339
There a lot of ways to handle this, but as a starting point I would recommend against polling your server every 30 seconds and moving to a more event-based system if you can. Polling isn't inherently evil or anything, but it does potentially run into scaling problems long term, and code maintenance headaches such as the one you're describing.
To start with, if you want to ensure your clients don't suddenly fail to sync in the middle of a game, you definitely need some kind of database to store the state of the game. Otherwise, any error or crash that causes your server-side code to restart will clear the game state and you're out of luck. You can pick from any of several, but I'd start by looking at something like Redis or Firebase depending on what kind of server presence you have / want to have.
Second, you can more easily reconstruct missed calls and such if you both emit events (e.g. using socket.io) to let clients know when the global game state has changed, and also store those events in a time-synced log. This way, if a client hasn't received an update in some amount of time (some games maintain a heartbeat from the server for this very reason), it can explicitly ask the server what it's missed, and get a set of events that have happened since the last known update. That last bit implies that every message you send includes a timestamp of when it's good as-of, so you can just ignore stuff that happened before your last update. Unique Event IDs are also very useful for keeping track of what events you've already processed or not.
EDIT
Ok, so given that you have no server-side to work with, you're really limited on options. I would think that the best thing to do then is to transform the data such that you can more easily detect changes, like perhaps a username-based hash:
const users = {
'user1': { deaths: 0, kills: 0, score: 0 }
'user2': { deaths: 1, kills: 5, score: 3000 }
}
Then on each response from the server, you'd need to just to check the existence of each user in the array:
// serverUsers is whatever collection of users you get back in your poll
const newUsers = serverUsers.filter(u => Object.keys(users).includes(u.name));
Obviously, detecting players going offline is similar, just done in the opposite direction, where you'd see who's in the 'users' collection that's not in the 'serverUsers' collection.
Upvotes: 3