diff --git a/docs/api.md b/docs/api.md index 828ae79..de69f12 100644 --- a/docs/api.md +++ b/docs/api.md @@ -185,8 +185,8 @@ | 字段 | 说明 | | -------- | -------------------------------------- | -| type | 类型 0: 碎片, 1: 代币购买 2: 试用 | -| moneyid |代币的item id | +| type | 类型 0: 碎片, 1: 试用 | + @@ -256,6 +256,10 @@ level: 1 // 当前等级 } ``` + +### 11. 将专属英雄碎片转化成经验 + + ## 三. 服务端接口列表 ### 1. 上传对战记录(服务端调用) diff --git a/src/constants/MoneyTypeConst.ts b/src/constants/MoneyTypeConst.ts new file mode 100644 index 0000000..9f9fac1 --- /dev/null +++ b/src/constants/MoneyTypeConst.ts @@ -0,0 +1,40 @@ +export class MoneyTypeConst { + /** + * coin: 金币 + */ + public static readonly COIN = 'coin'; + /** + * diamond: 钻石 + */ + public static readonly DIAMOND = 'diamond'; + /** + * hero_shard: 通用英雄碎片 + */ + public static readonly HERO_SHARD = 'hero_shard'; + /** + * hero_exp: 通用英雄经验 + */ + public static readonly HERO_EXP = 'hero_exp'; + /** + * card_scroll: 抽卡卷轴 + */ + public static readonly CARD_SCROLL = 'card_scroll'; + private static readonly HERO_SHARD_HEROID = 'hero_shard_'; + private static readonly HERO_EXP_HEROID = 'hero_exp_'; + + /** + * 英雄专用碎片 + * @param heroId + */ + public static getHeroShard(heroId: number): string { + return `${this.HERO_SHARD_HEROID}_${heroId}`; + } + + /** + * 英雄专用经验 + * @param heroId + */ + public static getHeroExp(heroId: number): string { + return `${this.HERO_EXP_HEROID}_${heroId}`; + } +} diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index b8e3004..7be4d06 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -5,6 +5,7 @@ import {ZError} from "../common/ZError"; import {Hero} from "../models/subdoc/Hero"; import {Card} from "../models/subdoc/Card"; import {CardGroup} from "../models/CardGroup"; +import {MoneyTypeConst} from "../constants/MoneyTypeConst"; export default class AccountController extends BaseController { @role('anon') @@ -81,5 +82,50 @@ export default class AccountController extends BaseController { return heros; } + @router('post /api/:accountid/hero/unlock/:heroid') + async unlockHero(req: any) { + let account = req.user; + let {heroid, type} = req.params; + if (!heroid) { + throw new ZError(101, '未指定heroid'); + } + if (account.heros.has(heroid + '')) { + if (!account.heros.get(heroid + '').free) { + throw new ZError(102, '你已经解锁了该英雄'); + } + } + let money0 = MoneyTypeConst.getHeroShard(heroid); + let money1 = MoneyTypeConst.HERO_SHARD; + let count0 = account.moneys.has(money0) ? account.moneys.get(money0) : 0; + let count1 = account.moneys.has(money1) ? account.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; + account.moneys.set(money0, 0); + } else { + needCount = 0; + account.moneys.set(money0, Math.abs(rest)); + } + } + // 上面已经过滤了 count0 + count1 > needcount, 所以这里不需要判断碎片是否足够 + if (needCount > 0) { + count1 = count1 - needCount; + account.moneys.set(money1, count1); + } + let hero = new Hero(); + hero.heroid = heroid; + hero.free = false; + hero.level = 1; + hero.exp = 0; + account.heros.set(heroid + '', hero); + return {}; + } + } diff --git a/src/controllers/CardController.ts b/src/controllers/CardController.ts index e7a860b..9d5e2e2 100644 --- a/src/controllers/CardController.ts +++ b/src/controllers/CardController.ts @@ -3,6 +3,7 @@ import {router} from "../decorators/router"; import {CardGroup} from "../models/CardGroup"; import {ZError} from "../common/ZError"; import {Card} from "../models/subdoc/Card"; +import {MoneyTypeConst} from "../constants/MoneyTypeConst"; export default class CardController extends BaseController { @router('post /api/:accountid/card_group/:heroid') @@ -81,4 +82,26 @@ export default class CardController extends BaseController { await record.save(); return {}; } + + @router('post /api/:accountid/card/draw/:count') + async drawCard(req: any) { + let {count} = req.params; + if (count < 10) { + count = 1; + } + if (count > 10) { + count = 10; + } + let account = req.user; + if (!account.moneys.has(MoneyTypeConst.CARD_SCROLL)) { + throw new ZError(101, '卷轴不足'); + } + let money = account.moneys.get(MoneyTypeConst.CARD_SCROLL); + if (money < count) { + throw new ZError(101, '卷轴不足'); + } + account.moneys.set(MoneyTypeConst.CARD_SCROLL, money - count); + // TODO: 随机取count张卡牌, 并与当前已有的卡比较, 将新卡添加进用户卡组 + + } }