Commit a2734daf authored by ganjingcun's avatar ganjingcun

投票统计

parent ffe402e9
...@@ -11,3 +11,4 @@ db.sql ...@@ -11,3 +11,4 @@ db.sql
upload/ upload/
plan.md plan.md
query.sql query.sql
test*
\ No newline at end of file
...@@ -236,17 +236,17 @@ router.post('/score', function (req, res) { ...@@ -236,17 +236,17 @@ router.post('/score', function (req, res) {
let expResult = utils.getExpScore(userA.exp, userB.exp, userscoreA, userscoreB) let expResult = utils.getExpScore(userA.exp, userB.exp, userscoreA, userscoreB)
// 3分钟以内结束的决斗,胜者不加DP,负者照常扣DP。 平局不扣DP不加DP : 把开始时间+3分钟,如果加完比结束时间靠后,说明比赛时间不足三分钟 // 3分钟以内结束的决斗,胜者不加DP,负者照常扣DP。 平局不扣DP不加DP : 把开始时间+3分钟,如果加完比结束时间靠后,说明比赛时间不足三分钟
var isLess3Min = moment(start).add(3, 'm').isAfter(moment(end)); // var isLess3Min = moment(start).add(3, 'm').isAfter(moment(end));
if (isLess3Min) { // if (isLess3Min) {
if (winner === usernameA) { // if (winner === usernameA) {
ptResult.ptA = userA.pt // ptResult.ptA = userA.pt
console.log(usernameA, '当局有人存在早退,胜利不加分', moment(start).format('YYYY-MM-DD HH:mm')) // console.log(usernameA, '当局有人存在早退,胜利不加分', moment(start).format('YYYY-MM-DD HH:mm'))
} // }
if (winner === usernameB) { // if (winner === usernameB) {
ptResult.ptB = userB.pt // ptResult.ptB = userB.pt
console.log(usernameB, '当局有人存在早退,胜利不加分', moment(start).format('YYYY-MM-DD HH:mm')) // console.log(usernameB, '当局有人存在早退,胜利不加分', moment(start).format('YYYY-MM-DD HH:mm'))
} // }
} // }
if (firstWin) { if (firstWin) {
if (winner === usernameA) { if (winner === usernameA) {
...@@ -894,6 +894,14 @@ router.post('/submitVote', function (req, res) { ...@@ -894,6 +894,14 @@ router.post('/submitVote', function (req, res) {
let user = req.body.user; let user = req.body.user;
let username = req.body.username; let username = req.body.username;
if (!user || !username || user == "undefined" || username == "undefined") {
var response = {};
response.code = 500;
res.json(response);
return
}
let voteid = req.body.voteid; let voteid = req.body.voteid;
let opid = req.body.opid; let opid = req.body.opid;
...@@ -937,7 +945,6 @@ router.post('/submitVote', function (req, res) { ...@@ -937,7 +945,6 @@ router.post('/submitVote', function (req, res) {
id = ${user} id = ${user}
where username = '${username}'`; where username = '${username}'`;
console.log(sql2);
async.waterfall([ async.waterfall([
function (callback) { function (callback) {
...@@ -947,11 +954,12 @@ router.post('/submitVote', function (req, res) { ...@@ -947,11 +954,12 @@ router.post('/submitVote', function (req, res) {
callback2(err); callback2(err);
}); });
}, function (err) { }, function (err) {
callback() callback(err)
}); });
}, },
function (callback) { function (callback) {
console.log(sql2);
client.query(sql2, function (err, result) { client.query(sql2, function (err, result) {
done() done()
callback(err) callback(err)
...@@ -971,7 +979,6 @@ router.post('/submitVote', function (req, res) { ...@@ -971,7 +979,6 @@ router.post('/submitVote', function (req, res) {
}); });
}); });
}); });
...@@ -1035,36 +1042,75 @@ router.get('/votes', function (req, res) { ...@@ -1035,36 +1042,75 @@ router.get('/votes', function (req, res) {
} }
var optionCountMap = {} var optionCountMap = {}
var voteCountMap = {}
var vates = result.rows; var vates = result.rows;
async.each(vates, function (vote, callback) { async.each(vates, function (vote, callback) {
var vateid = vote.id var vateid = vote.id
var options = JSON.parse(vote.options) var options = JSON.parse(vote.options)
async.each(options, function (option, callback2) { var option_ids = []
var queryVoteOptionCount = `SELECT count(*) from vote_result where vote_id='${vateid}' and option_id ='${option.key}'`
// console.log(queryVoteOptionCount) async.waterfall([
client.query(queryVoteOptionCount, function (err, result) { function (callback3) {
//call `done()` to release the client back to the pool
done() async.each(options, function (option, callback2) {
if (err) {
console.error('error running query', err) var queryVoteOptionCount = `SELECT count(*) from vote_result where vote_id='${vateid}' and option_id ='${option.key}' `
}
optionCountMap[option.key] = result.rows[0].count option_ids.push(String(option.key))
callback2(); // console.log(queryVoteOptionCount)
}); client.query(queryVoteOptionCount, function (err, result) {
//call `done()` to release the client back to the pool
done()
if (err) {
console.error('error running query', err)
}
optionCountMap[option.key] = result.rows[0].count
callback2();
});
}, function (err) {
if (err) {
console.error("get votes error :", err);
}
callback3()
});
},
function (callback3) {
var id_str = "("
_.each(option_ids, function (id) {
id_str = id_str + "'"+ id + "'" + ","
})
id_str = id_str.slice(0, -1)
id_str = id_str + ")"
var queryVoteCount = `SELECT count(DISTINCT userid) from vote_result where vote_id = '${vateid}' and option_id in ${id_str} `
console.log(queryVoteCount)
client.query(queryVoteCount, function (err, result) {
//call `done()` to release the client back to the pool
done()
if (err) {
console.error('error running query', err)
}
voteCountMap[vateid] = result.rows[0].count
callback3();
});
}, function (err) {
if (err) {
console.error("get votes error :", err);
} }
], function (err) {
callback() callback()
}); });
}, function (err) { }, function (err) {
if (err) { if (err) {
...@@ -1074,6 +1120,7 @@ router.get('/votes', function (req, res) { ...@@ -1074,6 +1120,7 @@ router.get('/votes', function (req, res) {
res.json({ res.json({
total: total - 0, total: total - 0,
data: result.rows, data: result.rows,
voteCountMap: voteCountMap,
optionCountMap: optionCountMap optionCountMap: optionCountMap
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment