-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
128 lines (99 loc) · 3.64 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
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
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const cors = require('cors');
const cp = require('child_process');
const util = require('util');
const fs = require('fs');
const read = util.promisify(fs.read);
const write = util.promisify(fs.write);
const template = require('./template');
let Users = {
Rafaell416_80: { admin: true },
CamiloTD_443 : { admin: true },
Maquinox_1521: { admin: true }
};
let Sessions = {};
let Challenges = {};
let current_challenge = null;
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
// res.header('Content-Security-Policy')
// res.header('upgrade-insecure-requests')
next()
})
http.listen(7777, () => console.log("Server up!"));
io.on('connection', function (sock) {
let user = null;
console.log('user connected')
sock.on('login', (name) => {
if(Sessions[name]) return sock.emit('login-error', "NAME_ALREADY_TAKEN");
if(!Users[name]) Users[name] = { admin: false };
user = Users[name]
user.name = name;
Sessions[name] = { name, sock, admin: user.admin };
if(user.admin) {
grantAdmin(sock);
} else {
grantUser(sock);
}
sock.emit('login-success', user);
});
sock.on('disconnect', () => {
//if(user) Sessions[name] = user.name;
});
function grantAdmin (sock) {
console.log('admin grantend')
sock.on('create', (data) => {
console.log(data)
});
}
function grantUser (sock) {
sock.on('submit', submitChallenge);
}
function createChallenge ({ name, description, input, output, timeout, runs }) {
console.log(name)
if(!description) description = "";
if(!input) input = [];
if(!output) output = [];
current_challenge = Challenges[name] = { name, description, input, output, timeout, runs, submits: {} };
io.emit('challenge', { name, description, timeout, runs });
}
async function submitChallenge (code) {
if(!current_challenge) return sock.emit("submit-failed", "CHALLENGE_IS_NOT_READY_YET");
let result = await TestCode(code);
if(!result.success) return sock.emit("submit-failed", result.error);
current_challenge.submits[user.name] = result;
sock.emit("submit-success", {
time: result.time,
length: result.length
});
for(let i in Sessions) if(Sessions[i].admin) Sessions[i].sock.emit("submit", {
player: user.name,
...result
})
}
function TestCode (code) {
return new Promise((resolve, reject) => {
let filename = Math.random().toString().substring(2) + ".js";
fs.writeFile(filename, template(code), function (err) {
if(err) {
console.log(err);
return reject({ code: "COULDNT_WRITE", message: "Cannot write codefile"});
}
let timer = setTimeout(() => {
reject({ code: "TIMEOUT", message: "Your code ran too slow."});
}, challenge.timeout)
});
});
}
if(current_challenge)
sock.emit('challenge', {
name: current_challenge.name,
description: current_challenge.description,
timeout: current_challenge.timeout,
runs: current_challenge.runs
});
});