GameData.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @description 游戏核心玩法数据
  3. * @author 邹勇
  4. */
  5. export class GameData {
  6. public dataFinish: boolean = false;
  7. public savePropFinish: boolean = false;
  8. public savePropsFinish: boolean = false;
  9. public getPropsFnish: boolean = false;
  10. public configs: any = {};
  11. public props: Map<number, number>;
  12. /**
  13. * 初始化游戏数据:网络配置信息,用户信息
  14. * @returns
  15. */
  16. public async init() {
  17. let data: any = { "versionCode": g.appData.version };
  18. let response = await mk.http.sendData('getAllConfigInfo', data);
  19. if (response.errcode != 0) {
  20. return;
  21. }
  22. this.configs = response.data;
  23. data = {};
  24. response = await mk.http.sendData('getInfoCrypt', data);
  25. if (response.errcode != 0) {
  26. return;
  27. }
  28. if (this.props == null) {
  29. this.props = new Map<number, number>();
  30. }
  31. this.initProps(response.data.gameUserData);
  32. this.dataFinish = true;
  33. }
  34. /**
  35. * 保存单个数据
  36. */
  37. public async setProp(key: GameProp, value: number) {
  38. let data = {
  39. key: key + '',
  40. value: value + ''
  41. };
  42. await mk.http.sendData('savePlayerProp', data);
  43. this.props.set(key, value);
  44. this.savePropFinish = true;
  45. }
  46. /**
  47. * 保存多个数据
  48. */
  49. public async setProps(arr: { key: GameProp, value: number }[]) {
  50. let needProp = {};
  51. for (let i = 0; i < arr.length; i++) {
  52. needProp[arr[i].key + ''] = arr[i].value + "";
  53. }
  54. let data = {
  55. needProp: needProp
  56. };
  57. await mk.http.sendData('saveAllPlayerProp', data);
  58. for (let i = 0; i < arr.length; i++) {
  59. this.props.set(arr[i].key, arr[i].value);
  60. }
  61. this.savePropsFinish = true;
  62. }
  63. /** 获取属性 */
  64. public getProp(key: GameProp): number {
  65. if (this.props == null) {
  66. return null;
  67. }
  68. return this.props.get(key);
  69. }
  70. /**
  71. * 向服务器请求所有属性后更新
  72. */
  73. public async getAllProps() {
  74. let response = await mk.http.sendData('getAllPlayerProp', {});
  75. if (response.errcode != 0) {
  76. return;
  77. }
  78. this.initProps(response.data);
  79. this.getPropsFnish = true;
  80. }
  81. private initProps(data) {
  82. for (let key in data) {
  83. this.props.set(parseInt(key), JSON.parse(data[key]));
  84. }
  85. }
  86. /**
  87. * 第二天需要重置的数据
  88. */
  89. public resetProps() {
  90. let arr = [];
  91. arr.push({ key: GameProp.sign_can, value: 1 });
  92. this.setProps(arr);
  93. }
  94. }
  95. /**
  96. * 所有模块的非校验数据
  97. */
  98. export enum GameProp {
  99. /** ------------------ 游戏核心数据 --------------------------- */
  100. /** ------------------ 转盘数据 ------------------------------ */
  101. turnable_leftTimes = 20,
  102. /** ------------------ 存钱罐数据 ----------------------------- */
  103. /** ------------------ 签到数据 ----------------------------- */
  104. /** 上次签到进度 */
  105. sign_last_bar = 120,
  106. /** 当前能否签到 */
  107. sign_can = 121,
  108. }