Repository navigation
Expand file tree
/
Copy pathindex-server.js
More file actions
166 lines (117 loc) · 3.87 KB
/
Copy pathindex-server.js
File metadata and controls
166 lines (117 loc) · 3.87 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
//SERVER SIDE CODE THAT INTERACTS WITH THE BACKEND JS CALCULATIONS AND THE WEBAPP
//Written by Milk
//// IMPORTS ////
//import express
const express = require('express');
const app = express();
app.use(express.static(__dirname));
app.use('favicon.ico', express.static('favicon.ico'));
//import http server
const http = require('http');
const server = http.createServer(app);
let PORT = 8080;
//import socket io library for communication
const { Server } = require("socket.io");
const io = new Server(server);
//file I/O
const fs = require('fs');
//import level imports
const jsonjs = require('./js/json_io');
//import execution code
const execjs = require('./js/exec');
//import gui code
const guijs = require('./js/gui');
//import simulation code
const simjs = require('./js/simulation')
//// SETUP THE SERVER ////
//connect the app to the html
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
//initial connection onstart
io.on('connection', (socket) =>{
//send the list of possible level sets + agents
io.emit('level-set-list', jsonjs.importLevelSets());
io.emit('agent-list', jsonjs.getAgentList());
});
//// FUNCTION DEFINITIONS ////
// IMPORT THE AGENT REPORT
function sendReport(dat){
let rep = jsonjs.importALSReport(dat['agent'],dat['levelSet']);
if (rep != null){
//add ascii maps (not included in report)
for(let i=0;i<rep.length;i++){
let l = jsonjs.getLevel(dat['levelSet'],rep[i]['id']);
if(l == null){continue;}
let am = l['ascii'];
rep[i]['ascii_map'] = am;
}
io.emit('return-agent-json',rep);
}else{
console.log(`- No JSON found for agent [ ${dat['agent']} ] ON LEVEL SET [ ${dat['levelSet']} ]... - `)
}
}
// SOLVE A LEVEL BY ID # FOR A LEVEL SET
function solveLevel(ls,id,agent){
io.emit('pending-level',id);
let res = execjs.solveLevel(ls,id,agent);
io.emit('finish-level',res);
console.log(`* FINISHED LEVEL [ ${id} ] *`)
}
//// SERVER INTERACTIONS ////
//start socket connection
io.on('connection', (socket) => {
//get the level set for the agent to populate the level table
socket.on('get-level-set', (dat) =>{
let ls = dat['levelSet'];
console.log(`-- RETRIEVING LEVEL SET [ ${ls} ] --`)
let j = jsonjs.getLevelSet(ls);
if(j != null){
io.emit('return-level-json', j);
}else{
console.log("ERROR: JSON NOT FOUND!");
io.emit('level-set-404');
}
})
//get agent json
socket.on('get-agent-json', (dat) =>{
console.log(`-- RETRIEVING AGENT REPORT [ ${dat['agent']} ]`)
sendReport(dat);
});
//start training an agent on specific level set
socket.on('solve-level', (dat) => {
console.log(`-- SOLVING LEVEL [ ${dat['levelID']} ] FROM LEVEL SET [ ${dat['levelSet']} ] WITH AGENT [ ${dat['agent']} ] --`);
solveLevel(dat['levelSet'],dat['levelID'],dat['agent']);
});
//delete a json report for a specific agent and level set
socket.on('delete-json-set', (dat) =>{
jsonjs.deleteALSReport(dat['agent'],dat['levelSet']);
console.log(`-- DELETED REPORT FOR [ ${dat['agent']} ] ON LEVEL SET [ ${dat['levelSet']} ] --`)
io.emit('reset-agent-json', []);
});
//initialize the ascii map
socket.on('reset-map', (dat) =>{
console.log(`LOADING NEW MAP: \n${dat['ascii_map']}`);
guijs.initMap(dat['ascii_map']);
});
//go through next step and update the map
socket.on('step-map', (dat) =>{
let d = guijs.updateMap(dat['step']);
io.emit('new-map', d);
});
//get the map key dictionary
socket.on('get-map-key', () =>{
let mk = simjs.getMapKey();
io.emit('ret-map-key',mk);
});
//check if the agent and level set exist
socket.on('als_exists',(dat) =>{
if(!jsonjs.agent_exists(dat["agent"])){io.emit('agent-404');}
if(!jsonjs.level_set_exists(dat["levelSet"])){io.emit('level-set-404');}
io.emit("set-als")
});
});
//listen on port 'localhost:8080' for incoming sockets
server.listen(PORT, () => {
console.log(`listening on localhost: ${PORT} ...`);
});