This commit is contained in:
nannal 2020-05-02 21:09:22 +03:00
parent 2129889248
commit 457182504f
11 changed files with 570 additions and 127 deletions

View File

@ -2,8 +2,8 @@
var randomstring = require("randomstring"); var randomstring = require("randomstring");
function charityGen() { 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"] ffnames = ["Quinn","Wendy","Erica","Rebecca","Tabitha","Yennifer","Ulanda","Irene","Omara","Penelope","Agne","Sarah","Diane","Febroch","Gillian","Hannah","Jessica","Karen","Laura","Zoe","Xephodette","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"] mfnames = ["Quintin","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"] 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"] establishments = ["Trust","Foundation","Service","Association","Fund","Philanthropic Enterprise","Centre","Endowment","Charity","Network","Service"]
acronym = randomstring.generate({length:3,charset: 'alphabetic'}).toUpperCase() acronym = randomstring.generate({length:3,charset: 'alphabetic'}).toUpperCase()

View File

@ -0,0 +1,7 @@
FROM nginx
ADD ./ /usr/share/nginx/html
LABEL traefik.port="80"
EXPOSE 80
CMD ["nginx", "-g", "'daemon off'"]

View File

@ -1,12 +1,109 @@
body{ body{
margin:40px auto; margin:40px auto;
max-width:650px; max-width:90%;
line-height:1.6; line-height:1.6;
font-size:18px; font-size:18px;
color:#444; color:#444;
padding:0 10px; padding:0 10px;
font-family: monospace;
} }
h1,h2,h3{ h1,h2,h3{
line-height:1.2 line-height:1.2
} }
.donateInput{
width: 35%;
}
select{
height: auto;
width: 80%;
font-size: large;
}
input{
height: auto;
width: 80%;
font-size: large;
}
button{
height: auto;
width: 18%;
font-size: large;
}
#controls{
width: 100%;
}
#Reigster-Container{
visibility: visible;
width: 100%;
}
#Make-Container{
visibility: hidden;
scale: 0;
}
#Join-Container{
visibility: hidden;
scale: 0;
}
#Start-Container{
visibility: hidden;
scale: 0;
}
#Donate-Container{
visibility: hidden;
scale: 0;
}
#Accuse-Container{
visibility: hidden;
scale: 0;
}
#messages{
visibility: hidden;
scale: 0;
}
#games{
visibility: hidden;
scale: 0;
}
#gameInfo{
visibility: hidden;
scale: 0;
}
#myCharity{
visibility: hidden;
scale: 0;
}
#charityStatus{
visibility: hidden;
scale: 0;
}
#boardFunding{
visibility: hidden;
scale: 0;
}
#transactionLog{
visibility: hidden;
scale: 0;
}
#accusationLog{
visibility: hidden;
scale: 0;
}

View File

@ -4,69 +4,117 @@
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"> <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding"/> <meta content="utf-8" http-equiv="encoding"/>
<script src="index.js"></script> <script src="index.js"></script>
<script src="jq.js"></script>
<script type="text/javascript">
//sorry
var _paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
_paq.push(["setCookieDomain", "*.nannal.com"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//matomo.nannal.com/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '1']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
(function(f, a, t, h, o, m){
a[h]=a[h]||function(){
(a[h].q=a[h].q||[]).push(arguments)
};
o=f.createElement('script'),
m=f.getElementsByTagName('script')[0];
o.async=1; o.src=t; o.id='fathom-script';
m.parentNode.insertBefore(o,m)
})(document, window, '//fathom.nannal.com/tracker.js', 'fathom');
fathom('set', 'siteId', 'QYBFW');
fathom('trackPageview');
</script>
<noscript><p><img src="//matomo.nannal.com/matomo.php?idsite=1&amp;rec=1" style="border:0;" alt="" /></p></noscript>
</head> </head>
<body> <body>
<div class="controls"> <h1>Embezzle</h1>
<div class="Reigster-Container"> <div id="controls">
</br>Register
<div id="Reigster-Container">
<div id="faq">
<h2>How To Play</h2>
By joining a game you become apointed to the board of a multinational company who in the name of extreme altruism (and for some significant tax breaks) must distribute some of their immesne profits to lowly charities. As the cunning fox you are, you've setup your own charity and must now redirect as much of this firehose of funds from silly rinky-dink operations like oxfam to your own back pocket.
</br>
However, you can't be <i>entierly</i> blatant about it as, after each quaters funds are distributed, a board member must be fired as they are undoubtedly performing the heinous crime of cold-heartedly stealing what few morsels of funding the benevolent company has seen fit to bestow on the worlds most needy and vulnerable. <b>The swines!</b>
<ul>Join a board of directors</ul>
<ul>Distribute funds to yourown and other's charities</ul>
<ul>Vote out one of your fellow board members</ul>
<ul>Have the most capital in your charity when only two members remain on the board to win</ul>
</div>
</br>Register a player</br>
<input id="register" type="text" placeholder="Name"/> <button onclick="register()">Register</button> <input id="register" type="text" placeholder="Name"/> <button onclick="register()">Register</button>
</div> </div>
<div class="Make-Container"> <div id="Make-Container">
</br>Make </br>Make A game (Select a number of player slots)</br>
<input id="makeSeats" type="number"/> <button onclick="make()">Make</button> <input id="makeSeats" type="number"/> <button onclick="make()">Make</button>
</div> </div>
<div class="Join-Container"> <div id="Join-Container">
</br>Join </br>Join a game (Using a GameID)</br>
<input id="join" type="text" placeholder="Name"/> <button onclick="join()">Join</button> <input id="join" type="text" placeholder="Name"/> <button onclick="join()">Join</button>
</div> </div>
<div class="Start-Container"> <div id="Start-Container">
</br>Start </br>Start the game!</br>
<button onclick="start()">Start</button> <button onclick="start()">Start</button>
</div> </div>
<div class="Donate-Container"> <div id="Donate-Container">
</br>Donate </br>Donate</br>
<select id="donate" name="list"> <select id="donate" name="list" class="donateInput">
<option value="x">x</option> <option value="stringthatbrickstheserver">If you can see this, you're being naughty</option>
</select> </select>
<input id="amount" type="number"> <input id="amount" type="number" class="donateInput">
<button onclick="donate()">Donate</button> <button onclick="donate()">Donate</button>
</div> </div>
<div class="Accuse-Container"> <div id="Accuse-Container">
</br>Accuse </br>Accuse a player of embezzlement</br>
<input id="accuse" type="text" placeholder="Name"/> <button onclick="accuse()">Accuse</button> <select id="accuse" name="list" class="accuseInput">
<option value="stringthatbrickstheserver">If you can see this, you're being naughty</option>
</select>
<button onclick="accuse()">Accuse</button>
</div> </div>
</div> </div>
<div id="info"> <div id="info">
<div id="gameID"></div>
<div id="messages"></div> <div id="messages"></div>
<div id="games"></div> <div id="games"></div>
<div id="gameInfo"></div> <div id="gameInfo"></div>
<div id="playerInfo"></div>
<div id="myCharity"></div> <div id="myCharity"></div>
<div id="charityStatus"></div> <div id="charityStatus"></div>
<div id="boardFunding"></div> <div id="boardFunding"></div>
<div id="transactionLog"></div> <div id="transactionLog"></div>
<div id="accusationLog"></div> <div id="accusationLog"></div>
<div id="messages"></div>
</div> </div>
</body> </body>

View File

@ -1,6 +1,5 @@
// Global Vars // Global Vars
serverAddr="https://embezzle-server.nannal.com"
serverAddr="http://localhost:3000"
ownerID='' ownerID=''
// ownerID='6298bc89-41cb-4cc7-84cd-7fd8e419f263' // ownerID='6298bc89-41cb-4cc7-84cd-7fd8e419f263'
gameID='' gameID=''
@ -24,6 +23,10 @@ function register(){
//hide register //hide register
//show make & join //show make & join
//show list of games //show list of games
cssHide("Reigster-Container")
cssShow("Make-Container")
cssShow("Join-Container")
listGames() listGames()
}) })
.catch((error) => { .catch((error) => {
@ -37,7 +40,8 @@ function listGames(){
return response.json(); return response.json();
}) })
.then((data) => { .then((data) => {
document.getElementById('messages').value='data.gamesArr' gamesFormat(data)
cssShow("games")
console.log(data); console.log(data);
}); });
} }
@ -60,6 +64,16 @@ function make(){
// console.log(data) // console.log(data)
gameID=data.gameID gameID=data.gameID
gameData(gameID) gameData(gameID)
document.getElementById("gameID").innerHTML="<h2>Your gameID is:"+gameID+"</h2><p> share the gameID with any users you want to join your game</p>"
cssHide("games")
cssHide("Make-Container")
cssHide("Join-Container")
cssShow("messages")
cssShow("gameInfo")
cssShow("playerInfo")
cssShow("Start-Container")
}) })
.catch((error) => { .catch((error) => {
@ -70,6 +84,15 @@ function make(){
function join(){ function join(){
console.log("join") console.log("join")
gameID=document.getElementById('join').value gameID=document.getElementById('join').value
gameData(gameID)
cssHide("games")
cssHide("Make-Container")
cssHide("Join-Container")
cssShow("messages")
cssShow("gameInfo")
cssShow("playerInfo")
const data = { const data = {
ownerID: ownerID, ownerID: ownerID,
gameID: gameID }; gameID: gameID };
@ -83,11 +106,19 @@ function join(){
}) })
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
document.getElementById("gameID").innerHTML="<h2>Your gameID is:"+gameID+"</h2><p> share the gameID with any users you want to join your game</p>"
// console.log(data) // console.log(data)
gameData(gameID)
}) })
.catch((error) => { .catch((error) => {
console.error('Error:', error); console.error('Error:', error);
cssShow("games")
cssShow("Make-Container")
cssShow("Join-Container")
cssHide("messages")
cssHide("gameInfo")
}); });
} }
@ -105,14 +136,18 @@ function gameData(gameID) {
gameData(gameID) gameData(gameID)
clearInterval(checkIfStarted) clearInterval(checkIfStarted)
}, 1000) }, 1000)
if (document.getElementById("messages").innerText != "Game Name: "+data.name+", GameID: "+data.gameID) { html="Waiting For "+data.name+" to start the game, "+data.players.length+"/"+data.seats+" seats are full</br>"
document.getElementById("messages").innerText="Game Name: "+data.name+", GameID: "+data.gameID data.players.forEach(element => {
html=html+"Player: "+element.name+"</br>"
})
if (document.getElementById("messages").innerHTML != html) {
document.getElementById("messages").innerHTML=html
} }
}) })
} }
function start(){ function start(){
console.log("start")
const data = { const data = {
ownerID: ownerID, ownerID: ownerID,
gameID: gameID }; gameID: gameID };
@ -127,7 +162,7 @@ function start(){
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
console.log(data) console.log(data)
cssHide("Start-Container")
}) })
.catch((error) => { .catch((error) => {
console.error('Error:', error); console.error('Error:', error);
@ -156,6 +191,7 @@ function donate(){
}) })
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
donateFormat(data)
// console.log(data) // console.log(data)
// gameData(gameID) // gameData(gameID)
}) })
@ -182,7 +218,7 @@ function accuse(){
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
// console.log(data) // console.log(data)
document.getElementById("accuseLog").innerText=data accuseFormat(data)
// gameData(gameID) // gameData(gameID)
}) })
.catch((error) => { .catch((error) => {
@ -219,7 +255,7 @@ function status() {
//Quick Recurring updates //Quick Recurring updates
setInterval(function(){ statusint = setInterval(function(){
fetch(serverAddr+'/game/'+gameID+'/boardFunding', { fetch(serverAddr+'/game/'+gameID+'/boardFunding', {
method: 'POST', method: 'POST',
headers: { headers: {
@ -249,9 +285,9 @@ function status() {
}); });
if (document.getElementById("donate").innerHTML != dropDownList){ if (document.getElementById("donate").innerHTML != dropDownList){
document.getElementById("donate").innerHTML=dropDownList document.getElementById("donate").innerHTML=dropDownList
statusObj.charitylist=data // statusObj.charitylist=data
} }
document.getElementById("charityStatus").innerHTML=charityStatusFormat(data) charityStatusFormat(data)
}); });
@ -260,7 +296,37 @@ function status() {
return response.json(); return response.json();
}) })
.then((data) => { .then((data) => {
statusObj.playerlist=data playerListFormat(data)
// statusObj.playerlist=data
// console.log(data);
});
fetch(serverAddr+'/game/'+gameID+'/accuseinfo')
.then((response) => {
return response.json();
})
.then((data) => {
accuseFormat(data)
// statusObj.playerlist=data
// console.log(data);
});
fetch(serverAddr+'/game/'+gameID+'/distributioninfo')
.then((response) => {
return response.json();
})
.then((data) => {
donateFormat(data)
// statusObj.playerlist=data
// console.log(data);
});
fetch(serverAddr+'/game/'+gameID+'/statusinfo')
.then((response) => {
return response.json();
})
.then((data) => {
statusFormat(data)
// statusObj.playerlist=data
// console.log(data); // console.log(data);
}); });
// fdocument.getElementById("info").innerText=prettyprint(statusObj) // fdocument.getElementById("info").innerText=prettyprint(statusObj)
@ -276,12 +342,127 @@ function myCharityFormat(data) {
function charityStatusFormat(data) { function charityStatusFormat(data) {
html="" html=""
data.forEach(element => { data.charatiesArr.forEach(element => {
html=html+"<b>"+element.name+":</b> "+element.capital html=html+"<b>"+element.name+":</b> "+element.capital+"</br>"
}) })
return html if (document.getElementById("charityStatus").innerHTML != html) {
document.getElementById("charityStatus").innerHTML=html
}
} }
function boardFundingFormat(data) { function boardFundingFormat(data) {
if (document.getElementById("boardFunding").innerHTML != "<b>My Funds: </b>"+data.funds) {
document.getElementById("boardFunding").innerHTML= "<b>My Funds: </b>"+data.funds
}
}
function donateFormat(data) {
html=""
data.log.forEach(element => {
html=html+element.donator+", donated "+element.amount+" to "+element.charity+"</br>"
})
if(document.getElementById("transactionLog").innerHTML != html){
document.getElementById("transactionLog").innerHTML=html
}
}
function accuseFormat(data) {
html=""
if(typeof data.winner != "undefined"){
document.getElementById("accusationLog").innerHTML="<h1>"+data.winner+" has won the game</h1>"
clearInterval(statusint)
}
else{
accusers=Object.keys(data.log)
accusers.forEach(element => {
html=html+element+" accuses "+data.log[element]+"</br>"
})
if(document.getElementById("accusationLog").innerHTML != html){
document.getElementById("accusationLog").innerHTML=html
}
}
}
function gamesFormat(data) {
html="<h4>Games:</h4></br>"
data.gamesArr.forEach(element => {
html=html+element.gameID+", "+element.name+"</br>"
})
if (document.getElementById("games").innerHTML != html) {
document.getElementById("games").innerHTML = html
}
}
function playerListFormat(data) {
html=""
accuseListFormat(data.playersArr)
data.playersArr.forEach(element => {
html=html+"<b>"+element.name+"</b>: Funds "+element.funds+", Status: "+element.status+"</br>"
})
if (document.getElementById("playerInfo").innerHTML != html){
document.getElementById("playerInfo").innerHTML=html
}
}
function statusFormat(data) {
if (document.getElementById("gameInfo").innerHTML != "<h1>Round Status: "+data.status+"</h1>") {
document.getElementById("gameInfo").innerHTML="<h1>Round Status: "+data.status+"</h1>"
}
if (data.status == "Distribution") {
cssHide("messages")
cssShow("gameInfo")
cssShow("myCharity")
cssShow("charityStatus")
cssShow("boardFunding")
cssShow("Donate-Container")
cssShow("transactionLog")
cssHide("messages")
cssHide("accusationLog")
cssHide("Accuse-Container")
}
else {
cssHide("Donate-Container")
cssShow("accusationLog")
cssShow("Accuse-Container")
}
}
function accuseListFormat(data){
dropDownList=""
data.forEach(element => {
dropDownList=dropDownList+'<option value="'+element.name+'">'+element.name+'</option>'
});
if (document.getElementById("accuse").innerHTML != dropDownList){
document.getElementById("accuse").innerHTML=dropDownList
// statusObj.charitylist=data
}
}
function cssHide(id){
if (document.getElementById(id).style.visibility != "Hidden") {
document.getElementById(id).style.lineHeight=0
document.getElementById(id).style.margin=0
document.getElementById(id).style.fontSize=0
document.getElementById(id).style.visibility="Hidden"
document.getElementById(id).style.size="0px"
document.getElementById(id).style.zIndex="100"
document.getElementById(id).style.scale="0"
}
}
function cssShow(id){
if (document.getElementById(id).style.visibility != "Visible") {
document.getElementById(id).style.lineHeight=1.6
document.getElementById(id).style.fontSize="Large"
document.getElementById(id).style.visibility="Visible"
document.getElementById(id).style.size="Auto"
document.getElementById(id).style.zIndex="0"
document.getElementById(id).style.scale="1"
}
} }

File diff suppressed because one or more lines are too long

10
dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM node
ADD ./ /emb
WORKDIR /emb
LABEL traefik.port="3000"
EXPOSE 3000
CMD ["node", "index.js"]

216
game.js
View File

@ -15,6 +15,29 @@ function shuffle(arr) {
return 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 = { const game = {
init: async (gameID) => { init: async (gameID) => {
@ -26,7 +49,8 @@ const game = {
gameObj.charities = [] gameObj.charities = []
gameObj.distributionLog = [] gameObj.distributionLog = []
gameObj.accuseLog = {} gameObj.accuseLog = {}
gameObj.status = "distribution" gameObj.status = "Distribution"
gameObj.round=1
for (let index = 0; index < gameObj.players.length; index++) { for (let index = 0; index < gameObj.players.length; index++) {
gameObj.charities.push({ gameObj.charities.push({
charityID: uuidv4(), charityID: uuidv4(),
@ -48,9 +72,8 @@ const game = {
shuffle(gameObj.charities) shuffle(gameObj.charities)
//set board funds //set board funds
gameObj.players.forEach(element => { gameObj.players.forEach(element => {
element.funds = 10000000 element.funds = 100000
element.status = "Employed" element.status = "Employed"
console.log(element)
}) })
//update game //update game
mongo.update("embezzle", "gamelist", { mongo.update("embezzle", "gamelist", {
@ -58,6 +81,25 @@ const game = {
}, gameObj) }, 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) => { myCharity: async (gameID, ownerID) => {
name = await mongo.get("embezzle", "players", { name = await mongo.get("embezzle", "players", {
"_id": ownerID "_id": ownerID
@ -67,25 +109,21 @@ const game = {
"gameID": gameID "gameID": gameID
}) })
secret = gameObj[0].secretLink secret = gameObj[0].secretLink
let mine = "" mychar=""
secret.forEach(char => {
if (char.owner == name) {
mychar=char.charity
}
})
charname=""
gameObj[0].charities.forEach(element => { gameObj[0].charities.forEach(element => {
secret.forEach(user => { if (element.charityID == mychar) {
console.log(name,user) charname=element.name
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 = { chairtyObj = {
charity: mine charity: charname
} }
return chairtyObj return chairtyObj
}, },
@ -105,7 +143,7 @@ const game = {
}) })
players = gameObj[0].players players = gameObj[0].players
return ({ return ({
charatiesArr: players playersArr: players
}) })
}, },
@ -119,7 +157,6 @@ const game = {
"gameID": gameID "gameID": gameID
}) })
players = players[0].players players = players[0].players
result = ""
players.forEach(element => { players.forEach(element => {
if (element.name == name) { if (element.name == name) {
r = {funds: element.funds} r = {funds: element.funds}
@ -128,6 +165,10 @@ const game = {
return r return r
}, },
distribution: async (gameID, ownerID, charityID, amount) => { 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 //turn the amount to an int
amount=parseInt(amount) amount=parseInt(amount)
@ -138,57 +179,62 @@ const game = {
gameObj = gameObj[0] gameObj = gameObj[0]
// console.log(gameObj) // console.log(gameObj)
if (gameObj.status != "distribution") { if (gameObj.status != "Distribution") {
return({error:"You have tried to distribute funds when it is not the distribution round"}) return({error:"You have tried to distribute funds when it is not the distribution round"})
} } else {
player = await mongo.get("embezzle", "players", { player = await mongo.get("embezzle", "players", {
"_id": ownerID "_id": ownerID
}) })
player = player[0] player = player[0]
// console.log(gameObj) // console.log(gameObj)
gameObj.players.forEach((element, index) => { gameObj.players.forEach((element, index) => {
if (player.name == element.name){ if (player.name == element.name){
player=gameObj.players[index] player=gameObj.players[index]
console.log(player) 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) {
//Wipe out the accuse log read for the next round element.capital = element.capital+amount
gameObj.accuseLog={} player.funds = player.funds - amount
if (amount > 0 ){
gameObj.distributionLog.push({
donator: player.name,
amount: amount,
charity: element.name
})
}
}
})
// Now we know the game is still on-going: reaminingFunding = 0
//add users funds to the charity & subtract donation from users funds gameObj.players.forEach(element => {
reaminingFunding=reaminingFunding+element.funds
})
//check if user is trying to allocate too much money & set the amount so the users funds are wiped out mongo.update("embezzle", "gamelist", {
if (amount > player.funds) { "gameID": gameID
amount=player.funds }, gameObj)
}
gameObj.charities.forEach( element => { if (reaminingFunding == 0) {
if (element.charityID == charityID) { game.roundStatus(gameID)
element.capital = element.capital+amount
player.funds = player.funds - amount
gameObj.distributionLog.push({
donator: player.name,
amount: amount,
charity: element.name
})
} }
})
reaminingFunding = 0 return ({log: gameObj.distributionLog})
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) => { 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 //Get votes on who should be fired
player = await mongo.get("embezzle", "players", { player = await mongo.get("embezzle", "players", {
"_id": ownerID "_id": ownerID
@ -198,11 +244,10 @@ const game = {
"gameID": gameID "gameID": gameID
}) })
gameObj = gameObj[0] gameObj = gameObj[0]
if (gameObj.status != "accuse"){ if (gameObj.status != "Accuse"){
return {Error: "Game status is not accuse, you'll want to distribute funds before trying to fire someone"} return {Error: "Game status is not accuse, you'll want to distribute funds before trying to fire someone"}
} }
gameObj.players.forEach(element => { gameObj.players.forEach(element => {
if (victim == element.name) { if (victim == element.name) {
gameObj.accuseLog[player.name] = victim gameObj.accuseLog[player.name] = victim
@ -221,7 +266,9 @@ const game = {
}) })
}) })
sorted = Object.keys(vote).sort(function(a,b){return vote[a]-vote[b]}) sorted = Object.keys(vote).sort(function(a,b){return vote[a]-vote[b]})
console.log("Sorted: ",sorted)
fired = sorted[sorted.length-1] fired = sorted[sorted.length-1]
console.log("Fired: ",fired)
gameObj.players.forEach(element => { gameObj.players.forEach(element => {
if (element.name == fired ) { if (element.name == fired ) {
element.status="Fired" element.status="Fired"
@ -237,7 +284,7 @@ const game = {
employedCount++ employedCount++
} }
}) })
if (employedCount == 2) { if (employedCount < 3) {
return game.winner(gameID) return game.winner(gameID)
} }
@ -245,7 +292,7 @@ const game = {
mongo.update("embezzle", "gamelist", { mongo.update("embezzle", "gamelist", {
"gameID": gameID "gameID": gameID
}, gameObj) }, gameObj)
return ({log: gameObj.distributionLog}) return ({log: gameObj.accuseLog})
}, },
winner: async (gameID) => { winner: async (gameID) => {
@ -269,6 +316,7 @@ const game = {
//Declare the winner //Declare the winner
gameObj.status="Finished" gameObj.status="Finished"
gameObj.winner=winner
mongo.update("embezzle", "gamelist", { mongo.update("embezzle", "gamelist", {
"gameID": gameID "gameID": gameID
}, gameObj) }, gameObj)
@ -279,18 +327,46 @@ const game = {
"gameID": gameID "gameID": gameID
}) })
gameObj = gameObj[0] gameObj = gameObj[0]
console.log(gameObj.status) if (typeof gameObj.status == "Lobby" || gameObj.status == "Accuse") {
if (typeof gameObj.status == "lobby" || gameObj.status == "accuse") { gameObj.status = "Distribution"
gameObj.status = "distribution" mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
game.reset(gameID)
} else { } else {
gameObj.status = "accuse" gameObj.status = "Accuse"
} }
console.log(gameObj.status)
mongo.update("embezzle", "gamelist", { mongo.update("embezzle", "gamelist", {
"gameID": gameID "gameID": gameID
}, gameObj) }, 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 module.exports = game

View File

@ -101,6 +101,32 @@ app.post('/game/:gameID/accuse', function (req, res) {
res.send(JSON.stringify(r)) res.send(JSON.stringify(r))
}) })
}) })
app.get('/game/:gameID/statusinfo', function (req, res) {
game.roundStatusInfo(req.params.gameID).then(r => {
res.send(JSON.stringify(r))
})
})
app.get('/game/:gameID/distributioninfo', function (req, res) {
game.distributionInfo(req.params.gameID).then(r => {
res.send(JSON.stringify(r))
})
})
app.get('/game/:gameID/accuseinfo', function (req, res) {
game.accuseInfo(req.params.gameID).then(r => {
res.send(JSON.stringify(r))
})
})
app.get('/game/:gameID/distributioninfo', function (req, res) {
game.distributIoninfo(req.params.gameID).then(r => {
res.send(JSON.stringify(r))
})
})
// roundStatus -- this should not be public wtf? // roundStatus -- this should not be public wtf?
// app.get('/game/:gameID/roundStatus', function (req, res) { // app.get('/game/:gameID/roundStatus', function (req, res) {
// game.roundStatus(req.params.gameID).then(r => { // game.roundStatus(req.params.gameID).then(r => {

View File

@ -7,7 +7,7 @@ var lobby = {
result = await mongo.get("embezzle", "gamelist", {}) result = await mongo.get("embezzle", "gamelist", {})
pubArr = [] pubArr = []
result.forEach(element => { result.forEach(element => {
if (element.status == "lobby") { if (element.status == "Lobby") {
obj = { obj = {
gameID: element.gameID, gameID: element.gameID,
name: element.name, name: element.name,
@ -25,7 +25,7 @@ var lobby = {
}) })
pubArr = [] pubArr = []
result.forEach(element => { result.forEach(element => {
if (element.status == "lobby") { if (element.status == "Lobby") {
obj = { obj = {
gameID: element.gameID, gameID: element.gameID,
name: element.name, name: element.name,
@ -39,7 +39,7 @@ var lobby = {
}, },
register: (name) => { register: (name) => {
id = uuidv4() id = uuidv4()
name=name+'-'+id.split('-')[3] name=encodeURIComponent(name.substring(0,16))+'-'+id.split('-')[3]
mongo.put("embezzle", "players", { mongo.put("embezzle", "players", {
_id: id, _id: id,
name: name name: name
@ -61,7 +61,7 @@ var lobby = {
ownerID: ownerID, ownerID: ownerID,
name: name, name: name,
seats: seats, seats: seats,
status: "lobby", status: "Lobby",
players: [] players: []
}) })
lobby.join(ownerID, gameID) lobby.join(ownerID, gameID)
@ -83,7 +83,7 @@ var lobby = {
playerObj = { playerObj = {
name: player.name name: player.name
} }
if (gameObj.status != "lobby") { if (gameObj.status != "Lobby" || gameObj.players.length == gameObj.seats) {
return {error:"Unable to join game"} return {error:"Unable to join game"}
} else { } else {
gameObj.players.push(playerObj) gameObj.players.push(playerObj)
@ -101,7 +101,7 @@ var lobby = {
"ownerID": ownerID, "ownerID": ownerID,
"gameID": gameID "gameID": gameID
}).then(value => { }).then(value => {
value[0].status = "playing" value[0].status = "Playing"
mongo.update("embezzle", "gamelist", { mongo.update("embezzle", "gamelist", {
"gameID": gameID, "gameID": gameID,
"ownerID": ownerID "ownerID": ownerID

View File

@ -1,6 +1,6 @@
var MongoClient = require('mongodb').MongoClient var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost:27017' var url = process.env.mongo||'mongodb://embmongo'
var options= { var options= {
useNewUrlParser: true, useNewUrlParser: true,
useUnifiedTopology: true, useUnifiedTopology: true,