You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
8.9 KiB

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 = {}
gameObj.status = "distribution"
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]
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"
console.log(element)
})
//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
let gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
secret = gameObj[0].secretLink
let mine = ""
gameObj[0].charities.forEach(element => {
secret.forEach(user => {
console.log(name,user)
if (name==user.name){
console.log(element, user)
console.log("User Found")
if (element.charityID == user.charity) {
console.log(element.charityID,user.charity)
(console.log("Charity Found"))
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 (ownerID,gameID) => {
r=""
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) {
r = {funds: element.funds}
}
})
return r
},
distribution: async (gameID, ownerID, charityID, amount) => {
//turn the amount to an int
amount=parseInt(amount)
//get the game info
gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
gameObj = gameObj[0]
// console.log(gameObj)
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]
// console.log(gameObj)
gameObj.players.forEach((element, index) => {
if (player.name == element.name){
player=gameObj.players[index]
console.log(player)
}
})
//Wipe out the accuse log read for the next round
gameObj.accuseLog={}
// 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) {
game.roundStatus(gameID)
}
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
return ({log: gameObj.distributionLog})
},
accuse: async (gameID, ownerID, victim) => {
//Get votes on who should be fired
player = await mongo.get("embezzle", "players", {
"_id": ownerID
})
player = player[0]
gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
gameObj = gameObj[0]
if (gameObj.status != "accuse"){
return {Error: "Game status is not accuse, you'll want to distribute funds before trying to fire someone"}
}
gameObj.players.forEach(element => {
if (victim == element.name) {
gameObj.accuseLog[player.name] = victim
} else {
return gameObj.accuseLog
}
})
if (Object.keys(gameObj.accuseLog).length == gameObj.players.length){
vote={}
Object.keys(gameObj.accuseLog).forEach(accuser => {
vote[accuser]=0
Object.values(gameObj.accuseLog).forEach(accused => {
if (accuser == accused) {
vote[accuser]=vote[accuser]+1
}
})
})
sorted = Object.keys(vote).sort(function(a,b){return vote[a]-vote[b]})
fired = sorted[sorted.length-1]
gameObj.players.forEach(element => {
if (element.name == fired ) {
element.status="Fired"
game.roundStatus(gameID)
}
})
}
// 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 == 2) {
return game.winner(gameID)
}
//change status of the user to fired
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
return ({log: gameObj.distributionLog})
},
winner: async (gameID) => {
winner=""
gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
gameObj = gameObj[0]
// Get the company with the highest value
x=gameObj.charities.sort((a, b) => parseFloat(a.capital) - parseFloat(b.capital));
winCharity=x[gameObj.charities.length-1].charityID
gameObj.secretLink.forEach(element => {
if (element.charity == winCharity) {
winner=element.owner
}
})
// Get the ID
// Tie the ID to the player using secret link
//Declare the winner
gameObj.status="Finished"
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
return {winner: winner}
},
roundStatus: async (gameID) => {
gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
gameObj = gameObj[0]
console.log(gameObj.status)
if (typeof gameObj.status == "lobby" || gameObj.status == "accuse") {
gameObj.status = "distribution"
} else {
gameObj.status = "accuse"
}
console.log(gameObj.status)
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
},
}
module.exports = game