card_info_svr/src/controllers/AccountController.ts

140 lines
4.2 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'
import ItemCtrl from 'logic/ItemCtrl'
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.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`]
if (cardMap.has(cardid + '')) {
continue
}
let 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()
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[] = [...account.cardMap.values()].map(o => o.toJson())
//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())
}
@router('post /api/:accountid/useitem')
async userItem(req: any) {
let { accountid, itemid, count } = req.params
let record = await BagItem.findOne({
accountid,
itemid,
itemtype: ItemType.UNKNOW
})
if (!record) {
throw new ZError(11, 'item not found')
}
if (record.count < count) {
throw new ZError(12, 'not enough item')
}
record.count -= count
//TODO:: 根据使用逻辑获取真实的物品id
let data = ItemCtrl.useItem(itemid, count);
// let data = [{ itemid: 10001, itemnum: 1 }]
for (let obj of data) {
let item = (await BagItem.findOrCreate({
accountid,
itemid: obj.itemid,
itemtype: ItemType.UNKNOW
})).doc
item.count += obj.itemnum
await item.save()
}
await record.save()
return data
}
}