Embezzle/index.js

109 lines
2.8 KiB
JavaScript

const lobby = require("./lobby.js")
const game = require("./game.js")
const express = require('express')
const bodyParser = require('body-parser');
const app = express()
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('Access-Control-Allow-Credentials', true)
next()
})
//top level
//get game list
app.get('/lobby/list' , async function (req, res) {
lobby.list().then(r => {
res.send(JSON.stringify({gamesArr: r }))
})
})
//get game details
app.get('/lobby/details/:gameID', function (req, res) {
lobby.details(req.params.gameID).then(r => {
res.send(JSON.stringify(r[0]))
})
})
//post register player
app.post('/lobby/register', function (req, res) {
let obj=req.body
response=JSON.stringify(lobby.register(obj.name))
res.send(response)
})
//post join an existing game
app.post('/lobby/join', function (req, res) {
let obj=req.body
response=JSON.stringify(lobby.join(obj.ownerID,obj.gameID))
res.send(response)
})
//get an empty game made
app.post('/lobby/make', function (req, res) {
let obj=req.body
lobby.make(obj.ownerID,obj.seats).then(result => {
res.send(JSON.stringify(result))
})
})
//start a game
app.post('/lobby/start', function (req, res) {
let obj=req.body
response=JSON.stringify(lobby.start(obj.ownerID, obj.gameID))
res.send(response)
})
//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))
// })
// })
console.log("Up")
app.listen(3000)