139 lines
7.5 KiB
TypeScript
139 lines
7.5 KiB
TypeScript
import SevenDayGiftBootstrapApi from "../../Script/seven_day_gift/SevenDayGiftBootstrapApi";
|
||
|
||
const { ccclass } = cc._decorator;
|
||
const CLAIM_KEY = "seven_day_gift_processed_claims_v1";
|
||
const AUTO_KEY = "seven_day_gift_auto_open_v1";
|
||
|
||
/** 仅独立测试服可用。只清本账号、本活动的本地记录,不改资产和服务器。 */
|
||
@ccclass
|
||
export default class SevenDayTest extends cc.Component {
|
||
private panel: cc.Node = null;
|
||
private output: cc.Label = null;
|
||
private snapshot: any = null;
|
||
private snapshotUid = "";
|
||
private snapshotUrl = "";
|
||
private responseCode: any = null;
|
||
private querying = false;
|
||
private isActivityOpen: () => boolean = () => false;
|
||
private recheck: () => void = () => {};
|
||
|
||
onLoad() {
|
||
if (!this.allowed()) { this.node.active = false; return; }
|
||
this.panel = this.node.getChildByName("panel");
|
||
const background = this.panel.addComponent(cc.Graphics);
|
||
background.fillColor = cc.color(25, 30, 42, 245);
|
||
background.roundRect(-500, -700, 1000, 1400, 18);
|
||
background.fill();
|
||
this.output = this.panel.getChildByName("viewport").getChildByName("content").getComponent(cc.Label);
|
||
this.bind(this.node.getChildByName("entry"), () => { this.panel.active = true; this.render("点击“查询接口”获取当前快照;查询可能影响服务端签到记录。"); });
|
||
this.bind(this.panel.getChildByName("close"), () => this.panel.active = false);
|
||
this.bind(this.panel.getChildByName("query"), () => this.query());
|
||
this.bind(this.panel.getChildByName("clearClaim"), () => this.clearCache(true));
|
||
this.bind(this.panel.getChildByName("clearAuto"), () => this.clearCache(false));
|
||
this.bind(this.panel.getChildByName("recheck"), () => {
|
||
if (this.querying || this.isActivityOpen()) { this.render("请先关闭活动弹窗,并等待查询结束。"); return; }
|
||
this.panel.active = false;
|
||
this.recheck();
|
||
});
|
||
}
|
||
|
||
public initialize(isActivityOpen: () => boolean, recheck: () => void) {
|
||
this.isActivityOpen = isActivityOpen;
|
||
this.recheck = recheck;
|
||
}
|
||
|
||
private allowed(): boolean {
|
||
return SevenDayGiftBootstrapApi.getTestEnvironment().allowed;
|
||
}
|
||
|
||
private bind(node: cc.Node, action: () => void) {
|
||
node.on(cc.Node.EventType.TOUCH_END, (event: cc.Event) => {
|
||
event.stopPropagation();
|
||
if (!this.allowed()) { this.node.active = false; return; }
|
||
action();
|
||
}, this);
|
||
}
|
||
|
||
private query() {
|
||
if (this.querying || this.isActivityOpen()) { this.render("请先关闭七日活动弹窗,并等待已有查询结束。"); return; }
|
||
const uid = SevenDayGiftBootstrapApi.getUid();
|
||
if (!uid) { this.render("当前没有登录 UID。"); return; }
|
||
this.querying = true;
|
||
this.responseCode = null;
|
||
this.snapshot = null;
|
||
this.snapshotUid = uid;
|
||
this.snapshotUrl = SevenDayGiftBootstrapApi.getTestEnvironment().baseUrl;
|
||
this.render("查询中…");
|
||
SevenDayGiftBootstrapApi.getInfo(response => {
|
||
if (!cc.isValid(this.node, true)) return;
|
||
this.querying = false;
|
||
if (!this.allowed()) { this.node.active = false; return; }
|
||
if (uid !== SevenDayGiftBootstrapApi.getUid() || this.snapshotUrl !== SevenDayGiftBootstrapApi.getTestEnvironment().baseUrl) {
|
||
this.snapshot = null;
|
||
this.render("账号或地址已改变,请重新查询。");
|
||
return;
|
||
}
|
||
this.responseCode = response && response.code;
|
||
this.snapshot = response && response.code === 1 && response.data || null;
|
||
this.render(this.snapshot ? "查询完成。仅展示数据,不自动领取。" : "接口失败或 data 为空/false,未取得有效活动快照。");
|
||
});
|
||
}
|
||
|
||
private clearCache(claims: boolean) {
|
||
if (this.querying || this.isActivityOpen()) { this.render("请先关闭七日活动弹窗,并等待查询结束。"); return; }
|
||
const uid = SevenDayGiftBootstrapApi.getUid();
|
||
const info = this.snapshot;
|
||
if (!info || !info.activityId || !info.startAt || uid !== this.snapshotUid || this.snapshotUrl !== SevenDayGiftBootstrapApi.getTestEnvironment().baseUrl) {
|
||
this.render("请先查询当前账号的有效活动快照,再清除此活动缓存。");
|
||
return;
|
||
}
|
||
const storage = cc.fx.StorageMessage;
|
||
if (claims) {
|
||
const records = storage.getStorage(CLAIM_KEY) || {};
|
||
const prefix = uid + ":";
|
||
Object.keys(records).forEach(key => {
|
||
if (key.indexOf(prefix) !== 0) return;
|
||
try {
|
||
const claim = JSON.parse(key.slice(prefix.length));
|
||
if (Array.isArray(claim) && claim[0] === info.activityId && claim[1] === info.startAt) delete records[key];
|
||
} catch (_) { /* 不清理其他格式的数据。 */ }
|
||
});
|
||
storage.setStorage(CLAIM_KEY, records);
|
||
} else {
|
||
storage.removeStorage(AUTO_KEY + ":" + uid + ":" + info.activityId);
|
||
}
|
||
this.render((claims ? "已清除本活动本地领取防重记录。" : "已清除本活动自动弹窗记录。") + "服务器进度和已到账资产不变;再次领取可能增加资产。");
|
||
}
|
||
|
||
private render(message: string) {
|
||
const env = SevenDayGiftBootstrapApi.getTestEnvironment();
|
||
const uid = SevenDayGiftBootstrapApi.getUid();
|
||
const info = this.snapshot;
|
||
const fields: any = {};
|
||
if (info) {
|
||
["serverNow", "activityId", "status", "triggerLevel", "triggerReached", "startAt", "endAt", "claimedCount", "todayClaimed", "canClaim", "nextRewardDay"].forEach(key => fields[key] = info[key]);
|
||
fields.rewards = Array.isArray(info.rewards) ? info.rewards.map(reward => ({
|
||
day: reward.day, claimed: reward.claimed,
|
||
items: Array.isArray(reward.items) ? reward.items.map(item => ({ type: item.type, count: item.count, amount: item.amount, itemId: item.itemId })) : [],
|
||
})) : [];
|
||
}
|
||
const autoKey = info && AUTO_KEY + ":" + uid + ":" + info.activityId;
|
||
const records = cc.fx.StorageMessage.getStorage(CLAIM_KEY) || {};
|
||
const claimId = info && uid + ":" + JSON.stringify([info.activityId, info.startAt, info.nextRewardDay]);
|
||
const details = Object.keys(fields).filter(key => key !== "rewards")
|
||
.map(key => key + ": " + JSON.stringify(fields[key]));
|
||
(fields.rewards || []).forEach(reward => {
|
||
details.push("day=" + reward.day + " claimed=" + reward.claimed);
|
||
reward.items.forEach(item => details.push(" " + JSON.stringify(item)));
|
||
});
|
||
this.output.string = [message, "环境:" + env.version, "当前地址:" + env.baseUrl,
|
||
"接口:" + env.baseUrl.replace(/\/$/, "") + "/signInActivityInfo", "UID:" + uid,
|
||
"响应 code:" + this.responseCode, "已弹日期记录:" + (autoKey ? cc.fx.StorageMessage.getStorage(autoKey) : "未查询"),
|
||
"当前奖励本地防重:" + (claimId ? records[claimId] === true : "未查询"),
|
||
"接口使用字段(体力原值为秒):", details.join("\n") || "尚无有效快照"].join("\n\n");
|
||
this.scheduleOnce(() => {
|
||
if (cc.isValid(this.node, true)) this.panel.getChildByName("viewport").getComponent(cc.ScrollView).scrollToTop(0);
|
||
}, 0);
|
||
}
|
||
}
|