diff --git a/charitygen.js b/charitygen.js new file mode 100644 index 0000000..9e36a2f --- /dev/null +++ b/charitygen.js @@ -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 \ No newline at end of file diff --git a/game.js b/game.js new file mode 100644 index 0000000..7e2e929 --- /dev/null +++ b/game.js @@ -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 \ No newline at end of file diff --git a/index.js b/index.js index b9ff22f..5b451f4 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ const lobby = require("./lobby.js") - +const game = require("./game.js") const express = require('express') const bodyParser = require('body-parser'); const app = express() @@ -19,16 +19,15 @@ app.use(bodyParser.json(),function (req, res, next) { app.get('/lobby/list' , async function (req, res) { lobby.list().then(r => { - console.log(r) + res.send(JSON.stringify({gamesArr: r })) }) }) //get game details app.get('/lobby/details/:gameID', function (req, res) { - let obj=req.body - lobby.details(obj.gameID).then(r => { - res.send(JSON.stringify(r)) + lobby.details(req.params.gameID).then(r => { + res.send(JSON.stringify(r[0])) }) }) @@ -36,7 +35,6 @@ app.get('/lobby/details/:gameID', function (req, res) { //post register player app.post('/lobby/register', function (req, res) { let obj=req.body - console.log(req.body) response=JSON.stringify(lobby.register(obj.name)) res.send(response) }) @@ -51,8 +49,10 @@ app.post('/lobby/join', function (req, res) { //get an empty game made app.post('/lobby/make', function (req, res) { let obj=req.body - response=JSON.stringify(lobby.make(obj.ownerID,obj.seats)) - res.send(response) + lobby.make(obj.ownerID,obj.seats).then(result => { + res.send(JSON.stringify(result)) + }) + }) //start a game @@ -61,21 +61,49 @@ app.post('/lobby/start', function (req, res) { response=JSON.stringify(lobby.start(obj.ownerID, obj.gameID)) 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") app.listen(3000) \ No newline at end of file diff --git a/lobby.js b/lobby.js index 4a150d5..1baee05 100644 --- a/lobby.js +++ b/lobby.js @@ -1,46 +1,110 @@ 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 () => { - result = await mongo.get("embezzle","gamelist",{}) - return(result) + result = await mongo.get("embezzle", "gamelist", {}) + 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) => { - result = await mongo.get("embezzle","gamelist",{"gameID":gameID}) - return (result) + details: async (search) => { + result = await mongo.get("embezzle", "gamelist", { + 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) => { - id=uuidv4() - mongo.put("embezzle","players",{_id:id,name:name}) - console.log("yoohoo") - return({ownerID:id}) - }, - make: (ownerID,seats) => { - gameID=uuidv4() - //todo write charity name generator - name="Charity Name Example" - mongo.put("embezzle","gamelist",{gameID:gameID,ownerID:ownerID,name:name,seats:seats, status:"lobby", players: []}) - // this.join(ownerID,gameID) - - return({gameID:gameID}) - }, - join: (ownerID,gameID) => { - mongo.get("embezzle","gamelist",{"gameID":gameID}).then(value => { - console.log(value[0]) - value[0].players.push(ownerID) - mongo.update("embezzle","gamelist",{"gameID":gameID},value[0]) + id = uuidv4() + mongo.put("embezzle", "players", { + _id: id, + name: name + }) + return ({ + ownerID: id + }) + }, + make: async (ownerID, seats) => { + gameID = uuidv4() + //todo write charity name generator + name = await mongo.get("embezzle", "players", { + "_id": ownerID + }) + name = name[0].name + mongo.put("embezzle", "gamelist", { + gameID: gameID, + ownerID: ownerID, + name: name, + seats: seats, + status: "lobby", + 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) => { - mongo.get("embezzle","gamelist",{"gameID":gameID}).then(value => { - console.log(value[0]) - value[0].status="playing" - mongo.update("embezzle","gamelist",{"gameID":gameID,"ownerID":ownerID}, value[0]) + mongo.get("embezzle", "gamelist", { + "ownerID": ownerID, + "gameID": gameID + }).then(value => { + value[0].status = "playing" + mongo.update("embezzle", "gamelist", { + "gameID": gameID, + "ownerID": ownerID + }, value[0]) + game.init(gameID) + }) + + return ({ + gameID: gameID }) - - return(gameID) }, } diff --git a/package-lock.json b/package-lock.json index d3883db..6306252 100644 --- a/package-lock.json +++ b/package-lock.json @@ -58,6 +58,11 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "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": { "version": "1.0.0", "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", "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": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", diff --git a/package.json b/package.json index 9b26e2b..0bb30ac 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "body-parser": "^1.19.0", "express": "^4.17.1", "mongodb": "^3.4.1", + "randomstring": "^1.1.5", "uuid": "^3.4.0" }, "devDependencies": {