修改router标签, 支持配置多个路由, 修改服务端调用的方法, 将路径改为svr

This commit is contained in:
zhl 2021-01-13 20:25:33 +08:00
parent a9445be97b
commit 9df9d0c216
7 changed files with 106 additions and 53 deletions

View File

@ -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);
})
}
}
}
/**

View File

@ -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;

View File

@ -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);

View File

@ -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();

View File

@ -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<Function | Function[], {
target?: any,
method?: string,
path?: string,
static decoratedRouters: Map<Function, {
roles?: string[],
permissions?: string[]
permissions?: string[],
data?: RouterData[]
}> = 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);
}

View File

@ -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;

View File

@ -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'})
}
}
})
};