PlayerData.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. isFavorite = 'isFavorite',//是否收藏
  26. pass_level = 'pass_level',//通关
  27. combine_num = 'combine_num',//匹配成语
  28. login_num = 'login_num',//登录奖励
  29. use_item = 'use_item',//使用道具
  30. watch_ad = 'watch_ad',//观看广告
  31. task_reward_state = 'task_reward_state',//任务奖励是否领取
  32. }
  33. //每周数据
  34. export enum week_data_type {
  35. sign_day = 'sign_day',//签到奖励
  36. }
  37. //每月数据
  38. export enum month_data_type {
  39. }
  40. export default class PlayerData extends GameData<data_type, day_data_type, week_data_type, month_data_type> {
  41. private static _instance: PlayerData;
  42. public event = ch.get_new_event<event_protocol>();
  43. //道具容器
  44. private items: Container = new Container();
  45. public static getInstance(gid: string, uid: string): PlayerData {
  46. if (!this._instance) {
  47. this._instance = new PlayerData(gid, uid, "PlayerData",
  48. new Map([
  49. [data_type.max_floor, { min: 0 }],
  50. [data_type.coin, { min: 0 }]
  51. ]),
  52. new Map([
  53. [day_data_type.isFavorite, { min: 0 }],
  54. [day_data_type.pass_level, { min: 0, max: 5 }],
  55. [day_data_type.combine_num, { min: 0, max: 100 }],
  56. [day_data_type.login_num, { min: 0, max: 1 }],
  57. [day_data_type.use_item, { min: 0, max: 3 }],
  58. [day_data_type.watch_ad, { min: 0, max: 8 }],
  59. ]),
  60. new Map([
  61. [week_data_type.sign_day, { min: 0, max: 7 }],
  62. ]));
  63. }
  64. return this._instance;
  65. }
  66. //数据初始化
  67. protected on_init(): void {
  68. ch.sign.init(2, null, 7);
  69. this.items.addItem({ type: 1, count: 1 });
  70. this.items.addItem({ type: 2, count: 1 });
  71. this.items.addItem({ type: 3, count: 1 });
  72. this.items.addItem({ type: 5, count: 1 });
  73. //道具初始化
  74. ch.log.log("道具初始化", this);
  75. }
  76. //序列化加入自定义数据
  77. protected on_serialize(data: { [key: string]: number | string | any; }): void {
  78. data.sign = ch.sign.getSignData();
  79. data.items = this.items.serialize();
  80. }
  81. //反序列化加入自定义数据
  82. protected on_unserialize(data: { [key: string]: number | string | any; }): void {
  83. this.items.unserialize(data.items);
  84. ch.sign.init(2, data.sign, 7);
  85. }
  86. //是否使用远程数据
  87. protected on_check(local: any, remote: any): boolean {
  88. return true;
  89. }
  90. /**使用道具*/
  91. async use_item(type: number, count: number = 1) {
  92. // if (GameLink.getInst().state != GameState.wait) return;
  93. const layout = gui.get(UI_Main).getLayout<Layout_Main>();
  94. let ret = this.items.useCount(type, count);
  95. if (ret) {
  96. let b = true;
  97. if (type == 1) {
  98. layout.Container.eliminate();
  99. } else if (type == 2) {
  100. layout.Container.shuffle();
  101. } else if (type == 3) {
  102. layout.Container.Empty();
  103. }
  104. if (!b) {
  105. this.items.addCount(type, count);
  106. } else {
  107. this.set_use_item_num(1);
  108. this.event.emit(this.event.key.item_count, ret.type, ret.count);
  109. this.setDirty();
  110. }
  111. }
  112. }
  113. /**增加道具*/
  114. add_item(type: number, count: number = 1): void {
  115. let ret = this.items.addCount(type, count);
  116. if (ret) {
  117. this.event.emit(this.event.key.item_count, ret.type, ret.count);
  118. this.setDirty();
  119. }
  120. }
  121. set_item(type: number, count: number = 4): void {
  122. const c = this.get_item(type);
  123. if (type < count) {
  124. this.add_item(type, count - c);
  125. }
  126. }
  127. get_item(type: number): number {
  128. return this.items.getCount(type);
  129. }
  130. //获取签到天数
  131. public get_sign(): number {
  132. return this.week_data.get(week_data_type.sign_day);
  133. }
  134. //设置签到天数
  135. public set_sign(count: number) {
  136. this.week_data.set(week_data_type.sign_day, count);
  137. }
  138. //获取是否当日首次通过收藏进入
  139. public get_is_favorite(): number {
  140. return this.day_data.get(day_data_type.isFavorite);
  141. }
  142. //设置已从收藏进入
  143. public set_is_favorite(): void {
  144. this.day_data.set(day_data_type.isFavorite, 1);
  145. this.setDirty();
  146. }
  147. //获取关卡数
  148. public get_max_floor(): number {
  149. return this.data.get(data_type.max_floor);
  150. }
  151. //设置关卡数
  152. public set_max_floor(floor: number): void {
  153. this.data.set(data_type.max_floor, floor);
  154. }
  155. //获取金币数量
  156. public get_coin(): number {
  157. return this.data.get(data_type.coin);
  158. }
  159. //设置金币数量
  160. public set_coin(coin: number): void {
  161. this.data.set(data_type.coin, coin);
  162. this.setDirty();
  163. }
  164. //获取今日通关数
  165. public get_pass_level(): number {
  166. return this.day_data.get(day_data_type.pass_level);
  167. }
  168. //设置今日通关数
  169. public set_pass_level(level: number): void {
  170. this.day_data.add(day_data_type.pass_level, level);
  171. }
  172. //获取匹配成语数量
  173. public get_combine_num(): number {
  174. return this.day_data.get(day_data_type.combine_num);
  175. }
  176. //设置匹配成语数量
  177. public set_combine_num(num: number): void {
  178. this.day_data.add(day_data_type.combine_num, num);
  179. this.setDirty();
  180. }
  181. //获取登录次数
  182. public get_login_num(): number {
  183. return this.day_data.get(day_data_type.login_num);
  184. }
  185. //设置登录次数
  186. public set_login_num(num: number): void {
  187. this.day_data.add(day_data_type.login_num, num);
  188. this.setDirty();
  189. }
  190. //获取今日使用道具数量
  191. public get_use_item_num(): number {
  192. return this.day_data.get(day_data_type.use_item);
  193. }
  194. //设置今日使用道具数量
  195. public set_use_item_num(num: number): void {
  196. this.day_data.add(day_data_type.use_item, num);
  197. }
  198. //获取今日广告观看数量
  199. public get_watch_ad_num(): number {
  200. return this.day_data.get(day_data_type.watch_ad);
  201. }
  202. //设置今日广告观看数量
  203. public set_watch_ad_num(num: number): void {
  204. this.day_data.add(day_data_type.watch_ad, num);
  205. this.setDirty();
  206. }
  207. //获取任务奖励状态
  208. public get_task_state(): number {
  209. return this.day_data.get_Array(day_data_type.task_reward_state);
  210. }
  211. //设置任务奖励状态
  212. public set_task_state(task_state: any): void {
  213. this.day_data.set_Array(day_data_type.task_reward_state, task_state);
  214. }
  215. //////////////////////////////////////////////////////////////////////////////////////////
  216. protected async load_data(): Promise<{ [key: string]: any; }> {
  217. return super.load_data();
  218. }
  219. public user_info: { uid: string, nickName: string, avatar: string, province: string };
  220. public get nickName(): string {
  221. return this.user_info.nickName;
  222. }
  223. public get avatarUrl(): string {
  224. return this.user_info.avatar;
  225. }
  226. public async init_user_info() {
  227. this.user_info = {
  228. uid: this.uid,
  229. nickName: ch.sdk.get_player_info().nickName,
  230. avatar: ch.sdk.get_player_info().avatarUrl,
  231. province: ch.sdk.get_player_info().province
  232. }
  233. }
  234. protected async save_data(save_data: { [key: string]: any; }): Promise<boolean> {
  235. return super.save_data(save_data);
  236. }
  237. public async save_rank_floor() {
  238. const floor = this.data.get(data_type.max_floor);
  239. ch.sdk.saveRankData(rand_type.floor, floor, ch.sdk.updateType.none, 0, { province: this.user_info.province });
  240. }
  241. public async get_rank_floor(): Promise<{ list: any[], owner: any, index: number }> {
  242. let index = 0;
  243. const d = await ch.sdk.loadRankData(rand_type.floor, ch.sdk.updateType.none, 100, true, false);
  244. console.log("排行榜", d);
  245. if (d.data.own) {
  246. for (let i = 0; i < d.data.list.length; i++) {
  247. if (d.data.own.userId === d.data.list[i].userId) {
  248. index = i + 1;
  249. }
  250. }
  251. } else {
  252. index = 101;
  253. }
  254. return { list: d.data.list, owner: d.data.own, index: index };
  255. }
  256. async loadPfInfo(): Promise<boolean> {
  257. return new Promise(async (resolve) => {
  258. let k = await ch.sdk.getUserInfo();
  259. if (k) {
  260. resolve(true);
  261. } else {
  262. UINotify.show("需要授权");
  263. resolve(false);
  264. }
  265. })
  266. }
  267. }