123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- import { _decorator, Component } from "cc";
- import { constant } from "./constant";
- import { StorageManager } from "./storageManager";
- import { util } from "./util";
- import { user } from "../../../extensions/i18n/@types/editor/i18n/languages/en";
- const { ccclass, property } = _decorator;
- @ccclass("playerData")
- export class playerData extends Component {
- /* class member could be defined like this */
- // dummy = '';
- 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 loadFromCache() {
- //读取玩家基础数据
- 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 = {
- createDate: new Date(), //记录创建时间
- gold: 100, //金币总数
- standsData: {}, //台面上的物品
- };
- this._isNewBee = true; //区分新老玩家
- if (loginData) {
- for (let key in loginData) {
- this._playerInfo[key] = loginData[key];
- }
- }
- this.savePlayerInfoToLocalCache();
- }
- /**
- * 生成随机账户
- * @returns
- */
- public generateRandomAccount(userid?:string) {
- this.userId =userid?userid: `${Date.now()}${util.getRandomInt(0, 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));
- }
- /**
- * 当数据同步完毕,即被覆盖的情况下,需要将数据写入到本地缓存,以免数据丢失
- */
- 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);
- }
- public setPlayMoney(value:any){
- this._playerInfo['gold'] = value;
- StorageManager.instance.setConfigData(constant.LOCAL_CACHE.PLAYER, JSON.stringify(this._playerInfo));
- }
- /**
- * 更新用户信息
- * 例如钻石、金币、道具
- * @param {String} key
- * @param {Number} value
- */
- public updatePlayerInfo(key: string, value: any) {
- let isChanged: boolean = false;
- if (this._playerInfo.hasOwnProperty(key)) {
- if (typeof value === 'number') {
- isChanged = true;
- this._playerInfo[key] += value;
- if (this._playerInfo[key] < 0) {
- this._playerInfo[key] = 0;
- }
- //return;
- } else if (typeof value === 'boolean' || typeof value === 'string') {
- isChanged = true;
- this._playerInfo[key] = value;
- }
- }
- if (isChanged) {
- //有修改就保存到localcache
- 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();
- }
- }
|