-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
199 lines (175 loc) · 5.65 KB
/
server.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
const express = require("express");
const app = express();
const server = require("http").Server(app);
const io = require("socket.io")(server);
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname + "/views"));
let cities = [
{
name: "Los Angeles",
state: "CA",
country: "USA",
capital: false,
population: 3900000,
isDelete: false,
},
// other city documents
];
// Keep track of all cities in a dictionary
// Keep track of all clients that have subscribed to dynamic queries
let dynamicQuerySubscribers = {};
// Listen for connections from clients
io.on("connection", (socket) => {
console.log("A client has connected");
// Send the current list of cities to the new client
socket.on("init", () => {
console.log("server init called");
// emit the allCities event to the client with the list of all cities
socket.emit("init", cities);
});
// Listen for 'create' events from clients
socket.on("create", (city) => {
console.log("Creating a new city:", city);
city.isDelete = false;
// Add the new city to the dictionary and broadcast the update to all clients
isAddOrEditDelete(city);
io.emit("create", city);
// Check if the new city satisfies any dynamic queries
checkDynamicQueries(city);
});
// Listen for 'update' events from clients
socket.on("update", (city) => {
console.log("Updating city:", city);
// Update the city in the dictionary and broadcast the update to all clients
isAddOrEditDelete(city);
io.emit("update", city);
// Check if the updated city satisfies any dynamic queries
checkDynamicQueries(city);
});
// Listen for 'delete' events from clients
socket.on("delete", (name) => {
console.log("Deleting city:", name);
// Remove the city from the dictionary and broadcast the update to all clients
let del = isAddOrEditDelete({ name }, true);
del[0].isDelete = true;
console.log(`del : ${del}`);
console.log(del);
io.emit("delete", name);
// Check if the deletion of the city satisfies any dynamic queries
checkDynamicQueries(del[0]);
});
// Listen for 'query' events from clients
socket.on("query", (query) => {
console.log("Querying cities:", query);
// Filter the list of cities based on the query and send the result to the client
let result = filterCities(query);
socket.emit("query", result);
});
// Listen for 'subscribe' events from clients
socket.on("subscribe", (query) => {
const key = JSON.stringify(query);
console.log("Client subscribed to dynamic query:", key);
// Add the client to the list of subscribers for the given query
if (!dynamicQuerySubscribers[key]) {
dynamicQuerySubscribers[key] = [];
}
dynamicQuerySubscribers[key].push(socket);
});
// Listen for 'unsubscribe' events from clients
socket.on("unsubscribe", (query) => {
const key = JSON.stringify(query);
console.log("Client unsubscribed from dynamic query:", key);
// Remove the client from the list of subscribers for the given query
try {
let index = dynamicQuerySubscribers[key].indexOf(socket);
dynamicQuerySubscribers[key].splice(index, 1);
} catch (error) {}
});
});
function doesCitySatisfyQuery(city, query) {
for (let key in query) {
if (!city.hasOwnProperty(key)) {
return false;
}
const cityValue = city[key];
const queryValue = query[key];
console.log(cityValue, queryValue);
if (queryValue) {
console.log(cityValue, queryValue);
if (typeof cityValue === "string") {
if (cityValue.toLowerCase() !== queryValue.toLowerCase()) {
return false;
}
} else if (typeof cityValue === "number") {
if (cityValue !== queryValue) {
return false;
}
} else if (typeof cityValue === "boolean") {
if (cityValue !== queryValue) {
return false;
}
} else {
// Handle other data types as needed
}
}
}
return true;
}
// Helper function to filter the list of cities based on the query
function filterCities(query, city) {
let filteredCities = [];
try {
filteredCities = city === undefined ? cities : [city];
filteredCities = filteredCities.filter((c) =>
doesCitySatisfyQuery(c, query)
);
return filteredCities;
} catch (error) {
console.log(error);
return filteredCities;
}
}
// Helper function to check if a city satisfies any dynamic queries
function checkDynamicQueries(city, event) {
for (let query in dynamicQuerySubscribers) {
let match = filterCities(JSON.parse(query), city).length > 0;
if (match) {
// The city satisfies the query, so emit it to all subscribers
let subscribers = dynamicQuerySubscribers[query];
for (let i = 0; i < subscribers.length; i++) {
subscribers[i].emit("dynamic query", city);
}
}
}
}
function isAddOrEditDelete(city, isDelete = false) {
const index = cities.findIndex((c) => c.name.toLowerCase() == city.name.toLowerCase());
if (isDelete) {
return cities.splice(index, 1);
} else {
console.log(`${index} ${city.name}`)
if (index !== -1) {
cities[index] = city;
} else {
cities.push(city);
}
}
}
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/listCity.html");
});
app.get("/createCity", (req, res) => {
res.render("createCity");
});
app.get("/updateCity/:name", (req, res) => {
const city = cities.find((c) => c.name === req.params.name);
if (city) {
res.render("updateCity", { city });
} else {
res.send("City not found");
}
});
server.listen(8080, () => {
console.log("Server listening on port 3000");
});