GameData.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, object>;
  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. this.initProps(response.data.gameUserData);
  29. this.dataFinish = true;
  30. }
  31. /**
  32. * 保存单个数据
  33. */
  34. public async saveProp(key: GameProp, value: number | string) {
  35. let data = {
  36. key: key + '',
  37. value: value + ''
  38. };
  39. await mk.http.sendData('savePlayerProp', data);
  40. this.savePropFinish = true;
  41. }
  42. /**
  43. * 保存多个数据
  44. */
  45. public async saveProps(arr: { key: GameProp, value: number | string }[]) {
  46. let needProp = {};
  47. for (let i = 0; i < arr.length; i++) {
  48. needProp[arr[i].key + ''] = arr[i].value + "";
  49. }
  50. let data = {
  51. needProp: needProp
  52. };
  53. await mk.http.sendData('saveAllPlayerProp', data);
  54. this.savePropsFinish = true;
  55. }
  56. /**
  57. * 向服务器请求所有属性后更新
  58. */
  59. public async getAllProps() {
  60. let response = await mk.http.sendData('getAllPlayerProp', {});
  61. if (response.errcode != 0) {
  62. return;
  63. }
  64. this.initProps(response.data);
  65. this.getPropsFnish = true;
  66. }
  67. private initProps(data) {
  68. if (this.props == null) {
  69. this.props = new Map<number, object>();
  70. }
  71. for (let key in data) {
  72. this.props.set(parseInt(key), JSON.parse(data[key]));
  73. }
  74. }
  75. /** 获取属性 */
  76. public getProp(key: GameProp): object {
  77. if (this.props == null) {
  78. return null;
  79. }
  80. return this.props.get(key);
  81. }
  82. }
  83. /**
  84. * 所有模块的非校验数据
  85. */
  86. export enum GameProp {
  87. /** ------------------ 游戏核心数据 --------------------------- */
  88. /** ------------------ 转盘数据 ------------------------------ */
  89. turnable_leftTimes = 20,
  90. /** ------------------ 存钱罐数据 ----------------------------- */
  91. /** ------------------ 签到数据 ----------------------------- */
  92. }