LocalData.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //本地数据管理
  2. import EventName from "./EventName/EventName";
  3. import { Global } from "./Global";
  4. export default class LocalData {
  5. private static _instance: LocalData = null;
  6. public static getInstance() {
  7. if (!this._instance) {
  8. this._instance = new LocalData();
  9. this._instance.init();
  10. }
  11. return this._instance;
  12. }
  13. private readonly $localkey: string = "柑橘味";
  14. //玩家金币
  15. private $goldnum: number = Global.defaultGold;
  16. //玩家最高纪录
  17. private $MaxRecord: number = Global.defaultMaxRecord;
  18. //拥有的抛物线皮肤
  19. private $HasParabolaSkin: number[] = Global.defaultParabolaSkin;
  20. ///拥有的人物皮肤
  21. private $HasCharacterSkin: number[] = Global.defaultCharacterSkin;
  22. ///当前使用的人物皮肤
  23. private $useParabolaSkin: number = Global.useParabolaSkin;
  24. ///当前使用的人物皮肤
  25. private $useCharacterSkin: number = Global.useCharacterSkin;
  26. private $lastLoginTime: number = 0;
  27. public init() {
  28. this.load();
  29. let now = Date.now();
  30. this.$lastLoginTime = now;
  31. this.save();
  32. this.setCurrentParabolaSkin(1001)
  33. }
  34. public setGold(num: number, Action: string) {
  35. this.$goldnum = this.Action(this.$goldnum, num, Action)
  36. this.save();
  37. cc.systemEvent.emit(EventName.changeGold);
  38. }
  39. public getGold() {
  40. return this.$goldnum;
  41. }
  42. public setMaxRecord(num: number, Action: string) {
  43. this.$MaxRecord = this.Action(this.$MaxRecord, num, Action)
  44. this.save();
  45. cc.systemEvent.emit(EventName.changeMaxRecord);
  46. }
  47. public getMaxRecord() {
  48. return this.$MaxRecord;
  49. }
  50. public setHasCharacterSkin(num: number) {
  51. this.$HasCharacterSkin.push(num)
  52. this.save();
  53. }
  54. public getHasCharacterSkin() {
  55. return this.$HasCharacterSkin;
  56. }
  57. public setHasParabolaSkin(num: number) {
  58. this.$HasParabolaSkin.push(num)
  59. this.save();
  60. }
  61. public getHasParabolaSkin() {
  62. return this.$HasParabolaSkin;
  63. }
  64. public setCurrentCharacterSkin(num: number) {
  65. this.$useCharacterSkin = num
  66. this.save();
  67. }
  68. public getCurrentCharacterSkin() {
  69. return this.$useCharacterSkin;
  70. }
  71. public setCurrentParabolaSkin(num: number) {
  72. this.$useParabolaSkin = num
  73. this.save();
  74. }
  75. public getCurrentParabolaSkin() {
  76. return this.$useParabolaSkin;
  77. }
  78. /////////////////////////////////////////////设置
  79. private $Music: boolean = true;
  80. private $Effects: boolean = true;
  81. public setMusic(Temp: boolean) {
  82. this.$Music = Temp
  83. this.save();
  84. }
  85. public getMusic() {
  86. return this.$Music;
  87. }
  88. public setEffects(Temp: boolean) {
  89. this.$Effects = Temp
  90. this.save();
  91. }
  92. public getEffects() {
  93. return this.$Effects;
  94. }
  95. /////////////////////////////////////////////签到
  96. private $Sign: { [key: number]: number }[] = []
  97. public setSign(Temp: { [key: number]: number }[]) {
  98. this.$Sign = Temp
  99. this.save();
  100. }
  101. public getSign() {
  102. return this.$Sign;
  103. }
  104. /////////////////////////////////////////////游戏内
  105. ///抛物线次数
  106. private $ParabolaCount: number = Global.FirstParabolaCount;//初始抛物线次数
  107. public setParabolaCount(num: number, Action: string) {
  108. this.$ParabolaCount = this.Action(this.$ParabolaCount, num, Action)
  109. this.save();
  110. cc.systemEvent.emit(EventName.changeParabolaCount);
  111. }
  112. public getParabolaCount() {
  113. return this.$ParabolaCount;
  114. }
  115. ///超级跳的次数
  116. private $Supjump: number = Global.FirstSupjumpCount;//初始超级跳次数
  117. public setSupjumpCount(num: number, Action: string) {
  118. this.$Supjump = this.Action(this.$Supjump, num, Action)
  119. this.save();
  120. cc.systemEvent.emit(EventName.changeSupjumpCount);
  121. }
  122. public getSupjumpCount() {
  123. return this.$Supjump;
  124. }
  125. ///飞的次数
  126. private $Fly: number = Global.FirstFlyCount;//初始超级跳次数
  127. public setFlyCount(num: number, Action: string) {
  128. this.$Fly = this.Action(this.$Fly, num, Action)
  129. this.save();
  130. cc.systemEvent.emit(EventName.changeFlyCount);
  131. }
  132. public getFlyCount() {
  133. return this.$Fly;
  134. }
  135. //保存玩家本地数据
  136. public save() {
  137. let data: any = {};
  138. for (const key in this) {
  139. if (Object.prototype.hasOwnProperty.call(this, key)) {
  140. if (key.charAt(0) == '$') {
  141. data[key] = this[key];
  142. }
  143. }
  144. }
  145. cc.sys.localStorage.setItem(this.$localkey, JSON.stringify(data));
  146. }
  147. //加载玩家本地数据
  148. private load() {
  149. let data = cc.sys.localStorage.getItem(this.$localkey);
  150. if (data) {
  151. data = JSON.parse(data);
  152. for (const key in data) {
  153. this[key] = data[key];
  154. }
  155. }
  156. }
  157. private Action(data: number, num: number, str: string) {
  158. switch (str) {
  159. case '+':
  160. return data += num
  161. case '-':
  162. return data -= num
  163. case '=':
  164. return data = num
  165. default:
  166. cc.error('出错了')
  167. return 0
  168. }
  169. }
  170. //#region 签到代码
  171. initSign() {
  172. if (this.getSign().length <= 0) {
  173. console.log('初始化了一次');
  174. let Total_temp: { [key: number]: number }[] = []
  175. for (let index = 0; index < 7; index++) {
  176. let temp: { [key: number]: number } = {}
  177. const today = new Date();
  178. const tomorrow = new Date(today);
  179. tomorrow.setDate(tomorrow.getDate() + index);
  180. tomorrow.setHours(0, 0, 0, 0);
  181. const todayTimestamp = tomorrow.getTime();
  182. temp[todayTimestamp] = 0
  183. Total_temp.push(temp)
  184. }
  185. this.setSign(Total_temp)
  186. }
  187. this.getSign().forEach((e, index) => {
  188. for (const key in e) {
  189. if (Object.prototype.hasOwnProperty.call(e, key)) {
  190. const element = e[key];
  191. console.log('数组排序的值', index);//数组排序的值
  192. console.log('当日零点时间戳值', key);//当日零点时间戳值
  193. console.log('签到次数', element);//签到次数
  194. if (index == 6) {
  195. const today = new Date();
  196. today.setHours(0, 0, 0, 0);
  197. const todayTimestamp = today.getTime();
  198. //如果以及是7天后了 那么就 重置一下
  199. if (todayTimestamp > parseInt(key)) {
  200. //清空
  201. this.setSign([])
  202. //重新赋值
  203. this.initSign()
  204. }
  205. }
  206. }
  207. }
  208. })
  209. }
  210. //返回今天签到次数
  211. getTodaySignCount(): [number, number] {
  212. const today = new Date();
  213. today.setHours(0, 0, 0, 0);
  214. const todayTimestamp = today.getTime();
  215. for (let i = 0; i < this.getSign().length; i++) {
  216. const e = this.getSign()[i];
  217. for (const key in e) {
  218. if (Object.prototype.hasOwnProperty.call(e, key)) {
  219. const element = e[key];
  220. if (todayTimestamp == parseInt(key)) {
  221. return [element, i]
  222. }
  223. }
  224. }
  225. }
  226. }
  227. //增加一次今天签到次数
  228. addTodaySignCount() {
  229. const today = new Date();
  230. today.setHours(0, 0, 0, 0);
  231. const todayTimestamp = today.getTime();
  232. for (let i = 0; i < this.getSign().length; i++) {
  233. const e = this.getSign()[i];
  234. for (const key in e) {
  235. if (Object.prototype.hasOwnProperty.call(e, key)) {
  236. if (todayTimestamp == parseInt(key)) {
  237. e[key]++
  238. }
  239. }
  240. }
  241. }
  242. }
  243. byIndex2Count(index: number): [number, number] {
  244. for (let i = 0; i < this.getSign().length; i++) {
  245. const e = this.getSign()[i];
  246. for (const key in e) {
  247. if (Object.prototype.hasOwnProperty.call(e, key)) {
  248. if (index == i) {
  249. return [parseInt(key), e[key]]
  250. }
  251. }
  252. }
  253. }
  254. }
  255. //#endregion
  256. }