Client working, server done bug smashing time

master
nannal 4 years ago
parent d50ccb48f8
commit 2129889248

@ -1,24 +0,0 @@
function register(){
console.log("register")
}
function make(){
console.log("make")
}
function join(){
console.log("join")
}
function start(){
console.log("start")
}
function donate(){
console.log("donate")
}
function accuse(){
console.log("accuse")
}

@ -1,37 +0,0 @@
<html>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding"/>
<script src="index.js"></script>
<div id="controls">
</br>Register
<input id="register" type="text" placeholder="Name"/> <button onclick="register()">Register</button>
</br>Make
<input id="make" type="text" placeholder="GameName"/> <button onclick="make()">Make</button>
</br>Join
<input id="join" type="text" placeholder="Name"/> <button onclick="join()">Join</button>
</br>Start
<button onclick="start()">Start</button>
</br>Donate
<select id="donate" name="list">
<option value="x">x</option>
</select>
<input id="amount" type="number">
<button onclick="donate()">Donate</button>
</br>Accuse
<input id="accuse" type="text" placeholder="Name"/> <button onclick="accuse()">Accuse</button>
</div>
<div id="info"></div>
</html>

@ -0,0 +1,12 @@
body{
margin:40px auto;
max-width:650px;
line-height:1.6;
font-size:18px;
color:#444;
padding:0 10px;
}
h1,h2,h3{
line-height:1.2
}

@ -0,0 +1,73 @@
<html>
<head>
<link rel="stylesheet" type="text/css" href="index.css">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding"/>
<script src="index.js"></script>
<script src="jq.js"></script>
</head>
<body>
<div class="controls">
<div class="Reigster-Container">
</br>Register
<input id="register" type="text" placeholder="Name"/> <button onclick="register()">Register</button>
</div>
<div class="Make-Container">
</br>Make
<input id="makeSeats" type="number"/> <button onclick="make()">Make</button>
</div>
<div class="Join-Container">
</br>Join
<input id="join" type="text" placeholder="Name"/> <button onclick="join()">Join</button>
</div>
<div class="Start-Container">
</br>Start
<button onclick="start()">Start</button>
</div>
<div class="Donate-Container">
</br>Donate
<select id="donate" name="list">
<option value="x">x</option>
</select>
<input id="amount" type="number">
<button onclick="donate()">Donate</button>
</div>
<div class="Accuse-Container">
</br>Accuse
<input id="accuse" type="text" placeholder="Name"/> <button onclick="accuse()">Accuse</button>
</div>
</div>
<div id="info">
<div id="messages"></div>
<div id="games"></div>
<div id="gameInfo"></div>
<div id="myCharity"></div>
<div id="charityStatus"></div>
<div id="boardFunding"></div>
<div id="transactionLog"></div>
<div id="accusationLog"></div>
<div id="messages"></div>
</div>
</body>
</html>

@ -0,0 +1,287 @@
// Global Vars
serverAddr="http://localhost:3000"
ownerID=''
// ownerID='6298bc89-41cb-4cc7-84cd-7fd8e419f263'
gameID=''
//Meta Game Utils
function register(){
const data = { name: document.getElementById('register').value };
fetch(serverAddr+'/lobby/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
// console.log(data)
ownerID=data.ownerID
document.getElementById('register').value=data.name
//hide register
//show make & join
//show list of games
listGames()
})
.catch((error) => {
console.error('Error:', error);
});
}
function listGames(){
fetch(serverAddr+"/lobby/list")
.then((response) => {
return response.json();
})
.then((data) => {
document.getElementById('messages').value='data.gamesArr'
console.log(data);
});
}
function make(){
console.log("make")
const data = {
ownerID: ownerID,
seats: document.getElementById('makeSeats').value };
fetch(serverAddr+'/lobby/make', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
// console.log(data)
gameID=data.gameID
gameData(gameID)
})
.catch((error) => {
console.error('Error:', error);
});
}
function join(){
console.log("join")
gameID=document.getElementById('join').value
const data = {
ownerID: ownerID,
gameID: gameID };
// console.log(data)
fetch(serverAddr+'/lobby/join', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
// console.log(data)
gameData(gameID)
})
.catch((error) => {
console.error('Error:', error);
});
}
function gameData(gameID) {
fetch(serverAddr+'/lobby/details/'+gameID)
.then((response) => {
if (response.status == 404) {
status()
}
return response.json();
})
.then((data) => {
checkIfStarted = setInterval(function(){
gameData(gameID)
clearInterval(checkIfStarted)
}, 1000)
if (document.getElementById("messages").innerText != "Game Name: "+data.name+", GameID: "+data.gameID) {
document.getElementById("messages").innerText="Game Name: "+data.name+", GameID: "+data.gameID
}
})
}
function start(){
console.log("start")
const data = {
ownerID: ownerID,
gameID: gameID };
// console.log(data)
fetch(serverAddr+'/lobby/start', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
console.log(data)
})
.catch((error) => {
console.error('Error:', error);
});
}
// Game controls
function donate(){
console.log("donate")
// /game/:gameID/distribution
amount=document.getElementById('amount').value
doante=document.getElementById('donate').value
const data = {
ownerID: ownerID,
gameID: gameID,
charityID: doante,
amount: amount };
// console.log(data)
fetch(serverAddr+'/game/'+gameID+'/distribution', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
// console.log(data)
// gameData(gameID)
})
.catch((error) => {
console.error('Error:', error);
});
}
function accuse(){
console.log("accuse")
name = document.getElementById('accuse').value
const data = {
ownerID: ownerID,
gameID: gameID,
player: name };
// console.log(data)
fetch(serverAddr+'/game/'+gameID+'/accuse', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
// console.log(data)
document.getElementById("accuseLog").innerText=data
// gameData(gameID)
})
.catch((error) => {
console.error('Error:', error);
});
}
// Game Utils
function status() {
//the game has started, get the data
// One off updates
const data = {
ownerID: ownerID,
gameID: gameID };
fetch(serverAddr+'/game/'+gameID+'/charity', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
// console.log(data)
myCharityFormat(data)
})
.catch((error) => {
console.error('Error:', error);
});
//Quick Recurring updates
setInterval(function(){
fetch(serverAddr+'/game/'+gameID+'/boardFunding', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
boardFundingFormat(data)
// statusObj.funding=data
})
.catch((error) => {
// console.error('Error:', error);
});
fetch(serverAddr+'/game/'+gameID+'/charityList')
.then((response) => {
return response.json();
})
.then((data) => {
// console.log(data);
dropDownList=""
data.charatiesArr.forEach(element => {
dropDownList=dropDownList+'<option value="'+element.charityID+'">'+element.name+'</option>'
});
if (document.getElementById("donate").innerHTML != dropDownList){
document.getElementById("donate").innerHTML=dropDownList
statusObj.charitylist=data
}
document.getElementById("charityStatus").innerHTML=charityStatusFormat(data)
});
fetch(serverAddr+'/game/'+gameID+'/playerList')
.then((response) => {
return response.json();
})
.then((data) => {
statusObj.playerlist=data
// console.log(data);
});
// fdocument.getElementById("info").innerText=prettyprint(statusObj)
}, 3000)
}
//Formating Functions
function myCharityFormat(data) {
document.getElementById("myCharity").innerText="Your Charity: "+data.charity
}
function charityStatusFormat(data) {
html=""
data.forEach(element => {
html=html+"<b>"+element.name+":</b> "+element.capital
})
return html
}
function boardFundingFormat(data) {
}

File diff suppressed because one or more lines are too long

@ -26,7 +26,7 @@ const game = {
gameObj.charities = []
gameObj.distributionLog = []
gameObj.accuseLog = {}
console.log(gameObj)
gameObj.status = "distribution"
for (let index = 0; index < gameObj.players.length; index++) {
gameObj.charities.push({
charityID: uuidv4(),
@ -38,7 +38,6 @@ const game = {
gameObj.secretLink = []
gameObj.players.forEach((element, index) => {
charity = gameObj.charities[index]
console.log(element)
let link = {
charity: charity.charityID,
owner: element.name
@ -51,6 +50,7 @@ const game = {
gameObj.players.forEach(element => {
element.funds = 10000000
element.status = "Employed"
console.log(element)
})
//update game
mongo.update("embezzle", "gamelist", {
@ -63,17 +63,25 @@ const game = {
"_id": ownerID
})
name = name[0].name
secret = await mongo.get("embezzle", "gamelist", {
let gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
secret = secret[0].secretLink
secret = gameObj[0].secretLink
let mine = ""
secret.forEach(element => {
console.log(element.owner,name)
if (element.name == name) {
mine = element.name
}
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
}
}
})
})
chairtyObj = {
@ -101,7 +109,8 @@ const game = {
})
},
boardFunding: async (gameID, ownerID) => {
boardFunding: async (ownerID,gameID) => {
r=""
name = await mongo.get("embezzle", "players", {
"_id": ownerID
})
@ -113,16 +122,21 @@ const game = {
result = ""
players.forEach(element => {
if (element.name == name) {
result = name
r = {funds: element.funds}
}
})
return r
},
distribution: async (gameID, ownerID, charityID, amount) => {
//get the player info
//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"})
@ -131,18 +145,18 @@ const game = {
"_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++
// console.log(gameObj)
gameObj.players.forEach((element, index) => {
if (player.name == element.name){
player=gameObj.players[index]
console.log(player)
}
})
if (employedCount < 3) {
return game.winner(gameID)
}
//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
@ -156,46 +170,122 @@ const game = {
element.capital = element.capital+amount
player.funds = player.funds - amount
gameObj.distributionLog.push({
donator:player.name,
donator: player.name,
amount: amount,
charity: element.name
})
}
})
reaminingFunding = 0
gameObj.players.forEach(element => {
reaminingFunding= reaminingFunding+element.funds
if (reaminingFunding == 0) {
roundStatus(gameID)
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) => {
//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]})
fired = sorted[sorted.length-1]
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 == 2) {
return game.winner(gameID)
}
//change status of the user to fired
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", {
winner=""
gameObj = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
winer = winner[0].winner
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"
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 == undefined || gameObj == "accuse") {
console.log(gameObj.status)
if (typeof gameObj.status == "lobby" || gameObj.status == "accuse") {
gameObj.status = "distribution"
} else {
gameObj.status = "accuse"
}
console.log(gameObj.status)
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)

@ -7,6 +7,7 @@ app.use(bodyParser.json(),function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
res.setHeader('Content-Type', 'application/json')
res.setHeader('Access-Control-Allow-Credentials', true)
next()
})
@ -27,6 +28,9 @@ app.get('/lobby/list' , async function (req, res) {
//get game details
app.get('/lobby/details/:gameID', function (req, res) {
lobby.details(req.params.gameID).then(r => {
if (r.length == 0){
res.status(404)
}
res.send(JSON.stringify(r[0]))
})
@ -80,8 +84,8 @@ app.get('/game/:gameID/playerList', function (req, res) {
})
})
// boardFunding
app.get('/game/:gameID/boardFunding', function (req, res) {
game.boardFunding(req.params.gameID).then(r => {
app.post('/game/:gameID/boardFunding', function (req, res) {
game.boardFunding(req.body.ownerID,req.params.gameID).then(r => {
res.send(JSON.stringify(r))
})
})

@ -39,17 +39,19 @@ var lobby = {
},
register: (name) => {
id = uuidv4()
name=name+'-'+id.split('-')[3]
mongo.put("embezzle", "players", {
_id: id,
name: name
})
return ({
ownerID: id
ownerID: id,
name: name
})
},
make: async (ownerID, seats) => {
gameID = uuidv4()
//todo write charity name generator
//Name the game after the person that makes it.
name = await mongo.get("embezzle", "players", {
"_id": ownerID
})
@ -69,6 +71,7 @@ var lobby = {
// return({gameID:gameID})
},
join: async (ownerID, gameID) => {
//TODO: Stop players joining the game more than once.
value = await mongo.get("embezzle", "gamelist", {
"gameID": gameID
})
@ -80,14 +83,18 @@ var lobby = {
playerObj = {
name: player.name
}
gameObj.players.push(playerObj)
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
return ({
gameID: gameID
})
if (gameObj.status != "lobby") {
return {error:"Unable to join game"}
} else {
gameObj.players.push(playerObj)
mongo.update("embezzle", "gamelist", {
"gameID": gameID
}, gameObj)
return ({
gameID: gameID
})
}
},
start: (ownerID, gameID) => {
mongo.get("embezzle", "gamelist", {

5
package-lock.json generated

@ -746,6 +746,11 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
},
"nodaemon": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/nodaemon/-/nodaemon-0.0.5.tgz",
"integrity": "sha512-4rfEhcDMZX/av809P6Yu9mxQf3qQL66fCOrAex+k6HkMnWKDd57IesiT4VCXUK4FTZWQ9MrvSZ76VrKgvSjnDg=="
},
"nodemon": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.2.tgz",

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

Loading…
Cancel
Save