Commit 35f0b7b5 authored by ganjingcun's avatar ganjingcun

首胜和每月扣分

parent e49bd326
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
"jade": "~1.11.0", "jade": "~1.11.0",
"moment": "^2.18.1", "moment": "^2.18.1",
"morgan": "~1.7.0", "morgan": "~1.7.0",
"node-schedule": "^1.2.1",
"pg": "^6.1.0", "pg": "^6.1.0",
"serve-favicon": "~2.3.0", "serve-favicon": "~2.3.0",
"sqlite3": "3.1.8", "sqlite3": "3.1.8",
......
...@@ -35,6 +35,43 @@ pool.on('error', function (err, client) { ...@@ -35,6 +35,43 @@ pool.on('error', function (err, client) {
console.error('idle client error', err.message, err.stack) console.error('idle client error', err.message, err.stack)
}) })
// cron job
/**
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
*/
var schedule = require('node-schedule');
var j = schedule.scheduleJob('1 0 1 * *', function () {
console.log('The scheduleJob run on first day of every month!');
pool.connect(function (err, client, done) {
if (err) {
return console.error('error fetching client from pool', err);
}
var sql = `update user_info set pt = (pt - (pt - 500) * 0.4 )
where and pt > 500`
client.query(sql, function (err, result) {
done()
if (err) {
return console.error('error running monthly scheduleJob', err);
}
console.log(result)
});
})
});
// 数据迁移 rating_index => user_info // 数据迁移 rating_index => user_info
// router.get('/mr',function(req,res){ // router.get('/mr',function(req,res){
// pool.connect(function (err, client, done) { // pool.connect(function (err, client, done) {
...@@ -107,17 +144,26 @@ router.post('/score', function (req, res) { ...@@ -107,17 +144,26 @@ router.post('/score', function (req, res) {
// athletic = 竞技 entertain = 娱乐 // athletic = 竞技 entertain = 娱乐
if (arena === 'athletic') { if (arena === 'athletic') {
// select count(*) from battle_history where (usernameA = '爱吉' OR usernameB = '爱吉') and start_time > date '2017-02-09'
// 日首胜 每日0点开始计算 日首胜的话是额外增加固定4DP
var firstWin = false
var winner = "xxxxx"
var today = moment().format('YYYY-MM-DD')
// 真实得分 S(胜=1分,和=0.5分,负=0分) // 真实得分 S(胜=1分,和=0.5分,负=0分)
let sa = 0, sb = 0 let sa = 0, sb = 0
if (userscoreA > userscoreB) { if (userscoreA > userscoreB) {
sa = 1 sa = 1
paramA['athletic_win'] = 1 paramA['athletic_win'] = 1
paramB['athletic_lose'] = 1 paramB['athletic_lose'] = 1
winner = usernameA
} }
if (userscoreA < userscoreB) { if (userscoreA < userscoreB) {
sb = 1 sb = 1
paramA['athletic_lose'] = 1 paramA['athletic_lose'] = 1
paramB['athletic_win'] = 1 paramB['athletic_win'] = 1
winner = usernameB
} }
if (userscoreA === userscoreB) { if (userscoreA === userscoreB) {
sa = 0.5 sa = 0.5
...@@ -126,9 +172,31 @@ router.post('/score', function (req, res) { ...@@ -126,9 +172,31 @@ router.post('/score', function (req, res) {
paramB['athletic_draw'] = 1 paramB['athletic_draw'] = 1
} }
var queryFirsrWinSql = `select count(*) from battle_history where (usernameA = '${winner}' OR usernameB = '${winner}') and start_time > date '${today}' `
client.query(queryFirsrWinSql, function (err, result) {
done()
var total = 0;
if (!err) {
total = result.rows[0].count - 0
if (total == 0) {
firstWin = true
}
}
let ptResult = utils.getEloScore(userA.pt, userB.pt, sa, sb) let ptResult = utils.getEloScore(userA.pt, userB.pt, sa, sb)
let expResult = utils.getExpScore(userA.exp, userB.exp, userscoreA, userscoreB) let expResult = utils.getExpScore(userA.exp, userB.exp, userscoreA, userscoreB)
if (firstWin) {
if (winner === usernameA) {
ptResult.ptA += 4
console.log(usernameA,'首胜多加4DP')
}
if (winner === usernameB) {
ptResult.ptB += 4
console.log(usernameB,'首胜多加4DP')
}
}
queries.push(`update user_info set exp = ${expResult.expA}, pt = ${ptResult.ptA}, queries.push(`update user_info set exp = ${expResult.expA}, pt = ${ptResult.ptA},
athletic_win = athletic_win + ${paramA.athletic_win}, athletic_win = athletic_win + ${paramA.athletic_win},
athletic_lose = athletic_lose + ${paramA.athletic_lose}, athletic_lose = athletic_lose + ${paramA.athletic_lose},
...@@ -161,6 +229,17 @@ router.post('/score', function (req, res) { ...@@ -161,6 +229,17 @@ router.post('/score', function (req, res) {
'${end}' '${end}'
)`) )`)
queries.map(function (q) {
// console.log(q)
return client.query(q)
}).pop().on('end', function () {
console.log("finished update score !")
done()
})
});
} else { } else {
let expResult = utils.getExpScore(userA.exp, userB.exp, userscoreA, userscoreB) let expResult = utils.getExpScore(userA.exp, userB.exp, userscoreA, userscoreB)
...@@ -209,8 +288,6 @@ router.post('/score', function (req, res) { ...@@ -209,8 +288,6 @@ router.post('/score', function (req, res) {
'${end}' '${end}'
)`) )`)
}
queries.map(function (q) { queries.map(function (q) {
// console.log(q) // console.log(q)
return client.query(q) return client.query(q)
...@@ -219,6 +296,8 @@ router.post('/score', function (req, res) { ...@@ -219,6 +296,8 @@ router.post('/score', function (req, res) {
done() done()
}) })
}
}) })
client.query(`select * from user_info where username = '${usernameA}'`).on('end', function (result) { client.query(`select * from user_info where username = '${usernameA}'`).on('end', function (result) {
......
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
var superagent = require('superagent') var superagent = require('superagent')
var mement = require('moment')
// var url = 'https://mycard.moe/ygopro/api/score'
var url = 'https://mycard.moe/ygopro/api/score' var url = 'http://localhost:3000/api/score'
// var url = 'http://localhost:3000/api/score'
superagent superagent
.post(url) .post(url)
.send({ .send({
...@@ -17,8 +17,8 @@ superagent ...@@ -17,8 +17,8 @@ superagent
usernameB: "zh99998", usernameB: "zh99998",
userscoreA: 1, userscoreA: 1,
userscoreB: 2, userscoreB: 2,
start: '2016-12-19T12:31:13+08:00', start: mement().format(),
end: '2016-12-19T12:41:13+08:00', end: mement().format(),
arena: 'athletic' // 'athletic' 竞技 or 'entertain' 娱乐 arena: 'athletic' // 'athletic' 竞技 or 'entertain' 娱乐
}) })
.end(function (err, res) { .end(function (err, res) {
......
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