From a1c65c3c9299e4c2d29960851eec603d1ac37ab8 Mon Sep 17 00:00:00 2001 From: totuworld Date: Wed, 4 Jan 2017 11:08:18 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9E=AC=EC=B6=A9=EC=A0=84=20=ED=86=B5?= =?UTF-8?q?=ED=99=94=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 재충전 통화 정의 테이블 추가 * 통화 정의 테이블 수정 * later.js 모듈 추가 --- README.md | 8 ++- docs/Wanlaf.png | Bin 0 -> 8067 bytes logics/currency.js | 68 +++++++++++++++++++++++ models/DefineCurrency.js | 21 +++++-- models/DefineRechargeCurrency.js | 22 ++++++++ models/OwnCurrency.js | 11 +++- package.json | 1 + routes/currency.js | 92 +++++++++++++++++++++++++++++-- yarn.lock | 4 ++ 9 files changed, 215 insertions(+), 12 deletions(-) create mode 100644 docs/Wanlaf.png create mode 100644 logics/currency.js create mode 100644 models/DefineRechargeCurrency.js diff --git a/README.md b/README.md index 82dd90e..76cf27c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +logo + # Wendy 소규모 게임 개발팀을 위한 게임 서버 @@ -9,7 +11,7 @@ Wendy는 게임을 운영할 때 필요한 기능을 포함한 게임 서버입 이런 문제에서 소규모 게임 개발팀이 조금이나마 도움이 되길 원합니다. ### 커뮤니티 -* 향후 트위터나 페이스북등의 공식채널을 제공합니다. +* twitter : [@magmakick](https://twitter.com/magmakick) * 버그나 이슈는 [issue tracker](https://github.com/totuworld/Wendy/issues)에 등록해주세요. * 새로운 기능이나 업데이트가 있으시다면 pull request 보내주세요. :) @@ -26,6 +28,7 @@ yarn 패키지 매니저를 통해 설치를 진행합니다. yarn이 이미 설 $ npm install -g yarn $ yarn install ``` + ### 실행방법 실행 방법은 아주 간단합니다. @@ -41,8 +44,9 @@ Wendy가 지원할 필수 기능입니다. - [x] 기기정보 관리 - [x] 사용자 정보 관리 -- [ ] 재화 관리 +- [x] 통화 관리 - [ ] 아이템 관리 +- [ ] 지급 관리 - [ ] 인앱 영수증 검증 ### 부가 기능 diff --git a/docs/Wanlaf.png b/docs/Wanlaf.png new file mode 100644 index 0000000000000000000000000000000000000000..3c7ab5ce6976181357a262224a71ddee0db3098c GIT binary patch literal 8067 zcmbt(^;gu-_x~-lKziwt2FayMLRw0sRuB+fVTl(ZwF^i|E-kGBN|%HnF5RGl zbSxkxpp>-o<^BB^K0nOdGjs2mnR{-XdE8i2V_j-WR!R^EM6IuNYi;wXk4M?B znay3|Z^zrty%$=&r&ydPlw_7U+SP1WS|VQ55cfpWUNe$!1y31D@h#?tHlNp4TlEzh ziA1{n{xCTcRPbRk^z%mGRlUU0gKgX250)06R?jVc=cfgn)L}7TDK1Dimifm2ZJ{0u z3ohzzNK0b_9JOn{Kizz>#PA9gXveDxVJt>D7(vzW%*@Gvf62|Q$Zc@Mc8zKx6!cUE zC7ZipWBi-Dy@fh}ZNn2RjkU&kpak}4x%RVg$|6yHGdo%C5y=Pris*1>#obxZQibYJ zsiX(v7wRwX9ZB-RC7^Mf2}nPhB^lVT26fSX=O9di^882%P44k^H?p5I7HZL_;smiL zVEtHFKW?FaeJ&OLQBb{(s^Eo(Ix~LRsG5rzSFDm*T@1?jce1 z!NM4_8hOI+&{*bM>0m#uzhaV*(;La-AAJbXi_`>e!d}D_FR(G%x!BYkK>>pwn zT8$pqq1lv%^XbjHVVx_MNKe@XOU05Rm^w~aKz$$iod~bMa;alrg-1Mny9j8oh$A%` zuGfvF1#~sGsnrv=X`nkJ|Hw@LIE0vTx%Yqy4xGWrs?UhFmj`F6U)YjkaAPc|K?tw7 z!d!V6+(F02ODE?o1&|AJl(oUaHqt#unL;a~_D>g}&7kBTeT2c7Cw`${g-X7#Y`%nv zLhKd^cDr5KI!k~Y8S4w5Jo^>PtjRX}Nxiz!9>QOEqRxi>7*^5vI8$rAufq?LYi-qY z!XR~rVb~MF;{bo?Cq?d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 + + //재충전 가능한 통화인가? + 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}; +} \ No newline at end of file diff --git a/models/DefineCurrency.js b/models/DefineCurrency.js index da71f39..e9d4c03 100644 --- a/models/DefineCurrency.js +++ b/models/DefineCurrency.js @@ -2,12 +2,25 @@ 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} + CurrencyID : { type : DataTypes.INTEGER, primaryKey: true }, //고유한 번호를 할당해야한다. + Name : { type:DataTypes.STRING(10) }, + MaxQNTY : { type:DataTypes.INTEGER, defaultValue:9000 } }, { timestamps: false, - tableName: 'DefineCurrency' + tableName: 'DefineCurrency', + classMethods: { + associate: function (models) { + //재충전되는 통화인 경우 해당 값을 가진다. + DefineCurrency.belongsTo(models.DefineRechargeCurrency, { + onDelete: "CASCADE", + foreignKey: { + name:'RechargeCurrencyID', + allowNull: true + }, + as: 'RechargeInfo' + }); + } + } }); return DefineCurrency; }; \ No newline at end of file diff --git a/models/DefineRechargeCurrency.js b/models/DefineRechargeCurrency.js new file mode 100644 index 0000000..f1b5fed --- /dev/null +++ b/models/DefineRechargeCurrency.js @@ -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; +}; \ No newline at end of file diff --git a/models/OwnCurrency.js b/models/OwnCurrency.js index da33daf..e2fd676 100644 --- a/models/OwnCurrency.js +++ b/models/OwnCurrency.js @@ -2,9 +2,12 @@ module.exports = function(sequelize, DataTypes) { let OwnCurrency= sequelize.define('OwnCurrency', { - OwnCurrencyUID : { type : DataTypes.INTEGER, primaryKey: true, autoIncrement:true}, + OwnCurrencyUID : { type : DataTypes.INTEGER, primaryKey: true, autoIncrement:true }, CurrencyID : { type : DataTypes.INTEGER }, - QNTY:{type:DataTypes.INTEGER, defaultValue:0} + 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', @@ -27,6 +30,10 @@ module.exports = function(sequelize, DataTypes) { } }); } + }, + getterMethods: { + //전체 보유수량 = NowMaxQNTY + AddMaxQNTY + TotalQNTY : function() { return this.NowMaxQNTY + this.AddMaxQNTY } } }); return OwnCurrency; diff --git a/package.json b/package.json index 30d9a0b..4d28568 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "debug": "^2.4.4", "express": "^4.14.0", "jsonwebtoken": "^7.2.1", + "later": "^1.2.0", "morgan": "^1.7.0", "pug": "^2.0.0-beta6", "sequelize": "^3.27.0", diff --git a/routes/currency.js b/routes/currency.js index f44bd36..26c24cf 100644 --- a/routes/currency.js +++ b/routes/currency.js @@ -1,10 +1,11 @@ 'use strict' -const debug = require('debug')('Wendy:router:device'); +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(); @@ -16,7 +17,12 @@ const router = express.Router(); * @apiHeader {String} Authorization JWT토큰을 전송 */ router.get('/define', auth.isAuthenticated, (req, res, next)=>{ - models.DefineCurrency.findAll() + models.DefineCurrency.findAll({ + include: [{ + model: models.DefineRechargeCurrency, + as: 'RechargeInfo' + }] + }) .then((defineCurrencyList)=>{ res.send({result:0, list:defineCurrencyList}); }) @@ -27,11 +33,89 @@ router.get('/define', auth.isAuthenticated, (req, res, next)=>{ * @apiName 보유 통화 목록 요청 * @apiHeader {String} Authorization JWT토큰을 전송 */ -router.get('/own', auth.isAuthenticated, (req, res, next)=>{ - models.OwnCurrency.findAll({where:{GameUserID:req.user.GameUserID}}) +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; \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 73f67f2..eb2e67c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -422,6 +422,10 @@ kind-of@^3.0.2: dependencies: is-buffer "^1.0.2" +later@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/later/-/later-1.2.0.tgz#f2cf6c4dd7956dd2f520adf0329836e9876bad0f" + lazy-cache@^1.0.3: version "1.0.4" resolved "https://registry.yarnpkg.com/lazy-cache/-/lazy-cache-1.0.4.tgz#a1d78fc3a50474cb80845d3b3b6e1da49a446e8e"