UserData.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import BasicObject from "../../Engine/Basic/BasicObject";
  2. import EventManager from "../../Engine/Event/EventManager";
  3. import JsonResources from "../../Engine/Resources/JsonResources";
  4. import Tool from "../../Engine/Tool/Tool";
  5. import { _EGameBehavior, _EGameJsonRes } from "../Data/GameDataType";
  6. import { _IUserData, _IUserDataConfig } from "../Data/User/UserConfig";
  7. import GameEventName from "../Event/GameEventName";
  8. /**
  9. * 用户数据
  10. * @param 设计目的 管理玩家信息
  11. * @param 可能问题 数据复杂时,把多余部分拆分成组件
  12. */
  13. export default class UserData extends BasicObject {
  14. /**用户数据 */
  15. public userData: _IUserData;
  16. constructor () {
  17. super();
  18. this.read();
  19. this.save();
  20. EventManager.onEvent(GameEventName.UserData.UserData_SetUserData, this, this.onSetUserData);
  21. EventManager.onEvent(GameEventName.UserData.UserData_GetUserData, this, this.onGetUserData);
  22. }
  23. /**
  24. * 保存
  25. * @param 使用范例 this.saveData();
  26. * @param 设计目的 保存用户数据
  27. * @param 可能问题 频繁、或在错误地点执行此方法,保存了错误数据
  28. */
  29. public save (): void {
  30. // 更新离线时间
  31. let date: Date = new Date();
  32. this.userData.offLineTime = {
  33. year: date.getFullYear(),
  34. month: date.getMonth(),
  35. day: date.getDay(),
  36. hour: date.getHours(),
  37. minute: date.getMinutes(),
  38. second: date.getSeconds(),
  39. };
  40. // if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
  41. // window.sessionStorage.setItem("UserInfo", JSON.stringify(this.userData));
  42. // }
  43. // else {
  44. // cc.sys.localStorage.setItem("UserInfo", JSON.stringify(this.userData));
  45. // }
  46. // Tool.log("");
  47. // Tool.log("保存用户数据");
  48. // Tool.log(this.userData);
  49. EventManager.sendEvent(GameEventName.UserData.UserData_UpdateUserData, this.userData);
  50. }
  51. /**
  52. * 读取
  53. * @param 使用范例 this.readData();
  54. * @param 设计目的 读取用户数据
  55. * @param 可能问题 保存错误数据后,读取错误
  56. */
  57. private read (): void {
  58. let string = "";
  59. if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
  60. string = window.sessionStorage.getItem("UserInfo");
  61. }
  62. else {
  63. string = cc.sys.localStorage.getItem("UserInfo");
  64. }
  65. let userDataConfig: _IUserDataConfig = JsonResources.getResources(_EGameJsonRes.UserDataConfig);
  66. let userData: _IUserData = userDataConfig.userData;
  67. this.userData = Tool.dataAllCover({}, userDataConfig.userData);
  68. if (string != null && string != "") {
  69. userData = JSON.parse(string) as _IUserData;
  70. this.userData = Tool.dataSupplementCover(userData, this.userData);
  71. // Tool.log("");
  72. // Tool.log("读取用户数据");
  73. // Tool.log(this.userData);
  74. }
  75. EventManager.sendEvent(GameEventName.UserData.UserData_ReadUserData, _EGameBehavior.InitLoad);
  76. }
  77. /**设置用户数据响应方法 */
  78. private onSetUserData (data: Object): void {
  79. this.userData = Tool.dataSupplementCover(this.userData, data);
  80. this.save();
  81. }
  82. /**获得用户数据响应方法 */
  83. private onGetUserData (callback: Function): void {
  84. callback(Tool.dataAllCover({}, this.userData));
  85. }
  86. _destroy () {
  87. this.userData = null;
  88. super._destroy();
  89. }
  90. }