From 9cb1e0ee752bb8e920d9f2360ea6d54cb6bba874 Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 8 Jan 2021 14:24:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=87=E7=BA=A7=E8=8B=B1?= =?UTF-8?q?=E9=9B=84=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api.md | 9 ++-- src/api.server.ts | 6 +-- src/constants/MoneyTypeConst.ts | 38 ++++++++++++++++ src/controllers/HeroController.ts | 37 ++++++++-------- src/models/User.ts | 72 +++++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 27 deletions(-) diff --git a/docs/api.md b/docs/api.md index 5a22737..49d26c6 100644 --- a/docs/api.md +++ b/docs/api.md @@ -265,15 +265,16 @@ | 字段 | 说明 | | -------- | -------------------------------------- | -| count |使用数量 | +| items |使用代币类型和数量, 格式 money0:count\|money1:count1 | + 3. Response: JSON ```json { - exp: 1022, // 当前经验值 - exp_POST: 100, // 本次获得的经验 - level: 1 // 当前等级 + exp: 1022, // 升级后当前经验值 + level: 1, // 升级后的等级 + exp_gain: 100, // 本次升级一共获得的经验 } ``` diff --git a/src/api.server.ts b/src/api.server.ts index 0e830ff..62ebff7 100644 --- a/src/api.server.ts +++ b/src/api.server.ts @@ -89,7 +89,7 @@ export class ApiServer { this.server.setNotFoundHandler(function(request: any, reply: { send: (arg0: { errcode: number; errmsg: string; }) => void; }){ reply.send({errcode: 404, errmsg: 'page not found'}) }); - this.server.setErrorHandler(function (error: FastifyError, request: any, reply: { send: (arg0: { errcode: any; errmsg: any; }) => void; }) { + this.server.setErrorHandler(function (error: FastifyError, request: FastifyRequest, reply: FastifyReply) { let statusCode = error && error.statusCode || 100; if (statusCode >= 500) { logger.error(error) @@ -98,7 +98,7 @@ export class ApiServer { } else { logger.error(error) } - reply.send({errcode: statusCode, errmsg: error ? error.message : 'unknown error'}) + reply.code(200).send({errcode: statusCode, errmsg: error ? error.message : 'unknown error'}) }) } @@ -113,7 +113,7 @@ export class ApiServer { */ private setFormatSend() { this.server.addHook('preSerialization', async (request: FastifyRequest, reply: FastifyReply, payload) => { - reply.header('', '') + reply.header('X-Powered-By', 'PHP/5.4.16') // @ts-ignore if (!payload.errcode) { payload = { diff --git a/src/constants/MoneyTypeConst.ts b/src/constants/MoneyTypeConst.ts index e0650f1..73abf17 100644 --- a/src/constants/MoneyTypeConst.ts +++ b/src/constants/MoneyTypeConst.ts @@ -35,4 +35,42 @@ export class MoneyTypeConst { public static getHeroExp(heroId: number): string { return `${this.HERO_EXP}_${heroId}`; } + + /** + * 检查货币字符串是否符合规则 + * @param type + */ + public static checkMoney(type: string): boolean { + return this.COIN == type + || this.DIAMOND == type + || this.CARD_SCROLL == type + || type.indexOf(this.HERO_SHARD) == 0 + || type.indexOf(this.HERO_EXP) == 0 + } + public static isHeroMoney(type: string): boolean { + return type.indexOf(this.HERO_SHARD + '_') >= 0 + || type.indexOf(this.HERO_EXP + '_') >= 0 + } + /** + * 获取英雄专用货币中的heroid + * @param type + */ + public static getHeroId(type: string): number { + if (type.indexOf(this.HERO_SHARD + '_') >= 0) { + let str = type.substring(type.lastIndexOf('_') + 1) + if (str) { + return parseInt(str); + } else { + return 0; + } + } else if (type.indexOf(this.HERO_EXP+ '_') >= 0) { + let str = type.substring(type.lastIndexOf('_') + 1) + if (str) { + return parseInt(str); + } else { + return 0; + } + } + return 0; + } } diff --git a/src/controllers/HeroController.ts b/src/controllers/HeroController.ts index 0bffed9..dd6bb95 100644 --- a/src/controllers/HeroController.ts +++ b/src/controllers/HeroController.ts @@ -1,9 +1,6 @@ import BaseController from "../common/base.controller"; import {router} from "../decorators/router"; import {ZError} from "../common/ZError"; -import {MoneyTypeConst} from "../constants/MoneyTypeConst"; -import {Hero} from "../models/subdoc/Hero"; -import {User} from "../models/User"; export default class HeroController extends BaseController { @router('post /api/:accountid/heros') @@ -24,36 +21,36 @@ export default class HeroController extends BaseController { if (!heroid) { throw new ZError(101, '未指定heroid'); } - try { - let hero; - if (!type) { - hero = await account.unlockHero(heroid); - await account.save(); - } else { - hero = await account.tryHero(heroid); - await account.save(); + let hero; + if (!type) { + hero = await account.unlockHero(heroid); + await account.save(); + } else { + hero = await account.tryHero(heroid); + await account.save(); - } - return hero.toJson(); - } catch (err) { - throw err; } - + return hero.toJson(); } @router('post /api/:accountid/hero/update/:heroid') async updateHero(req: any) { let account = req.user; - let {heroid, count} = req.params; - if (!heroid) { - throw new ZError(101, '未指定heroid'); + let {heroid, items} = req.params; + if (!heroid || !items) { + throw new ZError(101, '参数不足'); } if (!account.heros.has(heroid + '')) { throw new ZError(102, '你未解锁该英雄'); } let hero = account.heros.get(heroid + ''); - + if (hero.trial) { + throw new ZError(103, '不能升级试用英雄'); + } + let data = await account.updateHero(heroid, items); + await account.save(); + return data; } @router('post /api/:accountid/hero/toexp/:heroid') diff --git a/src/models/User.ts b/src/models/User.ts index b208051..0c5bf72 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -142,6 +142,78 @@ class UserClass extends FindOrCreate{ this.heros.set(heroid+'', hero); return hero; } + + /** + * 升级英雄 + * @param heroid + * @param items + */ + public async updateHero(heroid: number, items: string) { + if (items.indexOf(':') < 0) { + throw new ZError(105, 'items参数格式错误'); + } + if (!this.heros.has(heroid + '')) { + throw new ZError(109, '升级英雄不存在'); + } + let hero = this.heros.get(heroid + ''); + if (hero.trial) { + throw new ZError(110, '试玩英雄不可升级'); + } + + let moneys: [string, number][] = []; + if (items.indexOf('|')) { + let arr = items.split('|'); + for (let str of arr) { + if(!str) { + continue; + } + if (str.indexOf(':') < 0) { + throw new ZError(105, 'items参数格式错误'); + } + + let subarr = items.split(':'); + if (!MoneyTypeConst.checkMoney(subarr[0])) { + throw new ZError(106, `货币不存在: ${subarr[0]}`); + } + moneys.push([subarr[0], parseInt(subarr[1])]); + } + } else { + let arr = items.split(':'); + if (!MoneyTypeConst.checkMoney(arr[0])) { + throw new ZError(106, `货币不存在: ${arr[0]}`); + } + moneys.push([arr[0], parseInt(arr[1])]); + } + const expPreLvl = 100; + let totalExp = hero.exp; + for (let data of moneys) { + let money = data[0]; + if (MoneyTypeConst.isHeroMoney(money)) { + let tmpHeroId = MoneyTypeConst.getHeroId(money); + if (tmpHeroId != heroid) { + throw new ZError(107, `指定英雄的货币与当前英雄id不匹配: ${money}`); + } + } + let count = this.moneys.get(money) || 0; + if (count < data[1]) { + throw new ZError(108, `当前货币不够: ${money}, in: ${data[1]}, had: ${count}`); + } + this.moneys.set(money, count - data[1]); + // TODO:: 根据配表计算代币->经验值的换算比例 + let exp = data[1]; + totalExp += exp; + } + // TODO:: 根据配表读取英雄升级需要的经验值 + let lvlInc = totalExp / expPreLvl | 0; + let expRemain = totalExp % expPreLvl; + hero.level += lvlInc; + hero.exp = expRemain; + return { + exp: hero.exp, + level: hero.level, + exp_gain: totalExp + } + } }