PlayerData.ts 7.4 KB

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