-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.mjs
More file actions
181 lines (177 loc) · 5.27 KB
/
Server.mjs
File metadata and controls
181 lines (177 loc) · 5.27 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import express from "express";
import fs from "fs";
import bodyParser from "body-parser";
import mysql from "mysql";
import path from "path";
var app = express();
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
port: "3306",
database: "cooperatedocument",
multipleStatements: true
});
var __dirname = path.resolve();
String.prototype.insert = function (index, content) {
return this.slice(0, index - 1) + content + this.slice(index - 1);
};
Object.prototype.followSet = function (changeSet2) {
changeSet2.action.forEach(step2 => {
this.followChain(step2);
});
};
Object.prototype.followChain = function (step2) {
//this.action = this.action.map(i => 1);
this.action = this.action.map(step1 => {
var temp1, temp2;
temp1 = followStep(step1, step2); //左
temp2 = followStep(step2, step1); //右
step1 = temp2;
step2 = temp1;
return step1;
});
};
//传入两个元操作对象
function followStep(step1, step2) {
if (step1 === null || step2 === null) {
return step2;
} else if (step1.type === "i" && step2.type === "i") {
if (step1.index <= step2.index) {
step2.index++;
} else {}
return step2;
} else if (step1.type === "d" && step2.type === "d") {
if (step1.index < step2.index) {
step2.index--;
} else if (step1.index > step2.index) {} else {
step2 = null;
}
return step2;
} else if (step1.type === "d" && step2.type === "i") {
if (step1.index < step2.index) {
step2.index--;
} else {}
return step2;
} else if (step1.type === "i" && step2.type === "d") {
if (step1.index <= step2.index) {
step2.index++;
} else {}
return step2;
}
}
//返回一个转化过 先step1 再step2 的元操作
function initText(name) {
connection.connect();
connection.query("show tables like '" + name + "' ;", function (
error,
result
) {
if (error) throw error;
var x = new Array();
x[0] = new Object();
if (result.length === 0) {
fs.readFile("./" + name + ".txt", "utf-8", function (err, data) {
x[0].version = 0;
x[0].text = data;
var createSql =
"CREATE TABLE `" +
name +
"` (`version`int NOT NULL,`text`text NULL,`changeset`text NULL) ;";
connection.query(
createSql +
"insert into " +
name +
' (version,text) values (0,"' +
data +
'");',
function (error, result) {
if (error) throw error;
console.log(result);
}
);
});
} else {
connection.query(
"select * from " +
name +
" where version =(select max(version) from " +
name +
");",
function (error, result) {
x[0].version = result[0].version;
x[0].text = result[0].text;
x[0].changeSet = result[0].changeSet;
}
);
}
test = x;
});
}
var name = "test1";
var test;
initText(name);
app.use(bodyParser.json());
app.listen(1109);
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.get("/test", function (req, res) {
res.sendFile(__dirname + "/test.html");
});
app.get("/getText", function (req, res) {
res.send(test[test.length - 1]);
});
function apply(base, changeSet) {
changeSet.action.forEach(element => {
if (element.type === "i") {
if (element.content == null) {
base = base.insert(element.index, "\n");
} else {
base = base.insert(element.index, element.content);
}
}
if (element.type === "d") {
base = base.slice(0, element.index) + base.slice(element.index + 1);
}
});
console.log(base);
return base;
}
app.post("/submit", function (req, res) {
// fs.writeFile('name.txt', req.body.content, function (err) {
// if (err) {
// console.log(err);
// }
// });.
console.log(req.body);
if (req.body.length != 0) {
var i = req.body.version + 1;
for (; i < test.length; i++) {
req.body.followSet(test[i].changeSet);
}
var next = {
version: test[test.length - 1].version + 1,
text: apply(test[test.length - 1].text, req.body),
changeSet: req.body
};
test.push(next);
connection.query(
"insert into " +
name +
" (version,text,changeset) values (" +
next.version +
',"' +
next.text +
"\",'" +
JSON.stringify(next.changeSet) +
"');",
function (error, result) {
if (error) throw error;
console.log(result);
}
);
console.log(test);
}
res.send("ok");
});