Game Rounds (JS)¶
A game round is a record of one play of your game by one player: what kind of game it was, when it started and ended, the score, the place, and any extra data you want to keep. Use rounds for match history and stats.
The player must be signed in.
Save a round¶
const started = new Date();
// ...the player plays...
GameFuseGameRound.create({
gameType: 'Time Trial', // any name of yours
score: 1250,
startTime: started,
endTime: new Date(),
metadata: { track: 'Canyon', car: 'red' }, // any object
}, function (message, hasError) {
if (hasError) {
console.error('Could not save the round: ' + message);
return;
}
const round = GameFuseUser.CurrentUser.getGameRounds()[0]; // the new round is first
console.log(round.getGameType() + ': ' + round.getScore() + ' in ' + round.getDuration() + ' seconds');
});
Only gameType is needed. A round always belongs to the signed-in player.
Change a round¶
Save the round when play starts, then fill in the result when it ends.
round.update({ score: 1400, endTime: new Date() }, function (message, hasError) {
if (!hasError) {
console.log(round.getScore()); // 1400
}
});
update takes score, place, startTime, endTime and metadata.
A round with several players¶
Each player's game saves that player's own result. The first player starts a multiplayer round; the others add theirs to it with its ID.
// Player one
GameFuseGameRound.create({ gameType: 'Duel', multiplayer: true, place: 1, score: 21 }, function (message, hasError) {
if (hasError) return;
const mine = GameFuseUser.CurrentUser.getGameRounds()[0];
const sharedId = mine.getMultiplayerGameRoundID();
// Send sharedId to the other players, for example through your multiplayer room
});
// Every other player, with the ID they were sent
GameFuseGameRound.create({ gameType: 'Duel', multiplayerGameRoundID: sharedId, place: 2, score: 17 }, function (message, hasError) {
if (hasError) return;
const mine = GameFuseUser.CurrentUser.getGameRounds()[0];
for (const result of mine.getRankings()) {
console.log(result.place + '. ' + result.user.getUsername() + ' (' + result.score + ')');
}
});
getRankings() lists every player's result in the shared round: { user, place, score, startTime, endTime }.
With GameFuse Multiplayer, your room code can save the results on the server instead, where players cannot change them. See Saving GameFuse data.
Load the player's rounds¶
GameFuseGameRound.downloadGameRounds(function (message, hasError) {
if (hasError) return;
for (const round of GameFuseUser.CurrentUser.getGameRounds()) {
console.log(round.getGameType(), round.getScore(), round.getStartTime());
}
});
// Only one kind of game, and the second page (100 rounds a page, newest first)
GameFuseGameRound.downloadGameRounds(callback, { gameType: 'Duel', page: 2 });
Delete a round¶
Reference¶
| Call | What it does |
|---|---|
GameFuseGameRound.create(options, callback) |
Saves a round for the signed-in player |
GameFuseGameRound.downloadGameRounds(callback, { gameType, page }) |
Loads the player's rounds |
GameFuseUser.CurrentUser.getGameRounds() |
The loaded rounds, newest first |
round.update(changes, callback) |
Changes a round |
round.destroy(callback) |
Deletes a round |
round.getID(), getGameType(), getScore(), getPlace(), getMetadata() |
One round's details |
round.getStartTime(), getEndTime() |
Date objects, when set |
round.getDuration() |
Seconds from start to end, when both are set |
round.isMultiplayer(), getMultiplayerGameRoundID(), getRankings(), getNumberOfPlayers() |
For rounds with several players |
The options for create:
| Option | What it is |
|---|---|
gameType |
The kind of game. Needed. |
score, place |
Whole numbers |
startTime, endTime |
Date objects |
metadata |
Any object |
multiplayer |
true to start a round other players can add to |
multiplayerGameRoundID |
The shared ID, to add this player's result to a round another player started |