PlayerData.ts 11 KB

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