card_info_svr/src/controllers/AccountController.ts
2021-01-28 16:32:59 +08:00

122 lines
3.7 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 { BagItem, ItemType } from '../models/BagItem'
import { addHeroDefaultCardGroup } from '../dao/CardGroupDao'
import { RedisClient } from '../redis/RedisClient'
export default class AccountController extends BaseController {
@role('anon')
@router('post /api/:accountid/uinfo')
async info(req: any) {
let { accountid, nickname, avatar } = 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
}
if (nickname) {
account.nickname = nickname
}
if (avatar) {
account.avatar = avatar
}
let cardMap = account.cardMap
for (let [, cfg] of global.$cfg.get(BaseConst.EFFECTCARD)) {
if (cfg.org_gift == 1 && cfg.type_id == 1 && !cardMap.has(cfg.id + '')) {
const card = new Card()
card.cardid = cfg.id
card.owned = true
card.ban = false
card.usetype = 0
card.free = true
card.free_expire = 0
card.time = Date.now()
cardMap.set(card.cardid + '', card)
}
}
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)
await addHeroDefaultCardGroup(accountid, hero.heroid, cardMap)
}
}
}
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('get /svr/:accountid/uinfo')
async simpleInfo(req: any) {
let account = req.user
return {
nickname: account.nickname,
avatar: account.avatar
}
}
@router('get /svr/randomrobot')
async randomRobot(req: any) {
// @ts-ignore
let str: string = await new RedisClient().srandmember(BaseConst.ROBOT_INFO)
let arr = str.split(BaseConst.ROBOT_INFO_SEP)
return {
nickname: arr[0],
avatar: arr[1]
}
}
@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
}
}