card_info_svr/src/controllers/AccountController.ts
2021-02-19 11:30:31 +08:00

141 lines
4.2 KiB
TypeScript

import BaseController from '../common/base.controller'
import { role, router } from '../decorators/router'
import { Card } from '../models/subdoc/Card'
import { BaseConst } from '../constants/BaseConst'
import { BagItem, ItemType } from '../models/BagItem'
import { RedisClient } from '../redis/RedisClient'
import {
checkGameing,
getAccountRank,
setGameing,
usersByScore
} from '../service/rank'
import { fetchAccount } from '../dao/AccountDao'
import { generateId } from '../utils/security.util'
import { getRandom } from '../utils/number.util'
import { RobotDao } from '../dao/RobotDao'
const isProd = process.env.NODE_ENV === 'production'
const DEFAULT_NICKNAME = '匿名玩家'
export default class AccountController extends BaseController {
@role('anon')
@router('post /api/:accountid/uinfo')
async info(req: any) {
let { accountid, nickname, avatar } = req.params
const account = await fetchAccount({accountid, nickname, avatar})
account.isnew = 0
await account.save()
let result: any = { accountid: account.id }
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
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()
let rank = await getAccountRank(accountid)
if (typeof rank === 'string') {
result.rank = parseInt(rank)
} else {
result.rank = rank
}
result.rank += 1
return result
}
@router('post /svr/:accountid/uinfo')
async simpleInfo(req: any) {
let account = req.user
let {isMatch} = req.params
if (isMatch) {
const time = isProd? 15 * 60 : 30
await setGameing(account._id, time)
}
return {
accountid: account._id,
nickname: account.nickname || DEFAULT_NICKNAME,
avatar: account.avatar,
score: account.season_score,
heros: [...account.heros.keys()]
}
}
@router('post /svr/randomrobot')
async randomRobot(req: any) {
let { min, max, accounts } = req.params
let accountSet = new Set(accounts)
let datas: any = await usersByScore(min, max)
let accountid, targetScore
if (datas.length > 0) {
for (let i = 0; i < datas.length; i += 2) {
if (!datas[i].startsWith(BaseConst.ROBOT_PREFIX)) {
continue
}
if (accountSet.has(datas[i])) {
continue
}
let gameing = new RobotDao().checkGameing(datas[i])
if (gameing) {
continue
}
accountid = datas[i]
targetScore = datas[i + 1] | 0
break
}
}
if (!accountid) {
accountid = BaseConst.ROBOT_PREFIX + generateId()
targetScore = getRandom(min, max) | 0
new RobotDao().setGameing(accountid)
}
let account = await fetchAccount({accountid, robot: true})
if (account.isnew) {
// @ts-ignore
let str: string = await new RedisClient().srandmember(BaseConst.ROBOT_INFO)
let arr = [BaseConst.DEFAULT_ROBOT_NAME, BaseConst.DEFAULT_ROBOT_AVATAR]
if (str)
arr = str.split(BaseConst.ROBOT_INFO_SEP)
account.nickname = arr[0]
account.avatar = arr[1]
account.season_score = targetScore
account.isnew = 0
await account.save()
}
return {
accountid: account._id,
nickname: account.nickname || DEFAULT_NICKNAME,
avatar: account.avatar,
score: account.season_score
}
}
@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())
return result
}
}