PlayerData.ts 5.6 KB

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