-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
360 lines (297 loc) · 10.1 KB
/
script.js
File metadata and controls
360 lines (297 loc) · 10.1 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
const CONTENT =
"// Type code here...\n// Right click or Ctrl + S to upload your code.\n";
const EDITOR = "editor";
var sendMsgNext = true;
const client = new Date().getTime();
var editor = {};
let myFontSize = 13;
function identifier(id) {
return id.replace(".", "_").replace("/", "__");
}
function run() {
Edrys.sendMessage("run", "");
}
function initEditor({ id, content, language, theme }) {
const editor_ = monaco.editor.create(document.getElementById(id), {
value: content,
language: language,
theme: theme,
automaticLayout: true,
fontSize: myFontSize,
});
function save() {
const value = editor_.getValue();
if (sendMsgNext) {
Edrys.sendMessage("update", JSON.stringify({ id, value, client }));
} else {
sendMsgNext = !sendMsgNext;
}
Edrys.setItem(`editorText_${id}`, value);
}
editor_.addAction({
id: "upload-code",
label: "Save & upload code...",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S],
precondition: null,
keybindingContext: null,
contextMenuGroupId: "navigation",
contextMenuOrder: 0,
run: function (ed) {
save();
//run()
return null;
},
});
editor_.addAction({
id: "toggle-theme",
label: "Toggle dark theme",
precondition: null,
keybindingContext: null,
contextMenuGroupId: "navigation",
contextMenuOrder: 1,
run: function (ed) {
if (Edrys.module) {
let _theme =
Edrys.getItem("theme") == "vs-light" ? "vs-dark" : "vs-light";
ed.updateOptions({
theme: _theme,
});
Edrys.setItem("theme", _theme);
}
return null;
},
});
editor_.onDidChangeModelContent(save);
editor_.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, run);
return editor_;
}
const { createApp } = Vue;
const app = createApp({
data() {
return {
multiFileMode: null,
filename: [],
};
},
methods: {
runCode() {
run();
},
identifier(id) {
return identifier(id);
},
init({ id, content, language, theme }) {
if (typeof content === "string") {
// in this case only a single file will be generated
this.multiFileMode = false;
editor = {};
editor[this.identifier(id)] = initEditor({
id,
content,
language,
theme,
});
} else {
this.multiFileMode = true;
for (const name in content) {
this.filename.push(name);
}
}
},
initFiles({ id, content, language, theme }) {
editor = {};
for (const filename in content) {
let id = this.identifier(filename);
editor[id] = initEditor({
id,
content: content[filename],
language,
theme,
});
}
},
// reset the editor content to the starter code
resetCode() {
clearEditor();
},
// toggle editor theme
toggleTheme() {
const theme = Edrys.getItem("theme") == "vs-light" ? "vs-dark" : "vs-light";
Edrys.setItem("theme", theme);
changeTopBarBgColor(theme);
for (const id in editor) {
editor[id].updateOptions({
theme,
});
}
},
zoomIn() {
myFontSize += 3;
for (const id in editor) {
editor[id].updateOptions({
fontSize: myFontSize,
});
}
},
zoomOut() {
myFontSize -= 3;
for (const id in editor) {
editor[id].updateOptions({
fontSize: myFontSize,
});
}
},
updateEditorContent({ id, content }) {
editor[this.identifier(id)].setValue(content);
},
},
});
const ui = app.mount("#app");
const appElement = document.getElementById("app");
Edrys.onReady(() => {
const language = Edrys?.module?.config?.language || "cpp";
const theme =
Edrys?.module?.config?.theme ||
(window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches
? "vs-dark"
: "vs-light");
//changeTopBarBgColor(theme);
// if the challenge is time-restricted, the editor should be read-only, until the timer starts
if (Edrys.module.challengeType === "time-restricted" || Edrys.module.challengeType === "multiplayer" || Edrys.module.challengeId === "missing-led") {
disableEditor();
}
if (Edrys.module.config.file) {
const content = Edrys.module.config.file;
for (key in content) {
content[key] =
Edrys.getItem(`editorText_${identifier(key)}`) ||
content[key] ||
CONTENT;
}
ui.init({ id: "editor", content, language, theme });
setTimeout(function () {
ui.initFiles({ id: "editor", content, language, theme });
}, 1000);
} else {
const content =
Edrys.getItem(`editorText_${EDITOR}`) ||
Edrys.module.config.editorText ||
CONTENT;
ui.init({ id: EDITOR, content, language, theme });
}
});
// Functions to handle editor state
const clearEditor = () => {
const content = Edrys.module.config.editorText || CONTENT;
ui.updateEditorContent({ id: EDITOR, content: content });
displayMessage("");
};
const disableEditor = () => {
appElement.classList.add("disabled");
};
const enableEditor = () => {
appElement.classList.remove("disabled");
};
// listen for messages from other modules
Edrys.onMessage(({ from, subject, body, module }) => {
if (subject === "timer-started") {
// change the editor to writable when the timer starts
enableEditor();
} else if (subject === "timer-ended") {
// change the editor to read-only when the timer ends and reset starter code
clearEditor();
disableEditor();
} else if (subject === "player-turn" && body === Edrys.liveUser.displayName) {
clearEditor();
// change the editor to writable when it's the player's turn
enableEditor();
} else if (subject === "player-turn" && body !== Edrys.liveUser.displayName) {
disableEditor();
}
}, (promiscuous = true));
const displayMessage = (message) => {
document.getElementById("server-messages").innerHTML = message;
};
// connect to the websocket server
var socket = new WebSocket(Edrys?.module?.serverURL || "ws://localhost:8080");
// Event listener to handle received WebSocket messages
socket.onmessage = (event) => {
var data = JSON.parse(event.data);
if (data.error) {
Edrys.sendMessage("server-response", "Error: " + data.error);
Edrys.sendMessage("continue-timer", "");
} else if (data.testMessage) {
if (data.testPassed) {
Edrys.sendMessage(
"server-response",
`<p style='font-size: 2rem'>${data.testMessage} <i class='fa fa-circle-check' style='color: #63E6BE;'></i></p>`
);
// send winning message to the timer module
Edrys.sendMessage("challenge-solved", "Challenge solved");
} else {
Edrys.sendMessage(
"server-response",
`<p style='font-size: 2rem'>${data.testMessage} <i class='fa fa-circle-xmark' style='color: #ff0000;'></i></p>`
);
Edrys.sendMessage("continue-timer", "");
}
} else {
Edrys.sendMessage(
"server-response",
data.message + "\n" + (data.stdout ? data.stdout : data.stderr)
);
Edrys.sendMessage("continue-timer", "");
}
};
Edrys.onMessage(({ from, subject, body }) => {
switch (subject) {
case "update":
const b = JSON.parse(body);
if (b.client == client) return;
// console.log(b.client, client)
sendMsgNext = false;
editor[b.id].setValue(b.value);
break;
case "run":
body = "";
if (ui.multiFileMode) {
let file = {};
for (const name of ui.filename) {
file[name] = editor[identifier(name)].getValue();
}
body = JSON.stringify({ file });
} else {
body = editor[EDITOR].getValue();
}
if (Edrys.module.config.runCommand) {
Edrys.sendMessage(Edrys.module.config.runCommand, body);
}
// show loader (and stop timer) while waiting for response
Edrys.sendMessage("server-response", "<span class='loader'></span>");
Edrys.sendMessage("pause-timer", "");
if (Edrys.role === "station") {
// send the code through socket if connected
if (!socket || socket.readyState !== WebSocket.OPEN) {
Edrys.sendMessage("server-response", "Error: Server not connected!!");
} else {
socket.send(
JSON.stringify({
code: body,
challengeId: Edrys.module.challengeId,
})
);
}
}
break;
// Event listener to handle WebSocket responses
case "server-response":
displayMessage(body);
break;
}
});
// Function to change the theme toggle container background color
const changeTopBarBgColor = (theme) => {
const topBarContainer = document.querySelector(".top_bar_container");
theme === "vs-light" ? topBarContainer.style.backgroundColor = "#fffffe" : topBarContainer.style.backgroundColor = "#1e1e1e";
};