import { ch } from "../../ch/ch"; import { gui } from "../../core/ui/ui"; import { Container } from "../../core/util_class/Container"; import GameData from "../../core/util_class/GameData"; import { UINotify } from "../../module_basic/ui_notify/UINotify"; import { Hall } from "../../Scripts/Hall"; import { Start } from "../../start/Start"; import { Layout_Main } from "../ui/UI_Main/Layout_Main"; import { UI_Main } from "../ui/UI_Main/UI_Main"; //定义事件 interface event_protocol { item_count(type: number, count: number): void;//道具数量改变 } //排行榜key export enum rand_type { floor = "floor",//最高层数 } //自定义数据 export enum data_type { last_gift_sidebar = 'last_gift_sidebar',//最后一次侧边栏时间 max_floor = 'max_floor',//最高层数 coin = 'coin',//铜币 } //每日数据 export enum day_data_type { sign_day = 'sign_day',//签到奖励 isFavorite = 'isFavorite',//是否收藏 } //每周数据 export enum week_data_type { } //每月数据 export enum month_data_type { } export default class PlayerData extends GameData { private static _instance: PlayerData; public event = ch.get_new_event(); //道具容器 private items: Container = new Container(); public static getInstance(gid: string, uid: string): PlayerData { if (!this._instance) { this._instance = new PlayerData(gid, uid, "PlayerData", new Map([ [data_type.max_floor, { min: 0 }], [data_type.coin, { min: 0 }] ]), new Map([ [day_data_type.sign_day, { min: 0, max: 7 }], ])); } return this._instance; } //数据初始化 protected on_init(): void { ch.sign.init(2, null, 7); this.items.addItem({ type: 1, count: 1 }); this.items.addItem({ type: 2, count: 1 }); this.items.addItem({ type: 3, count: 1 }); //道具初始化 ch.log.log("道具初始化", this); } //序列化加入自定义数据 protected on_serialize(data: { [key: string]: number | string | any; }): void { data.sign = ch.sign.getSignData(); data.items = this.items.serialize(); } //反序列化加入自定义数据 protected on_unserialize(data: { [key: string]: number | string | any; }): void { this.items.unserialize(data.items); ch.sign.init(2, data.sign, 7); } //是否使用远程数据 protected on_check(local: any, remote: any): boolean { return true; } /**使用道具*/ async use_item(type: number, count: number = 1) { // if (GameLink.getInst().state != GameState.wait) return; const layout=gui.get(UI_Main).getLayout(); let ret = this.items.useCount(type, count); if (ret) { let b = true; if (type == 1) { layout.Container.eliminate(); } else if (type == 2) { layout.Container.shuffle(); } else if (type == 3) { layout.Container.Empty(); } if (!b) { this.items.addCount(type, count); } else { this.event.emit(this.event.key.item_count, ret.type, ret.count); this.setDirty(); } } } /**增加道具*/ add_item(type: number, count: number = 1): void { let ret = this.items.addCount(type, count); if (ret) { this.event.emit(this.event.key.item_count, ret.type, ret.count); this.setDirty(); } } set_item(type: number, count: number = 4): void { const c = this.get_item(type); if (type < count) { this.add_item(type, count - c); } } get_item(type: number): number { return this.items.getCount(type); } //获取签到天数 public get_sign(): number { return this.day_data.get(day_data_type.sign_day); } //设置签到天数 public set_sign(count: number) { this.day_data.set(day_data_type.sign_day, count); } //获取是否当日首次通过收藏进入 public get_is_favorite(): number { return this.day_data.get(day_data_type.isFavorite); } //设置已从收藏进入 public set_is_favorite(): void { this.day_data.set(day_data_type.isFavorite, 1); this.setDirty(); } //获取关卡数 public get_max_floor(): number { return this.data.get(data_type.max_floor); } //设置关卡数 public set_max_floor(floor: number): void { this.data.set(data_type.max_floor, floor); } //获取金币数量 public get_coin(): number { return this.data.get(data_type.coin); } //设置金币数量 public set_coin(coin: number): void { this.data.set(data_type.coin, coin); } ////////////////////////////////////////////////////////////////////////////////////////// protected async load_data(): Promise<{ [key: string]: any; }> { if (Start.packId == 1) { return new Promise((resolve, reject) => { }); } else { return super.load_data(); } } public user_info: { uid: string, nickName: string, avatar: string, province: string }; public get nickName(): string { return this.user_info.nickName; } public get avatarUrl(): string { return this.user_info.avatar; } public async init_user_info() { if (Start.packId == 1) { } else { this.user_info = { uid: this.uid, nickName: ch.sdk.get_player_info().nickName, avatar: ch.sdk.get_player_info().avatarUrl, province: ch.sdk.get_player_info().province } } } protected async save_data(save_data: { [key: string]: any; }): Promise { if (Start.packId == 1) { return true; } else { return super.save_data(save_data); } } public async save_rank_floor() { if (Start.packId == 1) { } else { const floor = this.data.get(data_type.max_floor); ch.sdk.saveRankData(rand_type.floor, floor, ch.sdk.updateType.none, 0, { province: this.user_info.province }); } } public async get_rank_floor(): Promise<{ list: any[], owner: any, index: number }> { let index = 0; if (Start.packId == 1) { } else { const d = await ch.sdk.loadRankData(rand_type.floor, ch.sdk.updateType.none, 100, true, false); console.log("排行榜", d); if (d.data.own) { for (let i = 0; i < d.data.list.length; i++) { if (d.data.own.userId === d.data.list[i].userId) { index = i + 1; } } } else { index = 101; } return { list: d.data.list, owner: d.data.own, index: index }; } } async loadPfInfo(): Promise { return new Promise(async (resolve) => { let k = await ch.sdk.getUserInfo(); if (k) { if (Start.packId == 1) { } resolve(true); } else { UINotify.show("需要授权"); resolve(false); } }) } }