game - ¸±±¾.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. let $box = $("#figureBox"),
  2. people = 0;
  3. let me = {
  4. $id: null,
  5. row: Math.floor(Math.random() * 3),
  6. col: Math.floor(Math.random() * 15),
  7. num: 0,
  8. }
  9. var arr = [
  10. [],
  11. [],
  12. []
  13. ];
  14. var ans = [10, 200, 100];
  15. // 开始答题
  16. let $go = $("#go"),
  17. $countDown = $("#countDown"),
  18. $ques = $("#ques"),
  19. $trap = $("#trapID"),
  20. $num = $("#num"),
  21. $renBox = $("#renBox");
  22. let quesData, quesLen, quesFlag = 0,
  23. quesHtml, quesTimer, quesInter, quesCount = 2;
  24. $.get("./question.json", function(res) {
  25. quesData = res.ques;
  26. quesLen = quesData.length;
  27. });
  28. // 答题
  29. var $fail = $("#fail"),
  30. $success = $("#success"),
  31. $endBox = $("#endBox");
  32. var nowAns = -1;
  33. var baseUrl="http://47.108.130.28:9000";
  34. var ansPostUrl=baseUrl+"/api/quepass/ansPost";
  35. var wsCountUrl=baseUrl+"/ws/quepass/onlinecounter";
  36. function init() {
  37. for (var i = 0; i < 3; i++) {
  38. figureInit(ans[i], i);
  39. }
  40. me.$id=$("#ren"+me.num);
  41. let timer = setTimeout(function() {
  42. clearTimeout(timer);
  43. for (let i = 0; i < 3; i++) {
  44. var l = arr[i].length;
  45. for (let j = 0; j < l; j++) {
  46. var delay = arr[i][j].col * 100 + "ms";
  47. getIn(arr[i][j], delay);
  48. }
  49. }
  50. }, 100);
  51. setTimeout(startQues, 3000);
  52. }
  53. function getIn(obj, delay) {
  54. if (obj.$id) {
  55. var $ren = obj.$id;
  56. } else {
  57. var $ren = $("#ren" + obj.num);
  58. obj.$id = $ren;
  59. }
  60. $ren[0].className = obj.figure + " d" + obj.row + "-" + obj.col;
  61. $ren.attr("style", "");
  62. if (delay) {
  63. $ren.css("transition-delay", delay);
  64. }
  65. }
  66. function figureInit(num, flag) {
  67. let html = "",
  68. figure = "";
  69. if (num > 36) num = 36;
  70. for (let i = 0; i < num; i++) {
  71. var left = Math.floor(Math.random() * 140) - 20 + "vw";
  72. figure = "figure f" + Math.floor(Math.random() * 20 + 1);
  73. html += `<div class="${figure}" id="ren${people+i}" style="left:${left}"></div>`;
  74. arr[flag].push({
  75. row: flag,
  76. col: i,
  77. figure,
  78. num: people + i
  79. });
  80. if (flag == me.row && i == me.col) {
  81. me.figure = figure + ` me`;
  82. me.num = people + i;
  83. arr[flag][i].figure+=" me";
  84. }
  85. }
  86. people += num;
  87. $box.append(html);
  88. }
  89. /* function answer(a1, a2, a3) {
  90. if(a1>36) a1=36;
  91. if(a2>36) a2=36;
  92. a3=a3>36?36:a3;
  93. arr[1] = Array(a1);
  94. arr[2] = Array(a2);
  95. arr[3]= Array(a3);
  96. getAnswer(a1, arr[1], 0);
  97. getAnswer(a2, arr[2], 1);
  98. getAnswer(a3, arr[3], 2);
  99. peopleArr = peopleArr.concat(arr[1], arr[2], arr[3]);
  100. }
  101. function getAnswer(num, arr, flag) {
  102. var rand = 0,
  103. $f, delay;
  104. for (var i = 0; i < num; i++) {
  105. if (flag == me.row && i == me.col) {
  106. console.log(flag, i);
  107. continue;
  108. }
  109. rand = Math.floor(Math.random() * peopleArr.length);
  110. arr[i] = peopleArr.splice(rand, 1)[0];
  111. $f = arr[i].$ren;
  112. $f[0].className = arr[i].className + " row"+flag+" d" + flag + "-" + i;
  113. delay = Math.floor(Math.random() * 3000) + "ms";
  114. $f.css("transition-delay", delay)
  115. }
  116. if (flag == me.row) {
  117. arr.splice(me.col, 1);
  118. }
  119. } */
  120. $("#ansBox").on("click", ".ans", function() {
  121. if (quesCount >= 0 && quesCount <= 15) {
  122. var $this = $(this);
  123. var dataId = $this.attr("data-id");
  124. if (dataId != nowAns) {
  125. $this.addClass("active").siblings().removeClass("active");
  126. choiseAnswer(Number(dataId),nowAns);
  127. nowAns = dataId;
  128. }
  129. }
  130. });
  131. function choiseAnswer(flag,prevAns) {
  132. /* $.post(ansPostUrl,JSON.stringify({
  133. qno:(quesFlag+1),
  134. ans:flag,
  135. iscorrect:quesData[quesFlag].right==ansArr[flag]
  136. })); */
  137. var rand = Math.floor(Math.random() * 15);
  138. rand=arr[flag].length>36?rand:arr[flag].length-1;
  139. var prevR = me.row,
  140. prevC = me.col;
  141. console.log(prevR,prevC);
  142. console.log(flag,rand);
  143. var len = Math.floor((arr[flag].length - rand) / 3)+1;
  144. for (var i = len - 1; i >= 0; i--) {
  145. var col = rand + 3 * i;
  146. if (i == 0) {
  147. getTemp(arr[flag][col], me);
  148. me={};
  149. $.extend(true,me,arr[flag][col]);
  150. console.log(me);
  151. }
  152. if (i == len - 1) {
  153. if(arr[prevAns].length>36){
  154. getTemp(arr[prevR][prevC], arr[flag][col]);
  155. }
  156. }
  157. if(i>0 && i<len - 1){
  158. getTemp(arr[flag][col], arr[flag][col - 3]);
  159. }
  160. }
  161. }
  162. function getTemp(obj1, obj2) {
  163. obj1.$id = obj2.$id;
  164. obj1.figure = obj2.figure;
  165. obj1.num = obj2.num;
  166. getIn(obj1);
  167. }
  168. function resetGame() {
  169. nowAns = me.row;
  170. quesCount = 2;
  171. $countDown.html(quesCount);
  172. $trap.html("").hide();
  173. ansArr = ["A", "B", "C"];
  174. }
  175. function startQues() {
  176. if (me.$id.hasClass("err")) {
  177. fail();
  178. return false;
  179. }
  180. resetGame();
  181. if (quesLen != 0 && quesFlag < quesLen) {
  182. $num.html(quesFlag + 1);
  183. var ques = quesData[quesFlag];
  184. quesHtml = ques.text;
  185. if (ques.img) {
  186. quesHtml += `<br/><img src="img/${ques.img}"/>`;
  187. }
  188. $ques.html(quesHtml);
  189. $go.fadeIn(function() {
  190. $renBox.fadeIn();
  191. });
  192. quesInter = setInterval(function() {
  193. if (quesCount == 1) {
  194. clearInterval(quesInter);
  195. $go.fadeOut();
  196. quesFlag++;
  197. $renBox.fadeOut();
  198. var tt = setTimeout(function() {
  199. nextGame(ques);
  200. clearTimeout(tt);
  201. }, 2000);
  202. }
  203. quesCount--;
  204. $countDown.html(quesCount);
  205. }, 10000000);
  206. } else {
  207. clearTimeout(quesTimer);
  208. success();
  209. }
  210. }
  211. function fail() {
  212. $endBox.remove($success).fadeIn(function() {
  213. $fail.show();
  214. })
  215. }
  216. function success() {
  217. $endBox.remove($fail).fadeIn(function() {
  218. $success.show();
  219. })
  220. }
  221. function nextGame(ques) {
  222. var trapHtml = "";
  223. for (var i in ansArr) {
  224. if (ansArr[i] == ques.right) {
  225. peopleArr = arr[i];
  226. } else {
  227. var rand = Math.floor(Math.random() * 4) + 1;
  228. trapHtml += `<div class="trap trap${rand} r${i}"></div>`;
  229. $(".row" + i).attr("style", "").addClass("err");
  230. }
  231. }
  232. $trap.html(trapHtml).show();
  233. quesTimer = setTimeout(startQues, 2000);
  234. }
  235. function getAnswer(){
  236. // var peopleArr=
  237. }
  238. // ws
  239. /* var ws = new WebSocket("ws://47.108.130.28:9000/ws/quepass/onlinewaiting");
  240. ws.onopen = function() {
  241. console.log("open");
  242. }
  243. ws.onmessage = function(e) {
  244. console.log(JSON.parse(e.data));
  245. }
  246. ws.onclose = function(e) {
  247. console.log(e);
  248. }
  249. var ws1 = new WebSocket("ws://47.108.130.28:9000/ws/quepass/onlinecounter");
  250. ws1.onopen = function() {
  251. console.log("open1");
  252. }
  253. ws1.onmessage = function(e) {
  254. console.log(JSON.parse(e.data));
  255. }
  256. ws1.onclose = function(e) {
  257. console.log(e);
  258. } */
  259. init();