MatchMaster/assets/seven_day_gift/SevenDayGiftApi.ts

99 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Utils from "../Script/module/Pay/Utils";
// 单项奖励;优先读取 count,兼容旧数据的 amount;infinite_health 数值单位为秒。
export interface SevenDayRewardItem {
type: "coin" | "hammer" | "freeze" | "magic_wand" | "infinite_health" | "cat_skin";
amount?: number;
count?: number;
itemId?: number; // 猫皮肤奖励对应的猫咪编号。
}
// 一天可以包含多项奖励,claimed 表示这一天已领取。
export interface SevenDayReward {
day: number;
items: SevenDayRewardItem[];
claimed: boolean;
}
// 服务端活动快照,也是弹窗显示和本次领取的数据来源。
export interface SevenDayActivityInfo {
serverNow: string | number;
activityId: string;
status: string;
triggerLevel: number;
triggerReached: boolean;
startAt: string | number;
endAt: string | number;
claimedCount: number;
todayClaimed: boolean;
canClaim: boolean;
nextRewardDay: number;
rewards: SevenDayReward[];
}
/** 从活动查询快照生成的本地同步记录,不是独立领取接口的响应。 */
export interface SevenDayRewardReceipt {
claimId: string;
rewardDay: number;
rewards: SevenDayRewardItem[];
claimedCount: number;
todayClaimed: boolean;
completed: boolean;
}
export interface SevenDayResponse<T> {
code: number;
data: T;
msg?: string;
}
/** 分包内的资产同步接口,不额外请求活动领取接口。 */
export default class SevenDayGiftApi {
// 把解锁的猫皮肤同步到当前账号。
public static saveCat(catNum: number, callback: (response: SevenDayResponse<{ catArr: number[] }>) => void) {
this.post("setCatArr", {
uid: this.getUid(),
action: "save",
catNum,
}, callback);
}
// 上传领取后的道具总量,不是本次奖励增量。
public static saveProps(propData: object, callback: (response: SevenDayResponse<any>) => void) {
this.post("userProp", {
uid: this.getUid(),
action: "save",
propType: 0,
propData: JSON.stringify(propData),
}, callback);
}
// 统一从当前登录信息取得奖励所属账号。
public static getUid(): string {
const storedUid = cc.fx.StorageMessage.getStorage("uid");
if (storedUid !== undefined && storedUid !== null && storedUid !== "") {
cc.fx.GameConfig.GM_INFO.uid = storedUid;
}
return cc.fx.GameConfig.GM_INFO.uid || "";
}
// 封装同步请求;超时和网络返回最多触发一次回调。
private static post<T>(endpoint: string, params: object, callback: (response: SevenDayResponse<T>) => void) {
let completed = false;
const timeoutHandle = setTimeout(() => {
if (completed) return;
completed = true;
cc.warn("[SevenDayGiftApi] 请求超时:" + endpoint);
callback({ code: 0, data: null, msg: "请求超时,请稍后重试" });
}, 5500);
Utils.POST(endpoint, params, response => {
if (completed) return;
completed = true;
clearTimeout(timeoutHandle);
callback(response);
});
}
}