import { ch } from "../../ch/ch"; import GameData from "../../core/util_class/GameData"; import { UINotify } from "../../module_basic/ui_notify/UINotify"; import { Start } from "../../start/Start"; //定义事件 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_gift = 'sign_gift',//签到奖励 isFavorite = 'isFavorite',//是否收藏 } //每周数据 export enum week_data_type { } //每月数据 export enum month_data_type { } export default class PlayerData extends GameData { private static _instance: PlayerData; public event: event_protocol; private items;//道具容器 public static getInstance(gid: string, uid: string): PlayerData { if (!this._instance) { this._instance = new PlayerData(gid, uid, "PlayerData", new Map([ [data_type.coin, { min: 0, max: 1000000 }] ]), new Map([ [day_data_type.sign_gift, { min: 0, max: 1 }], ])); } return this._instance; } //数据初始化 protected on_init(): void { ch.sign.init(2,null,7); //道具初始化 ch.log.log("道具初始化",this); } //序列化加入自定义数据 protected on_serialize(data: { [key: string]: number | string | any; }): void { data.sign = ch.sign.getSignData(); } //反序列化加入自定义数据 protected on_unserialize(data: { [key: string]: number | string | any; }): void { ch.sign.init(2,data.sign,7); } //是否使用远程数据 protected on_check(local: any, remote: any): boolean { return true; } //使用道具 async useItem(type: number, count: number){ } //增加道具 add_item(type: number, count: number):void { } //设置道具数量 set_item(type: number, count: number):void { } //获取道具数量 get_item(type: number):number { return 0; } //获取签到天数 public get_sign():number{ return this.day_data.get(day_data_type.sign_gift); } //设置签到天数 public set_sign(count:number){ this.day_data.set(day_data_type.sign_gift,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(); this.save(); } //获取金币数量 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 save_rank_floor(){ if(Start.packId == 1) { }else{ const floor=this.data.get(data_type.max_floor)-1; 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); 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); } }) } }