var mongo = require("./mongoHelp.js") const uuidv4 = require('uuid/v4') const game = require("./game.js") var lobby = { list: async () => { result = await mongo.get("embezzle", "gamelist", {}) pubArr = [] result.forEach(element => { if (element.status == "lobby") { obj = { gameID: element.gameID, name: element.name, seats: element.seats, players: element.players, } pubArr.push(obj) } }) return (pubArr) }, details: async (search) => { result = await mongo.get("embezzle", "gamelist", { gameID: search }) pubArr = [] result.forEach(element => { if (element.status == "lobby") { obj = { gameID: element.gameID, name: element.name, seats: element.seats, players: element.players, } pubArr.push(obj) } }) return (pubArr) }, register: (name) => { id = uuidv4() mongo.put("embezzle", "players", { _id: id, name: name }) return ({ ownerID: id }) }, make: async (ownerID, seats) => { gameID = uuidv4() //todo write charity name generator name = await mongo.get("embezzle", "players", { "_id": ownerID }) name = name[0].name mongo.put("embezzle", "gamelist", { gameID: gameID, ownerID: ownerID, name: name, seats: seats, status: "lobby", players: [] }) lobby.join(ownerID, gameID) return ({ gameID: gameID }) // return({gameID:gameID}) }, join: async (ownerID, gameID) => { value = await mongo.get("embezzle", "gamelist", { "gameID": gameID }) gameObj = value[0] player = await mongo.get("embezzle", "players", { "_id": ownerID }) player=player[0] playerObj = { name: player.name } gameObj.players.push(playerObj) mongo.update("embezzle", "gamelist", { "gameID": gameID }, gameObj) return ({ gameID: gameID }) }, start: (ownerID, gameID) => { mongo.get("embezzle", "gamelist", { "ownerID": ownerID, "gameID": gameID }).then(value => { value[0].status = "playing" mongo.update("embezzle", "gamelist", { "gameID": gameID, "ownerID": ownerID }, value[0]) game.init(gameID) }) return ({ gameID: gameID }) }, } module.exports = lobby