fetch = require('node-fetch') cron = require('node-cron') express = require('express') bodyParser = require('body-parser') userObj= gameObj = { users: { Agne: { USD:1000, BTC:0 }, Dan: { USD:1000, BTC:0 } }, bid: 0, ask: 0, price: 0 } cron.schedule('* * * * * *', () => { updateGame() }) async function updateGame(){ const response = await fetch('https://www.bitstamp.net/api/ticker/') const json = await response.json() gameObj.ask = json.ask gameObj.bid = json.bid gameObj.price = json.last } function trade(userID,asset){ returnObj = {message: "Something is wrong or someone was lazy"} switch (asset) { case "USD": if (gameObj.users[userID].USD != 0){ gameObj.users[userID].BTC=(gameObj.users[userID].USD/gameObj.ask) returnObj.message = "Bought "+gameObj.users[userID].BTC+" BTC for "+gameObj.users[userID].USD+" USD at "+gameObj.ask gameObj.users[userID].USD=0 return returnObj } else { returnObj.message = "You don't have any of that asset" return returnObj } case "BTC": if (gameObj.users[userID].BTC != 0){ gameObj.users[userID].USD=(gameObj.bid*gameObj.users[userID].BTC) returnObj.message = "Bought "+gameObj.users[userID].USD+" USD for "+gameObj.users[userID].BTC+" BTC at "+gameObj.bid gameObj.users[userID].BTC=0 return returnObj } else { returnObj.message = "You don't have any of that asset" return returnObj } default: returnObj.message = "Something went bad wrong" break; } } 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') res.setHeader('Access-Control-Allow-Headers', 'Content-Type') res.setHeader('Content-Type', 'application/json') res.setHeader('Access-Control-Allow-Credentials', true) next() }) app.post('/submit', async function (req, res) { res.send(trade(req.body.userID,req.body.asset)) }) app.get('/status', function (req,res){ res.send(JSON.stringify(gameObj)) }) app.listen(5000)