LocalData.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import EventName from "../EventName/EventName";
  2. import TimeOver from "../GameUI/TimeOver";
  3. export var WorkState = {
  4. 引导: 0,
  5. 上班: 1,
  6. 加班: 2,
  7. }
  8. //本地数据管理
  9. export default class LocalData {
  10. //临时数据 不保存
  11. //每日免费游玩总次数
  12. public FreePlayCount: number = 333;
  13. //当前免费游玩次数
  14. private $FreeGameCount: number;
  15. private static _instance: LocalData = null;
  16. public static getInstance() {
  17. if (!this._instance) {
  18. this._instance = new LocalData();
  19. this._instance.init();
  20. }
  21. return this._instance;
  22. }
  23. private readonly $localkey: string = "Daegongsi1";
  24. //上次登录时间
  25. private lastLoginTime: number = 0;
  26. //临时时间停止次数
  27. private $TimeStopCount: number = 0;
  28. //弹射入座次数
  29. private $NowSitDownCount: number = 0;
  30. //关卡状态
  31. private $_WorkState = WorkState.引导;
  32. private $Music: boolean = true;
  33. private $Effects: boolean = true;
  34. private $Shake: boolean = true;
  35. //我的职场等级
  36. private MyWorkplaceLevel: number = 1;
  37. public init() {
  38. // 尝试从本地存储中获取上次登录的时间戳
  39. const storedLastLogin = localStorage.getItem('lastLoginTime');
  40. this.lastLoginTime = storedLastLogin ? parseInt(storedLastLogin) : 0;
  41. this.load();
  42. this.Restore()
  43. this.save();
  44. }
  45. ////////////////////////////////////////////加班状态
  46. public setWorkState(Temp: number) {
  47. this.$_WorkState = Temp
  48. this.save();
  49. }
  50. public getWorkState() {
  51. return this.$_WorkState;//
  52. }
  53. /////////////////////////////////////////////我的职场等级
  54. public setMyWorkplaceLevel(num: number) {
  55. this.MyWorkplaceLevel = num
  56. }
  57. public getMyWorkplaceLevel() {
  58. return this.MyWorkplaceLevel;
  59. }
  60. public getMyWorkplaceLevel_Name(num?: number): string {
  61. let temp = 0
  62. if (num) {
  63. temp = num
  64. } else {
  65. temp = this.MyWorkplaceLevel
  66. }
  67. let name = {
  68. 1: '实习生',
  69. 2: '正式员工',
  70. 3: '资深员工',
  71. 4: '小组长',
  72. 5: '主管',
  73. 6: '总监',
  74. 7: '部门经理',
  75. 8: '副总经理',
  76. 9: '总经理',
  77. 10: '执行副总裁',
  78. 11: '不用执行副总裁',
  79. 12: '首席运营官',
  80. 13: 'CE0',
  81. 14: '董事长',
  82. }
  83. return name[temp]
  84. }
  85. /////////////////////////////////////////////时间停止次数
  86. public setTimeStopCount(num: number) {
  87. this.$TimeStopCount = num
  88. this.save();
  89. cc.systemEvent.emit(EventName.UpdataPropCount)
  90. }
  91. public LessTimeStopCount() {
  92. if (this.$TimeStopCount > 0) {
  93. this.$TimeStopCount--
  94. this.save();
  95. cc.systemEvent.emit(EventName.UpdataPropCount)
  96. }
  97. }
  98. public AddTimeStopCount() {
  99. this.$TimeStopCount++
  100. this.save();
  101. cc.systemEvent.emit(EventName.UpdataPropCount)
  102. }
  103. public getTimeStopCount() {
  104. return this.$TimeStopCount;
  105. }
  106. /////////////////////////////////////////////弹射入座次数
  107. public setNowSitDownCount(num: number) {
  108. this.$NowSitDownCount = num
  109. this.save();
  110. cc.systemEvent.emit(EventName.UpdataPropCount)
  111. }
  112. public LessNowSitDownCount() {
  113. if (this.$NowSitDownCount > 0) {
  114. this.$NowSitDownCount--
  115. this.save();
  116. cc.systemEvent.emit(EventName.UpdataPropCount)
  117. }
  118. }
  119. public AddNowSitDownCount() {
  120. this.$NowSitDownCount++
  121. this.save();
  122. cc.systemEvent.emit(EventName.UpdataPropCount)
  123. }
  124. public getNowSitDownCount() {
  125. return this.$NowSitDownCount;
  126. }
  127. /////////////////////////////////////////////巴士皮肤
  128. public getBusSkin() {
  129. // return this.BusSkin;
  130. //1 巴士
  131. //2 地铁
  132. //3 豪车
  133. //4 高铁
  134. //5 火车
  135. //6 游艇
  136. //7 飞机
  137. let temp: { [key: number]: number } = {
  138. 1: 1,
  139. 2: 1,
  140. 3: 2,
  141. 4: 2,
  142. 5: 5,
  143. 6: 5,
  144. 7: 4,
  145. 8: 4,
  146. 9: 7,
  147. 10: 7,
  148. 11: 3,
  149. 12: 3,
  150. 13: 6,
  151. 14: 6,
  152. }
  153. return temp[this.MyWorkplaceLevel]
  154. }
  155. /////////////////////////////////////////////设置
  156. public setMusic(Temp: boolean) {
  157. this.$Music = Temp
  158. this.save();
  159. }
  160. public getMusic() {
  161. return this.$Music;
  162. }
  163. public setEffects(Temp: boolean) {
  164. this.$Effects = Temp
  165. this.save();
  166. }
  167. public getEffects() {
  168. return this.$Effects;
  169. }
  170. public setVibrate(Temp: boolean) {
  171. this.$Shake = Temp
  172. this.save();
  173. }
  174. public getVibrate() {
  175. return this.$Shake;
  176. }
  177. /////////////////////////////////////////////免费游玩局数设置
  178. //还原
  179. private FreeGameCountResorte() {
  180. this.$FreeGameCount = this.FreePlayCount
  181. this.save();
  182. }
  183. // 设置
  184. public setFreeGameCount(num: number) {
  185. this.$FreeGameCount = num
  186. this.save();
  187. }
  188. // 得到
  189. public getFreeGameCount() {
  190. return this.$FreeGameCount;
  191. }
  192. //保存玩家本地数据
  193. public save() {
  194. let data: any = {};
  195. for (const key in this) {
  196. if (Object.prototype.hasOwnProperty.call(this, key)) {
  197. if (key.charAt(0) == '$') {
  198. data[key] = this[key];
  199. }
  200. }
  201. }
  202. cc.sys.localStorage.setItem(this.$localkey, JSON.stringify(data));
  203. }
  204. //加载玩家本地数据
  205. private load() {
  206. let data = cc.sys.localStorage.getItem(this.$localkey);
  207. if (data) {
  208. data = JSON.parse(data);
  209. for (const key in data) {
  210. this[key] = data[key];
  211. }
  212. }
  213. }
  214. //每日还原默认值
  215. private Restore() {
  216. if (this.checkIfNewDay()) {
  217. //还原每日游玩次数
  218. this.FreeGameCountResorte()
  219. TimeOver.isVideobtn=false
  220. }else{
  221. TimeOver.isVideobtn=true
  222. }
  223. }
  224. public checkIfNewDay(): boolean {
  225. const currentDate = new Date();
  226. const todayStart = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()).getTime();
  227. const isNewDay = todayStart > this.lastLoginTime;
  228. // 更新上次登录时间为今天
  229. this.lastLoginTime = todayStart;
  230. localStorage.setItem('lastLoginTime', this.lastLoginTime.toString());
  231. return isNewDay;
  232. }
  233. }