game bar accuse, untested, buggy, vulnerable

This commit is contained in:
nannal 2020-02-17 00:59:33 +02:00
parent 2e5f7eafdc
commit 8708bac1a2
6 changed files with 406 additions and 55 deletions

39
charitygen.js Normal file
View File

@ -0,0 +1,39 @@
var randomstring = require("randomstring");
function charityGen() {
ffnames = ["Quren","Wendy","Erica","Rebecca","Tabitha","Yennifer","Ulanda","Irene","Omara","Penelope","Agne","Sarah","Diane","Febroch","Gillian","Hannah","Jessica","Karen","Laura","Zoe","Xephod","Caitlyn","Violet","Betty","Noreen","Martha"]
mfnames = ["Qwin","William","Eric","Robert","Thomas","Yakov","Umber","Ian","Odin","Peter","Adam","Sam","Daniel","Fredrich","Geoff","Hubert","Jack","Kevin","Liam","Zack","Xaphod","Carl","Victor","Benjamin","Nathan","Mark"]
lnames = ["Qwerner","Windburg","Eoinson","Rasputin","Thunburgler","Yankovic","Udenminster","Irason","Onglangling","Petrovic","Ainsly","Sumpworth","Donnington","Fontain","Goodly","Henderson","Jillersworth","Kurteen","Luth","Zundar","Xembi","Coats","Veloure","Bunglesworth","Nempham","Macklesmith"]
establishments = ["Trust","Foundation","Service","Association","Fund","Philanthropic Enterprise","Centre","Endowment","Charity","Network","Service"]
acronym = randomstring.generate({length:3,charset: 'alphabetic'}).toUpperCase()
rand = Math.floor((Math.random() * 7) + 1)
charityString=""
switch(rand) {
case 1 :
charityString = "The "+ffnames[Math.floor(Math.random() * ffnames.length)]+" "+establishments[Math.floor(Math.random() * establishments.length)]
break
case 2:
charityString = "The "+ffnames[Math.floor(Math.random() * ffnames.length)]+" "+ lnames[Math.floor(Math.random() * lnames.length)] +" "+establishments[Math.floor(Math.random() * establishments.length)]
break
case 3:
charityString = "The "+mfnames[Math.floor(Math.random() * mfnames.length)]+" "+establishments[Math.floor(Math.random() * establishments.length)]
break
case 4:
charityString = "The "+mfnames[Math.floor(Math.random() * mfnames.length)]+" "+ lnames[Math.floor(Math.random() * lnames.length)] +" "+establishments[Math.floor(Math.random() * establishments.length)]
break
case 5:
charityString = "The "+lnames[Math.floor(Math.random() * lnames.length)] +" "+establishments[Math.floor(Math.random() * establishments.length)]
break
default:
charityString = acronym+" "+establishments[Math.floor(Math.random() * establishments.length)]
}
return charityString
}
module.exports = charityGen

206
game.js Normal file
View File

@ -0,0 +1,206 @@
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

View File

@ -1,5 +1,5 @@
const lobby = require("./lobby.js") const lobby = require("./lobby.js")
const game = require("./game.js")
const express = require('express') const express = require('express')
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const app = express() const app = express()
@ -19,16 +19,15 @@ app.use(bodyParser.json(),function (req, res, next) {
app.get('/lobby/list' , async function (req, res) { app.get('/lobby/list' , async function (req, res) {
lobby.list().then(r => { lobby.list().then(r => {
console.log(r)
res.send(JSON.stringify({gamesArr: r })) res.send(JSON.stringify({gamesArr: r }))
}) })
}) })
//get game details //get game details
app.get('/lobby/details/:gameID', function (req, res) { app.get('/lobby/details/:gameID', function (req, res) {
let obj=req.body lobby.details(req.params.gameID).then(r => {
lobby.details(obj.gameID).then(r => { res.send(JSON.stringify(r[0]))
res.send(JSON.stringify(r))
}) })
}) })
@ -36,7 +35,6 @@ app.get('/lobby/details/:gameID', function (req, res) {
//post register player //post register player
app.post('/lobby/register', function (req, res) { app.post('/lobby/register', function (req, res) {
let obj=req.body let obj=req.body
console.log(req.body)
response=JSON.stringify(lobby.register(obj.name)) response=JSON.stringify(lobby.register(obj.name))
res.send(response) res.send(response)
}) })
@ -51,8 +49,10 @@ app.post('/lobby/join', function (req, res) {
//get an empty game made //get an empty game made
app.post('/lobby/make', function (req, res) { app.post('/lobby/make', function (req, res) {
let obj=req.body let obj=req.body
response=JSON.stringify(lobby.make(obj.ownerID,obj.seats)) lobby.make(obj.ownerID,obj.seats).then(result => {
res.send(response) res.send(JSON.stringify(result))
})
}) })
//start a game //start a game
@ -61,21 +61,49 @@ app.post('/lobby/start', function (req, res) {
response=JSON.stringify(lobby.start(obj.ownerID, obj.gameID)) response=JSON.stringify(lobby.start(obj.ownerID, obj.gameID))
res.send(response) res.send(response)
}) })
//gamelevel (??) //myCharity
app.post('/game/:gameID/charity', function (req, res) {
game.myCharity(req.params.gameID, req.body.ownerID).then(r => {
res.send(JSON.stringify(r))
})
})
// charityList
app.get('/game/:gameID/charityList', function (req, res) {
game.charityList(req.params.gameID).then(r => {
res.send(JSON.stringify(r))
})
})
//playerList
app.get('/game/:gameID/playerList', function (req, res) {
game.playerList(req.params.gameID).then(r => {
res.send(JSON.stringify(r))
})
})
// boardFunding
app.get('/game/:gameID/boardFunding', function (req, res) {
game.boardFunding(req.params.gameID).then(r => {
res.send(JSON.stringify(r))
})
})
// distribution
app.post('/game/:gameID/distribution', function (req, res) {
game.distribution(req.params.gameID, req.body.ownerID, req.body.charityID, req.body.amount).then(r => {
res.send(JSON.stringify(r))
})
})
// accuse
app.post('/game/:gameID/accuse', function (req, res) {
game.accuse(req.params.gameID, req.body.ownerID, req.body.player).then(r => {
res.send(JSON.stringify(r))
})
})
// roundStatus -- this should not be public wtf?
// app.get('/game/:gameID/roundStatus', function (req, res) {
// game.roundStatus(req.params.gameID).then(r => {
// res.send(JSON.stringify(r))
// })
// })
// get this game players
// get charity details
// get board funding details (how much money team has to work with)
// post distribution
// post accusation
// get winner
// get round status
console.log("Up") console.log("Up")
app.listen(3000) app.listen(3000)

114
lobby.js
View File

@ -1,46 +1,110 @@
var mongo = require("./mongoHelp.js") var mongo = require("./mongoHelp.js")
const uuidv4 = require('uuid/v4'); const uuidv4 = require('uuid/v4')
const game = require("./game.js")
var lobby = { var lobby = {
list: async () => { list: async () => {
result = await mongo.get("embezzle", "gamelist", {}) result = await mongo.get("embezzle", "gamelist", {})
return(result) 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)
}, },
details: async (gameID) => { details: async (search) => {
result = await mongo.get("embezzle","gamelist",{"gameID":gameID}) result = await mongo.get("embezzle", "gamelist", {
return (result) 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)
}, },
register: (name) => { register: (name) => {
id = uuidv4() id = uuidv4()
mongo.put("embezzle","players",{_id:id,name:name}) mongo.put("embezzle", "players", {
console.log("yoohoo") _id: id,
return({ownerID:id}) name: name
})
return ({
ownerID: id
})
}, },
make: (ownerID,seats) => { make: async (ownerID, seats) => {
gameID = uuidv4() gameID = uuidv4()
//todo write charity name generator //todo write charity name generator
name="Charity Name Example" name = await mongo.get("embezzle", "players", {
mongo.put("embezzle","gamelist",{gameID:gameID,ownerID:ownerID,name:name,seats:seats, status:"lobby", players: []}) "_id": ownerID
// this.join(ownerID,gameID) })
name = name[0].name
return({gameID:gameID}) mongo.put("embezzle", "gamelist", {
}, gameID: gameID,
join: (ownerID,gameID) => { ownerID: ownerID,
mongo.get("embezzle","gamelist",{"gameID":gameID}).then(value => { name: name,
console.log(value[0]) seats: seats,
value[0].players.push(ownerID) status: "lobby",
mongo.update("embezzle","gamelist",{"gameID":gameID},value[0]) players: []
})
lobby.join(ownerID, gameID)
return ({
gameID: gameID
})
// return({gameID:gameID})
},
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
}) })
return({gameID:gameID})
}, },
start: (ownerID, gameID) => { start: (ownerID, gameID) => {
mongo.get("embezzle","gamelist",{"gameID":gameID}).then(value => { mongo.get("embezzle", "gamelist", {
console.log(value[0]) "ownerID": ownerID,
"gameID": gameID
}).then(value => {
value[0].status = "playing" value[0].status = "playing"
mongo.update("embezzle","gamelist",{"gameID":gameID,"ownerID":ownerID}, value[0]) mongo.update("embezzle", "gamelist", {
"gameID": gameID,
"ownerID": ownerID
}, value[0])
game.init(gameID)
}) })
return(gameID) return ({
gameID: gameID
})
}, },
} }

13
package-lock.json generated
View File

@ -58,6 +58,11 @@
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
}, },
"array-uniq": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/array-uniq/-/array-uniq-1.0.2.tgz",
"integrity": "sha1-X8w3OSB3VyPP1k1lxkvvU7+eum0="
},
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
@ -892,6 +897,14 @@
"resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
"integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==" "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ=="
}, },
"randomstring": {
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/randomstring/-/randomstring-1.1.5.tgz",
"integrity": "sha1-bfBij3XL1ZMpMNn+OrTpVqGFGMM=",
"requires": {
"array-uniq": "1.0.2"
}
},
"range-parser": { "range-parser": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",

View File

@ -16,6 +16,7 @@
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"express": "^4.17.1", "express": "^4.17.1",
"mongodb": "^3.4.1", "mongodb": "^3.4.1",
"randomstring": "^1.1.5",
"uuid": "^3.4.0" "uuid": "^3.4.0"
}, },
"devDependencies": { "devDependencies": {