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.

372 lines
11 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
}
async function check (gameID,ownerID) {
gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
gameObj = gameObj[0]
name = await mongo.get("embezzle", "players", {
"_id": ownerID
})
name = name[0].name
found=0
gameObj.players.forEach(element => {
if (element.name == name) {
console.log(name)
console.log(element.name)
found=1
}
})
console.log("Found: ",found)
return found
}
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"
gameObj.round=1
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 = 100000
element.status = "Employed"
})
//update game
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
},
reset: async (gameID) => {
let gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
gameObj=gameObj[0]
gameObj.players.forEach(element =>{
fundvar=100000
roundvar=fundvar+(fundvar/(gameObj.round))
if (element.status != "Fired"){
element.funds=roundvar
} else {element.funds=roundvar/gameObj.players.length}
})
gameObj.accuseLog={}
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
mychar=""
secret.forEach(char => {
if (char.owner == name) {
mychar=char.charity
}
})
charname=""
gameObj[0].charities.forEach(element => {
if (element.charityID == mychar) {
charname=element.name
}
})
chairtyObj = {
charity: charname
}
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 ({
playersArr: 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
players.forEach(element => {
if (element.name == name) {
r = {funds: element.funds}
}
})
return r
},
distribution: async (gameID, ownerID, charityID, amount) => {
if (await check(gameID, ownerID) == 0) {
return ({Error: "You're not a player"})
}
//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"})
} else {
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)
}
})
//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
if (amount > 0 ){
gameObj.distributionLog.push({
donator: player.name,
amount: amount,
charity: element.name
})
}
}
})
reaminingFunding = 0
gameObj.players.forEach(element => {
reaminingFunding=reaminingFunding+element.funds
})
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
if (reaminingFunding == 0) {
game.roundStatus(gameID)
}
return ({log: gameObj.distributionLog})
}
},
accuse: async (gameID, ownerID, victim) => {
if (await check(gameID, ownerID) == 0) {
return {Error: "You're not a player"}
}
//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]})
console.log("Sorted: ",sorted)
fired = sorted[sorted.length-1]
console.log("Fired: ",fired)
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 < 3) {
return game.winner(gameID)
}
//change status of the user to fired
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
return ({log: gameObj.accuseLog})
},
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"
gameObj.winner=winner
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
return {winner: winner}
},
roundStatus: async (gameID) => {
gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
gameObj = gameObj[0]
if (typeof gameObj.status == "Lobby" || gameObj.status == "Accuse") {
gameObj.status = "Distribution"
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
game.reset(gameID)
} else {
gameObj.status = "Accuse"
}
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
},
roundStatusInfo: async (gameID) => {
gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
gameObj = gameObj[0]
return ({status: gameObj.status})
},
accuseInfo: async (gameID) => {
gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
gameObj = gameObj[0]
if (typeof gameObj.winner != "undefined") {
return ({winner: gameObj.winner})
} else {
return ({log: gameObj.accuseLog})
}
},
distributionInfo: async (gameID) => {
gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
gameObj = gameObj[0]
return ({log: gameObj.distributionLog})
},
}
module.exports = game