LocalData.ts 9.0 KB

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