You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
6.9 KiB

// 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) {
}