HuaweiDemo/assets/script/framework/playerData.ts

248 lines
5.9 KiB
TypeScript
Raw Permalink Normal View History

2023-11-07 01:17:57 +00:00
import { _decorator, Component, log } from "cc";
import { Constant } from "./constant";
import { StorageManager } from "./storageManager";
import { Util } from "./util";
const { ccclass, property } = _decorator;
@ccclass("PlayerData")
export class PlayerData extends Component {
static _instance: PlayerData;
public serverTime: number = 0;
public localTime: number = 0;
public static get instance () {
if (this._instance) {
return this._instance;
}
this._instance = new PlayerData();
return this._instance;
}
private _userId: string = '';
private _playerInfo: any = null;
private _history: any = null;
private _settings: any = null;
private _isNewBee: boolean = false; //默认非新手
private _dataVersion: string = '';
public get userId () {
return this._userId;
}
public set userId (v: string) {
this._userId = v;
}
public get settings () {
return this._settings;
}
public set settings (v: any) {
this._settings = v;
}
public get playerInfo () {
return this._playerInfo;
}
public get history () {
return this._history;
}
public get isNewBee () {
return this._isNewBee;
}
public set isNewBee (v: boolean) {
this._isNewBee = v;
}
/**
*
*/
public loadGlobalCache() {
let userId: string = StorageManager.instance.getUserId();
if (userId) {
this._userId = userId;
}
}
public isInit:boolean = false;
/**
*
*/
public loadFromCache() {
this.loadGlobalCache();
if (!this.userId) {
this.generateRandomAccount();
}
//读取玩家基础数据
this._playerInfo = this._loadDataByKey(Constant.LOCAL_CACHE.PLAYER);
this._history = this._loadDataByKey(Constant.LOCAL_CACHE.HISTORY);
this._settings = this._loadDataByKey(Constant.LOCAL_CACHE.SETTINGS);
}
/**
*
* @param {string}keyName
* @returns
*/
private _loadDataByKey (keyName: any) {
let ret = {};
let str = StorageManager.instance.getConfigData(keyName);
if (str) {
try {
ret = JSON.parse(str);
} catch (e) {
ret = {};
}
}
return ret;
}
/**
*
* @param loginData
*/
public createPlayerInfo(loginData?:any) {
this._playerInfo = {};
if (loginData) {
for (let key in loginData) {
this._playerInfo[key] = loginData[key];
}
}
this.savePlayerInfoToLocalCache();
}
/**
*
* @returns
*/
public generateRandomAccount () {
this.userId = new Date().getTime() + "_" + Math.floor(Math.random() * 1000);
StorageManager.instance.setUserId(this._userId);
}
/**
*
* @param userId
*/
public saveAccount(userId: any) {
this._userId = userId;
StorageManager.instance.setUserId(userId);
}
/**
*
*/
public savePlayerInfoToLocalCache() {
StorageManager.instance.setConfigData(Constant.LOCAL_CACHE.PLAYER, JSON.stringify(this._playerInfo));
}
/**
*
*/
public saveSettingsToLocalCache () {
StorageManager.instance.setConfigData(Constant.LOCAL_CACHE.SETTINGS, JSON.stringify(this._settings));
StorageManager.instance.save();
}
/**
*
*/
public saveAll() {
StorageManager.instance.setConfigDataWithoutSave(Constant.LOCAL_CACHE.PLAYER, JSON.stringify(this._playerInfo));
StorageManager.instance.setConfigDataWithoutSave(Constant.LOCAL_CACHE.HISTORY, JSON.stringify(this._history));
StorageManager.instance.setConfigDataWithoutSave(Constant.LOCAL_CACHE.SETTINGS, JSON.stringify(this._settings));
StorageManager.instance.setConfigData(Constant.LOCAL_CACHE.DATA_VERSION, this._dataVersion);
}
/**
*
*
* @param {String} key
* @param {Number} value
*/
public updatePlayerInfo(key:string, value: any) {
this._playerInfo[key] = value;
StorageManager.instance.setConfigData(Constant.LOCAL_CACHE.PLAYER, JSON.stringify(this._playerInfo));
}
/**
*
* @param {string} key
*/
public getSetting (key: string) {
if (!this._settings) {
return null;
}
if (!this._settings.hasOwnProperty(key)) {
return null;
}
return this._settings[key];
}
/**
*
* @param {string} key
* @param {*} value
*/
public setSetting (key: string, value: any) {
if (!this._settings) {
this._settings = {};
}
this._settings[key] = value;
this.saveSettingsToLocalCache();
}
/**
*
*/
public clear () {
this._playerInfo = {};
this._settings = {};
this.saveAll();
}
// 触摸参数
private _easyTouchInfo: object = {};
/**
*
*
* @param {String} key
* @param {Number} value
*/
public updateEasyTouchInfo (key: string, value: number): void {
this._easyTouchInfo[key] = value;
}
/**
*
*
* @param {String} key
* @param {Number} value
*/
public getEasyTouchInfo (key: string) {
if (!this._easyTouchInfo.hasOwnProperty(key)) {
this._easyTouchInfo[key] = 0;
}
return this._easyTouchInfo[key];
}
}