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
|
#!/usr/bin/env node
const express = require("express");
const http = require("http");
const socketio = require("socket.io");
const fs = require("fs");
const [DBFILE, PORT] = (() => {
if (process.argv.length == 3) {
return [process.argv[2], 8000];
} else if (process.argv.length == 4) {
return [process.argv[2], +process.argv[3]];
} else {
console.log("Usage: ./server.js <dbfile.txt> [port]");
process.exit(1);
}
})();
const app = express();
const httpServer = http.Server(app);
const io = socketio(httpServer);
const STATIC = __dirname + "/static";
// ------------------------------
// "database"
// ------------------------------
class Database {
#filename;
#list;
#lastbackup;
constructor(filename) {
this.#filename = filename;
this.#list = JSON.parse(fs.readFileSync(this.#filename));
this.#lastbackup = new Date();
}
add_new(string) {
for (let i = 0; i < this.#list.length; i++) {
if (this.#list[i][1] == string) {
return "already existed";
}
}
this.#list.push([0, string]);
this.#sort();
this.#persist();
return null;
}
set_votes(string, votes) {
for (let i = 0; i < this.#list.length; i++) {
if (this.#list[i][1] == string) {
this.#list[i][0] = votes;
this.#sort();
this.#persist();
return null;
}
}
return `'${string}' does not exist`;
}
remove(string) {
for (let i = 0; i < this.#list.length; i++) {
if (this.#list[i][1] == string) {
this.#list.splice(i, 1);
this.#persist();
return null;
}
}
return "does not exist";
}
get() {
return this.#list;
}
#sort() {
this.#list.sort((a, b) => {
if (a[0] != b[0]) return b[0] - a[0];
if (a[1] < b[1]) return -1;
if (a[1] > b[1]) return 1;
return 0;
});
}
#persist() {
fs.writeFile(this.#filename, JSON.stringify(this.#list), err => {
if (err != null) {
console.error("Error writing to file:", err);
console.log(JSON.stringify(this.#list));
}
});
const now = new Date();
if (now - this.#lastbackup > 60 * 1000) {
const dotidx = this.#filename.lastIndexOf(".");
const bakfname =
dotidx == -1
? this.#filename + now.getTime()
: this.#filename.slice(0, dotidx) + "." + now.getTime() + this.#filename.slice(dotidx);
fs.writeFile(bakfname, JSON.stringify(this.#list), err => {
if (err != null) {
console.error("Error copying to backup file:", err);
} else {
console.log(`Copied to ${bakfname}`);
}
});
this.#lastbackup = now;
}
}
};
const database = new Database(DBFILE);
// ------------------------------
// Express routes
// ------------------------------
app.use(express.static(STATIC));
app.get("/", (req, res) => {
res.sendFile(STATIC + "/index.html");
});
// ------------------------------
// socket.io bindings
// ------------------------------
let next_socket_id = 1;
let num_connected = 0;
io.on("connection", socket => {
const socket_id = next_socket_id++;
if (++num_connected >= 2) {
socket.emit("alert", `You're user number ${num_connected}, multi-user settings will mess up!`);
}
console.log(`new socket connection id=${socket_id} (nc=${num_connected})`);
socket.on("disconnect", () => {
num_connected--;
console.log(`disconnect id=${socket_id} (nc=${num_connected})`);
});
socket.on("new", string => {
const res = database.add_new(string);
if (res != null) socket.emit("error", res);
});
socket.on("set_votes", (string, votes) => {
const res = database.set_votes(string, votes);
if (res != null) socket.emit("error", res);
});
socket.on("delete", string =>{
const res = database.remove(string);
if (res != null) socket.emit("error", res);
});
socket.emit("init", database.get());
});
// ------------------------------
// setup
// ------------------------------
const server = httpServer.listen(PORT, () => {
const host = server.address().address;
const port = server.address().port;
console.log("Server listening at http://" + host + ":" + port);
});
|