| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- 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 { Toast } from "../../core/util_class/Toast";
- export enum LvDir {
- none = 0,
- left = 1,
- right = 2,
- up = 3,
- down = 4,
- xCenter = 5,
- xOut = 6,
- yCenter = 7,
- yOut = 8,
- }
- //定义事件
- interface event_protocol {
- item_count(type: number, count: number): void;//道具数量改变
- chang_skin(type: number): void;//皮肤改变
- }
- //排行榜key
- export enum rand_type {
- floor = "floor",//最高层数
- day_lv = 'day_lv',//每日通关关数
- }
- //自定义数据
- export enum data_type {
- last_gift_sidebar = 'last_gift_sidebar',//最后一次侧边栏时间
- max_floor = 'max_floor',//最高层数
- coin = 'coin',//铜币
- life = 'life',//体力
- lv = 'lv',//当日所在关卡数
- is_win = 'is_win', // 连胜状态
- lv_dir = 'lv_dir' // 关卡方向状态
- }
- //每日数据
- export enum day_data_type {
- floor = 'floor',//每日完成
- try = 'try',//每日重试次数
- total_lv = 'total_lv',//每日通关数
- everyday_gift = 'everyday_gift',//每日获得礼包
- everyday_gift_redpoint = 'everyday_gift_redpoint',//每日获得礼包红点
- get_free_coin = 'get_free_coin',//获得免费金币
- get_free_lottery = 'get_free_lottery',//获得免费抽奖
- use_item = 'use_item',//使用道具
- watch_ad = 'watch_ad',//观看广告
- task_reward_state = 'task_reward_state',//任务奖励是否领取
- }
- //每周数据
- export enum week_data_type {
- sign_day = 'sign_day',//签到奖励
- }
- //每月数据
- export enum month_data_type {
- }
- export default class PlayerData extends GameData<data_type, day_data_type, week_data_type, month_data_type> {
- private static _instance: PlayerData;
- public event = ch.get_new_event<event_protocol>();
- //道具容器
- private items: Container = new Container();
-
- public provinceInfo: [number, string][] = []
- 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: 1 }],
- [data_type.coin, { min: 0 }],
- [data_type.life, { min: 0, max: 5 }],
- [data_type.lv, { min: 1 }]
- ]),
- new Map([
- [day_data_type.floor, { min: 0 }],
- [day_data_type.try, { min: 0 }],
- [day_data_type.everyday_gift, { min: 0, max: 3 }],
- [day_data_type.everyday_gift_redpoint, { min: 0, max: 1 }],
- [day_data_type.get_free_coin, { min: 0, max: 1 }],
- [day_data_type.get_free_lottery, { min: 0, max: 1 }],
- [day_data_type.use_item, { min: 0, max: 3 }],
- [day_data_type.watch_ad, { min: 0, max: 8 }],
- ]),
- new Map([
- [week_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 });
- this.items.addItem({ type: 5, count: 0 });
- //道具初始化
- ch.log.log("道具初始化", this);
- }
- //序列化加入自定义数据
- protected on_serialize(data: { [key: string]: number | string | any; }): void {
- data.sign = ch.sign.getSignData();
- data.items = this.items.serialize();
-
- // data.lv_dir = this.data.get(data_type.lv_dir);
- // data.is_win = this.data.get(data_type.is_win);
- }
- //反序列化加入自定义数据
- protected on_unserialize(data: { [key: string]: number | string | any; }): void {
- this.items.unserialize(data.items);
- ch.sign.init(2, data.sign, 7);
-
- // if (data.lv_dir !== undefined) {
- // this.data.set(data_type.lv_dir, data.lv_dir);
- // }
- // if (data.is_win !== undefined) {
- // this.data.set(data_type.is_win, data.is_win);
- // }
- }
- //是否使用远程数据
- 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(Main).getLayout<Layout_Main>();
- 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();
- } else if (type == 5) {
- // layout.Container.AddTime();
- }
- this.set_use_item_num(1);
- 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 = -1): 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.week_data.get(week_data_type.sign_day);
- }
- //设置签到天数
- public set_sign(count: number) {
- this.week_data.set(week_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): any {
- this.data.set(data_type.max_floor, floor);
- this.setDirty();
- }
- //获取关卡数
- public get_lv(): number {
- return this.data.get(data_type.lv);
- }
- //设置关卡数
- public set_lv(lv: number): void {
- this.data.set(data_type.lv, lv);
- this.setDirty();
- }
- //获取铜币数量
- public get_coin(): number {
- return this.data.get(data_type.coin);
- }
- //设置铜币数量
- public set_coin(coin: number): void {
- this.data.set(data_type.coin, coin);
- this.setDirty();
- }
- //获取今日使用道具数量
- public get_use_item_num(): number {
- return this.day_data.get(day_data_type.use_item);
- }
- //设置今日使用道具数量
- public set_use_item_num(num: number): void {
- this.day_data.add(day_data_type.use_item, num);
- }
- //获取今日广告观看数量
- public get_watch_ad_num(): number {
- return this.day_data.get(day_data_type.watch_ad);
- }
- //设置今日广告观看数量
- public set_watch_ad_num(num: number): void {
- this.day_data.add(day_data_type.watch_ad, num);
- this.setDirty();
- }
- //获取任务奖励状态
- public get_task_state() {
- return this.day_data.get_Array(day_data_type.task_reward_state);
- }
- //设置任务奖励状态
- public set_task_state(task_state: any): void {
- this.day_data.set_Array(day_data_type.task_reward_state, task_state);
- this.setDirty();
- }
-
- //获取体力
- public get_life(): number {
- return this.data.get(data_type.life);
- }
- //设置体力
- public set_life(life: number): void {
- if (life > 5) {
- life = 5;
- } else if (life < 0) {
- life = 0;
- }
- this.data.set(data_type.life, life);
- this.setDirty();
- }
- public async initProvinceInfo() {
- this.provinceInfo = await chsdk.getAllProvince();
- }
- public async getAllProvinceInfo() {
- return this.provinceInfo
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- protected async load_data(): Promise<{ [key: string]: any; }> {
- 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() {
- 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<boolean> {
- return super.save_data(save_data);
- }
- public async save_rank_floor() {
- 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;
- 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;
- }
- }
- if (d.data.own.rank > 100) {
- index = 101;
- }
- } else {
- index = 101;
- }
- return { list: d.data.list, owner: d.data.own, index: index };
- }
- public async save_Province_floor() {//地区
- const floor = this.data.get(data_type.max_floor);
- ch.sdk.saveProvinceRankData(rand_type.floor, floor, ch.sdk.updateType.none);
- }
- public async get_Province_Rank(hid: number): Promise<{ list: any[], ower: any, index: number }> {
- let index = 0;
- console.log("hid", hid);
- const d = await ch.sdk.loadProvinceRankData(hid, rand_type.floor, ch.sdk.updateType.none, 100, true);
- 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;
- }
- }
- if (d.data.own.rank > 100) {
- index = 101;
- }
- } else {
- index = 101;
- }
- return { list: d.data.list, ower: d.data.own, index };
- }
- async loadPfInfo(): Promise<boolean> {
- return new Promise(async (resolve) => {
- let k = await ch.sdk.getUserInfo();
- if (k) {
- resolve(true);
- } else {
- Toast.makeText(gui.getLayerNode(5), '需要授权').show();
- resolve(false);
- }
- })
- }
- }
|