const express = require('express') const axios = require('axios') var http = require('http'); var https = require('https'); const fs = require('fs'); const sh = require('secrethash'); var key = fs.readFileSync('./projectdivar.com/privkey.pem'); var cert = fs.readFileSync('./projectdivar.com/cert.pem'); var options = { key: key, cert: cert }; const app = express() var server = https.createServer(options, app); const port = 4505 server.listen(port, () => console.log(`Example app listening at http://localhost:${port}`)) const bodyParser = require('body-parser') const { json } = require('body-parser') const moment = require('moment'); const Pool = require('pg').Pool app.use(bodyParser.json()) app.use( bodyParser.urlencoded({ extended: true, }) ) let allowCrossDomain = function(req, res, next) { res.header('Access-Control-Allow-Origin', "*"); res.header('Access-Control-Allow-Headers', "*"); res.header('Access-Control-Allow-Methods', "*"); next(); } app.use(allowCrossDomain); var db = new Pool({ user: 'postgres', password: '', host: 'postgres', database: 'sigcrafting', port: 5432, }) app.get('/',async(req,res)=>{ res.status(200).send('BUN is love, BUN is life.') }) app.get('/getData',(req,res)=>{ db.query('select * from crafting_list order by id asc') .then((data)=>{ if (data.rows.length>0) { res.status(200).json(data.rows) } else { res.status(204).send("No data") } }) .catch((err)=>{ res.status(500).send(err.message) }) }) app.get('/getNotifications',(req,res)=>{ db.query('select * from audit_log where date>$1 order by id asc limit 10',[req.query.date]) .then((data)=>{ res.status(200).json(data.rows) }) .catch((err)=>{ console.log(err) res.status(500).send(err.message) }) }) app.get('/lastUpdate',(req,res)=>{ db.query("select * from site_data limit 1") .then((data)=>{ res.status(200).json(data.rows) }) .catch((err)=>{ res.status(500).send(err.message) }) }) app.post("/updateItem",(req,res)=>{ var itemIcon="" db.query("update crafting_list set obtained=$1 where id=$2 returning *",[req.body.obtained,req.body.id]) .then((data)=>{ itemIcon = "https://xivapi.com"+data.rows[0].icon return db.query("update site_data set last_modified=$1",[req.body.last_modified]) }) .then((data)=>{ return axios.post(process.env.DISCORD_WEBHOOK,{embeds:[{ author:{ name:req.body.item_name, icon_url:itemIcon }, color:req.body.obtained==req.body.required?32768:Number(req.body.obtained)>0?20680:2172201, description:"**"+req.body.username+"** "+(req.body.operation==="FINISH"?" has finished collecting "+req.body.required+" __"+req.body.item_name+"__!": req.body.operation==="INCREASE"?" has collected "+req.body.obtained+" / "+req.body.required+" __"+req.body.item_name+"__ (+"+(req.body.obtained-req.body.previous_amt)+")" :" has set __"+req.body.item_name+"__ to "+req.body.obtained+" / "+req.body.required)+"\n\n[Sig Planner - Sig crafts all the things](http://projectdivar.com:3001)", footer:{ "text":"Progress: "+((req.body.itemCount+req.body.obtained-req.body.previous_amt)/req.body.totalItemCount*100).toFixed(2)+"% ("+(req.body.itemCount+req.body.obtained-req.body.previous_amt)+"/"+req.body.totalItemCount+")" } }] })}) .then((data)=>{ return db.query("insert into audit_log(username,obtained,required,item_name,operation,date,previous_amt) values($1,$2,$3,$4,$5,$6,$7)",[req.body.username,req.body.obtained,req.body.required,req.body.item_name,req.body.operation,req.body.last_modified,req.body.previous_amt]) }) .then((data)=>{ res.status(200).send("Yay!") }) .catch((err)=>{ console.log(err.message) res.status(500).send(err.message) }) }) app.post('/setItem',async(req,res)=>{ await db.query('select * from crafting_list where itemid=$1',[req.body.itemid]) .then((data)=>{ if (data.rows.length==0){ db.query('insert into crafting_list(itemid,name,obtained,required,icon) values($1,$2,$3,$4,$5)',[req.body.itemid,req.body.name,req.body.obtained,req.body.required,req.body.icon]) .then((data)=>{ res.status(200).send("Yay!") }) .catch((err)=>{ res.status(500).send(err.message) }) } else { db.query('update crafting_list set required=$1 where itemid=$2',[Number(data.rows[0].required)+Number(req.body.required),req.body.itemid]) .then((data)=>{ res.status(200).send("Yayay!") }) .catch((err)=>{ res.status(500).send(err.message) }) } }) }) app.post('/addItem',async(req,res)=>{ await db.query('select * from crafting_list where itemid=$1',[req.body.itemid]) .then((data)=>{ if (data.rows.length>0){ db.query('update crafting_list set obtained=$1 where itemid=$2',[Math.min(data.rows[0].required,Number(data.rows[0].obtained)+Number(req.body.obtained)),req.body.itemid]) .then((data)=>{ res.status(200).send("AYAYA") }) .catch((err)=>{ res.status(500).send(err.message) }) } else { res.status(200).send("Nothing to do!") } }) }) var runInventoryScan = async()=>{ for (var item of myInv) { const it = item await db.query('select * from crafting_list where itemid=$1',[item.itemId]) .then(async(data)=>{ for (var row of data.rows) { if (row.required-row.obtained<=it.quantity) { await db.query('update crafting_list set obtained=$1 where itemid=$2',[row.required,it.itemId]) .catch((err)=>{ console.log(err.message) }) } else { await db.query('update crafting_list set obtained=$1 where itemid=$2',[row.obtained+it.quantity,it.itemId]) .catch((err)=>{ console.log(err.message) }) break; } } }) .catch((err)=>{ console.log(err.message) }) } } //runInventoryScan() //console.log(process.env.DISCORD_WEBHOOK)