-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
294 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
{ | ||
"production":{ | ||
"host":"url", | ||
"host":"wendy.database.windows.net", | ||
"port": 1433, | ||
"dialect": "mssql", | ||
"username": "replace_username", | ||
"password": "replace_password", | ||
"dialectOptions":{ | ||
"encrypt": true, | ||
"database":"db" | ||
"database":"wendydb" | ||
} | ||
}, | ||
"development":{ | ||
"host":"url", | ||
"host":"wendy.database.windows.net", | ||
"port": 1433, | ||
"dialect": "mssql", | ||
"username": "replace_username", | ||
"password": "replace_password", | ||
"dialectOptions":{ | ||
"encrypt": true, | ||
"database":"db" | ||
"database":"wendydb" | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict' | ||
|
||
const later = require('later'); | ||
|
||
function GetOffSetTime(time, offsetTime) { | ||
return new Date(time + (offsetTime*3600000)).getTime(); | ||
} | ||
|
||
|
||
/** | ||
* 재충전 가능한 통화의 충전 처리 | ||
* 최대치 충전 주기나 재충전 주기를 고려한다. | ||
* @param OwnCurrencyData {Object} | ||
* @param GameUser {Object} | ||
* @param RechargeInfo {Object} | ||
* @param NowDate {Date} | ||
*/ | ||
exports.CheckForRecharge = (OwnCurrencyData, GameUser, RechargeInfo, NowDate)=>{ | ||
|
||
//재충전 가능한 통화인가? | ||
if(OwnCurrencyData.RechargeCurrencyID === null | ||
|| OwnCurrencyData.CurrentQNTY >= OwnCurrencyData.TotalQNTY) //혹은 최대치만큼 보유했는가? | ||
return {code:false}; | ||
|
||
let nowTime = NowDate.getTime(); | ||
let oldTime = new Date(OwnCurrencyData.UpdateTimeStamp).getTime(); | ||
let oldOffTime = GetOffSetTime(oldTime, GameUser.OffsetTime); | ||
let updateValue = {CurrentQNTY:null, UpdateTimeStamp:null}; | ||
|
||
//최대치 충전 주기가 있는가? | ||
if(RechargeInfo.SetMaxSwitch === true) { | ||
let convertLater = later.parse.cron(RechargeInfo.SetMaxPattern); | ||
let prevTime = later.schedule(convertLater).prev(); | ||
let prevOffsetTime = GetOffSetTime(prevTime.getTime(), GameUser.OffsetTime ); | ||
//UpdateTimeStamp가 prevTime보다 뒤에있는가? | ||
if(oldOffTime < prevOffsetTime) { | ||
updateValue.CurrentQNTY = OwnCurrencyData.TotalQNTY; | ||
updateValue.UpdateTimeStamp = NowDate; | ||
return {code:true, update:updateValue} | ||
} | ||
} | ||
|
||
//시간경과에 따른 업데이트 항목만 작동. | ||
let spendTime = (nowTime - oldTime)/1000; | ||
let rechargeAmount = Math.floor(spendTime / RechargeInfo.IntervalTime); | ||
|
||
let totalAmount = OwnCurrencyData.CurrentQNTY + rechargeAmount; | ||
|
||
|
||
//현재 보유할 수 있는 최대수량을넘어가는가? | ||
if(totalAmount >= OwnCurrencyData.TotalQNTY) { | ||
updateValue.CurrentQNTY = OwnCurrencyData.TotalQNTY; | ||
updateValue.UpdateTimeStamp = NowDate; | ||
return {code:true, update:updateValue} | ||
} | ||
//최대 수량에 미치지 못하나 충전할만한 량이 있을때 | ||
else if( rechargeAmount > 0) { | ||
updateValue.CurrentQNTY = totalAmount; | ||
updateValue.UpdateTimeStamp = | ||
new Date( | ||
oldTime + | ||
(rechargeAmount * RechargeInfo.IntervalTime)*1000 | ||
); | ||
return {code:true, update:updateValue} | ||
} | ||
|
||
return {code:false}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
module.exports = function(sequelize, DataTypes) { | ||
let DefineCurrency= sequelize.define('DefineCurrency', { | ||
CurrencyID : { type : DataTypes.INTEGER, primaryKey: true }, //고유한 번호를 할당해야한다. | ||
Name : { type:DataTypes.STRING(10) }, | ||
MaxQNTY : { type:DataTypes.INTEGER, defaultValue:9000 } | ||
}, { | ||
timestamps: false, | ||
tableName: 'DefineCurrency', | ||
classMethods: { | ||
associate: function (models) { | ||
//재충전되는 통화인 경우 해당 값을 가진다. | ||
DefineCurrency.belongsTo(models.DefineRechargeCurrency, { | ||
onDelete: "CASCADE", | ||
foreignKey: { | ||
name:'RechargeCurrencyID', | ||
allowNull: true | ||
}, | ||
as: 'RechargeInfo' | ||
}); | ||
} | ||
} | ||
}); | ||
return DefineCurrency; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
module.exports = function(sequelize, DataTypes) { | ||
let DefineRechargeCurrency= sequelize.define('DefineRechargeCurrency', { | ||
RechargeCurrencyID : { type : DataTypes.INTEGER, primaryKey: true}, | ||
IntervalTime : { type : DataTypes.INTEGER, defaultValue : 3600 }, | ||
IntervalChargeAmount : { type : DataTypes.INTEGER, defaultValue:1 }, | ||
SetMaxSwitch : { type : DataTypes.BOOLEAN, defaultValue:false }, | ||
SetMaxPattern : { //cron 표현식을 통해 최대치 충전 주기를설정한다. | ||
type : DataTypes.STRING(32), | ||
validate : { //반드시 올바른 형태의 입력인지 체크한다. | ||
is: /^\s*($|#|\w+\s*=|(\?|\*|(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?(?:,(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?)*)\s+(\?|\*|(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?(?:,(?:[0-5]?\d)(?:(?:-|\/|\,)(?:[0-5]?\d))?)*)\s+(\?|\*|(?:[01]?\d|2[0-3])(?:(?:-|\/|\,)(?:[01]?\d|2[0-3]))?(?:,(?:[01]?\d|2[0-3])(?:(?:-|\/|\,)(?:[01]?\d|2[0-3]))?)*)\s+(\?|\*|(?:0?[1-9]|[12]\d|3[01])(?:(?:-|\/|\,)(?:0?[1-9]|[12]\d|3[01]))?(?:,(?:0?[1-9]|[12]\d|3[01])(?:(?:-|\/|\,)(?:0?[1-9]|[12]\d|3[01]))?)*)\s+(\?|\*|(?:[1-9]|1[012])(?:(?:-|\/|\,)(?:[1-9]|1[012]))?(?:L|W)?(?:,(?:[1-9]|1[012])(?:(?:-|\/|\,)(?:[1-9]|1[012]))?(?:L|W)?)*|\?|\*|(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?(?:,(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?)*)\s+(\?|\*|(?:[0-6])(?:(?:-|\/|\,|#)(?:[0-6]))?(?:L)?(?:,(?:[0-6])(?:(?:-|\/|\,|#)(?:[0-6]))?(?:L)?)*|\?|\*|(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?(?:,(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?)*)(|\s)+(\?|\*|(?:|\d{4})(?:(?:-|\/|\,)(?:|\d{4}))?(?:,(?:|\d{4})(?:(?:-|\/|\,)(?:|\d{4}))?)*))$/ | ||
}, | ||
defaultValue : '0 0 12 * * ?' //기본값(매일 낮 12시) | ||
} | ||
|
||
}, { | ||
timestamps: false, | ||
tableName: 'DefineRechargeCurrency' | ||
}); | ||
return DefineRechargeCurrency; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
module.exports = function(sequelize, DataTypes) { | ||
let OwnCurrency= sequelize.define('OwnCurrency', { | ||
OwnCurrencyUID : { type : DataTypes.INTEGER, primaryKey: true, autoIncrement:true }, | ||
CurrencyID : { type : DataTypes.INTEGER }, | ||
CurrentQNTY : { type:DataTypes.INTEGER, defaultValue:0 }, //실제 현재보유수량 | ||
NowMaxQNTY : { type:DataTypes.INTEGER, defaultValue:100 }, //현재 최대보유수량 | ||
AddMaxQNTY : { type:DataTypes.INTEGER, defaultValue:0 }, //어떤 조건으로 추가되는 최대 보유수량 | ||
UpdateTimeStamp: { type:DataTypes.DATE, defaultValue:sequelize.NOW } | ||
}, { | ||
timestamps: false, | ||
tableName: 'OwnCurrency', | ||
classMethods: { | ||
associate: function (models) { | ||
//보유한 GameUserID 외래키 등록 | ||
OwnCurrency.belongsTo(models.GameUser, { | ||
onDelete: "CASCADE", | ||
foreignKey: { | ||
name:'GameUserID', | ||
allowNull: false | ||
} | ||
}); | ||
//보유한 CurrencyID 외래키 등록 | ||
OwnCurrency.belongsTo(models.DefineCurrency, { | ||
onDelete: "CASCADE", | ||
foreignKey: { | ||
name:'CurrencyID', | ||
allowNull: false | ||
} | ||
}); | ||
} | ||
}, | ||
getterMethods: { | ||
//전체 보유수량 = NowMaxQNTY + AddMaxQNTY | ||
TotalQNTY : function() { return this.NowMaxQNTY + this.AddMaxQNTY } | ||
} | ||
}); | ||
return OwnCurrency; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
'use strict' | ||
|
||
const debug = require('debug')('Wendy:router:currency'); | ||
const auth = require('../utils/auth'); | ||
const commonFunc = require('../utils/commonFunc'); | ||
const models = require("../models"); | ||
const wendyError = require('../utils/error'); | ||
const currencyLogic = require('../logics/currency'); | ||
|
||
const express = require('express'); | ||
const router = express.Router(); | ||
|
||
|
||
/** | ||
* @api {GET} /currency/define 통화 목록 요청 | ||
* @apiName 정의된 통화 목록 요청 | ||
* @apiHeader {String} Authorization JWT토큰을 전송 | ||
*/ | ||
router.get('/define', auth.isAuthenticated, (req, res, next)=>{ | ||
models.DefineCurrency.findAll({ | ||
include: [{ | ||
model: models.DefineRechargeCurrency, | ||
as: 'RechargeInfo' | ||
}] | ||
}) | ||
.then((defineCurrencyList)=>{ | ||
res.send({result:0, list:defineCurrencyList}); | ||
}) | ||
}); | ||
|
||
/** | ||
* @api {GET} /currency/own 보유 통화 목록 요청 | ||
* @apiName 보유 통화 목록 요청 | ||
* @apiHeader {String} Authorization JWT토큰을 전송 | ||
*/ | ||
router.get('/own', auth.isAuthenticated, (req, res, next) => { | ||
|
||
let saveGameUser; | ||
let saveOwnCurrencyList; | ||
|
||
//OwnCurrency 목록조회 | ||
models.OwnCurrency.findAll({ | ||
where: { GameUserID: req.user.GameUserID }, | ||
//재충전 계산이 필요할 수 있으니 DefineCurrency도 포함한다. | ||
include: [{ | ||
model: models.DefineCurrency, | ||
include: { | ||
model: models.DefineRechargeCurrency, | ||
as: 'RechargeInfo' | ||
} | ||
} | ||
] | ||
}) | ||
.then((ownCurrencyList) => { | ||
saveOwnCurrencyList = ownCurrencyList; | ||
//재충전 주기를 체크해야하는지 확인한다. | ||
for (let row of saveOwnCurrencyList) { | ||
if (row.DefineCurrency.RechargeCurrencyID !== null) | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject('pass'); | ||
}) | ||
//재충전 주기 확인 시 사용될 GameUser를 찾는다. | ||
.then(() => { | ||
return models.GameUser.findOne({ | ||
where: { GameUserID: req.user.GameUserID } | ||
}) | ||
}) | ||
.then((gameUser) => { | ||
saveGameUser = gameUser; | ||
return Promise.resolve(); | ||
}) | ||
//재충전 주기를 살필 통화가 있는지 확인한다. | ||
.then(() => { | ||
let nowDate = new Date(); | ||
let promises = []; | ||
let tempResult; | ||
for (let row of saveOwnCurrencyList) { | ||
if (row.DefineCurrency.RechargeCurrencyID !== null) { | ||
tempResult = currencyLogic.CheckForRecharge( | ||
row, | ||
saveGameUser, | ||
row.DefineCurrency.RechargeInfo, | ||
nowDate); | ||
|
||
if (tempResult.code === true) { | ||
promises.push( | ||
models.OwnCurrency.update( | ||
tempResult.update, | ||
{ where: { OwnCurrencyUID: row.OwnCurrencyUID } }) | ||
); | ||
} | ||
} | ||
} | ||
if (promises.length > 0) return Promise.all(promises); | ||
return Promise.resolve(); | ||
}) | ||
.catch((err) => { | ||
if (err === 'pass') return Promise.resolve(saveOwnCurrencyList); | ||
return Promise.reject(err); | ||
}) | ||
//업데이트된 항목이 있을 수 있으니 OwnCurrency를 다시 로딩한다. | ||
.then(() => { | ||
//OwnCurrencyUID를 Array로 뽑아서 쿼리에 사용한다. | ||
let OwnCurrencyUIDs = []; | ||
for (let row of saveOwnCurrencyList) { | ||
OwnCurrencyUIDs.push(row.OwnCurrencyUID); | ||
} | ||
return models.OwnCurrency.findAll({ | ||
where: { OwnCurrencyUID: { $in: OwnCurrencyUIDs } } | ||
}) | ||
}) | ||
.then((ownCurrencyList)=>{ | ||
res.send({result:0, list:ownCurrencyList}); | ||
}) | ||
.catch((err)=>{ | ||
next(err); | ||
}) | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters