Controller.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. var GameCookieKey = "pic2wordkey";
  2. var LevelsCookieKey = "pic2wordlevels";
  3. function Controller() {
  4. //questions
  5. this.currentQuestionId;
  6. this.currentQuestionLevel;
  7. this.currentQuestionIndex;
  8. this.nextQuestionLevel;
  9. this.questions = [];
  10. this.questionLevels = [];
  11. this.questionRepo = [];
  12. this.questionRepoSize = 10;
  13. this.currentQuestionBatch = 1;
  14. this.isFinish = false;
  15. this.difficultyBoundary = 30;
  16. //cookie related
  17. this.forceFromCurrent = false;
  18. this.isAllowCookie = true;
  19. this.readLevelsFromCookie = false;
  20. //preload
  21. this.minPreloadTime = 3500;
  22. this.isPreloadFinished = false;
  23. this.isPreloadTimeUp = false;
  24. this.preloadTimer;
  25. //data url
  26. this.dataBaseUrl = "data/";
  27. this.loadCharactors();
  28. }
  29. Controller.currentQuestionId;
  30. Controller.currentQuestionIndex;
  31. Controller.questionRepo;
  32. Controller.currentQuestionBatch;
  33. Controller.forceFromCurrent;
  34. Controller.charactors;
  35. Controller.needPreload;
  36. Controller.prototype.startGame = function () {
  37. if (readCookie(GameCookieKey) != null) {
  38. this.loadFromCookie();
  39. }
  40. if (readCookie(LevelsCookieKey) != null) {
  41. this.loadLevelsFromCookie();
  42. }
  43. this.initBaseOnUrl();
  44. }
  45. Controller.prototype.stopGame = function()
  46. {
  47. }
  48. Controller.prototype.loadCharactors = function () {
  49. var that = this;
  50. $.getJSON("data/charactors.json", function (data) {
  51. that.charactors = data["charactors"];
  52. });
  53. }
  54. Controller.prototype.loadFromCookie = function () {
  55. var controllerData = readCookie(GameCookieKey);
  56. var values = controllerData.split(',');
  57. this.currentQuestionLevel = parseInt(values[0]);
  58. this.currentQuestionIndex = parseInt(values[1]);
  59. this.currentQuestionBatch = parseInt(values[2]);
  60. this.nextQuestionLevel = parseInt(values[3]);
  61. this.forceFromCurrent = true;
  62. // 每次读取cookie的时候,设定分享内容
  63. if (!!qike) {
  64. if(controller && controller.currentQuestionLevel){
  65. qikeContentPreSet.shareContent.title = qikeContentPreSet.endShareContentTitle;
  66. // gameLevel指代游戏里面存储玩的关卡或者杀敌数等成绩的一个数值变量
  67. qikeContentPreSet.shareContent.desc = qikeContentPreSet.endShareContentPreFix + controller.currentQuestionLevel + qikeContentPreSet.endShareContentSuffix;
  68. }
  69. }
  70. }
  71. Controller.prototype.saveInCookie = function () {
  72. if ( this.isAllowCookie ) {
  73. createCookie( GameCookieKey, this.currentQuestionLevel + "," + this.currentQuestionIndex + "," + this.currentQuestionBatch + "," + this.nextQuestionLevel, 1000);
  74. }
  75. // 每次保存cookie的时候,更新分享内容
  76. if (!!qike) {
  77. if(controller && controller.currentQuestionLevel){
  78. qikeContentPreSet.shareContent.title = qikeContentPreSet.endShareContentTitle;
  79. // gameLevel指代游戏里面存储玩的关卡或者杀敌数等成绩的一个数值变量
  80. qikeContentPreSet.shareContent.desc = qikeContentPreSet.endShareContentPreFix + controller.currentQuestionLevel + qikeContentPreSet.endShareContentSuffix;
  81. }
  82. }
  83. }
  84. Controller.prototype.loadLevelsFromCookie = function() {
  85. var controllerData = readCookie(LevelsCookieKey);
  86. this.questionLevels = controllerData.split(',');
  87. this.readLevelsFromCookie = true;
  88. }
  89. Controller.prototype.saveLevelsInCookie = function() {
  90. if ( this.isAllowCookie ) {
  91. createCookie( LevelsCookieKey, this.questionLevels.join(','));
  92. }
  93. }
  94. Controller.prototype.handlePreloadRequest = function() {
  95. if ( this.questions.length < 1 ) {
  96. this.loadAllQuestions();
  97. } else {
  98. this.loadCurrentQuestions();
  99. }
  100. }
  101. Controller.prototype.initBaseOnUrl = function() {
  102. var date = getURLParameter('date');
  103. if ( date != null && date != "" ) {
  104. this.dataBaseUrl = "data/" + date + "/";
  105. this.forceFromCurrent = false;
  106. this.readLevelsFromCookie = false;
  107. this.isAllowCookie = false;
  108. this.currentQuestionBatch = 1;
  109. this.currentQuestionId = 1;
  110. this.currentQuestionIndex = 0;
  111. this.currentQuestionLevel = 1;
  112. }
  113. }
  114. Controller.prototype.loadAllQuestions = function () {
  115. var that = this;
  116. $.getJSON( this.dataBaseUrl + "questions.json", function(data) {
  117. that.questions = data["questions"];
  118. if ( data["questionRepoSize"] ) {
  119. that.questionRepoSize = data["questionRepoSize"];
  120. }
  121. that.generateLevels();
  122. that.loadCurrentQuestions();
  123. });
  124. }
  125. Controller.prototype.generateLevels = function() {
  126. if ( this.readLevelsFromCookie && this.questionLevels.length == this.questions.length) {
  127. return;
  128. }
  129. var array = [], length = this.questions.length, difficultyBoundary = this.difficultyBoundary, i;
  130. this.questionLevels = [];
  131. for ( i = 0 ; i < Math.min( difficultyBoundary, length ); i ++ ) {
  132. array.push(i);
  133. }
  134. $.merge(this.questionLevels, shuffleArray(array) );
  135. if ( length > difficultyBoundary ) {
  136. for ( i = difficultyBoundary, array = []; i < length; i ++ ) {
  137. array.push(i);
  138. }
  139. $.merge(this.questionLevels, shuffleArray(array) );
  140. }
  141. this.saveLevelsInCookie();
  142. }
  143. Controller.prototype.loadCurrentQuestions = function() {
  144. var start, end, repoLevels=[], that = this;
  145. start = (this.currentQuestionBatch - 1) * this.questionRepoSize;
  146. end = Math.min( this.questions.length, start + this.questionRepoSize);
  147. if ( start > end ) {
  148. //no more questions
  149. this.saveInCookie();
  150. controller.isFinish = true;
  151. SM.SetStateByName('finish');
  152. return;
  153. }
  154. this.questionRepo = [];
  155. repoLevels = this.questionLevels.slice(start, end);
  156. repoLevels.forEach( function(value, index ) {
  157. that.questionRepo.push(that.questions[value]);
  158. });
  159. if (this.forceFromCurrent == false) {
  160. this.currentQuestionIndex = 0;
  161. this.currentQuestionLevel = 1;
  162. this.nextQuestionLevel = this.currentQuestionLevel + 1;
  163. }
  164. this.currentQuestionId = this.questionRepo[this.currentQuestionIndex]["ID"];
  165. this.saveInCookie();
  166. preloadImages(this.questionRepo);
  167. }
  168. Controller.prototype.isAnswerCorrect = function () {
  169. var answerText = '';
  170. $(sprintf("#question-%d .answer-key", this.currentQuestionId)).filter(function () {
  171. return $(this).attr("data-key") != "";
  172. }).each(function (index, elem) {
  173. answerText += elem.getAttribute("data-key");
  174. });
  175. return this.isAnswerCorrectByText(answerText);
  176. }
  177. Controller.prototype.isAnswerCorrectByText = function (answerText) {
  178. if (this.questionRepo[this.currentQuestionIndex]["answer"] == answerText) {
  179. return true;
  180. }
  181. return false;
  182. }
  183. Controller.prototype.processToNextQuestion = function () {
  184. if (this.questionRepo.length > this.currentQuestionIndex + 1) {
  185. this.currentQuestionIndex++;
  186. this.currentQuestionLevel++;
  187. this.nextQuestionLevel++;
  188. this.currentQuestionId = this.questionRepo[this.currentQuestionIndex]["ID"];
  189. this.saveInCookie();
  190. controller.needPreload = false;
  191. }
  192. else {
  193. this.forceFromCurrent = true;
  194. this.currentQuestionBatch++;
  195. this.currentQuestionLevel++;
  196. this.nextQuestionLevel++;
  197. this.currentQuestionIndex = 0;
  198. controller.needPreload = true;
  199. }
  200. }
  201. Controller.prototype.removeLetters = function () {
  202. }
  203. Controller.prototype.update = function () {
  204. }