每日添加购买次数逻辑
This commit is contained in:
@@ -2,9 +2,11 @@ import { Application, BackendSession } from 'pinus';
|
||||
import { DailyRecordModel } from '../../../db/DailyRecord';
|
||||
import { BattleRecordModel } from '../../../db/BattleRecord';
|
||||
import { getGamedata } from '../../../pubUtils/gamedata';
|
||||
import { WAR_TYPE } from '../../../consts/consts';
|
||||
import { WAR_TYPE, GONGSHI } from '../../../consts/consts';
|
||||
import { STATUS } from '../../../consts/statusCode';
|
||||
import { resResult } from '../../../pubUtils/util';
|
||||
import { RoleModel } from '../../../db/Role';
|
||||
import { getDailyNum } from '../../../services/dailyBattleService';
|
||||
|
||||
export default function(app: Application) {
|
||||
return new DailyBattleHandler(app);
|
||||
@@ -24,9 +26,8 @@ export class DailyBattleHandler {
|
||||
let dicDailyWar = getGamedata('dic_zyz_gk_daily');
|
||||
|
||||
let result = new Array();
|
||||
for(let {dailyType: type, name, timesPerDay: sum} of dicDaily) {
|
||||
for(let {dailyType: type, name, timesPerDay, timesCanBuy } of dicDaily) {
|
||||
let refreshResult = await DailyRecordModel.refreshRecord(roleId, type);
|
||||
let {count} = refreshResult;
|
||||
let wars = new Array();
|
||||
for(let {war_id, dailyType, cost, gk_name, previousGk } of dicDailyWar) {
|
||||
if(dailyType == type) {
|
||||
@@ -48,13 +49,14 @@ export class DailyBattleHandler {
|
||||
}
|
||||
}
|
||||
wars.push({
|
||||
battleId: war_id, cost, star, status, name: gk_name
|
||||
battleId: war_id, cost, star, status, name: gk_name
|
||||
});
|
||||
}
|
||||
}
|
||||
let checkDailyResult = getDailyNum(refreshResult, timesPerDay, timesCanBuy);
|
||||
|
||||
result.push({
|
||||
type, count, sum, name,
|
||||
type, name, ...checkDailyResult,
|
||||
wars
|
||||
});
|
||||
}
|
||||
@@ -62,4 +64,37 @@ export class DailyBattleHandler {
|
||||
return resResult(STATUS.SUCCESS, { list: result });
|
||||
}
|
||||
|
||||
// 购买每日次数
|
||||
async buyNum(msg: { type: number, count: number }, session: BackendSession) {
|
||||
let {type, count} = msg;
|
||||
const roleId = session.get('roleId');
|
||||
|
||||
const dicDaily = getGamedata('dic_zyz_daily');
|
||||
const curDaily = dicDaily.find(cur => cur.dailyType == type);
|
||||
if(!curDaily) return resResult(STATUS.DAILY_TYPE_NOT_FOUND);
|
||||
let { timesPerDay, timesCanBuy } = curDaily;
|
||||
let { buyCount = 0 } = await DailyRecordModel.refreshRecord(roleId, type);
|
||||
let { vLv } = await RoleModel.findByRoleId(roleId);
|
||||
if(vLv > 0) {
|
||||
timesCanBuy += 0; // 暂留,用于处理vip等级增加最大购买次数
|
||||
}
|
||||
if(buyCount + 1 > curDaily.timesCanBuy ) {
|
||||
return resResult(STATUS.DAILY_REFRESH_TIMES_LACK)
|
||||
}
|
||||
let buyCost = 0;
|
||||
for(let i = 1; i <= count; i++) {
|
||||
let num = buyCount + i;
|
||||
buyCost += eval(GONGSHI.DAILY_REFRESH_NUM_COST);
|
||||
}
|
||||
let {gold} = await RoleModel.findByRoleId(roleId);
|
||||
if(buyCost > gold) {
|
||||
return resResult(STATUS.DAILY_REFRESH_GOLD_LACK);
|
||||
}
|
||||
await RoleModel.costGold(roleId, buyCost);
|
||||
let newDailyRecord = await DailyRecordModel.increseBuyCount(roleId, type, count);
|
||||
|
||||
console.log(buyCost);
|
||||
let checkDailyResult = getDailyNum(newDailyRecord, timesPerDay, timesCanBuy);
|
||||
return resResult(STATUS.SUCCESS, { type, ...checkDailyResult, buyCost});
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ export class NormalBattleHandler {
|
||||
if(checkResult.status == -1) {
|
||||
return checkResult.resResult
|
||||
}
|
||||
dailyNum = { type: checkResult.type, count: checkResult.count, sum: checkResult.sum };
|
||||
dailyNum = Object.assign(dailyNum, checkResult.data);
|
||||
} else if (warInfo.warType == WAR_TYPE.TOWER) {
|
||||
let checkResult = await checkTowerWar(roleId, battleId, heroes);
|
||||
if(checkResult.status == -1) {
|
||||
@@ -185,7 +185,7 @@ export class NormalBattleHandler {
|
||||
if(checkResult.status == -1) {
|
||||
return checkResult.resResult;
|
||||
}
|
||||
dailyNum = { type: checkResult.type, count: checkResult.count, sum: checkResult.sum };
|
||||
dailyNum = Object.assign(dailyNum, checkResult.data)
|
||||
} else if (warInfo.warType == WAR_TYPE.EVENT) {
|
||||
// 记录事件状态
|
||||
await setBattleStatus(this.app, session, roleId, battleId, isSuccess, battleCode);
|
||||
@@ -276,7 +276,7 @@ export class NormalBattleHandler {
|
||||
if(checkResult.status == -1) {
|
||||
return checkResult.resResult
|
||||
}
|
||||
dailyNum = { type: checkResult.type, count: checkResult.count, sum: checkResult.sum };
|
||||
dailyNum = Object.assign(dailyNum, checkResult.data)
|
||||
}
|
||||
|
||||
// 发奖励
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
* 每日本相关
|
||||
*/
|
||||
|
||||
import { DailyRecordModel } from '../db/DailyRecord';
|
||||
import DailyRecord, { DailyRecordModel } from '../db/DailyRecord';
|
||||
import { getGamedata } from '../pubUtils/gamedata';
|
||||
import { resResult } from '../pubUtils/util';
|
||||
import { STATUS } from '../consts/statusCode';
|
||||
import { RoleModel } from '../db/Role';
|
||||
|
||||
// 检查每日本次数checkBattle使用
|
||||
export async function checkDaily(roleId: string, battleId: number, inc: number) {
|
||||
@@ -18,14 +19,22 @@ export async function checkDaily(roleId: string, battleId: number, inc: number)
|
||||
let curDaily = dicDaily.find(cur => cur.dailyType == type);
|
||||
if(!curDaily) return { status: -1, resResult: resResult(STATUS.DAILY_TYPE_NOT_FOUND) };
|
||||
let dailyRecord = await DailyRecordModel.refreshRecord(roleId, type);
|
||||
let { count } = dailyRecord;
|
||||
if(count + inc > curDaily.timesPerDay ) {
|
||||
let { count = 0, buyCount = 0 } = dailyRecord;
|
||||
if(count + inc > curDaily.timesPerDay + buyCount ) {
|
||||
return { status: -1, resResult: resResult(STATUS.DAILY_TIMES_LACK) }
|
||||
}
|
||||
return {status: 1, type, count, sum: curDaily.timesPerDay};
|
||||
let checkDailyResult = getDailyNum(curDaily, curDaily.timesPerDay, curDaily.timesCanBuy);
|
||||
return {status: 1, data: {type, ...checkDailyResult}};
|
||||
}
|
||||
|
||||
// 检查每日本次数warEnd和warSweep使用
|
||||
/**
|
||||
* 检查每日本次数并添加,warEnd和warSweep使用
|
||||
* @param roleId
|
||||
* @param battleId
|
||||
* @param inc 增加的次数
|
||||
* @param isRef 在计算之前是否检查每日的刷新
|
||||
*/
|
||||
|
||||
export async function checkDailyAndIncrease(roleId: string, battleId: number, inc: number, isRef: boolean) {
|
||||
let dicDaily = getGamedata('dic_zyz_daily');
|
||||
let dicDailyWar = getGamedata('dic_zyz_gk_daily');
|
||||
@@ -37,15 +46,31 @@ export async function checkDailyAndIncrease(roleId: string, battleId: number, in
|
||||
if(!curDaily) return { status: -1, resResult: resResult(STATUS.DAILY_TYPE_NOT_FOUND) };
|
||||
|
||||
let dailyRecord: any;
|
||||
if(isRef) {
|
||||
if(isRef) {
|
||||
dailyRecord = await DailyRecordModel.refreshRecord(roleId, type);
|
||||
} else {
|
||||
} else { // 如果是手动挑战,由于在进入时已经检查过了,所以即使结算时过去了一天也还是累计在上一天
|
||||
dailyRecord = await DailyRecordModel.getDailyRecordById(roleId, type);
|
||||
}
|
||||
let { count } = dailyRecord;
|
||||
if(count + inc > curDaily.timesPerDay ) {
|
||||
let { count = 0, buyCount = 0 } = dailyRecord;
|
||||
if(count + inc > curDaily.timesPerDay + buyCount ) {
|
||||
return { status: -1, resResult: resResult(STATUS.DAILY_TIMES_LACK) }
|
||||
}
|
||||
let result = await DailyRecordModel.increseDailyCount(roleId, type, inc);
|
||||
return {status: 1, type, count: result.count, sum: curDaily.timesPerDay};
|
||||
let checkDailyResult = getDailyNum(result, curDaily.timesPerDay, curDaily.timesCanBuy);
|
||||
return {status: 1, data: {type, ...checkDailyResult}};
|
||||
}
|
||||
|
||||
export async function getDailyNum(dailyRecord: DailyRecord, timesPerDay?: number, timesCanBuy?: number) {
|
||||
let {roleId, type, count = 0, buyCount = 0} = dailyRecord;
|
||||
if(!timesPerDay || !timesCanBuy) {
|
||||
const dicDaily = getGamedata('dic_zyz_daily');
|
||||
const curDaily = dicDaily.find(cur => cur.dailyType == type);
|
||||
timesPerDay = curDaily.timesPerDay;
|
||||
timesCanBuy = curDaily.timesCanBuy;
|
||||
}
|
||||
let {vLv} = await RoleModel.findByRoleId(roleId);
|
||||
if(vLv > 0) {
|
||||
timesCanBuy += 0; // 暂留,用于处理vip等级增加最大购买次数
|
||||
}
|
||||
return {count: timesPerDay - count + buyCount, buyCount: timesCanBuy - buyCount}
|
||||
}
|
||||
@@ -177,5 +177,6 @@ export const DEFAULT_HEROES = [ 312, 314, 311, 309, 315];
|
||||
export const FIX_SMS_CODE_TELS = ['18855953630', '13911134885', '15167549151', '15618654010'];
|
||||
|
||||
export const GONGSHI = {
|
||||
"TOWER_HANG_UP_SPEED_COST": "50*num"
|
||||
"TOWER_HANG_UP_SPEED_COST": "50",
|
||||
"DAILY_REFRESH_NUM_COST": "50*num"
|
||||
};
|
||||
|
||||
@@ -28,6 +28,8 @@ export const STATUS = {
|
||||
DAILY_WAR_NOT_FOUND: { code: 20201, simStr: '未找到该关卡' },
|
||||
DAILY_TYPE_NOT_FOUND: { code: 20202, simStr: '未找到该类型' },
|
||||
DAILY_TIMES_LACK: { code: 20203, simStr: '次数不足' },
|
||||
DAILY_REFRESH_TIMES_LACK: { code: 20203, simStr: '刷新次数不足' },
|
||||
DAILY_REFRESH_GOLD_LACK: { code: 20204, simStr: '元宝不足' },
|
||||
// 奇遇 20300 - 20399
|
||||
EVENT_RECORD_NOT_FOUND: { code: 20301, simStr: '未找到记录' },
|
||||
EVENT_STATUS_ERROR: { code: 20302, simStr: '状态错误' },
|
||||
|
||||
@@ -14,23 +14,26 @@ export default class DailyRecord extends BaseModel {
|
||||
@prop({ required: true, default: 0 })
|
||||
count: number; // 挑战次数
|
||||
@prop({ required: true, default: 0 })
|
||||
buyCount: number; // 购买次数次数
|
||||
@prop({ required: true, default: 0 })
|
||||
refTime: number; // 刷新时间
|
||||
|
||||
public static async getDailyRecordById(roleId: string, type: number, lean = true) {
|
||||
const result = await DailyRecordModel.findOneAndUpdate({ roleId, type }, {}, {new: true, upsert: true}).select('count refTime').lean(lean);
|
||||
const result = await DailyRecordModel.findOneAndUpdate({ roleId, type }, {}, {new: true, upsert: true}).select('type count buyCount refTime').lean(lean);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async refreshRecord(roleId: string, type: number, lean = true) {
|
||||
const dailyRecord = await DailyRecordModel.findOne({ roleId, type }).lean(lean);
|
||||
let {count = 0, refTime = 0} = dailyRecord||{};
|
||||
let {count = 0, refTime = 0, buyCount = 0} = dailyRecord||{};
|
||||
let now = new Date();
|
||||
let today = now.setHours(0, 0, 0, 0);
|
||||
if(today > refTime) {
|
||||
refTime = today;
|
||||
count = 0;
|
||||
buyCount = 0;
|
||||
}
|
||||
let result = await DailyRecordModel.findOneAndUpdate({roleId, type}, {$set: {refTime, count}}, {new: true, upsert: true});
|
||||
let result = await DailyRecordModel.findOneAndUpdate({roleId, type}, {$set: {refTime, count, buyCount}}, {new: true, upsert: true});
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -43,6 +46,11 @@ export default class DailyRecord extends BaseModel {
|
||||
let result = await DailyRecordModel.deleteMany({roleId}).lean(lean);
|
||||
return result||{};
|
||||
}
|
||||
|
||||
public static async increseBuyCount(roleId: string, type: number, count: number, lean = true) {
|
||||
const result = await DailyRecordModel.findOneAndUpdate({ roleId, type }, {$inc: { buyCount:count } }, {new: true, upsert: true}).lean(lean);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const DailyRecordModel = getModelForClass(DailyRecord);
|
||||
|
||||
@@ -1 +1,34 @@
|
||||
[{"dailyType":1,"name":"护送商人","timesPerDay":1,"description":"内有不同难度,可获得大量铜钱"},{"dailyType":2,"name":"守卫城池","timesPerDay":1,"description":"内有不同难度,可获得大量武将经验书"},{"dailyType":3,"name":"保护矿产","timesPerDay":1,"description":"内有不同难度,可获得大量装备材料"}]
|
||||
[
|
||||
{
|
||||
"dailyType": 1,
|
||||
"name": "护送商人",
|
||||
"timesPerDay": 5,
|
||||
"timesCanBuy": 10,
|
||||
"difficultLvl": "3001&3002&3003&3004&3005",
|
||||
"description": "内有不同难度,可获得大量铜钱"
|
||||
},
|
||||
{
|
||||
"dailyType": 2,
|
||||
"name": "城池守卫",
|
||||
"timesPerDay": 5,
|
||||
"timesCanBuy": 10,
|
||||
"difficultLvl": "3010&3011&3012&3013&3014",
|
||||
"description": "内有不同难度,可获得大量武将经验书"
|
||||
},
|
||||
{
|
||||
"dailyType": 3,
|
||||
"name": "保护矿产",
|
||||
"timesPerDay": 5,
|
||||
"timesCanBuy": 10,
|
||||
"difficultLvl": "3020&3021&3022&3023&3024",
|
||||
"description": "内有不同难度,可获得大量装备材料"
|
||||
},
|
||||
{
|
||||
"dailyType": 4,
|
||||
"name": "测试测试",
|
||||
"timesPerDay": 5,
|
||||
"timesCanBuy": 10,
|
||||
"difficultLvl": "3020&3021&3022&3023&3024",
|
||||
"description": "内有不同难度,测试用"
|
||||
}
|
||||
]
|
||||
@@ -5,21 +5,19 @@
|
||||
"bg_img_id": 3001,
|
||||
"script_id": 0,
|
||||
"fixReward": "&",
|
||||
"conditionReward": "&",
|
||||
"RandomReward": "&",
|
||||
"warType": 2,
|
||||
"warType": 4,
|
||||
"gk_name": "每日&护送商人",
|
||||
"kingExp": 100,
|
||||
"lvLimted": 10,
|
||||
"turnLimted": 20,
|
||||
"forcedCharactor": "&",
|
||||
"fobiddenCharactor": "&",
|
||||
"victoryInfoInUI": "商人到达安全地区",
|
||||
"loseInfoInUI": "商人死亡\n我方全部阵亡",
|
||||
"starInfoInUI": "无",
|
||||
"cost": 15,
|
||||
"sSpineInUI": "1&daobing|1&gongbing",
|
||||
"detailUIBg": "zhending",
|
||||
"victoryInfoInUI": "马车到达安全地区",
|
||||
"loseInfoInUI": "马车被抢\n我方全部阵亡",
|
||||
"starInfoInUI": "1.我方无人阵亡;\n2.在5回合内获得胜利",
|
||||
"cost": 0,
|
||||
"iconInMap": "dengji1",
|
||||
"iconName": "新手",
|
||||
"recommendedPower": 10008,
|
||||
"previousGk": 0,
|
||||
"dailyType": 1
|
||||
@@ -30,21 +28,19 @@
|
||||
"bg_img_id": 3002,
|
||||
"script_id": 0,
|
||||
"fixReward": "&",
|
||||
"conditionReward": "&",
|
||||
"RandomReward": "&",
|
||||
"warType": 2,
|
||||
"warType": 4,
|
||||
"gk_name": "每日&护送商人",
|
||||
"kingExp": 100,
|
||||
"lvLimted": 10,
|
||||
"turnLimted": 20,
|
||||
"forcedCharactor": "&",
|
||||
"fobiddenCharactor": "&",
|
||||
"victoryInfoInUI": "商人到达安全地区",
|
||||
"loseInfoInUI": "商人死亡\n我方全部阵亡",
|
||||
"starInfoInUI": "无",
|
||||
"cost": 15,
|
||||
"sSpineInUI": "1&daobing|1&gongbing",
|
||||
"detailUIBg": "zhending",
|
||||
"victoryInfoInUI": "马车到达安全地区",
|
||||
"loseInfoInUI": "马车被抢\n我方全部阵亡",
|
||||
"starInfoInUI": "1.我方无人阵亡;\n2.在5回合内获得胜利",
|
||||
"cost": 0,
|
||||
"iconInMap": "dengji2",
|
||||
"iconName": "普通",
|
||||
"recommendedPower": 10008,
|
||||
"previousGk": 3001,
|
||||
"dailyType": 1
|
||||
@@ -55,21 +51,19 @@
|
||||
"bg_img_id": 3003,
|
||||
"script_id": 0,
|
||||
"fixReward": "&",
|
||||
"conditionReward": "&",
|
||||
"RandomReward": "&",
|
||||
"warType": 2,
|
||||
"warType": 4,
|
||||
"gk_name": "每日&护送商人",
|
||||
"kingExp": 100,
|
||||
"lvLimted": 10,
|
||||
"turnLimted": 20,
|
||||
"forcedCharactor": "&",
|
||||
"fobiddenCharactor": "&",
|
||||
"victoryInfoInUI": "商人到达安全地区",
|
||||
"loseInfoInUI": "商人死亡\n我方全部阵亡",
|
||||
"starInfoInUI": "无",
|
||||
"cost": 15,
|
||||
"sSpineInUI": "1&daobing|1&gongbing",
|
||||
"detailUIBg": "zhending",
|
||||
"victoryInfoInUI": "马车到达安全地区",
|
||||
"loseInfoInUI": "马车被抢\n我方全部阵亡",
|
||||
"starInfoInUI": "1.我方无人阵亡;\n2.在5回合内获得胜利",
|
||||
"cost": 0,
|
||||
"iconInMap": "dengji3",
|
||||
"iconName": "困难",
|
||||
"recommendedPower": 10008,
|
||||
"previousGk": 3002,
|
||||
"dailyType": 1
|
||||
@@ -80,21 +74,19 @@
|
||||
"bg_img_id": 3004,
|
||||
"script_id": 0,
|
||||
"fixReward": "&",
|
||||
"conditionReward": "&",
|
||||
"RandomReward": "&",
|
||||
"warType": 2,
|
||||
"warType": 4,
|
||||
"gk_name": "每日&护送商人",
|
||||
"kingExp": 100,
|
||||
"lvLimted": 10,
|
||||
"turnLimted": 20,
|
||||
"forcedCharactor": "&",
|
||||
"fobiddenCharactor": "&",
|
||||
"victoryInfoInUI": "商人到达安全地区",
|
||||
"loseInfoInUI": "商人死亡\n我方全部阵亡",
|
||||
"starInfoInUI": "无",
|
||||
"cost": 15,
|
||||
"sSpineInUI": "1&daobing|1&gongbing",
|
||||
"detailUIBg": "zhending",
|
||||
"victoryInfoInUI": "马车到达安全地区",
|
||||
"loseInfoInUI": "马车被抢\n我方全部阵亡",
|
||||
"starInfoInUI": "1.我方无人阵亡;\n2.在5回合内获得胜利",
|
||||
"cost": 0,
|
||||
"iconInMap": "dengji4",
|
||||
"iconName": "恶梦",
|
||||
"recommendedPower": 10008,
|
||||
"previousGk": 3003,
|
||||
"dailyType": 1
|
||||
@@ -105,23 +97,44 @@
|
||||
"bg_img_id": 3005,
|
||||
"script_id": 0,
|
||||
"fixReward": "&",
|
||||
"conditionReward": "&",
|
||||
"RandomReward": "&",
|
||||
"warType": 2,
|
||||
"warType": 4,
|
||||
"gk_name": "每日&护送商人",
|
||||
"kingExp": 100,
|
||||
"lvLimted": 10,
|
||||
"turnLimted": 20,
|
||||
"forcedCharactor": "&",
|
||||
"fobiddenCharactor": "&",
|
||||
"victoryInfoInUI": "商人到达安全地区",
|
||||
"loseInfoInUI": "商人死亡\n我方全部阵亡",
|
||||
"starInfoInUI": "无",
|
||||
"cost": 15,
|
||||
"sSpineInUI": "1&daobing|1&gongbing",
|
||||
"detailUIBg": "zhending",
|
||||
"victoryInfoInUI": "马车到达安全地区",
|
||||
"loseInfoInUI": "马车被抢\n我方全部阵亡",
|
||||
"starInfoInUI": "1.我方无人阵亡;\n2.在5回合内获得胜利",
|
||||
"cost": 0,
|
||||
"iconInMap": "dengji5",
|
||||
"iconName": "地狱",
|
||||
"recommendedPower": 10008,
|
||||
"previousGk": 3004,
|
||||
"dailyType": 1
|
||||
},
|
||||
{
|
||||
"war_id": 0,
|
||||
"dispatchJsonId": 0,
|
||||
"bg_img_id": 0,
|
||||
"script_id": 0,
|
||||
"fixReward": 0,
|
||||
"warType": 0,
|
||||
"gk_name": 0,
|
||||
"kingExp": 0,
|
||||
"lvLimted": 0,
|
||||
"turnLimted": 0,
|
||||
"forcedCharactor": 0,
|
||||
"fobiddenCharactor": 0,
|
||||
"victoryInfoInUI": 0,
|
||||
"loseInfoInUI": 0,
|
||||
"starInfoInUI": 0,
|
||||
"cost": 0,
|
||||
"iconInMap": 0,
|
||||
"iconName": 0,
|
||||
"recommendedPower": 0,
|
||||
"previousGk": " ",
|
||||
"dailyType": 0
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user