-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdMapCRUD.js
286 lines (224 loc) · 9.27 KB
/
oldMapCRUD.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
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
///////////////////
//Actual map data//
///////////////////
//Insert fullmap here with a name of fullMap
//var fullmap = {...};
//Save frequently and have a backup
///////////////////////
//Vertex CRUD methods//
///////////////////////
//add a new vertex to the map
function addVertex() {
var vertName = prompt("input Vertex Name");
if (!fullMap[vertName]) { //only if it does not exist
var faculty = prompt("input Faculty: \nDefault is engineering", "Engineering");
var locationType = prompt("input type of location, e.g. Building, Junction, Busstop", "Busstop");
var coordinates = prompt("input latlong\nlat followed by a comma and space then long"); //copy from google maps or map box
var latlong = coordinates.split(", "); //", " to make copying easier
//add vertex object
fullMap[vertName] = {
'faculty': faculty, //change as needed for different faculties
'locationType': locationType,
'latlongCoordinates': [parseFloat(latlong[0]), parseFloat(latlong[1])],
'roomList': [],
'edges': {}
};
console.log(vertName + " : " + JSON.stringify(fullMap[vertName]));
console.log("Vertex " + vertName + " added");
} else {
console.log("vertex already exists ");
}
}
//edits some attributes of the vertex
function editVertex() {
var vertName = prompt("input Vertex Name");
if (fullMap[vertName]) { //only if it exists
var faculty = prompt("input Faculty: \nDefault is engineering", "Engineering");
var locationType = prompt("input type of location, e.g. Building, Junction, Busstop", "Busstop");
var coordinates = prompt("input latlong\nlat followed by a comma and space then long"); //copy from google maps or map box
var latlong = coordinates.split(", "); //", " to make copying easier
fullMap[vertName].faculty = faculty;
fullMap[vertName].locationType = locationType;
fullMap[vertName].latlongCoordinates = [parseFloat(latlong[0]), parseFloat(latlong[1])];
console.log(vertName + " : " + JSON.stringify(fullMap[vertName]));
console.log("Vertex " + vertName + " edited");
} else {
console.log(vertName + " does not exist");
}
}
//reads the vertex
function readVertex() {
var vertName = prompt("input Vertex Name");
if (fullMap[vertName]) { //only if it exists
vertExists = true;
console.log(vertName + " : " + JSON.stringify(fullMap[vertName]));
} else {
console.log(vertName + " does not exist");
}
}
//remove a vertex from the map
function removeVertex() {
var vertName = prompt("input Vertex Name");
if (fullMap[vertName]) { //only if it exists
delete fullMap[vertName];
console.log(vertName + " removed ");
} else {
console.log(vertName + " does not exist");
}
}
function readMap() {
console.log(JSON.stringify(fullMap));
}
/////////////////////////////
//Paired Edges CRUD methods//
/////////////////////////////
//returns true only if both vertexes are linked both ways
function areVertexesLinked(name1, name2) {
var one = false;
var two = false;
if (fullMap[name1] && fullMap[name1].edges[name2]) {
one = true;
}
if (fullMap[name2] && fullMap[name2].edges[name1]) {
two = true;
}
console.log("name1" + " to " + name2 + " exists " + one);
console.log("name2" + " to " + name1 + " exists " + two);
return one && two;
}
//input edges between two vertices both ways
function addEdges() {
var name1 = prompt("from vertex");
var name2 = prompt("to vertex");
var walkingSpeed = 1.4;
//if both directions do not exist
if (areVertexesLinked(name1, name2) === false) {
//input direction is name1 to name2
//var time = parseFloat(prompt("timetaken"));
var distance = parseFloat(prompt("distance"));
var stairsDirection = parseFloat(prompt("number of flights of stairs diff\ndown 0, flat ground 1, up 2, \ndefault is flat ground", 1));
var shelterMultiplier = parseFloat(prompt("shelter 0 for sheltered, 1 not sheltered\n default is sheltered ", 0));
//from name 1 to name 2
var weightedStairs = (stairsDirection === 2) ? stairsDirection * distance : distance;
//add forward direction edge
if (fullMap[name1]) { //if from vertex exists
fullMap[name1].edges[name2] = {
'time': distance / walkingSpeed / 60,
'distance': distance,
'stairsDirection': stairsDirection,
'stairsWeight': weightedStairs,
'shelterRating': shelterMultiplier * distance,
'shelterMultiplier': shelterMultiplier,
'directions': 'Insert directions',
'imageLinks': []
};
console.log(name1 + " to " + name2 + " " + JSON.stringify(fullMap[name1].edges[name2]));
} else {
console.log(name1 + " does not exist");
}
//add reverse direction edge
if (fullMap[name2]) { //if to vertex exists
var flippedDirection = (stairsDirection === 1) ? 1 : ((stairsDirection === 0) ? 2 : 0);
var weightedStairs = (flippedDirection === 2) ? flippedDirection * distance : distance;
//add reverse direction edge
fullMap[name2].edges[name1] = {
'time': distance / walkingSpeed / 60,
'distance': distance,
//if it is 0 , dont change, else swap between 0(down) and 2(up)
'stairsDirection': flippedDirection,
'stairsWeight': weightedStairs,
'shelterRating': shelterMultiplier * distance,
'shelterMultiplier': shelterMultiplier,
'directions': 'Insert directions',
'imageLinks': []
};
console.log(name2 + " to " + name1 + " " + JSON.stringify(fullMap[name2].edges[name1]));
} else {
console.log(name2 + " does not exist");
}
console.log(name1 + " <-> " + name2);
console.log("2 edges added")
} else {
console.log("pair of edges" + name1 + " <-> " + name2 + "already exists")
}
}
//edits both edges of 2 vertices
function editEdges() {
var name1 = prompt("from vertex");
var name2 = prompt("to vertex");
var walkingSpeed = 1.4;
//if both directions edges exist
if (areVertexesLinked(name1, name2) === true) {
//input direction is name1 to name2
//var time = parseFloat(prompt("timetaken"));
var distance = parseFloat(prompt("distance"));
var stairsDirection = parseFloat(prompt("number of flights of stairs diff\ndown 0, flat ground 1, up 2, \ndefault is flat ground", 1));
var shelterMultiplier = parseFloat(prompt("shelter 0 for sheltered, 1 not sheltered\n default is sheltered ", 0));
//edit forward direction
fullMap[name1].edges[name2].time = distance / walkingSpeed / 60;
fullMap[name1].edges[name2].distance = distance;
var weightedStairs = (stairsDirection === 2) ? stairsDirection * distance : distance;
fullMap[name1].edges[name2].stairsWeight = weightedStairs;
fullMap[name1].edges[name2].stairsDirection = stairsDirection;
fullMap[name1].edges[name2].shelterRating = shelterMultiplier * distance;
fullMap[name1].edges[name2].shelterMultiplier = shelterMultiplier;
console.log(name1 + " to " + name2 + " " + JSON.stringify(fullMap[name1].edges[name2]));
//edit reverse direction
fullMap[name2].edges[name1].time = distance / walkingSpeed / 60;
fullMap[name2].edges[name1].distance = distance;
//if it is 1 , dont change, else swap between 0(down) and 2(up)
var flippedDirection = (stairsDirection === 1) ? 1 : ((stairsDirection === 0) ? 2 : 0);
weightedStairs = (flippedDirection === 2) ? flippedDirection * distance : distance;
fullMap[name2].edges[name1].stairsWeight = weightedStairs;
fullMap[name2].edges[name1].stairsDirection = flippedDirection;
fullMap[name2].edges[name1].shelterRating = shelterMultiplier * distance;
fullMap[name2].edges[name1].shelterMultiplier = shelterMultiplier;
console.log(name2 + " to " + name1 + " " + JSON.stringify(fullMap[name2].edges[name1]));
console.log("2 edges edited")
} else {
console.log("the 2 vertices are not linked")
}
console.log(name1 + " <-> " + name2);
}
//reads both edges of 2 vertices
function readEdges() {
var name1 = prompt("from vertex");
var name2 = prompt("to vertex");
//if both directions edge exist
if (areVertexesLinked(name1, name2) === true) {
//name1 edge to name 2
console.log(name1 + " to " + name2 + " " + JSON.stringify(fullMap[name1].edges[name2]));
//name2 edge to name 1
console.log(name2 + " to " + name1 + " " + JSON.stringify(fullMap[name2].edges[name1]));
console.log("2 edges read")
} else {
console.log("either the edges do not exist or they are not linked");
}
console.log(name1 + " <-> " + name2);
}
//removes both edges connceting 2 vertices
function removeEdges() {
var name1 = prompt("from vertex");
var name2 = prompt("to vertex");
//if both directions edge exist
if (areVertexesLinked(name1, name2) === true) {
console.log(fullMap[name1].edges[name2])
//delete name1 edge to name2
delete fullMap[name1].edges[name2];
//delete name2 edge to name1
delete fullMap[name2].edges[name1];
console.log("2 edges removed")
} else {
console.log("either the edges do not exist or they are not linked");
}
console.log(name1 + " <-> " + name2);
}
function changeWalkingSpeed() {
var walkingSpeed = prompt("Input the new walking speed in m/s?", 1.4);
for (v in fullMap) {
for (e in fullMap[v].edges) {
var time = fullMap[v].edges[e].distance / walkingSpeed / 60;
fullMap[v].edges[e].time = time;
}
}
}