增加升级英雄的接口

This commit is contained in:
zhl 2021-01-08 14:24:26 +08:00
parent c7127b35d1
commit 9cb1e0ee75
5 changed files with 135 additions and 27 deletions

View File

@ -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, // 本次升级一共获得的经验
}
```

View File

@ -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 = {

View File

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

View File

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

View File

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