-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (123 loc) · 3.44 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
129
130
131
132
133
134
135
136
137
const findpath = require('./find.js')
const express = require('express')
const cookieParser = require('cookie-parser')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(cookieParser("100 ruppey ki pepsi, judges are sexy"))
app.get('/login', (req, res) => {
if (req.signedCookies.user) return res.redirect('/')
res.sendFile(__dirname + '/public/login.html')
})
app.post('/login', (req, res) => {
if (req.signedCookies.user) return res.redirect('/')
var username = req.body.username
var password = req.body.password
// console.log('username: ' + username)
// console.log('password: ' + password)
if (username == 'admin' && password == 'password') {
res.cookie('user', 'admin', { signed: true })
// console.log('cookie created successfully')
res.redirect('/')
} else {
// console.log('incorrect username or password')
res.redirect('/login')
}
})
app.get('/logout', (req, res) => {
res.clearCookie('user')
res.redirect('/login')
})
app.get('/', (req, res) => {
if (req.signedCookies.user == undefined) {
res.redirect('/login')
return;
}
res.sendFile(__dirname + '/public/main.html')
})
function renderMap(omap, path) {
const nmap = [...omap];
var start = null;
for (let row = 0; row < nmap.length; row++) {
nmap[row] = [...nmap[row]];
for (let col = 0; col < nmap[row].length; col++) {
if (nmap[row][col] === "s") {
nmap[row][col] = "start"
start = [row, col];
}
if (nmap[row][col] === "e") {
nmap[row][col] = "end"
}
if (nmap[row][col] != "start" & nmap[row][col] != "end" & nmap[row][col] != " "){
nmap[row][col] = "active"
}
if (nmap[row][col] === " ") {
nmap[row][col] = "empty"
}
}
}
var row = start[0];
var col = start[1];
for (let i = 0; i < path.length; i++) {
if (path[i] === "down") {
row += 1;
nmap[row][col] = "path";
} else if (path[i] === "up") {
row -= 1;
nmap[row][col] = "path";
} else if (path[i] === "right") {
col += 1;
nmap[row][col] = "path";
} else if (path[i] === "left") {
col -= 1;
nmap[row][col] = "path";
} else if (path[i] === "down-right") {
row += 1;
col += 1;
nmap[row][col] = "path";
} else if (path[i] === "up-left") {
row -= 1;
col -= 1;
nmap[row][col] = "path";
} else if (path[i] === "down-left") {
row += 1;
col -= 1;
nmap[row][col] = "path";
} else if (path[i] === "up-right") {
row -= 1;
col += 1;
nmap[row][col] = "path";
}
}
return nmap;
}
app.get("/resources", (req, res) => {
if (req.signedCookies.user == undefined) {
res.redirect('/login')
return;
}
res.sendFile(__dirname + '/public/resourcelist.html')
})
app.post('/solve', async (req, res) => {
if (req.signedCookies.user == undefined) {
res.redirect('/login')
return;
}
var grid = req.body.grid
var grid = grid.reduce((result, value, index, array) => {
const chunkIndex = Math.floor(index / req.body.width)
if (!result[chunkIndex]) {
result[chunkIndex] = []
}
result[chunkIndex].push(value)
return result
}, [])
var path = await findpath(grid)
if (!path) return res.send({ path: null, map: null })
var map = await renderMap(grid, path)
res.send({ path, map })
})
app.use(express.static('public'))
app.listen(3000, () => {
console.log('listening on port 3000')
})