playerData.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import { _decorator, Component } from "cc";
  2. import { constant } from "./constant";
  3. import { StorageManager } from "./storageManager";
  4. import { util } from "./util";
  5. import { user } from "../../../extensions/i18n/@types/editor/i18n/languages/en";
  6. const { ccclass, property } = _decorator;
  7. @ccclass("playerData")
  8. export class playerData extends Component {
  9. /* class member could be defined like this */
  10. // dummy = '';
  11. static _instance: playerData;
  12. public serverTime: number = 0;
  13. public localTime: number = 0;
  14. public static get instance() {
  15. if (this._instance) {
  16. return this._instance;
  17. }
  18. this._instance = new playerData();
  19. return this._instance;
  20. }
  21. private _userId: string = '';
  22. private _playerInfo: any = null;
  23. private _history: any = null;
  24. private _settings: any = null;
  25. private _isNewBee: boolean = false; //默认非新手
  26. private _dataVersion: string = '';
  27. public get userId() {
  28. return this._userId;
  29. }
  30. public set userId(v: string) {
  31. this._userId = v;
  32. }
  33. public get settings() {
  34. return this._settings;
  35. }
  36. public set settings(v: any) {
  37. this._settings = v;
  38. }
  39. public get playerInfo() {
  40. return this._playerInfo;
  41. }
  42. public get history() {
  43. return this._history;
  44. }
  45. public get isNewBee() {
  46. return this._isNewBee;
  47. }
  48. public set isNewBee(v: boolean) {
  49. this._isNewBee = v;
  50. }
  51. /**
  52. * 加上用户数据
  53. */
  54. public loadGlobalCache() {
  55. let userId: string = StorageManager.instance.getUserId();
  56. if (userId) {
  57. this._userId = userId;
  58. }
  59. }
  60. /**
  61. * 加载本地存储数据
  62. */
  63. public loadFromCache() {
  64. //读取玩家基础数据
  65. this._playerInfo = this._loadDataByKey(constant.LOCAL_CACHE.PLAYER);
  66. this._history = this._loadDataByKey(constant.LOCAL_CACHE.HISTORY);
  67. this._settings = this._loadDataByKey(constant.LOCAL_CACHE.SETTINGS);
  68. }
  69. /**
  70. * 获取本地存储数据
  71. * @param {string}keyName
  72. * @returns
  73. */
  74. private _loadDataByKey(keyName: any) {
  75. let ret = {};
  76. let str = StorageManager.instance.getConfigData(keyName);
  77. if (str) {
  78. try {
  79. ret = JSON.parse(str);
  80. } catch (e) {
  81. ret = {};
  82. }
  83. }
  84. return ret;
  85. }
  86. /**
  87. * 创建角色数据
  88. * @param loginData
  89. */
  90. public createPlayerInfo(loginData?: any) {
  91. this._playerInfo = {
  92. createDate: new Date(), //记录创建时间
  93. gold: 100, //金币总数
  94. standsData: {}, //台面上的物品
  95. };
  96. this._isNewBee = true; //区分新老玩家
  97. if (loginData) {
  98. for (let key in loginData) {
  99. this._playerInfo[key] = loginData[key];
  100. }
  101. }
  102. this.savePlayerInfoToLocalCache();
  103. }
  104. /**
  105. * 生成随机账户
  106. * @returns
  107. */
  108. public generateRandomAccount(userid?:string) {
  109. this.userId =userid?userid: `${Date.now()}${util.getRandomInt(0, 1000)}`;
  110. StorageManager.instance.setUserId(this._userId);
  111. }
  112. /**
  113. * 存用户数据
  114. * @param userId
  115. */
  116. public saveAccount(userId: any) {
  117. this._userId = userId;
  118. StorageManager.instance.setUserId(userId);
  119. }
  120. /**
  121. * 保存玩家数据
  122. */
  123. public savePlayerInfoToLocalCache() {
  124. StorageManager.instance.setConfigData(constant.LOCAL_CACHE.PLAYER, JSON.stringify(this._playerInfo));
  125. }
  126. /**
  127. * 保存玩家设置相关信息
  128. */
  129. public saveSettingsToLocalCache() {
  130. StorageManager.instance.setConfigData(constant.LOCAL_CACHE.SETTINGS, JSON.stringify(this._settings));
  131. }
  132. /**
  133. * 当数据同步完毕,即被覆盖的情况下,需要将数据写入到本地缓存,以免数据丢失
  134. */
  135. public saveAll() {
  136. StorageManager.instance.setConfigDataWithoutSave(constant.LOCAL_CACHE.PLAYER, JSON.stringify(this._playerInfo));
  137. StorageManager.instance.setConfigDataWithoutSave(constant.LOCAL_CACHE.HISTORY, JSON.stringify(this._history));
  138. StorageManager.instance.setConfigDataWithoutSave(constant.LOCAL_CACHE.SETTINGS, JSON.stringify(this._settings));
  139. StorageManager.instance.setConfigData(constant.LOCAL_CACHE.DATA_VERSION, this._dataVersion);
  140. }
  141. public setPlayMoney(value:any){
  142. this._playerInfo['gold'] = value;
  143. StorageManager.instance.setConfigData(constant.LOCAL_CACHE.PLAYER, JSON.stringify(this._playerInfo));
  144. }
  145. /**
  146. * 更新用户信息
  147. * 例如钻石、金币、道具
  148. * @param {String} key
  149. * @param {Number} value
  150. */
  151. public updatePlayerInfo(key: string, value: any) {
  152. let isChanged: boolean = false;
  153. if (this._playerInfo.hasOwnProperty(key)) {
  154. if (typeof value === 'number') {
  155. isChanged = true;
  156. this._playerInfo[key] += value;
  157. if (this._playerInfo[key] < 0) {
  158. this._playerInfo[key] = 0;
  159. }
  160. //return;
  161. } else if (typeof value === 'boolean' || typeof value === 'string') {
  162. isChanged = true;
  163. this._playerInfo[key] = value;
  164. }
  165. }
  166. if (isChanged) {
  167. //有修改就保存到localcache
  168. StorageManager.instance.setConfigData(constant.LOCAL_CACHE.PLAYER, JSON.stringify(this._playerInfo));
  169. }
  170. }
  171. /**
  172. * 获取玩家杂项值
  173. * @param {string} key
  174. */
  175. public getSetting(key: string) {
  176. if (!this._settings) {
  177. return null;
  178. }
  179. if (!this._settings.hasOwnProperty(key)) {
  180. return null;
  181. }
  182. return this._settings[key];
  183. }
  184. /**
  185. * 设置玩家杂项值
  186. * @param {string} key
  187. * @param {*} value
  188. */
  189. public setSetting(key: string, value: any) {
  190. if (!this._settings) {
  191. this._settings = {};
  192. }
  193. this._settings[key] = value;
  194. this.saveSettingsToLocalCache();
  195. }
  196. /**
  197. * 清除用户信息
  198. */
  199. public clear() {
  200. this._playerInfo = {};
  201. this._settings = {};
  202. this.saveAll();
  203. }
  204. }