165 lines
4.8 KiB
TypeScript
165 lines
4.8 KiB
TypeScript
import BaseController from '../common/base.controller'
|
|
import { router } from '../decorators/router'
|
|
import { SvrConfig } from '../service/SvrConfig'
|
|
import { SignRecord } from '../models/SignRecord'
|
|
import { ZError } from '../common/ZError'
|
|
import { parseGameAccountId } from '../utils/string.util'
|
|
import { calcBetweenDays } from '../utils/time.util'
|
|
import ItemCtrl from '../logic/ItemCtrl'
|
|
import { ItemInfo } from '../logic/ItemDef'
|
|
import { BagItem } from '../models/BagItem'
|
|
|
|
export default class DaySignController extends BaseController {
|
|
/**
|
|
* 获取签到列表
|
|
*/
|
|
@router('post /api/:accountid/sign/list')
|
|
async signList(req: any) {
|
|
let { accountid, retroactive } = req.params
|
|
retroactive = retroactive || 1
|
|
let { gameid, channel } = parseGameAccountId(accountid)
|
|
if (!gameid || !channel) {
|
|
throw new ZError(11, '无法从accountid获取gameid和channel')
|
|
}
|
|
const cfgs = await new SvrConfig().getSignCfg(gameid, channel)
|
|
let record = (await SignRecord.findOrCreate({ accountid })).doc
|
|
if (!cfgs || !Array.isArray(cfgs)) {
|
|
throw new ZError(10, 'error get svr sign cfg')
|
|
}
|
|
let userData: any = {}
|
|
for (let cfg of cfgs) {
|
|
cfg.status = record.data.get(cfg.id + '') || 0
|
|
if (record.data.has(cfg.id + '')) {
|
|
userData[cfg.id] = 3
|
|
}
|
|
}
|
|
let betweenDays = 0
|
|
if (record.last == 0) {
|
|
betweenDays = 1
|
|
} else {
|
|
betweenDays = calcBetweenDays(Date.now(), record.last)
|
|
}
|
|
let doubleReward = betweenDays == 0 ? record.double : 0
|
|
let needNext = true
|
|
if (betweenDays === 0) {
|
|
needNext = false
|
|
}
|
|
if (needNext) {
|
|
if (betweenDays == 2 && !!retroactive) {
|
|
for (let i = 0; i < cfgs.length - 1; i++) {
|
|
if (cfgs[i].status == 3 && cfgs[i + 1].status == 0) {
|
|
cfgs[i + 1].status = 1 //可补签
|
|
if (i + 2 < cfgs.length) {
|
|
cfgs[i + 2].status = 2 //可领取
|
|
} else {
|
|
cfgs[0].status = 2
|
|
}
|
|
needNext = false
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (needNext) {
|
|
//正常或者超过两天的
|
|
let count = 0
|
|
for (let obj of cfgs) {
|
|
obj.status == 3 && count++
|
|
}
|
|
if (count == 7 || betweenDays > 1) {
|
|
//一轮领完 或者过期了, 2上面处理过了
|
|
// this.refreshData();
|
|
for (let obj of cfgs) {
|
|
obj.status = 0
|
|
}
|
|
record.markModified('data')
|
|
record.data.clear()
|
|
record.last = 0
|
|
betweenDays = 1
|
|
doubleReward = 0
|
|
userData = {}
|
|
await record.save()
|
|
}
|
|
for (let obj of cfgs) {
|
|
if (obj.status == 0) {
|
|
obj.status = 2
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
cfgs,
|
|
last: record.last / 1000 | 0,
|
|
betweenDays,
|
|
doubleReward,
|
|
userData
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 签到
|
|
*/
|
|
@router('post /api/:accountid/sign/get')
|
|
async receiveReward(req: any) {
|
|
// type: 1: 普通领取, 2: 双倍领取, 3: 补签
|
|
let { accountid, type, id } = req.params
|
|
type = parseInt(type)
|
|
let { gameid, channel } = parseGameAccountId(accountid)
|
|
if (!gameid || !channel) {
|
|
throw new ZError(11, '无法从accountid获取gameid和channel')
|
|
}
|
|
const cfgs: any[] = await new SvrConfig().getSignCfg(gameid, channel)
|
|
let record = (await SignRecord.findOrCreate({ accountid })).doc
|
|
let rate = 1
|
|
let cfg = cfgs.find(o => o.id === id)
|
|
if (!cfg) {
|
|
throw new ZError(13, '无法获取对应的配置')
|
|
}
|
|
switch (type) {
|
|
case 1:
|
|
if (record.data.has(id + '')) {
|
|
throw new ZError(12, '不满足领取条件')
|
|
}
|
|
record.double = 0
|
|
record.last = Date.now()
|
|
break
|
|
case 2:
|
|
if (!record.data.has(id + '')) {
|
|
rate = 2
|
|
}
|
|
record.double = 1
|
|
record.last = Date.now()
|
|
break
|
|
case 3:
|
|
if (record.data.has(id + '')) {
|
|
throw new ZError(12, '不满足补签条件')
|
|
}
|
|
record.double = 0
|
|
record.last = Date.now() - 3600 * 24 * 1000
|
|
break
|
|
default:
|
|
if (record.data.has(id + '')) {
|
|
throw new ZError(12, '不满足领取条件')
|
|
}
|
|
record.double = 0
|
|
record.last = Date.now()
|
|
break
|
|
}
|
|
record.data.set(id + '', 3)
|
|
let itemStr = ''
|
|
if (cfg.reward_list && Array.isArray(cfg.reward_list)) {
|
|
for (let sub of cfg.reward_list) {
|
|
if (itemStr.length > 0) itemStr += '|'
|
|
itemStr += `${sub.reward_type}:${sub.reward_count}`
|
|
}
|
|
} else {
|
|
itemStr += `${cfg.reward_type}:${cfg.reward_count}`
|
|
}
|
|
const itemInfos: ItemInfo[] = ItemCtrl.getItemsByInfo(itemStr)
|
|
await BagItem.addItems(accountid, itemInfos, 'sign', type)
|
|
await record.save()
|
|
return itemInfos
|
|
}
|
|
}
|