-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
60 lines (53 loc) · 2.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const cron = require('node-cron')
const express = require('express')
const moment = require('moment')
const path = require('path')
const router = express.Router()
const stat_service = require('./stat-service')
const seasonStartDate = 20210522
const dotenv = require('dotenv')
dotenv.config()
app = express()
app.set('view engine', 'pug')
//VIEWS
router.get('/', async (req, res) => {
await updateStandings(true)
var standings = stat_service.getStandingsInfo()
standings.lastUpdated = moment(standings.lastUpdated, 'YYYYMMDD').format('dddd, MMMM Do YYYY')
res.render('index', { standings: standings })
})
router.get('/roster/:team', async (req, res) => {
await updateStandings(true)
let rosterData = stat_service.getRosterInfo(req.params.team)
res.render('roster', { team: rosterData })
})
app.get('/standings', async (req, res, next) => {
await updateStandings(true)
var standings = stat_service.getStandingsInfo()
res.header('Content-Type', 'application/json')
res.send(JSON.stringify(standings), null, 4)
})
app.get('/draft', async (req, res, next) => {
let players = await stat_service.getPlayers()
res.render('draft', { players: players })
})
//Run as 12 GMT which will be 08:00 ETC all games should be done and reported
cron.schedule('0 12 * * *', async () => {
await updateStandings(true)
await stat_service.tweetStandings(standingsFile)
})
const updateStandings = async () => {
let startDate = moment(seasonStartDate, 'YYYYMMDD').startOf('day')
let todaysDate = moment.utc().startOf('day')
var duration = moment.duration(todaysDate.diff(startDate))
//Get the days since the beginning of the seson and run all those days
var days = duration.asDays().toFixed()
while (days > 0) {
let dateToRun = moment.utc().subtract(days, 'days').format('YYYYMMDD')
await stat_service.getCurrentStandingsByDate(dateToRun)
days--
}
}
app.use('/', router)
//Add some service to get the current stadning s points totals from store/standtings file
app.listen(process.env.PORT || 9280)