每日添加购买次数逻辑
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}
|
||||
}
|
||||
Reference in New Issue
Block a user