206 lines
5.9 KiB
JavaScript
206 lines
5.9 KiB
JavaScript
var mongo = require("./mongoHelp.js")
|
|
const charityGen = require("./charitygen.js")
|
|
const uuidv4 = require('uuid/v4')
|
|
|
|
function shuffle(arr) {
|
|
var i,
|
|
j,
|
|
temp
|
|
for (i = arr.length - 1; i > 0; i--) {
|
|
j = Math.floor(Math.random() * (i + 1))
|
|
temp = arr[i]
|
|
arr[i] = arr[j]
|
|
arr[j] = temp
|
|
}
|
|
return arr
|
|
}
|
|
|
|
|
|
const game = {
|
|
init: async (gameID) => {
|
|
gameObj = await mongo.get("embezzle", "gamelist", {
|
|
"gameID": gameID
|
|
})
|
|
gameObj = gameObj[0]
|
|
//gen charities
|
|
gameObj.charities = []
|
|
gameObj.distributionLog = []
|
|
gameObj.accuseLog = {}
|
|
console.log(gameObj)
|
|
for (let index = 0; index < gameObj.players.length; index++) {
|
|
gameObj.charities.push({
|
|
charityID: uuidv4(),
|
|
name: charityGen(),
|
|
capital: 0
|
|
})
|
|
}
|
|
//assign charities to users
|
|
gameObj.secretLink = []
|
|
gameObj.players.forEach((element, index) => {
|
|
charity = gameObj.charities[index]
|
|
console.log(element)
|
|
let link = {
|
|
charity: charity.charityID,
|
|
owner: element.name
|
|
}
|
|
gameObj.secretLink.push(link)
|
|
})
|
|
// Randomise the order of the arrays
|
|
shuffle(gameObj.charities)
|
|
//set board funds
|
|
gameObj.players.forEach(element => {
|
|
element.funds = 10000000
|
|
element.status = "Employed"
|
|
})
|
|
//update game
|
|
mongo.update("embezzle", "gamelist", {
|
|
"gameID": gameID
|
|
}, gameObj)
|
|
|
|
},
|
|
myCharity: async (gameID, ownerID) => {
|
|
name = await mongo.get("embezzle", "players", {
|
|
"_id": ownerID
|
|
})
|
|
name = name[0].name
|
|
secret = await mongo.get("embezzle", "gamelist", {
|
|
"gameID": gameID
|
|
})
|
|
secret = secret[0].secretLink
|
|
|
|
let mine = ""
|
|
secret.forEach(element => {
|
|
console.log(element.owner,name)
|
|
if (element.name == name) {
|
|
mine = element.name
|
|
}
|
|
})
|
|
|
|
chairtyObj = {
|
|
charity: mine
|
|
}
|
|
return chairtyObj
|
|
},
|
|
charityList: async (gameID) => {
|
|
charArr = await mongo.get("embezzle", "gamelist", {
|
|
"gameID": gameID
|
|
})
|
|
charArr = charArr[0].charities
|
|
return ({
|
|
charatiesArr: charArr
|
|
})
|
|
|
|
},
|
|
playerList: async (gameID) => {
|
|
gameObj = await mongo.get("embezzle", "gamelist", {
|
|
"gameID": gameID
|
|
})
|
|
players = gameObj[0].players
|
|
return ({
|
|
charatiesArr: players
|
|
})
|
|
|
|
},
|
|
boardFunding: async (gameID, ownerID) => {
|
|
name = await mongo.get("embezzle", "players", {
|
|
"_id": ownerID
|
|
})
|
|
name = name[0].name
|
|
players = await mongo.get("embezzle", "gamelist", {
|
|
"gameID": gameID
|
|
})
|
|
players = players[0].players
|
|
result = ""
|
|
players.forEach(element => {
|
|
if (element.name == name) {
|
|
result = name
|
|
}
|
|
})
|
|
},
|
|
distribution: async (gameID, ownerID, charityID, amount) => {
|
|
//get the player info
|
|
gameObj = await mongo.get("embezzle", "gamelist", {
|
|
"gameID": gameID
|
|
})
|
|
gameObj = gameObj[0]
|
|
|
|
if (gameObj.status != "distribution") {
|
|
return({error:"You have tried to distribute funds when it is not the distribution round"})
|
|
}
|
|
player = await mongo.get("embezzle", "players", {
|
|
"_id": ownerID
|
|
})
|
|
player = player[0]
|
|
//Wipe out the accuse log read for the next round
|
|
gameObj.accuseLog={}
|
|
// Check if we have two players left and if so, decide on a winner
|
|
employedCount = 0
|
|
gameObj.players.forEach(element => {
|
|
if (element.status == "Employed") {
|
|
employedCount++
|
|
}
|
|
})
|
|
if (employedCount < 3) {
|
|
return game.winner(gameID)
|
|
}
|
|
|
|
// Now we know the game is still on-going:
|
|
//add users funds to the charity & subtract donation from users funds
|
|
|
|
//check if user is trying to allocate too much money & set the amount so the users funds are wiped out
|
|
if (amount > player.funds) {
|
|
amount=player.funds
|
|
}
|
|
gameObj.charities.forEach( element => {
|
|
if (element.charityID == charityID) {
|
|
element.capital = element.capital+amount
|
|
player.funds = player.funds - amount
|
|
gameObj.distributionLog.push({
|
|
donator:player.name,
|
|
amount: amount,
|
|
charity: element.name
|
|
})
|
|
}
|
|
})
|
|
|
|
reaminingFunding = 0
|
|
gameObj.players.forEach(element => {
|
|
reaminingFunding= reaminingFunding+element.funds
|
|
if (reaminingFunding == 0) {
|
|
roundStatus(gameID)
|
|
}
|
|
})
|
|
|
|
mongo.update("embezzle", "gamelist", {
|
|
"gameID": gameID
|
|
}, gameObj)
|
|
return ({log: gameObj.distributionLog})
|
|
},
|
|
accuse: async (gameID, ownerID, player) => {
|
|
//???
|
|
},
|
|
winner: async (gameID) => {
|
|
winner = await mongo.get("embezzle", "gamelist", {
|
|
"gameID": gameID
|
|
})
|
|
winer = winner[0].winner
|
|
},
|
|
roundStatus: async (gameID) => {
|
|
gameObj = await mongo.get("embezzle", "gamelist", {
|
|
"gameID": gameID
|
|
})
|
|
gameObj = gameObj[0]
|
|
if (typeof gameObj == undefined || gameObj == "accuse") {
|
|
gameObj.status = "distribution"
|
|
} else {
|
|
gameObj.status = "accuse"
|
|
}
|
|
|
|
mongo.update("embezzle", "gamelist", {
|
|
"gameID": gameID
|
|
}, gameObj)
|
|
|
|
},
|
|
}
|
|
|
|
module.exports = game |