@ -15,6 +15,29 @@ function shuffle(arr) {
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 ) => {
@ -26,7 +49,8 @@ const game = {
gameObj . charities = [ ]
gameObj . distributionLog = [ ]
gameObj . accuseLog = { }
gameObj . status = "distribution"
gameObj . status = "Distribution"
gameObj . round = 1
for ( let index = 0 ; index < gameObj . players . length ; index ++ ) {
gameObj . charities . push ( {
charityID : uuidv4 ( ) ,
@ -48,9 +72,8 @@ const game = {
shuffle ( gameObj . charities )
//set board funds
gameObj . players . forEach ( element => {
element . funds = 100000 00
element . funds = 100000
element . status = "Employed"
console . log ( element )
} )
//update game
mongo . update ( "embezzle" , "gamelist" , {
@ -58,6 +81,25 @@ const game = {
} , 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
@ -67,25 +109,21 @@ const game = {
"gameID" : gameID
} )
secret = gameObj [ 0 ] . secretLink
let mine = ""
mychar = ""
secret . forEach ( char => {
if ( char . owner == name ) {
mychar = char . charity
}
} )
charname = ""
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
}
}
} )
if ( element . charityID == mychar ) {
charname = element . name
}
} )
chairtyObj = {
charity : min e
charity : charname
}
return chairtyObj
} ,
@ -105,7 +143,7 @@ const game = {
} )
players = gameObj [ 0 ] . players
return ( {
charatie sArr: players
player sArr: players
} )
} ,
@ -119,7 +157,6 @@ const game = {
"gameID" : gameID
} )
players = players [ 0 ] . players
result = ""
players . forEach ( element => {
if ( element . name == name ) {
r = { funds : element . funds }
@ -128,6 +165,10 @@ const game = {
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 )
@ -138,57 +179,62 @@ const game = {
gameObj = gameObj [ 0 ]
// console.log(gameObj)
if ( gameObj . status != " d istribution") {
if ( gameObj . status != " D istribution") {
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 )
} 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
}
} )
//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
} )
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 )
}
} )
reaminingFunding = 0
gameObj . players . forEach ( element => {
reaminingFunding = reaminingFunding + element . funds
} )
if ( reaminingFunding == 0 ) {
game . roundStatus ( gameID )
return ( { log : gameObj . distributionLog } )
}
mongo . update ( "embezzle" , "gamelist" , {
"gameID" : gameID
} , gameObj )
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
@ -198,11 +244,10 @@ const game = {
"gameID" : gameID
} )
gameObj = gameObj [ 0 ]
if ( gameObj . status != " a ccuse") {
if ( gameObj . status != " A ccuse") {
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
@ -221,7 +266,9 @@ const game = {
} )
} )
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"
@ -237,7 +284,7 @@ const game = {
employedCount ++
}
} )
if ( employedCount == 2 ) {
if ( employedCount < 3 ) {
return game . winner ( gameID )
}
@ -245,7 +292,7 @@ const game = {
mongo . update ( "embezzle" , "gamelist" , {
"gameID" : gameID
} , gameObj )
return ( { log : gameObj . distribution Log} )
return ( { log : gameObj . accuse Log} )
} ,
winner : async ( gameID ) => {
@ -269,6 +316,7 @@ const game = {
//Declare the winner
gameObj . status = "Finished"
gameObj . winner = winner
mongo . update ( "embezzle" , "gamelist" , {
"gameID" : gameID
} , gameObj )
@ -279,18 +327,46 @@ const game = {
"gameID" : gameID
} )
gameObj = gameObj [ 0 ]
console . log ( gameObj . status )
if ( typeof gameObj . status == "lobby" || gameObj . status == "accuse" ) {
gameObj . status = "distribution"
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"
gameObj . status = " A ccuse"
}
console . log ( gameObj . status )
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