Files
MatchMaster/assets/shop/script/passCheckMgr.ts
T
2026-10-09 10:03:29 +08:00

653 lines
26 KiB
TypeScript

import scrollViewList from "../../Script/module/List/scrollviewList";
import Utils from "../../Script/module/Pay/Utils";
import PassCheckItem from "./passCheckItem";
import { canClaimPass, hasPassReward, passBounds } from '../../Script/module/Config/BattlePassModel';
const { ccclass, property } = cc._decorator;
@ccclass
export default class passCheckMgr extends cc.Component {
private list: scrollViewList = null;
private passCheckItem: PassCheckItem = null;
listData: any[] = [];
private progressLevel: number = 0;
private progressBar3: cc.Node = null;
private progressBar4: cc.Node = null;
private wujin: cc.Node = null;
private ProgressIndex: number = 0;
private passCheckInfo: any[] = [];
private endTime: any;
private stage: any;
private passData: any;
private rewardUpdateTimeout: any = null;
private scrollOffsetY: number = 0;
onLoad() {
}
start() {
}
init(data: any[], progressLevel: number, oldPassCheckInfo: any[], time: any, stage?: any, passData?: any, progressStart?: number) {
// 确保组件已经被正确加载
if (!this.node) {
console.error("passCheckMgr: node is null");
return;
}
this.endTime = time;
this.stage = stage;
this.passData = passData;
this.passCheckInfo = oldPassCheckInfo;
// 设置当前进度等级
if (progressLevel !== undefined) {
this.progressLevel = progressLevel;
}
this.ProgressIndex = progressStart === undefined ? this.progressLevel : progressStart;
// 获取ScrollView组件(使用不区分大小写的查找)
let scrollViewNode: cc.Node | null = null;
const children = this.node.children;
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (child.name.toLowerCase() === "scrollview") {
scrollViewNode = child;
break;
}
}
if (!scrollViewNode) {
console.error("passCheckMgr: Cannot find ScrollView node under this.node");
return;
}
// 尝试获取scrollViewList组件,如果没有则自动添加
this.list = scrollViewNode.getComponent(scrollViewList);
if (!this.list) {
this.list = scrollViewNode.addComponent(scrollViewList);
}
// 手动调用初始化,因为组件可能是在运行时添加的
this.list["init"]();
// 设置数据
this.listData = data;
// console.log("列表数据", data);
if (this.progressBar3) this.progressBar3.parent = scrollViewNode;
if (this.progressBar4) this.progressBar4.parent = scrollViewNode;
this.wujin = this.wujin || scrollViewNode.getChildByName('wujin');
if (this.wujin) this.wujin.parent = scrollViewNode;
this.list.setData(data);
const view = scrollViewNode.getComponent(cc.ScrollView);
const rows = passBounds(this.stage, data.length).content;
const height = this.list.itemHeight > 0 ? this.list.itemHeight : this.list['itemHeightActual'];
view.content.height = this.list.paddingTop + rows * height + Math.max(0, rows - 1) * this.list.spacingY;
view.elastic = data.length > rows;
if (this.wujin) {
this.wujin.parent = view.content;
this.wujin.active = data.length > 35;
this.wujin.zIndex = 3;
this.wujin.setPosition(view.content.width / 2, -this.list.paddingTop - 35 * (height + this.list.spacingY));
}
this.scrollToLastClaimedReward(scrollViewNode);
// 创建进度条
this.setupProgressBars(scrollViewNode);
// 刷新list数据
// this.updateListItem(data);
}
private scrollToLastClaimedReward(scrollViewNode: cc.Node) {
if (this.listData.length === 0) {
return;
}
const getLastClaimedIndex = (key: string) => {
for (let i = this.listData.length - 1; i >= 0; i--) {
if (this.listData[i][key] === 0) {
return i;
}
}
return -1;
};
const leftIndex = getLastClaimedIndex("free");
const lastClaimedIndex = cc.fx.GameConfig.GM_INFO.passCheckActivate
? Math.min(leftIndex, getLastClaimedIndex("passCheck"))
: leftIndex;
const targetIndex = Math.max(0, lastClaimedIndex);
const itemHeight = this.list.itemHeight > 0
? this.list.itemHeight
: this.list["itemHeightActual"];
const desiredOffsetY = this.list.paddingTop + targetIndex * (itemHeight + this.list.spacingY);
const scrollView = scrollViewNode.getComponent(cc.ScrollView);
const targetOffsetY = Math.min(desiredOffsetY, scrollView.getMaxScrollOffset().y);
this.scheduleOnce(() => {
const maxOffsetY = scrollView.getMaxScrollOffset().y;
const startOffsetY = Math.max(0, Math.min(scrollView.getScrollOffset().y, maxOffsetY));
if (Math.abs(targetOffsetY - startOffsetY) < 1) {
scrollView.scrollToOffset(cc.v2(0, targetOffsetY), 0);
return;
}
const targetLevel = targetIndex + 1;
const totalDuration = Math.min(1, 0.5 + Math.max(0, targetLevel - 10) * 0.025);
const brakeDuration = 0.3;
const fastDuration = totalDuration - brakeDuration;
const brakeProgress = 3 * fastDuration / (brakeDuration + 3 * fastDuration);
const brakeOffsetY = startOffsetY + (targetOffsetY - startOffsetY) * brakeProgress;
this.scrollOffsetY = startOffsetY;
scrollView.stopAutoScroll();
cc.Tween.stopAllByTarget(this);
cc.tween(this)
.to(fastDuration, { scrollOffsetY: brakeOffsetY }, {
easing: "linear",
onUpdate: () => scrollView.scrollToOffset(cc.v2(0, this.scrollOffsetY), 0),
})
.to(brakeDuration, { scrollOffsetY: targetOffsetY }, {
easing: "cubicOut",
onUpdate: () => scrollView.scrollToOffset(cc.v2(0, this.scrollOffsetY), 0),
})
.call(() => scrollView.scrollToOffset(cc.v2(0, targetOffsetY), 0))
.start();
});
}
private setupProgressBars(scrollViewNode: cc.Node) {
const content = scrollViewNode.getComponent(cc.ScrollView).content;
// 获取进度条节点
this.progressBar3 = this.node.getChildByName("scrollView").getChildByName("progress_bar_3");
this.progressBar4 = this.node.getChildByName("scrollView").getChildByName("progress_bar_4");
// 确保进度条节点存在
if (!this.progressBar3 || !this.progressBar4) {
console.error("passCheckMgr: Progress bar nodes not found");
return;
}
// 确保进度条有Sprite组件
let sprite3 = this.progressBar3.getComponent(cc.Sprite);
if (!sprite3) {
sprite3 = this.progressBar3.addComponent(cc.Sprite);
}
let sprite4 = this.progressBar4.getComponent(cc.Sprite);
if (!sprite4) {
sprite4 = this.progressBar4.addComponent(cc.Sprite);
}
// 设置进度条父节点为content
this.progressBar3.parent = content;
this.progressBar4.parent = content;
// 设置进度条属性
this.progressBar3.width = 32;
this.progressBar3.anchorX = 0.5;
this.progressBar3.anchorY = 0.5;
this.progressBar3.zIndex = 1; // 确保进度条在背景层
this.progressBar4.width = 32;
this.progressBar4.anchorX = 0.5;
this.progressBar4.anchorY = 0.5;
this.progressBar4.zIndex = 1; // 确保进度条在背景层
// 激活进度条
this.progressBar3.active = true;
this.progressBar4.active = true;
// 更新进度条显示
this.updateProgressBars();
}
// ... existing code ...
private updateProgressBars() {
// 确保进度条已创建
if (!this.progressBar3 || !this.progressBar4) {
console.log("passCheckMgr: Progress bars not created yet");
return;
}
// 确保有足够的数据来计算进度条位置
if (this.listData.length === 0) {
console.warn("passCheckMgr: No data to calculate progress bar positions");
return;
}
// 获取scrollViewList组件的配置
const itemHeight = this.list.itemHeight > 0 ? this.list.itemHeight : this.list["itemHeightActual"];
const spacingY = this.list.spacingY;
const paddingTop = this.list.paddingTop;
const paddingBottom = this.list.paddingBottom;
// 计算第一个item的中心Y位置
const firstItemCenterY = -paddingTop - itemHeight / 2;
// 计算最后一个item的中心Y位置
const lastIndex = this.listData.length - 1;
const lastItemCenterY = -paddingTop - itemHeight / 2 - lastIndex * (itemHeight + spacingY);
// 计算总长度
const totalLength = Math.abs(firstItemCenterY - lastItemCenterY);
const centerX = this.progressBar3.parent.width / 2;
// 设置总长度进度条 (progress_bar_3)
this.progressBar3.height = totalLength + itemHeight + spacingY;
this.progressBar3.setAnchorPoint(0.5, 1);
this.progressBar3.setPosition(centerX, firstItemCenterY);
// 与上方动画使用同一期次、同一次读取的已完成档数。
if (this.ProgressIndex > this.progressLevel) {
this.ProgressIndex = this.progressLevel;
}
const adjustedProgressIndex = Math.max(0, this.progressLevel - 1);
const adjustedStoredIndex = Math.max(0, this.ProgressIndex - 1);
// 计算进度位置
const progressIndex1 = Math.min(adjustedStoredIndex, this.listData.length - 1);
const progressIndex2 = Math.min(adjustedProgressIndex, this.listData.length - 1);
// 计算对应位置的Y坐标
const progressItemCenterY1 = -paddingTop - itemHeight / 2 - progressIndex1 * (itemHeight + spacingY);
const progressItemCenterY2 = -paddingTop - itemHeight / 2 - progressIndex2 * (itemHeight + spacingY);
// 计算进度条高度
const progressLength1 = Math.abs(firstItemCenterY - progressItemCenterY1);
const progressLength2 = Math.abs(firstItemCenterY - progressItemCenterY2);
this.progressBar4.setAnchorPoint(0.5, 1);
this.progressBar4.setPosition(centerX, firstItemCenterY);
cc.Tween.stopAllByTarget(this.progressBar4);
this.progressBar4.height = progressLength1;
let growthLength = progressLength2 - progressLength1;
const targetHeight = progressLength1 + growthLength;
if (growthLength > 0) {
cc.tween(this.progressBar4)
.delay(0.5)
.to(1, { height: targetHeight })
.call(() => {
this.progressBar4.height = progressLength2;
})
.start();
} else if (growthLength < 0) {
// 如果是倒退的情况(虽然不太可能)
this.progressBar4.height = progressLength2;
} else if (growthLength === 0 && progressLength2 === 0) {
// 特殊情况:当进度为1级时,进度条应该为0
this.progressBar4.height = 0;
}
}
public updatePassCheckStatus(updates?: Array<{ itemIndex: number, isLeftReward: boolean }>) {
// 如果提供了updates数组,则批量更新对应的奖励状态
if (updates && Array.isArray(updates) && updates.length > 0) {
// 批量更新本地数据状态
for (const update of updates) {
const { itemIndex, isLeftReward } = update;
if (this.listData[itemIndex]) {
if (isLeftReward) {
this.listData[itemIndex].free = 0; // 标记为已领取
} else {
this.listData[itemIndex].passCheck = 0; // 标记为已领取
}
}
}
}
console.log("给服务器发的值", updates);
const passCheck = { ...this.passData };
const key = String(passCheck[1].time) === String(this.endTime) ? 1 : 2;
passCheck[key] = this.stage;
for (const lane of ['free', 'passCheck']) for (let i = 0; i < this.listData.length; i++) {
if (this.listData[i][lane] === 0) passCheck[key][lane][i] = 0;
}
// 发送到服务器
Utils.setPassCheckInfo((res) => {
if (res.code === 1) {
console.log("通行证状态保存成功");
console.log("服务器返回的成功响应:", res);
this.checkAllRewardsClaimed();
// this.refreshPassCheckItems();
} else {
console.log("通行证状态保存失败");
}
}, passCheck);
}
// 检查所有奖励是否领取完毕
private checkAllRewardsClaimed() {
const allClaimed = !hasPassReward(this.stage) && !hasPassReward(this.passCheckInfo);
if (allClaimed) this.hidePassCheckRedDot();
return allClaimed;
}
// 隐藏通行证按钮上的红点
private hidePassCheckRedDot() {
const canvas = cc.find("Canvas");
if (canvas) {
const jiaZai = canvas.getComponent("JiaZai");
if (jiaZai) {
const top = jiaZai.node.getChildByName("Load")?.getChildByName("Top");
const passBtn = top?.getChildByName("passBtn");
const redNode = passBtn?.getChildByName("red");
if (redNode) {
redNode.active = false;
}
}
}
}
public refreshPassCheckItems() {
this.updatePassCheckStatus();
// 通知所有 passCheckItem 组件更新界面
if (this.list && this.list.node) {
// 通过ScrollView组件获取content节点
const scrollViewComponent = this.list.node.getComponent(cc.ScrollView);
if (scrollViewComponent && scrollViewComponent.content) {
const content = scrollViewComponent.content;
// 遍历所有子节点,查找 passCheckItem 组件并刷新
for (let i = 0; i < content.childrenCount; i++) {
const child = content.children[i];
// 查找 passCheckItem 节点(通常是间接子节点)
if (child.name === "passCheckItem") {
const passCheckItem = child.getComponent("passCheckItem");
if (passCheckItem) {
passCheckItem.dataChanged();
}
// }
}
}
}
}
}
public updateRewardStatus(updates: Array<{ itemIndex: number, isLeftReward: boolean }>) {
const currentTime = Date.now();
// 使用防抖机制,延迟处理奖励更新
if (this.rewardUpdateTimeout) {
clearTimeout(this.rewardUpdateTimeout);
}
console.log("传给服务器", updates)
this.rewardUpdateTimeout = setTimeout(() => {
// 修改为传入数组
this.updatePassCheckStatus(updates);
this.rewardUpdateTimeout = null;
}, 50);
}
public updateActivateStatus() {
this.updatePassCheckStatus();
}
// 奖励队列和处理状态
private rewardQueue: Array<{ itemIndex: number, itemId: number, itemNum: number, isLeftReward: boolean }> = [];
private isProcessingRewards: boolean = false;
private rewardProcessTimeout: any = null;
// 整合自passCheckItem.ts的claimReward方法,支持处理可能的循环调用
public claimReward(itemIndex: number, itemId: number, itemNum: number, isLeftReward: boolean) {
// Once the period closes, only the atomic settlement endpoint may grant the remainder.
if (Number(this.endTime) <= Date.now()) return;
if (!canClaimPass(this.stage, itemIndex, isLeftReward)) return;
this.stage[isLeftReward ? 'free' : 'passCheck'][itemIndex] = 0;
// 将奖励请求添加到队列中
console.log("领取奖励ID", itemIndex, "物品ID", itemId, "数量", itemNum)
this.rewardQueue.push({ itemIndex, itemId, itemNum, isLeftReward });
// 清除之前的定时器
if (this.rewardProcessTimeout) {
clearTimeout(this.rewardProcessTimeout);
}
// 设置延迟处理,合并短时间内多次调用
this.rewardProcessTimeout = setTimeout(() => {
this.processRewardQueue();
}, 50); // 50ms延迟,合并短时间内多次调用
}
// 处理奖励队列
private processRewardQueue() {
// 如果正在处理中,则等待下次处理
if (this.isProcessingRewards) {
// 重新设置定时器,稍后再次尝试
if (this.rewardProcessTimeout) {
clearTimeout(this.rewardProcessTimeout);
}
this.rewardProcessTimeout = setTimeout(() => {
this.processRewardQueue();
}, 100);
return;
}
// 如果队列为空,直接返回
if (this.rewardQueue.length === 0) {
return;
}
// 标记为正在处理
this.isProcessingRewards = true;
// 处理队列中的所有奖励
this.processRewardsInternal([...this.rewardQueue]);
// 清空队列
this.rewardQueue = [];
// 处理完成
this.isProcessingRewards = false;
}
// 内部处理奖励方法
private processRewardsInternal(rewards: Array<{ itemIndex: number, itemId: number, itemNum: number, isLeftReward: boolean }>) {
if (Number(this.endTime) <= Date.now()) {
for (const reward of rewards) this.stage[reward.isLeftReward ? 'free' : 'passCheck'][reward.itemIndex] = 1;
return;
}
// 处理所有奖励的逻辑(道具增加等)
const rewardData = [];
const canvasTemp = cc.find("Canvas");
let JiaZai = null;
if (canvasTemp) {
JiaZai = canvasTemp.getComponent("JiaZai");
}
// 处理每个奖励
for (const reward of rewards) {
cc.fx.GameTool.getPassCheckPorp(reward.itemId, reward.itemNum, false);
cc.fx.GameTool.shushu_Track("get_battle_pass", {
cur_bp_level: reward.itemIndex + 1,
is_paid_bp_usr: !reward.isLeftReward,
});
if (JiaZai) {
switch (reward.itemId) {
case 1001: // 金币
JiaZai.updateCoin();
break;
case 2001: // 冰冻道具
case 2002: // 锤子道具
case 2003: // 魔棒道具
break;
default:
JiaZai.updatePower();
break;
}
}
console.log("领取奖励", reward.itemId, reward.itemNum);
// 通知对应的passCheckItem组件更新UI
this.updateRewardUIAfterClaim(reward.itemIndex, reward.isLeftReward);
// 收集奖励数据
rewardData.push({ itemId: reward.itemId, itemNum: reward.itemNum });
}
// 合并相同类型的奖励
const mergedRewards = this.processRewardData(rewardData);
// 显示奖励弹窗(只显示一次)
if (JiaZai && mergedRewards.length > 0) {
JiaZai.openRewardWindow(mergedRewards);
}
// 更新服务器状态
console.log("更新奖励状态到服务器:", rewards.length);
if (rewards.length > 0) {
const rewardUpdates = rewards.map(reward => ({
itemIndex: reward.itemIndex,
isLeftReward: reward.isLeftReward
}));
this.updateRewardStatus(rewardUpdates);
}
}
// 封装奖励数据处理方法,处理循环调用并合并相同类型奖励
private processRewardData(rewards: Array<{ itemId: number, itemNum: number }>): Array<{ type: string, count: number }> {
// 创建一个映射来存储每种类型的奖励总和
const rewardMap = new Map<string, number>();
// 处理每个奖励项
for (const reward of rewards) {
let type: string = "";
switch (reward.itemId) {
case 1001: // 金币
type = "coin";
break;
case 2001: // 冰冻道具
type = "freeze";
break;
case 2002: // 锤子道具
type = "hammer";
break;
case 2003: // 魔棒道具
type = "magic";
break;
case 3001: // 无限体力 900秒
type = "infinite_health";
reward.itemNum = 900;
break;
case 3002: // 无限体力 1800秒
type = "infinite_health";
reward.itemNum = 1800;
break;
case 3003: // 无限体力 3600秒
type = "infinite_health";
reward.itemNum = 3600;
break;
default:
// 默认处理为无限体力,使用传入的数量
type = "infinite_health";
break;
}
console.log("处理奖励数据", reward.itemId, reward.itemNum);
// 如果该类型已存在,累加数量;否则设置新值
if (rewardMap.has(type)) {
rewardMap.set(type, rewardMap.get(type)! + reward.itemNum);
} else {
rewardMap.set(type, reward.itemNum);
}
}
// 转换为数组格式
const result: Array<{ type: string, count: number }> = [];
rewardMap.forEach((count, type) => {
result.push({ type, count });
});
return result;
}
// 整合自passCheckItem.ts的updateRewardUIAfterClaim方法
private updateRewardUIAfterClaim(itemIndex: number, isLeftReward: boolean) {
// 通知对应的passCheckItem组件更新UI
if (this.list && this.list.node) {
// 通过ScrollView组件获取content节点
const scrollViewComponent = this.list.node.getComponent(cc.ScrollView);
if (scrollViewComponent && scrollViewComponent.content) {
const content = scrollViewComponent.content;
// 遍历所有子节点,查找对应索引的 passCheckItem 组件并更新UI
for (let i = 0; i < content.childrenCount; i++) {
const child = content.children[i];
// 查找 passCheckItem 节点
if (child.name === "passCheckItem") {
const passCheckItem = child.getComponent("passCheckItem");
if (passCheckItem && passCheckItem.itemIndex === itemIndex) {
// 调用passCheckItem的updateRewardUIAfterClaim方法
if (passCheckItem.updateRewardUIAfterClaim) {
passCheckItem.updateRewardUIAfterClaim(isLeftReward);
}
break;
}
}
}
}
}
}
allGetReward() {
const allRewardsToClaim = [];
for (let i = 0; i < this.listData.length; i++) {
// 检查免费奖励是否可以领取
if (this.listData[i].free === 1) {
allRewardsToClaim.push({
itemIndex: i,
itemId: this.listData[i].ItemID1,
itemNum: this.listData[i].ItemNum1,
isLeftReward: true
});
}
if (cc.fx.GameConfig.GM_INFO.passCheckActivate) {
// 检查通行证奖励是否可以领取
if (this.listData[i].passCheck === 1) {
allRewardsToClaim.push({
itemIndex: i,
itemId: this.listData[i].ItemID2,
itemNum: this.listData[i].ItemNum2,
isLeftReward: false
});
}
}
}
console.log("批量领取所有物品:", allRewardsToClaim);
// 批量处理所有奖励
for (const reward of allRewardsToClaim) {
this.claimReward(
reward.itemIndex,
reward.itemId,
reward.itemNum,
reward.isLeftReward
);
}
}
protected onDestroy(): void {
console.log("销毁passCheckMgr");
cc.Tween.stopAllByTarget(this);
if (this.progressBar4) cc.Tween.stopAllByTarget(this.progressBar4);
// 清理可能存在的列表数据
if (this.listData) {
this.listData = null;
}
// 如果列表组件存在,尝试清理它
if (this.list && cc.isValid(this.list.node)) {
try {
// 先清理数据
if (this.list.setData) {
this.list.setData([]);
}
} catch (e) {
console.warn("清理列表数据时出错:", e);
}
}
this.list = null;
this.passCheckItem = null;
}
}