PlayerData.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { ch } from "../../ch/ch";
  2. import { gui } from "../../core/ui/ui";
  3. import { Container } from "../../core/util_class/Container";
  4. import GameData from "../../core/util_class/GameData";
  5. import { UINotify } from "../../module_basic/ui_notify/UINotify";
  6. import { Start } from "../../start/Start";
  7. import { Layout_Main } from "../ui/UI_Main/Layout_Main";
  8. import { UI_Main } from "../ui/UI_Main/UI_Main";
  9. //定义事件
  10. interface event_protocol {
  11. item_count(type: number, count: number): void;//道具数量改变
  12. }
  13. //排行榜key
  14. export enum rand_type {
  15. floor = "floor",//最高层数
  16. }
  17. //自定义数据
  18. export enum data_type {
  19. last_gift_sidebar = 'last_gift_sidebar',//最后一次侧边栏时间
  20. max_floor = 'max_floor',//最高层数
  21. coin = 'coin',//铜币
  22. }
  23. //每日数据
  24. export enum day_data_type {
  25. sign_day = 'sign_day',//签到奖励
  26. isFavorite = 'isFavorite',//是否收藏
  27. }
  28. //每周数据
  29. export enum week_data_type {
  30. }
  31. //每月数据
  32. export enum month_data_type {
  33. }
  34. export default class PlayerData extends GameData<data_type, day_data_type, week_data_type, month_data_type> {
  35. private static _instance: PlayerData;
  36. public event = ch.get_new_event<event_protocol>();
  37. //道具容器
  38. private items: Container = new Container();
  39. public static getInstance(gid: string, uid: string): PlayerData {
  40. if (!this._instance) {
  41. this._instance = new PlayerData(gid, uid, "PlayerData", new Map([
  42. [data_type.max_floor, { min: 0 }],
  43. [data_type.coin, { min: 0 }]
  44. ]),
  45. new Map([
  46. [day_data_type.sign_day, { min: 0, max: 7 }],
  47. ]));
  48. }
  49. return this._instance;
  50. }
  51. //数据初始化
  52. protected on_init(): void {
  53. ch.sign.init(2, null, 7);
  54. this.items.addItem({ type: 1, count: 1 });
  55. this.items.addItem({ type: 2, count: 1 });
  56. this.items.addItem({ type: 3, count: 1 });
  57. //道具初始化
  58. ch.log.log("道具初始化", this);
  59. }
  60. //序列化加入自定义数据
  61. protected on_serialize(data: { [key: string]: number | string | any; }): void {
  62. data.sign = ch.sign.getSignData();
  63. data.items = this.items.serialize();
  64. }
  65. //反序列化加入自定义数据
  66. protected on_unserialize(data: { [key: string]: number | string | any; }): void {
  67. this.items.unserialize(data.items);
  68. ch.sign.init(2, data.sign, 7);
  69. }
  70. //是否使用远程数据
  71. protected on_check(local: any, remote: any): boolean {
  72. return true;
  73. }
  74. /**使用道具*/
  75. async use_item(type: number, count: number = 1) {
  76. // if (GameLink.getInst().state != GameState.wait) return;
  77. const layout=gui.get(UI_Main).getLayout<Layout_Main>();
  78. let ret = this.items.useCount(type, count);
  79. if (ret) {
  80. let b = true;
  81. if (type == 1) {
  82. layout.Container.eliminate();
  83. } else if (type == 2) {
  84. layout.Container.shuffle();
  85. } else if (type == 3) {
  86. layout.Container.Empty();
  87. }
  88. if (!b) {
  89. this.items.addCount(type, count);
  90. } else {
  91. this.event.emit(this.event.key.item_count, ret.type, ret.count);
  92. this.setDirty();
  93. }
  94. }
  95. }
  96. /**增加道具*/
  97. add_item(type: number, count: number = 1): void {
  98. let ret = this.items.addCount(type, count);
  99. if (ret) {
  100. this.event.emit(this.event.key.item_count, ret.type, ret.count);
  101. this.setDirty();
  102. }
  103. }
  104. set_item(type: number, count: number = 4): void {
  105. const c = this.get_item(type);
  106. if (type < count) {
  107. this.add_item(type, count - c);
  108. }
  109. }
  110. get_item(type: number): number {
  111. return this.items.getCount(type);
  112. }
  113. //获取签到天数
  114. public get_sign(): number {
  115. return this.day_data.get(day_data_type.sign_day);
  116. }
  117. //设置签到天数
  118. public set_sign(count: number) {
  119. this.day_data.set(day_data_type.sign_day, count);
  120. }
  121. //获取是否当日首次通过收藏进入
  122. public get_is_favorite(): number {
  123. return this.day_data.get(day_data_type.isFavorite);
  124. }
  125. //设置已从收藏进入
  126. public set_is_favorite(): void {
  127. this.day_data.set(day_data_type.isFavorite, 1);
  128. this.setDirty();
  129. }
  130. //获取关卡数
  131. public get_max_floor(): number {
  132. return this.data.get(data_type.max_floor);
  133. }
  134. //设置关卡数
  135. public set_max_floor(floor: number): void {
  136. this.data.set(data_type.max_floor, floor);
  137. }
  138. //获取金币数量
  139. public get_coin(): number {
  140. return this.data.get(data_type.coin);
  141. }
  142. //设置金币数量
  143. public set_coin(coin: number): void {
  144. this.data.set(data_type.coin, coin);
  145. }
  146. //////////////////////////////////////////////////////////////////////////////////////////
  147. protected async load_data(): Promise<{ [key: string]: any; }> {
  148. if (Start.packId == 1) {
  149. return new Promise((resolve, reject) => {
  150. });
  151. }
  152. else {
  153. return super.load_data();
  154. }
  155. }
  156. public user_info: { uid: string, nickName: string, avatar: string, province: string };
  157. public get nickName(): string {
  158. return this.user_info.nickName;
  159. }
  160. public get avatarUrl(): string {
  161. return this.user_info.avatar;
  162. }
  163. public async init_user_info() {
  164. if (Start.packId == 1) {
  165. } else {
  166. this.user_info = {
  167. uid: this.uid,
  168. nickName: ch.sdk.get_player_info().nickName,
  169. avatar: ch.sdk.get_player_info().avatarUrl,
  170. province: ch.sdk.get_player_info().province
  171. }
  172. }
  173. }
  174. protected async save_data(save_data: { [key: string]: any; }): Promise<boolean> {
  175. if (Start.packId == 1) {
  176. return true;
  177. }
  178. else {
  179. return super.save_data(save_data);
  180. }
  181. }
  182. public async save_rank_floor() {
  183. if (Start.packId == 1) {
  184. } else {
  185. const floor = this.data.get(data_type.max_floor);
  186. ch.sdk.saveRankData(rand_type.floor, floor, ch.sdk.updateType.none, 0, { province: this.user_info.province });
  187. }
  188. }
  189. public async get_rank_floor(): Promise<{ list: any[], owner: any, index: number }> {
  190. let index = 0;
  191. if (Start.packId == 1) {
  192. } else {
  193. const d = await ch.sdk.loadRankData(rand_type.floor, ch.sdk.updateType.none, 100, true, false);
  194. console.log("排行榜", d);
  195. if (d.data.own) {
  196. for (let i = 0; i < d.data.list.length; i++) {
  197. if (d.data.own.userId === d.data.list[i].userId) {
  198. index = i + 1;
  199. }
  200. }
  201. } else {
  202. index = 101;
  203. }
  204. return { list: d.data.list, owner: d.data.own, index: index };
  205. }
  206. }
  207. async loadPfInfo(): Promise<boolean> {
  208. return new Promise(async (resolve) => {
  209. let k = await ch.sdk.getUserInfo();
  210. if (k) {
  211. if (Start.packId == 1) {
  212. }
  213. resolve(true);
  214. } else {
  215. UINotify.show("需要授权");
  216. resolve(false);
  217. }
  218. })
  219. }
  220. }