Embezzle/lobby.js

111 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-02-15 14:59:27 +00:00
var mongo = require("./mongoHelp.js")
const uuidv4 = require('uuid/v4')
const game = require("./game.js")
2020-02-15 14:59:27 +00:00
var lobby = {
2020-02-15 23:22:09 +00:00
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)
2020-02-15 14:59:27 +00:00
},
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)
2020-02-15 14:59:27 +00:00
},
register: (name) => {
id = uuidv4()
mongo.put("embezzle", "players", {
_id: id,
name: name
})
return ({
ownerID: id
})
2020-02-15 14:59:27 +00:00
},
make: async (ownerID, seats) => {
gameID = uuidv4()
2020-02-15 23:22:09 +00:00
//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})
2020-02-15 23:22:09 +00:00
},
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
2020-02-15 23:22:09 +00:00
})
2020-02-15 14:59:27 +00:00
},
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
2020-02-15 23:22:09 +00:00
})
2020-02-15 14:59:27 +00:00
},
}
module.exports = lobby