From 9df9d0c216c4d1f3010127d65541bce3727e2aa7 Mon Sep 17 00:00:00 2001 From: zhl Date: Wed, 13 Jan 2021 20:25:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9router=E6=A0=87=E7=AD=BE,=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=85=8D=E7=BD=AE=E5=A4=9A=E4=B8=AA=E8=B7=AF?= =?UTF-8?q?=E7=94=B1,=20=E4=BF=AE=E6=94=B9=E6=9C=8D=E5=8A=A1=E7=AB=AF?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E7=9A=84=E6=96=B9=E6=B3=95,=20=E5=B0=86?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E6=94=B9=E4=B8=BAsvr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api.server.ts | 12 +++---- src/controllers/CardController.ts | 3 +- src/controllers/HeroController.ts | 34 +++++++++++++++++- src/controllers/RecordController.ts | 2 +- src/decorators/router.ts | 35 +++++++++++++------ src/models/User.ts | 53 ++++++++++++++++------------- src/plugins/apiauth.ts | 20 ++++++----- 7 files changed, 106 insertions(+), 53 deletions(-) diff --git a/src/api.server.ts b/src/api.server.ts index 1db870c..fe594a4 100644 --- a/src/api.server.ts +++ b/src/api.server.ts @@ -44,19 +44,17 @@ export class ApiServer { logger.log('register api routers'); let self = this; for (let [controller, config] of RouterMap.decoratedRouters) { - let controllers = - Array.isArray(controller) ? controller : [controller] - controllers.forEach((controller) => { - logger.info('find api router', config.method || 'all', - config.path, controller.name); + for(let data of config.data) { + logger.info('find api router', data.method || 'all', + data.path, controller.name); // @ts-ignore - self.server[config.method || 'all'](config.path, { + self.server[data.method || 'all'](data.path, { preValidation: async function (request: FastifyRequest, reply: FastifyReply) { request.roles = config.roles; await this.apiAuth(request, reply); } }, controller); - }) + } } } /** diff --git a/src/controllers/CardController.ts b/src/controllers/CardController.ts index 7a513d7..9e947ed 100644 --- a/src/controllers/CardController.ts +++ b/src/controllers/CardController.ts @@ -24,8 +24,7 @@ export default class CardController extends BaseController { return result; } - @role('svr') - @router('get /api/:accountid/group_info/:heroid/:gid') + @router('get /svr/:accountid/group_info/:heroid/:gid') async cardGroupInfo(req: any) { let {accountid, heroid, gid} = req.params; let record; diff --git a/src/controllers/HeroController.ts b/src/controllers/HeroController.ts index fa225b6..c0cc0ff 100644 --- a/src/controllers/HeroController.ts +++ b/src/controllers/HeroController.ts @@ -1,6 +1,9 @@ import BaseController from "../common/base.controller"; import {router} from "../decorators/router"; import {ZError} from "../common/ZError"; +import {BaseConst} from "../constants/BaseConst"; +import {CardGroup} from "../models/CardGroup"; +import {Card} from "../models/subdoc/Card"; export default class HeroController extends BaseController { @router('post /api/:accountid/heros') @@ -15,6 +18,7 @@ export default class HeroController extends BaseController { } @router('post /api/:accountid/hero/unlock/:heroid') + @router('post /svr/:accountid/hero/unlock/:heroid') async unlockHero(req: any) { let account = req.user; let {heroid, type} = req.params; @@ -23,7 +27,35 @@ export default class HeroController extends BaseController { } let hero; if (!type) { - hero = await account.unlockHero(heroid); + let userMoney = !req.url.startsWith('/svr'); + hero = await account.unlockHero(heroid, userMoney); + // 将该英雄的默认卡组添加到玩家的卡组中, + // 将默认卡组里的卡添加到玩家可用卡牌中 + let cardSet = new Set(account.cards); + let cfg = global.$cfg.get(BaseConst.HERO).get(parseInt(hero.heroid)); + let cardgroup = new CardGroup({}); + let cards: Card[] = []; + for (let i = 1; i < 10; i++) { + if (!cfg[`follower${i}id`]) { + break; + } + let card = new Card(); + card.cardid = cfg[`follower${i}id`]; + card.owned = false; + card.ban = false; + card.usetype = 0; + card.free = true; + card.free_expire = 0; + cards.push(card); + cardSet.add(card.cardid); + } + cardgroup.accountid = account.id; + cardgroup.heroid = hero.heroid; + cardgroup.selected = false; + cardgroup.isdefault = true; + cardgroup.cards = cards; + await cardgroup.save(); + account.cards = [...cardSet]; await account.save(); } else { hero = await account.tryHero(heroid); diff --git a/src/controllers/RecordController.ts b/src/controllers/RecordController.ts index a03de80..3cb0181 100644 --- a/src/controllers/RecordController.ts +++ b/src/controllers/RecordController.ts @@ -17,7 +17,7 @@ export default class RecordController extends BaseController { } @role('anon') - @router('post /api/record/save') + @router('post /svr/record/save') async upload(req: any) { let record = new GameRecord(req.params); await record.save(); diff --git a/src/decorators/router.ts b/src/decorators/router.ts index b0c177d..88c9c36 100644 --- a/src/decorators/router.ts +++ b/src/decorators/router.ts @@ -1,12 +1,16 @@ import BaseController from '../common/base.controller'; +export class RouterData { + target?: any + method?: string + path?: string + fun?: Function +} export class RouterMap { - static decoratedRouters: Map = new Map() } export function router(route?: string) { @@ -23,17 +27,26 @@ export function router(route?: string) { const [method, path] = split // @ts-ignore const key = target[name]; - let routerObj = { - target: target, - path: path, - method: method - }; + let routerData = new RouterData(); + routerData.target = target; + routerData.method = method; + routerData.path = path; + // @ts-ignore + routerData.fun = target[name]; + if (RouterMap.decoratedRouters.has(key)) { let objCurrent = RouterMap.decoratedRouters.get(key); - Object.assign(objCurrent, routerObj); + if (!objCurrent.data) { + objCurrent.data = [routerData]; + } else { + objCurrent.data.push(routerData); + } // @ts-ignore RouterMap.decoratedRouters.set(target[name], objCurrent); } else { + let routerObj = { + data: [routerData] + }; // @ts-ignore RouterMap.decoratedRouters.set(target[name], routerObj); } diff --git a/src/models/User.ts b/src/models/User.ts index 625486c..d7bbbfa 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -74,6 +74,11 @@ class UserClass extends FindOrCreate{ @prop({type: Number, default: [0,0,0,0]}) public season_stat: number[]; + /** + * 当前赛季排位分 + */ + @prop({type: Number, default: 1000}) + public season_score: number; /** * 所有未定义的信息, 供扩展 */ @@ -86,35 +91,37 @@ class UserClass extends FindOrCreate{ } } - public async unlockHero(heroid: number) { + public async unlockHero(heroid: number, useMoney: boolean) { if (this.heros.has(heroid + '')) { if (!this.heros.get(heroid + '').trial) { throw new ZError(102, '你已经解锁了该英雄'); } } - let money0 = MoneyTypeConst.getHeroShard(heroid); - let money1 = MoneyTypeConst.HERO_SHARD; - let count0 = this.moneys.has(money0) ? this.moneys.get(money0) : 0; - let count1 = this.moneys.has(money1) ? this.moneys.get(money1) : 0; - // TODO:: 根据配置获取解锁英雄需要的碎片数量 - let needCount = 30; - if (count0 + count1 < needCount) { - throw new ZError(102, '碎片数量不足'); - } - if (count0 > 0) { - let rest = needCount - count0; - if (rest >= 0) { - needCount = rest; - this.moneys.set(money0, 0); - } else { - needCount = 0; - this.moneys.set(money0, Math.abs(rest)); + if (useMoney) { + let money0 = MoneyTypeConst.getHeroShard(heroid); + let money1 = MoneyTypeConst.HERO_SHARD; + let count0 = this.moneys.has(money0) ? this.moneys.get(money0) : 0; + let count1 = this.moneys.has(money1) ? this.moneys.get(money1) : 0; + // TODO:: 根据配置获取解锁英雄需要的碎片数量 + let needCount = 30; + if (count0 + count1 < needCount) { + throw new ZError(102, '碎片数量不足'); + } + if (count0 > 0) { + let rest = needCount - count0; + if (rest >= 0) { + needCount = rest; + this.moneys.set(money0, 0); + } else { + needCount = 0; + this.moneys.set(money0, Math.abs(rest)); + } + } + // 上面已经过滤了 count0 + count1 > needcount, 所以这里不需要判断碎片是否足够 + if (needCount > 0) { + count1 = count1 - needCount; + this.moneys.set(money1, count1); } - } - // 上面已经过滤了 count0 + count1 > needcount, 所以这里不需要判断碎片是否足够 - if (needCount > 0) { - count1 = count1 - needCount; - this.moneys.set(money1, count1); } let hero = new Hero(); hero.heroid = heroid; diff --git a/src/plugins/apiauth.ts b/src/plugins/apiauth.ts index 225d414..94a8a22 100644 --- a/src/plugins/apiauth.ts +++ b/src/plugins/apiauth.ts @@ -26,11 +26,15 @@ const apiAuthPlugin: FastifyPluginAsync = async function( // 只有路由配置的role为anon才不需要过滤 fastify.decorate("apiAuth", async function(request: FastifyRequest, reply: FastifyReply) { - if (!request.roles || request.roles.indexOf('anon') == -1) { - try { - if (request.roles && request.roles.indexOf('svr') >= 0) { - // TODO: check svr - } else { + if (request.url.startsWith('/svr')) { + // @ts-ignore + let { accountid } = request.params; + if (accountid) { + request.user = await User.findById(accountid); + } + } else { + if (!request.roles || request.roles.indexOf('anon') == -1) { + try { // @ts-ignore let { accountid, sessionid } = request.params; //TODO: 增加sessionid的校验 @@ -52,13 +56,13 @@ const apiAuthPlugin: FastifyPluginAsync = async function( return reply.send({code: 4, msg: 'account locked'}); } request.user = account; + } catch (err) { + return reply.send({code: 401, msg: 'need auth'}) } - - } catch (err) { - return reply.send({code: 401, msg: 'need auth'}) } } + }) };