card_info_svr/src/controllers/AccountController.ts

106 lines
3.3 KiB
TypeScript

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, ItemType } 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 cardMap = account.cardMap
for (let [, cfg] of global.$cfg.get(BaseConst.HERO)) {
if (cfg.org_gift == 1) {
let hero = new Hero()
hero.heroid = cfg.id
hero.free = true
hero.trial = false
hero.level = 1
hero.slot = 1
hero.time = Date.now()
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++) {
const cardid = cfg[`follower${ i }id`]
let card = cardMap.get(cardid + '')
if (!card) {
card = new Card()
card.cardid = cardid
card.owned = true
card.ban = false
card.usetype = 0
card.free = true
card.free_expire = 0
card.time = Date.now()
cards.push(card)
cardMap.set(cardid + '', card)
}
}
cardgroup.accountid = account.id
cardgroup.heroid = cfg.id
cardgroup.selected = false
cardgroup.isdefault = true
cardgroup.cards = cards
await cardgroup.save()
}
}
}
result.cards = [...account.cardMap.values()].map(o => o.toJson())
let heros: any[] = []
for (let [key, hero] of account.heros) {
heros.push(hero.toJson())
}
result.heros = heros
await account.save()
const moneyList = await BagItem.find({accountid, itemtype: ItemType.MONEY})
result.moneys = moneyList.map(o => o.toJson())
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[] = [...account.cardMap.values()].map(o => o.toJson())
//TODO:: 添加限免英雄和免费英雄
return result
}
}