import BaseController from '../common/base.controller' import { role, router } from '../decorators/router' import { User } from '../models/User' import { ZError } from '../common/ZError' import { Card } from '../models/subdoc/Card' import { BaseConst } from '../constants/BaseConst' import { Hero } from '../models/subdoc/Hero' import { CardGroup } from '../models/CardGroup' import { BagItem } from '../models/BagItem' export default class AccountController extends BaseController { @role('anon') @router('post /api/:accountid/uinfo') async info(req: any) { let { accountid } = req.params let account = (await User.findOrCreate({ _id: accountid })).doc let result: any = { accountid: account.id } if (account.locked) { throw new ZError(4, 'account locked') } const formulaCfg = global.$cfg.get(BaseConst.FORMULA) if (account.season_score == -1) { account.season_score = formulaCfg.get(70003).number } let cardSet = new Set(account.cards) for (let [, cfg] of global.$cfg.get(BaseConst.HERO)) { if (cfg.id <= 30100) { let hero = new Hero() hero.heroid = cfg.id hero.free = true hero.trial = false hero.level = 1 hero.exp = 0 if (!account.heros.has(cfg.id + '')) { account.heros.set(cfg.id + '', hero) let cardgroup = new CardGroup({}) let cards: Card[] = [] for (let i = 1; i <= 4; i++) { 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 = cfg.id cardgroup.selected = false cardgroup.isdefault = true cardgroup.cards = cards await cardgroup.save() } } } account.cards = [...cardSet] result.cards = account.cards let heros: any[] = [] for (let [key, hero] of account.heros) { heros.push(hero.toJson()) } result.heros = heros await account.save() result.moneys = account.moneys result.normal_stat = account.normal_stat result.extinfo = account.extinfo result.season_score = account.season_score result.season_data = account.season_data result.match_score = account.getMatchScore() return result } @router('post /api/:accountid/season_data') async seasonData(req: any) { let result: any = {} let account = req.user for (let [key, val] of account.season_data) { result[key] = val } result.score = account.season_score result.match_score = account.getMatchScore() return result } @router('post /api/:accountid/cards') async cardlist(req: any) { let account = req.user let result: Card[] = [] for (let cardid of account.cards) { let card = new Card() card.cardid = cardid card.owned = true card.ban = false card.usetype = 0 card.free = false result.push(card) } //TODO:: 添加限免英雄和免费英雄 return result } @router('post /api/:accountid/items') async itemList(req: any) { let { accountid } = req.params let items = await BagItem.find({ accountid }) return items.map(o => o.toJson()) } }