GameEvent.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. GameStates: {
  5. default: null,
  6. type: cc.Node,
  7. serializable: true,
  8. },
  9. GameConfig: {
  10. default: null,
  11. type: cc.Node,
  12. serializable: true,
  13. },
  14. VarietyShow: {
  15. default: null,
  16. type: cc.Node,
  17. serializable: true,
  18. },
  19. Interview: {
  20. default: null,
  21. type: cc.Node,
  22. serializable: true,
  23. },
  24. Movie: {
  25. default: null,
  26. type: cc.Node,
  27. serializable: true,
  28. },
  29. HangOut: {
  30. default: null,
  31. type: cc.Node,
  32. serializable: true,
  33. },
  34. },
  35. OnActive()
  36. {
  37. //Game Logic
  38. this.GameConfigScript = this.GameConfig.getComponent('GameConfig');
  39. this.GameStatesScript = this.GameStates.getComponent('GameStates');
  40. //VarietyShow
  41. //Button
  42. this.VarietyShowStartBtn = this.VarietyShow.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
  43. this.VarietyShowFinishBtn = this.VarietyShow.getChildByName('UnlockBtn').getChildByName('FinishBtn');
  44. //TotalTime
  45. this.VarietyShowTotalTimeLabel = this.VarietyShow.getChildByName('Panel').getChildByName('TotalTimeLabel');
  46. this.VarietyShowTotalTimeScript = this.VarietyShow.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
  47. //ProgressBar
  48. this.VarietyShowProgressBar = this.VarietyShow.getChildByName('Panel').getChildByName('ProgressBar');
  49. //Interview
  50. //Button
  51. this.InterviewStartBtn = this.Interview.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
  52. this.InterviewFinishBtn = this.Interview.getChildByName('UnlockBtn').getChildByName('FinishBtn');
  53. //TotalTime
  54. this.InterviewTotalTimeLabel = this.Interview.getChildByName('Panel').getChildByName('TotalTimeLabel');
  55. this.InterviewTotalTimeScript = this.Interview.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
  56. //ProgressBar
  57. this.InterviewProgressBar = this.Interview.getChildByName('Panel').getChildByName('ProgressBar');
  58. //Movie
  59. //Button
  60. this.MovieStartBtn = this.Movie.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
  61. this.MovieFinishBtn = this.Movie.getChildByName('UnlockBtn').getChildByName('FinishBtn');
  62. //TotalTime
  63. this.MovieTotalTimeLabel = this.Movie.getChildByName('Panel').getChildByName('TotalTimeLabel');
  64. this.MovieTotalTimeScript = this.Movie.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
  65. //ProgressBar
  66. this.MovieProgressBar = this.Movie.getChildByName('Panel').getChildByName('ProgressBar');
  67. //HangOut
  68. //Button
  69. this.HangOutStartBtn = this.HangOut.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
  70. this.HangOutFinishBtn = this.HangOut.getChildByName('UnlockBtn').getChildByName('FinishBtn');
  71. //TotalTime
  72. this.HangOutTotalTimeLabel = this.HangOut.getChildByName('Panel').getChildByName('TotalTimeLabel');
  73. this.HangOutTotalTimeScript = this.HangOut.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
  74. //ProgressBar
  75. this.HangOutProgressBar = this.HangOut.getChildByName('Panel').getChildByName('ProgressBar');
  76. this.ShowDefaultUIState();
  77. },
  78. ShowDefaultUIState()
  79. {
  80. this.GetRemainTime(function () {
  81. // VarietyShow
  82. if( this.GameStatesScript.VarietyShowRemainTime>0)
  83. {
  84. this.VarietyShowFinishBtn.active=true;
  85. this.VarietyShowFinishBtn.getComponent(cc.Button).interactable = false;
  86. this.VarietyShowStartBtn.active = false;
  87. this.ShowProgressBar('VarietyShow');
  88. }
  89. else
  90. {
  91. if(this.GameStatesScript.BVarietyShowFinished)
  92. {
  93. this.VarietyShowFinishBtn.active=false;
  94. this.VarietyShowStartBtn.active = true;
  95. this.VarietyShowTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.VarietyShowTotalTime);
  96. }
  97. else {
  98. this.VarietyShowFinishBtn.active=true;
  99. this.VarietyShowFinishBtn.getComponent(cc.Button).interactable = true;
  100. this.VarietyShowStartBtn.active = false;
  101. this.VarietyShowTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(0);
  102. }
  103. }
  104. // Interview
  105. if( this.GameStatesScript.InterviewRemainTime>0)
  106. {
  107. this.InterviewFinishBtn.active=true;
  108. this.InterviewFinishBtn.getComponent(cc.Button).interactable = false;
  109. this.InterviewStartBtn.active = false;
  110. this.ShowProgressBar('Interview');
  111. }
  112. else
  113. {
  114. if(this.GameStatesScript.BInterviewFinished)
  115. {
  116. this.InterviewFinishBtn.active=false;
  117. this.InterviewStartBtn.active = true;
  118. this.InterviewTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.InterviewTotalTime);
  119. }
  120. else {
  121. this.InterviewFinishBtn.active=true;
  122. this.InterviewFinishBtn.getComponent(cc.Button).interactable = true;
  123. this.InterviewStartBtn.active = false;
  124. this.InterviewTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(0);
  125. }
  126. }
  127. // Movie
  128. if( this.GameStatesScript.MovieRemainTime>0)
  129. {
  130. this.MovieFinishBtn.active=true;
  131. this.MovieFinishBtn.getComponent(cc.Button).interactable = false;
  132. this.MovieStartBtn.active = false;
  133. this.ShowProgressBar('Movie');
  134. }
  135. else
  136. {
  137. if(this.GameStatesScript.BMovieFinished)
  138. {
  139. this.MovieFinishBtn.active=false;
  140. this.MovieStartBtn.active = true;
  141. this.MovieTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.MovieTotalTime);
  142. }
  143. else {
  144. this.MovieFinishBtn.active=true;
  145. this.MovieFinishBtn.getComponent(cc.Button).interactable = true;
  146. this.MovieStartBtn.active = false;
  147. this.MovieTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(0);
  148. }
  149. }
  150. // HangOut
  151. if( this.GameStatesScript.HangOutRemainTime>0)
  152. {
  153. this.HangOutFinishBtn.active=true;
  154. this.HangOutFinishBtn.getComponent(cc.Button).interactable = false;
  155. this.HangOutStartBtn.active = false;
  156. this.ShowProgressBar('HangOut');
  157. }
  158. else
  159. {
  160. if(this.GameStatesScript.BHangOutFinished)
  161. {
  162. this.HangOutFinishBtn.active=false;
  163. this.HangOutStartBtn.active = true;
  164. this.HangOutTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.HangOutTotalTime);
  165. }
  166. else {
  167. this.HangOutFinishBtn.active=true;
  168. this.HangOutFinishBtn.getComponent(cc.Button).interactable = true;
  169. this.HangOutStartBtn.active = false;
  170. this.HangOutTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(0);
  171. }
  172. }
  173. }.bind(this));
  174. },
  175. GetRemainTime(CallBack)
  176. {
  177. let URL = this.GameConfigScript.Host+'GameEvent/GetEventRemainTime?Openid='+ cc.sys.localStorage.getItem('Openid');
  178. let XHR = new XMLHttpRequest();
  179. XHR.onreadystatechange = function () {
  180. if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400)) {
  181. let response = XHR.responseText;
  182. // console.log(response);
  183. let ResultJson = JSON.parse(response);
  184. // VarietyShow
  185. this.GameStatesScript.VarietyShowRemainTime = ResultJson.VarietyShowRemainTime;
  186. this.GameStatesScript.BVarietyShowFinished = ResultJson.BVarietyShowFinished;
  187. // Interview
  188. this.GameStatesScript.InterviewRemainTime = ResultJson.InterviewRemainTime;
  189. this.GameStatesScript.BInterviewFinished = ResultJson.BInterviewFinished;
  190. // Movie
  191. this.GameStatesScript.MovieRemainTime = ResultJson.MovieRemainTime;
  192. this.GameStatesScript.BMovieFinished = ResultJson.BMovieFinished;
  193. // HangOut
  194. this.GameStatesScript.HangOutRemainTime = ResultJson.HangOutRemainTime;
  195. this.GameStatesScript.BHangOutFinished = ResultJson.BHangOutFinished;
  196. CallBack();
  197. }
  198. }.bind(this);
  199. XHR.open("GET", URL, true);
  200. XHR.setRequestHeader("Content-Type" , "application/json");
  201. XHR.send();
  202. },
  203. OnStar(event, customEventData)
  204. {
  205. let CourseName = customEventData;
  206. let Data = 'Openid'+ cc.sys.localStorage.getItem('Openid');
  207. let URL = this.GameConfigScript.Host+'GameEvent/Start'+CourseName+'/';
  208. let XHR = new XMLHttpRequest();
  209. XHR.onreadystatechange = function ()
  210. {
  211. if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400))
  212. {
  213. let response = XHR.responseText;
  214. // console.log(response);
  215. if(CourseName == 'VarietyShow')
  216. {
  217. this.VarietyShowFinishBtn.active = true;
  218. this.VarietyShowFinishBtn.getComponent(cc.Button).interactable = false;
  219. this.VarietyShowStartBtn.active = false;
  220. }
  221. else if(CourseName == 'Interview')
  222. {
  223. this.InterviewFinishBtn.active = true;
  224. this.InterviewFinishBtn.getComponent(cc.Button).interactable = false;
  225. this.InterviewStartBtn.active = false;
  226. }
  227. else if(CourseName == 'Movie')
  228. {
  229. this.MovieFinishBtn.active = true;
  230. this.MovieFinishBtn.getComponent(cc.Button).interactable = false;
  231. this.MovieStartBtn.active = false;
  232. }
  233. else if(CourseName == 'HangOut')
  234. {
  235. this.HangOutFinishBtn.active = true;
  236. this.HangOutFinishBtn.getComponent(cc.Button).interactable = false;
  237. this.HangOutStartBtn.active = false;
  238. }
  239. this.UpdateGameStates(response);
  240. this.GetRemainTime(function () {
  241. this.ShowProgressBar(CourseName);
  242. }.bind(this));
  243. }
  244. }.bind(this);
  245. XHR.open("POST", URL, false);
  246. XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  247. XHR.send(Data);
  248. },
  249. ShowProgressBar(CourseName)
  250. {
  251. let CourseScript = null;
  252. let TotalTime = 0;
  253. let RemainTime = 0;
  254. if(CourseName=='VarietyShow')
  255. {
  256. this.VarietyShowProgressBar.active = true;
  257. CourseScript = this.VarietyShowTotalTimeScript;
  258. TotalTime= this.GameConfigScript.VarietyShowTotalTime;
  259. RemainTime = this.GameStatesScript.VarietyShowRemainTime;
  260. }
  261. else if(CourseName=='Interview')
  262. {
  263. this.InterviewProgressBar.active = true;
  264. CourseScript = this.InterviewTotalTimeScript;
  265. TotalTime= this.GameConfigScript.InterviewTotalTime;
  266. RemainTime = this.GameStatesScript.InterviewRemainTime;
  267. }
  268. else if(CourseName=='Movie')
  269. {
  270. this.MovieProgressBar.active = true;
  271. CourseScript = this.MovieTotalTimeScript;
  272. TotalTime= this.GameConfigScript.MovieTotalTime;
  273. RemainTime = this.GameStatesScript.MovieRemainTime;
  274. }
  275. else if(CourseName=='HangOut')
  276. {
  277. this.HangOutProgressBar.active = true;
  278. CourseScript = this.HangOutTotalTimeScript;
  279. TotalTime= this.GameConfigScript.HangOutTotalTime;
  280. RemainTime = this.GameStatesScript.HangOutRemainTime;
  281. }
  282. // 重复次数
  283. let Repeat = cc.REPEAT_FOREVER;
  284. // 以秒为单位的时间间隔
  285. let Interval = 1;
  286. // 开始延时
  287. let Delay = 0.01;
  288. let Index = Math.floor(RemainTime);
  289. let StartCourseSchedule = function()
  290. {
  291. Index--;
  292. CourseScript.SetProgressBar(Index,TotalTime);
  293. if(0==Index)
  294. {
  295. this.FinishCallBack(CourseName);
  296. this.unschedule(StartCourseSchedule);
  297. }
  298. }.bind(this);
  299. this.schedule(StartCourseSchedule, Interval, Repeat-1, Delay);
  300. },
  301. FinishCallBack(CourseName)
  302. {
  303. // console.log(CourseName)
  304. if(CourseName=='VarietyShow')
  305. {
  306. this.VarietyShowFinishBtn.getComponent(cc.Button).interactable = true;
  307. this.VarietyShowProgressBar.active = false;
  308. }
  309. else if(CourseName=='Interview')
  310. {
  311. this.InterviewFinishBtn.getComponent(cc.Button).interactable = true;
  312. this.InterviewProgressBar.active = false;
  313. }
  314. else if(CourseName=='Movie')
  315. {
  316. this.MovieFinishBtn.getComponent(cc.Button).interactable = true;
  317. this.MovieProgressBar.active = false;
  318. }
  319. else if(CourseName=='HangOut')
  320. {
  321. this.HangOutFinishBtn.getComponent(cc.Button).interactable = true;
  322. this.HangOutProgressBar.active = false;
  323. }
  324. },
  325. OnFinished(event, customEventData)
  326. {
  327. let CourseName = customEventData;
  328. if(CourseName=='VarietyShow')
  329. {
  330. this.VarietyShowFinishBtn.active = false;
  331. this.VarietyShowStartBtn.active = true;
  332. this.VarietyShowTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.VarietyShowTotalTime);
  333. }
  334. else if(CourseName=='Interview')
  335. {
  336. this.InterviewFinishBtn.active = false;
  337. this.InterviewStartBtn.active = true;
  338. this.InterviewTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.InterviewTotalTime);
  339. }
  340. else if(CourseName=='Movie')
  341. {
  342. this.MovieFinishBtn.active = false;
  343. this.MovieStartBtn.active = true;
  344. this.MovieTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.MovieTotalTime);
  345. }
  346. else if(CourseName=='HangOut')
  347. {
  348. this.HangOutFinishBtn.active = false;
  349. this.HangOutStartBtn.active = true;
  350. this.HangOutTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.HangOutTotalTime);
  351. }
  352. let Data = 'Openid='+ cc.sys.localStorage.getItem('Openid');
  353. let URL = this.GameConfigScript.Host+'GameEvent/Finish'+CourseName+'/';
  354. // console.log(URL)
  355. let XHR = new XMLHttpRequest();
  356. XHR.onreadystatechange = function ()
  357. {
  358. if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400))
  359. {
  360. let response = XHR.responseText;
  361. this.UpdateGameStates(response);
  362. }
  363. }.bind(this);
  364. XHR.open("POST", URL, false);
  365. XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  366. XHR.send(Data);
  367. },
  368. UpdateGameStates(response)
  369. {
  370. let GameStates = JSON.parse(response);
  371. // console.log(ResultJson);
  372. this.GameStatesScript.PlusActionPower(GameStates.ActionPower,false);
  373. this.GameStatesScript.PlusCoin(GameStates.Coin);
  374. this.GameStatesScript.PlusDiamond(GameStates.Diamond);
  375. this.GameStatesScript.PlusFans(GameStates.Fans);
  376. this.GameStatesScript.PlusFavorableImpression(GameStates.FavorableImpression);
  377. },
  378. CastSecondsToHours(Time)
  379. {
  380. if(Time<60) return Time+'秒';
  381. if(Time<3600 && Time >=60)
  382. {
  383. return Math.floor(Time/60)+'分'+Time%60+'秒';
  384. }
  385. if(Time>=3600)
  386. {
  387. let Hour = Math.floor(Time/3600);
  388. let s = Time%3600;
  389. let Min = Math.floor(s/60);
  390. return Hour+'小时'+Min+'分';
  391. }
  392. return Math.floor(Time/3600);
  393. }
  394. });