-
Notifications
You must be signed in to change notification settings - Fork 3
/
competition.js
151 lines (142 loc) · 3.98 KB
/
competition.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const express = require('express')
const fs = require('fs')
const { DateTime } = require('luxon')
const url = require('url')
const compiler = require('c-preprocessor')
const auth = require('./auth')
const activityCode = require('./activity_code')
const events = require('./events')
const extension = require('./extension')
const perf = require('./perf')
const pugFunctions = require('./pug_functions')
const functions = require('./functions/functions')
const driver = require('./parser/driver')
const parser = require('./parser/parser')
var router = express.Router()
function listFiles() {
if (!process.env.SCRIPT_BASE) {
return []
}
out = []
dirs = ['.']
i = 0;
while (dirs.length && i < 100) {
dir = dirs.pop()
files = fs.readdirSync(process.env.SCRIPT_BASE + '/' + dir)
for (const f of files) {
if (f.endsWith('.cs')) {
out.push((dir + '/' + f).substring(2))
} else {
stats = fs.lstatSync(process.env.SCRIPT_BASE + '/' + dir + '/' + f);
if (stats.isDirectory()) {
dirs.push(dir + '/' + f)
}
}
}
}
return out
}
router.use('/:competitionId', async (req, res, next) => {
req.logger = new perf.PerfLogger()
try {
req.logger.start('fetch')
req.competition = await auth.getWcif(req.params.competitionId, req, res)
req.logger.stop('fetch')
next()
} catch (e) {
console.log(e)
res.redirect('/')
}
})
router.get('/:competitionId', async (req, res) => {
var script = ''
var filename = ''
if (req.query.script) {
req.session.script = req.query.script
req.session.filename = req.query.filename
res.redirect(url.parse(req.originalUrl).pathname)
return
}
if (req.session.script) {
script = req.session.script
filename = req.session.filename
delete req.session.script
delete req.session.filename
}
await runScript(req, res, script || req.query.script, filename || req.query.filename, true, req.query.clearCache)
})
router.post('/:competitionId', async (req, res) => {
await runScript(req, res, req.body.script, req.body.filename, req.body.dryrun, req.body.clearCache)
})
async function runScript(req, res, script, filename, dryrun, clearCache) {
var logger = req.logger
var params = {
comp: req.competition,
fn: pugFunctions,
script: script,
outputs: [],
dryrun: dryrun,
dryrunWarning: false,
clearCache: clearCache,
files: listFiles(),
selectedFile: filename,
}
if (filename) {
script = `#include "${filename}"
${script}`
}
if (clearCache) {
fs.unlinkSync(auth.cachePath(req.params.competitionId))
}
if (script) {
compiler.compile(script, {
basePath: process.env.SCRIPT_BASE + '/',
newLine: '\r\n',
}, async (err, newScript) => {
if (err) {
params.outputs = [{type: 'Error', data: err}]
res.render('script', params)
return
}
newScript = newScript.trim()
var ctx = {
competition: req.competition,
command: newScript,
allFunctions: functions.allFunctions,
dryrun: dryrun,
logger,
udfs: {},
}
try {
ctx.logger.start('parse')
var scriptResult = await parser.parse(newScript, req, res, ctx, false)
ctx.logger.stop('parse')
params.outputs = scriptResult.outputs
if (scriptResult.mutations.length) {
if (dryrun) {
params.dryrunWarning = true
} else {
ctx.logger.start('patch')
await auth.patchWcifWithRetries(ctx.competition, scriptResult.mutations, req, res)
ctx.logger.stop('patch')
}
}
} catch (e) {
params.outputs.splice(0, 0, {type: 'Exception', data: e.stack })
console.log(e)
}
logger.start('render')
res.render('script', params)
logger.stop('render')
logger.log()
})
} else {
logger.start('render')
res.render('script', params)
logger.stop('render')
logger.log()
}
}
module.exports = {
router: router
}