Skip to content
Download all docs .md

Saving GameFuse Data from Room Code

room.gamefuse only ever reaches your own game

Room code has no database and no API of its own. room.gamefuse is the only way to GameFuse data, and every call it makes goes out under a key that GameFuse seals to your game. GameFuse takes the game from that key and from nowhere else, so there is no call, ID or argument that reaches another game's players, scores, rounds or items. Asking for an ID that belongs to another game answers "No player with id … in this game."

Your room code can read and write your game's GameFuse data with room.gamefuse. Scores, credits, attributes, leaderboards, game rounds, variables and store items are all there.

This matters because room code runs on the multiplayer server, not in a player's browser. When the room code saves the score, a player cannot send a made-up one.

How it works

  • room.gamefuse has full access to the data of your one game. It cannot reach any other game. A user id that belongs to another game is an error.
  • Every call returns a promise. Use await.
  • You may make 120 calls a minute. See limits.
async gameEnd(room, result) {
  await room.gamefuse.users.addScore(result.winnerId, 100);
  await room.gamefuse.leaderboard.add(result.winnerId, 'wins', 1);
},

In the calls below, id is a player's GameFuse user id. That is the same as player.id in room code.

Users

users.get(id)

Returns { id, username, score, credits, guest, attributes: { key: value } }.

const user = await room.gamefuse.users.get(player.id);
room.log(user.username, 'has', user.credits, 'credits');
if (user.guest) room.send(player, 'hint', { text: 'Sign up to keep your score.' });

users.addScore(id, n) and users.setScore(id, n)

addScore adds n to the user's score. setScore sets the score to n. Both return { score }.

const { score } = await room.gamefuse.users.addScore(player.id, 10);
await room.gamefuse.users.setScore(player.id, 0);

users.addCredits(id, n) and users.setCredits(id, n)

addCredits adds n to the user's credits. setCredits sets the credits to n. Both return { credits }.

const { credits } = await room.gamefuse.users.addCredits(player.id, 25);
await room.gamefuse.users.setCredits(player.id, 500);

Attributes

attributes.set(id, key, value)

Sets one attribute on a user. Returns { key, value }.

await room.gamefuse.attributes.set(player.id, 'rank', 'gold');

attributes.remove(id, key)

Removes one attribute from a user. Returns {}.

await room.gamefuse.attributes.remove(player.id, 'rank');

Leaderboards

leaderboard.add(id, name, score, metadata)

Adds an entry for that user to the leaderboard called name. metadata is optional. Returns { id }.

await room.gamefuse.leaderboard.add(player.id, 'fastest-win', 42, { seconds: room.time });

leaderboard.get(name, limit)

Reads a leaderboard. limit is optional. Returns { entries: [{ user_id, username, score, metadata }] }.

const { entries } = await room.gamefuse.leaderboard.get('fastest-win', 10);
room.broadcast('top10', entries);

leaderboard.clear(id, name)

Clears that user's leaderboard entries. name is optional. Returns {}.

await room.gamefuse.leaderboard.clear(player.id, 'fastest-win');

Rounds

room.round

A game round is made for you when the game starts. You do not have to create one. room.round holds its ids:

room.round.multiplayerGameRoundId    // the round shared by everyone in the room
room.round.gameRoundIds[player.id]   // that one player's own round

The round's game type is the queue name, or 'private' for a private room.

A player who joins a game already under way (config.joinAfterStart: true) gets their own game round in the same shared round, added to room.round.gameRoundIds a moment after playerJoined runs.

rounds.update(gameRoundId, { score, place, metadata, end_time })

Updates one player's round. Every field is optional. Pass end_time: true to set the end time to now. Returns { id }.

async gameEnd(room, result) {
  for (const player of room.players) {
    await room.gamefuse.rounds.update(room.round.gameRoundIds[player.id], {
      score: result.scores[player.id],
      place: result.places[player.id],
      end_time: true,
    });
  }
  await room.gamefuse.rounds.finishMultiplayer(room.round.multiplayerGameRoundId);
},

rounds.finishMultiplayer(id, metadata)

Sets the end time of the shared round. id is room.round.multiplayerGameRoundId. metadata is optional. Returns { id }.

await room.gamefuse.rounds.finishMultiplayer(room.round.multiplayerGameRoundId, { map: 'harbor' });

rounds.createMultiplayer({ game_type, metadata, user_ids })

Makes another shared round, with one round for each user in user_ids. Use it when one room plays several games in a row. metadata is optional. Returns { multiplayer_game_round_id, game_round_ids: { "<user_id>": id } }.

const next = await room.gamefuse.rounds.createMultiplayer({
  game_type: 'rematch',
  user_ids: room.players.map((p) => p.id),
});
room.state.roundIds = next.game_round_ids;

Variables

variables.get()

Reads your game's variables. Returns { variables: { key: value } }.

start(room) {
  room.state.goal = 10;
  room.gamefuse.variables.get().then(({ variables }) => {
    if (variables.goal) room.state.goal = Number(variables.goal);
  });
},

Store

store.items()

Lists your game's store items. Returns { items: [...] }.

const { items } = await room.gamefuse.store.items();

store.give(id, itemId) and store.remove(id, itemId)

Gives a store item to a user, or takes it away. itemId is the store item's id. Both return {}.

await room.gamefuse.store.give(result.winnerId, 12);
await room.gamefuse.store.remove(player.id, 12);

All calls

Call Returns
users.get(id) { id, username, score, credits, guest, attributes: { key: value } }
users.addScore(id, n), users.setScore(id, n) { score }
users.addCredits(id, n), users.setCredits(id, n) { credits }
attributes.set(id, key, value) { key, value }
attributes.remove(id, key) {}
leaderboard.add(id, name, score, metadata) { id }
leaderboard.get(name, limit) { entries: [{ user_id, username, score, metadata }] }
leaderboard.clear(id, name) {}
rounds.createMultiplayer({ game_type, metadata, user_ids }) { multiplayer_game_round_id, game_round_ids: { "<user_id>": id } }
rounds.update(gameRoundId, { score, place, metadata, end_time }) { id }
rounds.finishMultiplayer(id, metadata) { id }
variables.get() { variables: { key: value } }
store.items() { items: [...] }
store.give(id, itemId), store.remove(id, itemId) {}

Stop players writing their own scores

Saving from room code is only half of it. A player's browser can still call the normal GameFuse routes, such as "Add to a user's score", with the player's own sign-in. Switch those routes off for players once your room code does the saving.

  1. Go to the GameFuse dashboard and open your game.
  2. Open the API access tab.
  3. Scroll to the API route access section. Each route has two columns of boxes: Players and Service keys.
  4. Untick the Players box on each row that your room code now handles. For example:
    • Add to a user's score
    • Set a user's score
    • Add store credits for a user
    • Set store credits for a user
    • Create a leaderboard entry
    • Create a game round for a user
    • Update a game round
  5. Press Save route access.

After that, a request from a player's browser to one of those routes is refused.

Room code is not affected by these switches. room.gamefuse always has full access to that one game.

Only untick the routes your browser code no longer needs. If your game still sets a score from the browser somewhere else, such as a single-player mode, that will stop working too.

The same table is described in Service Key Access.