PlayerData.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 { Toast } from "../../core/util_class/Toast";
  6. import { Layout_Main } from "../ui/UI_Main/Layout_Main";
  7. import { UI_Main } from "../ui/UI_Main/UI_Main";
  8. //定义事件
  9. interface event_protocol {
  10. item_count(type: number, count: number): void;//道具数量改变
  11. }
  12. //排行榜key
  13. export enum rand_type {
  14. floor = "floor",//最高层数
  15. }
  16. //自定义数据
  17. export enum data_type {
  18. last_gift_sidebar = 'last_gift_sidebar',//最后一次侧边栏时间
  19. max_floor = 'max_floor',//最高层数
  20. coin = 'coin',//铜币
  21. }
  22. //每日数据
  23. export enum day_data_type {
  24. isFavorite = 'isFavorite',//是否收藏
  25. pass_level = 'pass_level',//通关
  26. combine_num = 'combine_num',//匹配成语
  27. login_num = 'login_num',//登录奖励
  28. use_item = 'use_item',//使用道具
  29. watch_ad = 'watch_ad',//观看广告
  30. task_reward_state = 'task_reward_state',//任务奖励是否领取
  31. store_state= 'store_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: 0 });
  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. } else if (type == 5) {
  104. layout.Container.AddTime();
  105. }
  106. this.set_use_item_num(1);
  107. this.event.emit(this.event.key.item_count, ret.type, ret.count);
  108. this.setDirty();
  109. }
  110. }
  111. /**增加道具*/
  112. add_item(type: number, count: number = 1): void {
  113. let ret = this.items.addCount(type, count);
  114. if (ret) {
  115. this.event.emit(this.event.key.item_count, ret.type, ret.count);
  116. this.setDirty();
  117. }
  118. }
  119. set_item(type: number, count: number = -1): void {
  120. const c = this.get_item(type);
  121. if (type < count) {
  122. this.add_item(type, count - c);
  123. }
  124. }
  125. get_item(type: number): number {
  126. return this.items.getCount(type);
  127. }
  128. //获取签到天数
  129. public get_sign(): number {
  130. return this.week_data.get(week_data_type.sign_day);
  131. }
  132. //设置签到天数
  133. public set_sign(count: number) {
  134. this.week_data.set(week_data_type.sign_day, count);
  135. }
  136. //获取是否当日首次通过收藏进入
  137. public get_is_favorite(): number {
  138. return this.day_data.get(day_data_type.isFavorite);
  139. }
  140. //设置已从收藏进入
  141. public set_is_favorite(): void {
  142. this.day_data.set(day_data_type.isFavorite, 1);
  143. this.setDirty();
  144. }
  145. //获取关卡数
  146. public get_max_floor(): number {
  147. return this.data.get(data_type.max_floor);
  148. }
  149. //设置关卡数
  150. public set_max_floor(floor: number): void {
  151. this.data.set(data_type.max_floor, floor);
  152. }
  153. //获取铜币数量
  154. public get_coin(): number {
  155. return this.data.get(data_type.coin);
  156. }
  157. //设置铜币数量
  158. public set_coin(coin: number): void {
  159. this.data.set(data_type.coin, coin);
  160. this.setDirty();
  161. }
  162. //获取今日通关数
  163. public get_pass_level(): number {
  164. return this.day_data.get(day_data_type.pass_level);
  165. }
  166. //设置今日通关数
  167. public set_pass_level(level: number): void {
  168. this.day_data.add(day_data_type.pass_level, level);
  169. }
  170. //获取匹配成语数量
  171. public get_combine_num(): number {
  172. return this.day_data.get(day_data_type.combine_num);
  173. }
  174. //设置匹配成语数量
  175. public set_combine_num(num: number): void {
  176. this.day_data.add(day_data_type.combine_num, num);
  177. this.setDirty();
  178. }
  179. //获取登录次数
  180. public get_login_num(): number {
  181. return this.day_data.get(day_data_type.login_num);
  182. }
  183. //设置登录次数
  184. public set_login_num(num: number): void {
  185. this.day_data.add(day_data_type.login_num, num);
  186. this.setDirty();
  187. }
  188. //获取今日使用道具数量
  189. public get_use_item_num(): number {
  190. return this.day_data.get(day_data_type.use_item);
  191. }
  192. //设置今日使用道具数量
  193. public set_use_item_num(num: number): void {
  194. this.day_data.add(day_data_type.use_item, num);
  195. }
  196. //获取今日广告观看数量
  197. public get_watch_ad_num(): number {
  198. return this.day_data.get(day_data_type.watch_ad);
  199. }
  200. //设置今日广告观看数量
  201. public set_watch_ad_num(num: number): void {
  202. this.day_data.add(day_data_type.watch_ad, num);
  203. this.setDirty();
  204. }
  205. //获取任务奖励状态
  206. public get_task_state() {
  207. return this.day_data.get_Array(day_data_type.task_reward_state);
  208. }
  209. //设置任务奖励状态
  210. public set_task_state(task_state: any): void {
  211. this.day_data.set_Array(day_data_type.task_reward_state, task_state);
  212. this.setDirty();
  213. }
  214. //获取商店免费领取状态
  215. public get_store_state() {
  216. return this.day_data.get_Array(day_data_type.store_state);
  217. }
  218. //设置商店免费领取状态
  219. public set_store_state(shop_state: any): void {
  220. this.day_data.set_Array(day_data_type.store_state, shop_state);
  221. this.setDirty();
  222. }
  223. //////////////////////////////////////////////////////////////////////////////////////////
  224. protected async load_data(): Promise<{ [key: string]: any; }> {
  225. return super.load_data();
  226. }
  227. public user_info: { uid: string, nickName: string, avatar: string, province: string };
  228. public get nickName(): string {
  229. return this.user_info.nickName;
  230. }
  231. public get avatarUrl(): string {
  232. return this.user_info.avatar;
  233. }
  234. public async init_user_info() {
  235. this.user_info = {
  236. uid: this.uid,
  237. nickName: ch.sdk.get_player_info().nickName,
  238. avatar: ch.sdk.get_player_info().avatarUrl,
  239. province: ch.sdk.get_player_info().province
  240. }
  241. }
  242. protected async save_data(save_data: { [key: string]: any; }): Promise<boolean> {
  243. return super.save_data(save_data);
  244. }
  245. public async save_rank_floor() {
  246. const floor = this.data.get(data_type.max_floor);
  247. ch.sdk.saveRankData(rand_type.floor, floor, ch.sdk.updateType.none, 0, { province: this.user_info.province });
  248. }
  249. public async get_rank_floor(): Promise<{ list: any[], owner: any, index: number }> {
  250. let index = 0;
  251. const d = await ch.sdk.loadRankData(rand_type.floor, ch.sdk.updateType.none, 100, true, false);
  252. console.log("排行榜", d);
  253. if (d.data.own) {
  254. for (let i = 0; i < d.data.list.length; i++) {
  255. if (d.data.own.userId === d.data.list[i].userId) {
  256. index = i + 1;
  257. }
  258. }
  259. } else {
  260. index = 101;
  261. }
  262. return { list: d.data.list, owner: d.data.own, index: index };
  263. }
  264. async loadPfInfo(): Promise<boolean> {
  265. return new Promise(async (resolve) => {
  266. let k = await ch.sdk.getUserInfo();
  267. if (k) {
  268. resolve(true);
  269. } else {
  270. Toast.makeText(gui.getLayerNode(5),'需要授权').show();
  271. resolve(false);
  272. }
  273. })
  274. }
  275. }