corgi/src/api/controllers/game_user.controller.ts

152 lines
4.9 KiB
TypeScript

import BaseController from '../../common/base.controller'
import { role, router } from '../../decorators/router'
import { GameUser } from '../../models/user/GameUser'
import { ZError } from '../../common/ZError'
import { getRandom } from '../../utils/number.util'
import { UserReward } from '../../models/user/UserReward'
import { UserItem } from '../../models/user/UserItem'
import { LOTTERY_TICKET } from '../../constants/BaseConst'
import { getShopInviteeNum } from '../../services/JCFW'
import { Shop } from '../../models/shop/Shop'
import { UserCoupon } from '../../models/user/UserCoupon'
import { ShareCfgClass, ShopCfg } from '../../models/shop/ShopCfg'
import { Coupon } from '../../models/shop/Coupon'
import { getCouponUrl } from '../../services/File'
class GameUserController extends BaseController {
// TODO:: 增加返回未使用的券
@role('anon')
@router('post /api/:accountId/login')
@router('post /weapp/login')
async gameUserLogin(req, res) {
const { accountId } = req.params
if (!accountId) {
throw new ZError(11, 'accountId needed')
}
let user = (await GameUser.findOrCreate({ accountId })).doc
user.updateFromReq(req.params)
let sid = req.params.shop
if (sid) {
const shop = await Shop.fetchByID(sid)
if (shop) {
user.shop = shop.id
user.shops.pushOnce(shop.id)
}
}
await user.save()
const token = await res.jwtSign({ id: user.id, accountId })
return { token }
}
//TODO:: 根据真实的配表获取数据
@role('anon')
@router('post /api/:accountid/stats')
async userStatic(req: any) {
const data: any = {
map: [
{ id: 0, name: '知识面', score: getRandom(20), max: 20 },
{ id: 1, name: '知识深度', score: getRandom(20), max: 20 },
{ id: 2, name: '反应能力', score: getRandom(20), max: 20 },
{ id: 3, name: '毅力', score: getRandom(20), max: 20 },
{ id: 4, name: '其他', score: getRandom(20), max: 20 },
],
rightCount: 100,
errorCount: 100,
singleCount: 20,
singleWin: 15,
singleLose: 5,
activityCount: 20,
examCount: 21,
}
return data
}
@role('anon')
@router('post /api/:accountid/tickets')
async userTickets(req: any) {
const { accountid, sid } = req.params
const result = await UserCoupon.ticketList(accountid, sid)
return result
}
@role('anon')
@router('post /api/:accountId/info')
async userInfo(req: any) {
const { accountId, sid, sessionid } = req.params
if (!sid) {
throw new ZError(10, '缺少必要参数: sid')
}
const shop = await Shop.fetchByID(sid)
if (!shop) {
throw new ZError(11, '无法找到对应店铺')
}
let result: any = {}
result.tocket_lottery = await UserItem.fetchCount({ accountId, shop: shop.id, item: LOTTERY_TICKET })
return result
}
@role('anon')
@router('post /api/:accountId/share_rewards')
async shareRewards(req: any) {
const { accountId, sid, sessionid } = req.params
if (!sid || !sessionid) {
throw new ZError(10, '缺少必要参数: sid')
}
const shop = await Shop.fetchByID(sid)
if (!shop) {
throw new ZError(11, '无法找到对应店铺')
}
let numInvite = await getShopInviteeNum(accountId, sessionid, shop.numid)
const cfg = await ShopCfg.findOne({ shop: shop.id })
if (!cfg) {
throw new ZError(12, '当前店铺未配置相关信息')
}
if (!cfg.share_cfgs || cfg.share_cfgs.length === 0) {
throw new ZError(13, '当前店铺未配置相关信息')
}
let rewardList = []
let newItems: any[] = []
let couponMap: Map<string, any> = new Map()
const gotSet: Set<string> = await UserReward.userGotSet(accountId, shop.id, 'share')
for (let reward of cfg.share_cfgs) {
let name = '抽奖券'
let couponUrl = reward.icon
if (reward.rewardType === 0) {
if (!couponMap.has(reward.coupon)) {
let coupon = await Coupon.findById(reward.coupon)
couponMap.set(coupon.id, coupon)
}
name = couponMap.get(reward.coupon).name
couponUrl = reward.icon || getCouponUrl(shop.id, reward.coupon)
}
if (!gotSet.has(reward._id + '') && numInvite >= reward.rank) {
let obj = await UserReward.saveOneRecord({
accountId,
itemId: reward.coupon,
count: reward.count,
shop: shop.id,
rewardId: reward._id + '',
activityId: 'share',
source: 'share',
})
gotSet.add(reward._id + '')
newItems.push(obj)
}
rewardList.push({
coupon: reward.coupon,
name,
count: reward.count,
couponUrl,
score: reward.rank,
rewardType: reward.rewardType,
geted: gotSet.has(reward._id + '') ? 1 : 0,
})
}
return {
num: numInvite,
newget: newItems,
rewards: rewardList,
}
}
}