forked from rayshen/ballgame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
340 lines (317 loc) · 10.9 KB
/
index.js
File metadata and controls
340 lines (317 loc) · 10.9 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
var express = require('express');
var bodyParser = require('body-parser');
var async = require('async');
var app = express();
var server = require('http').createServer(app);
var PORT = process.env.PORT || 8002;
server.listen(PORT);
console.log('Server running.');
//连接mysql
var mysql = require('mysql');
var db_config = {
host: 'localhost',
user: 'root',
password: 'root',
database:'ballgame',
port: 3306
};
function handleDisconnect() {
conn = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
conn.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
conn.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
console.log("Connect Mysql Success");
}
handleDisconnect();
//设定路由
//使用body-parser解析body参数
app.use(bodyParser.urlencoded({
extended: true
}));
/***创建比赛,获取id */
app.post('/creategame', function (req, res) {
res.contentType('json');//返回的数据类型
console.log("creategame called");
console.log(req.body);
if(req.body.userid == undefined){
res.send(JSON.stringify({error:"请传递userid"}));
return;
}
var userid = req.body.userid;
var createsql = 'INSERT INTO gameHistory (createTime) VALUES ('+ new Date().getTime()+')';
//创建一个比赛
conn.query(createsql, function (err, result) {
if (err){
console.log(err);
res.send(JSON.stringify({error:"error"}));
return;
};
var gameid = result.insertId;
var sqlsArray = [
'INSERT INTO gameUserSteps (gameid,userid,steps) VALUES ('+gameid+',"'+userid+'",0)',
'INSERT INTO gameUserShot (gameid,userid,shotSum) VALUES ('+gameid+',"'+userid+'",0)',
];
//初始化比赛数据
async.mapLimit(sqlsArray,sqlsArray.length,
function (sql, finishCallback) {
conn.query(sql, function (err, result) {
if (err){
console.log(err);
finishCallback(err);
return;
};
finishCallback(null,sql);
});
},
function (err, result) {
if(err){
console.log("error is:"+err);
res.send(JSON.stringify({error:"error"}));
return;
}
res.send(JSON.stringify({error:null,gameid:gameid}));
}
);
});
});
/** 设置运动员步数*/
app.post('/setusersteps', function (req, res) {
res.contentType('json');//返回的数据类型
console.log("setusersteps called");
console.log(req.body);
if(req.body.gameid == undefined || req.body.userid == undefined || req.body.steps == undefined){
res.send(JSON.stringify({error:"请求参数不完整"}));
return;
}
var gameid = req.body.gameid;
var userid = req.body.userid;
var steps = req.body.steps;
var sql = 'UPDATE gameUserSteps SET steps = '+steps+' where gameid='+gameid+' AND userid="'+userid+'"';
console.log(sql);
//select
conn.query(sql, function (err, result) {
if (err){
console.log(err);
res.send(JSON.stringify({error:"error"}));
return;
};
console.log(result);
res.send(JSON.stringify({error:null}));
});
});
/** 更新运动员投篮参数*/
app.post('/addusershot', function (req, res) {
res.contentType('json');//返回的数据类型
console.log("addusershot called");
console.log(req.body);
if(req.body.gameid == undefined || req.body.userid == undefined || req.body.shotIn == undefined){
res.send(JSON.stringify({error:"请求参数不完整"}));
return;
}
if(req.body.shotIn == 1 && (req.body.angle == undefined || req.body.distance == undefined)){
res.send(JSON.stringify({error:"请求参数不完整"}));
return;
}
var gameid = req.body.gameid;
var userid = req.body.userid;
var shotIn = req.body.shotIn;
var angle = req.body.angle;
var distance = req.body.distance;
var shotTime = new Date().getTime();
var sql;
if(shotIn == 0){
sql = 'INSERT gameUserShotDetail (gameid,userid,shotIn) VALUES ('+gameid+',"'+userid+'",0)';
}else{
sql = 'INSERT gameUserShotDetail (gameid,userid,shotIn,angle,distance,shotTime) VALUES ('+gameid+',"'+userid+'",1,'+angle+','+distance+','+shotTime+')';
}
console.log(sql);
//select
var sqlsArray = [
sql,
'UPDATE gameUserShot SET shotSum=shotSum+1 where gameid='+gameid+' AND userid="'+userid+'"'
];
//同步更新个人投篮总记录
async.mapLimit(sqlsArray,sqlsArray.length,
function (sql, finishCallback) {
conn.query(sql, function (err, result) {
if (err){
console.log(err);
finishCallback(err);
return;
};
finishCallback(null,sql);
});
},
function (err, result) {
if(err){
console.log("error is:"+err);
res.send(JSON.stringify({error:"error"}));
return;
}
res.send(JSON.stringify({error:null}));
}
);
});
/****************************查询接口 *****************************/
/** 查询最近场次的比赛*/
app.get('/recentgame', function (req, res) {
res.contentType('json');//返回的数据类型
console.log("recentgame called");
var sql = 'SELECT * FROM gameHistory ORDER BY id DESC';
console.log(sql);
//select
conn.query(sql, function (err, result) {
if (err){
console.log(err);
res.send(JSON.stringify({error:"error"}));
return;
};
if(result.length == 0){
res.send(JSON.stringify({error:"no found data"}));
return;
}
console.log(result[0]);
var gameInfo = {
error:null,
gameid:result[0].id,
createTime:result[0].createTime
}
res.send(JSON.stringify(gameInfo));
});
});
/** 查询某用户在某个gameid*/
app.get('/userSteps', function (req, res) {
res.contentType('json');//返回的数据类型
console.log("userSteps called");
if(req.query.gameid == undefined || req.query.userid == undefined){
res.send(JSON.stringify({error:"请传递完整参数"}));
return;
}
var gameid = req.query.gameid;
var userid = req.query.userid;
var sql = 'SELECT * FROM gameUserSteps WHERE gameid='+gameid+' AND userid="'+userid+'"';
console.log(sql);
//select
conn.query(sql, function (err, result) {
if (err){
console.log(err);
res.send(JSON.stringify({error:"error"}));
return;
};
if(result.length == 0){
res.send(JSON.stringify({error:"no found data"}));
return;
}
var userStepsInfo = {
error:null,
gameid:result[0].gameid,
userid:result[0].userid,
steps:result[0].steps
}
res.send(JSON.stringify(userStepsInfo));
});
});
/** 查询某用户某个比赛的投球情况*/
app.get('/userShotResults', function (req, res) {
res.contentType('json');//返回的数据类型
console.log("userShotResults called");
if(req.query.gameid == undefined || req.query.userid == undefined){
res.send(JSON.stringify({error:"请传递完整参数"}));
return;
}
var gameid = req.query.gameid;
var userid = req.query.userid;
var shotIn = req.query.shotIn;
var sql = 'select shotIn, COUNT(*) as NUM FROM gameUserShotDetail WHERE gameid='+gameid+' AND userid="'+userid+'" GROUP BY shotIn';
console.log(sql);
//select
conn.query(sql, function (err, result) {
if (err){
console.log(err);
res.send(JSON.stringify({error:"error"}));
return;
};
if(result.length == 0){
res.send(JSON.stringify({error:"no found data"}));
return;
}
//console.log(result);
//console.log(result.length);
var userShotResults;
if(result.length==2){
userShotResults= {
error:null,
shotNum:(result[0].NUM + result[1].NUM),
shotInNum:result[1].NUM,
}
}else if(result.length==1){
userShotResults= {
error:null,
shotNum:result[0].NUM,
shotInNum:result[0].shotIn == 0 ? 0:result[0].NUM,
}
}else{
userShotResults= {
error:null,
shotNum:0,
shotInNum:0,
}
}
res.send(JSON.stringify(userShotResults));
});
});
/** 查询某用户某比赛的投球细节*/
app.get('/userShotDetails', function (req, res) {
res.contentType('json');//返回的数据类型
console.log("userShotDetails called");
if(req.query.gameid == undefined || req.query.userid == undefined){
res.send(JSON.stringify({error:"请传递完整参数"}));
return;
}
var gameid = req.query.gameid;
var userid = req.query.userid;
var sql = 'SELECT * FROM gameUserShotDetail WHERE gameid='+gameid+' AND userid="'+userid+'"';
console.log(sql);
//select
conn.query(sql, function (err, result) {
if (err){
console.log(err);
res.send(JSON.stringify({error:"error"}));
return;
};
if(result.length == 0){
res.send(JSON.stringify({error:"no found data"}));
return;
}
var userShotDetails = {
error:null,
detail:result,
}
res.send(JSON.stringify(userShotDetails));
});
});
var fs = require('fs');
/** 主页*/
app.get('/online', function (req, res) {
fs.readFile('./shotBall/ball.html', 'utf-8',function (err, data) {//读取内容
if (err) throw err;
res.writeHead(200, {
"Content-Type": "text/html"
});
res.write(data);
res.end();
});
});