Skip to content

Instantly share code, notes, and snippets.

@djave-co
Last active April 8, 2020 10:10
Show Gist options
  • Save djave-co/15ac3cb1d38359dc21e9f622dff0ea07 to your computer and use it in GitHub Desktop.
Save djave-co/15ac3cb1d38359dc21e9f622dff0ea07 to your computer and use it in GitHub Desktop.
async function getGameId(){
// Create a 6 figure game code.
let idSize = 2;
let id = Math.floor(Math.random() * Math.pow(10, idSize)).toString();
id = id.padStart(idSize, '0');
// See if that game already exists
let snapshot = await db.collection("games").doc(id).get()
if (snapshot.exists) {
console.log("Game " + id + " already exists");
return getGameId();
}
return id;
}
// Create a game, setting the owner as the current player
function createGame(player_id){
return getGameId().then(id => {
// The below creates a new promise.
// How can I make createGame() return this promise?
return db.collection("games").doc(id).set({owner: player_id});
});
}
createGame(req.body.player_id).then((game) => {
console.log("complete!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment