game.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. var baseUrl = "47.108.130.28:9000";
  2. var registUrl="http://"+baseUrl+"/api/quepass/regist";
  3. var ansPostUrl = "http://"+baseUrl + "/api/quepass/anspost";
  4. var wsCountUrl = "ws://"+baseUrl + "/ws/quepass/onlinecounter";
  5. var wsWaitUrl="ws://"+baseUrl+"/ws/quepass/onlinewaiting";
  6. let $box = $("#figureBox"),
  7. people = 0;
  8. let me = {
  9. row: Math.floor(Math.random() * 3),
  10. col: Math.floor(Math.random() * 15)
  11. };
  12. var arr = [
  13. new Array(36),
  14. new Array(36),
  15. new Array(36)
  16. ];
  17. var ans = [100,100,100];
  18. // 开始答题
  19. let $go = $("#go"),
  20. $countDown = $("#countDown"),
  21. $ques = $("#ques"),
  22. $trap = $("#trapID"),
  23. $num = $("#num"),
  24. // $renBox = $("#renBox"),
  25. $rens = [$("#people1"), $("#people2"), $("#people3")];
  26. let quesData, quesLen=0, quesFlag = 0,
  27. quesHtml, quesTimer, quesInter, quesCount = quesEnd =30,isQuesEnd=false;
  28. $.getJSON("question.json", function(res) {
  29. quesData = res.ques;
  30. quesLen = quesData.length;
  31. $("#allQues").html(quesLen);
  32. });
  33. // 答题
  34. var $fail = $("#fail"),
  35. $success = $("#success"),
  36. $endBox = $("#endBox");
  37. var nowAns = -1;
  38. var peopleArr = [];
  39. //?? peopleArr一直都是全部的ren 删减#ren时才改变
  40. function init() {
  41. for (var i = 0; i < 3; i++) {
  42. figureInit(ans[i], i);
  43. }
  44. me.$id = $("#ren" + me.num);
  45. let timer = setTimeout(function() {
  46. clearTimeout(timer);
  47. for (let i = 0; i < 3; i++) {
  48. for (let j = 0; j < 36; j++) {
  49. if (arr[i][j]) {
  50. var delay = j * 100 + "ms";
  51. getIn(arr[i][j], i, j, delay);
  52. }
  53. }
  54. }
  55. }, 100);
  56. setTimeout(function(){
  57. startQues();
  58. getCounter();
  59. }, 5000);
  60. }
  61. function getIn(str, i, j, delay) {
  62. var $ren = $(str);
  63. $ren.addClass("d" + i + "-" + j);
  64. if (str == me.id) {
  65. $ren.addClass("me");
  66. }
  67. $ren.removeAttr("style");
  68. if (delay) {
  69. $ren.css("transition-delay", delay);
  70. }
  71. }
  72. function figureInit(num, flag) {
  73. let html = "",
  74. figure = "";
  75. if (num > 36) num = 36;
  76. for (let i = 0; i < num; i++) {
  77. var left = Math.floor(Math.random() * 140) - 20 + "vw";
  78. figure = "figure f" + Math.floor(Math.random() * 20 + 1);
  79. html += `<div class="${figure}" id="ren${people+i}" style="left:${left}"></div>`;
  80. arr[flag][i] = "#ren" + (people + i);
  81. peopleArr.push(arr[flag][i]);
  82. if (flag == me.row && i == me.col) {
  83. me.id = arr[flag][i];
  84. peopleArr.pop();
  85. }
  86. }
  87. people += num;
  88. $box.append(html);
  89. }
  90. $("#ansBox").on("click", ".ans", function() {
  91. if (quesCount >= 0 && quesCount <= quesEnd) {
  92. var $this = $(this);
  93. var dataId = $this.attr("data-id");
  94. $this.addClass("active").siblings().removeClass("active");
  95. if (dataId != nowAns) {
  96. // $this.addClass("active").siblings().removeClass("active");
  97. choiseAnswer(Number(dataId));
  98. nowAns = dataId;
  99. }
  100. }
  101. });
  102. function choiseAnswer(flag) {
  103. $.post(ansPostUrl, JSON.stringify({
  104. uid,
  105. uname,
  106. qno: (quesFlag + 1),
  107. ans: flag,
  108. iscorrect: quesData[quesFlag].right == ansArr[flag]
  109. }));
  110. var rand = Math.floor(Math.random() * 15);
  111. rand = arr[flag].length > 15 ? rand : arr[flag].length - 1;
  112. var prevR = me.row,
  113. prevC = me.col;
  114. arr[prevR][prevC] = null;
  115. // 当新位置有值时
  116. if (arr[flag][rand]) {
  117. var id = arr[flag][rand];
  118. var $id = $(id);
  119. // 当前行选择多于等于36时,需要将当前位置的元素移除
  120. if (ans[flag] >= 36) {
  121. $id.css("z-index", 11).transition({
  122. y: "-100vh"
  123. }, 1000, function() {
  124. $id.remove();
  125. var num = peopleArr.indexOf(id);
  126. if (num >= 0) {
  127. peopleArr.splice(num, 1);
  128. }
  129. })
  130. } else {
  131. // 当前行选择少于36时,将元素放到最后一个位置上
  132. arr[flag][ans[flag]] = id;
  133. removePos($id).addClass("d" + flag + "-" + ans[flag]);
  134. }
  135. }
  136. // 当旧位置多于36个时,需要添加新元素
  137. if (ans[prevR] > 36) {
  138. people++;
  139. var left = 10 + 30 * prevR + "vw";
  140. figure = "figure f" + Math.floor(Math.random() * 20 + 1);
  141. var html = `<div class="${figure}" id="ren${people}" style="left:${left}"></div>`;
  142. $box.append(html);
  143. arr[prevR][prevC] = "#ren" + people;
  144. peopleArr.push(arr[prevR][prevC]);
  145. var timer = setTimeout(function() {
  146. $(arr[prevR][prevC]).removeAttr("style").addClass("d" + prevR + "-" + prevC);
  147. }, 100);
  148. }
  149. arr[flag][rand] = me.id;
  150. removePos($(me.id)).removeAttr("style").addClass("d" + flag + "-" + rand);
  151. me.row = flag;
  152. me.col = rand;
  153. }
  154. function removePos($obj) {
  155. let match = $obj.attr("class").match(/d\d\-\d{0,2}/);
  156. if (match) {
  157. return $obj.removeClass(function() {
  158. return match[0];
  159. })
  160. } else {
  161. return $obj
  162. }
  163. }
  164. function getTemp(obj1, obj2) {
  165. obj1.$id = obj2.$id;
  166. obj1.figure = obj2.figure;
  167. obj1.num = obj2.num;
  168. getIn(obj1);
  169. }
  170. function resetGame() {
  171. nowAns = me.row;
  172. quesCount = quesEnd;
  173. $countDown.html(quesCount);
  174. $trap.html("").hide();
  175. ansArr = ["A", "B", "C"];
  176. $(".figure").removeAttr("data-err");
  177. }
  178. function startQues() {
  179. if ($(me.id).attr("data-err")) {
  180. fail();
  181. isQuesEnd=true;
  182. return false;
  183. }
  184. resetGame();
  185. if (quesLen != 0 && quesFlag < quesLen && !isQuesEnd) {
  186. $num.html(quesFlag + 1);
  187. var ques = quesData[quesFlag];
  188. quesHtml = ques.text;
  189. if (ques.img) {
  190. quesHtml += `<br/><img src="img/${ques.img}"/>`;
  191. }
  192. $ques.html(quesHtml);
  193. $go.fadeIn();
  194. // 需要删除
  195. /* ans = [10, 50, 10];
  196. getAnswer(); */
  197. quesInter = setInterval(function() {
  198. if (quesCount == 1) {
  199. clearInterval(quesInter);
  200. $go.fadeOut();
  201. quesFlag++;
  202. var tt = setTimeout(function() {
  203. nextGame(ques);
  204. clearTimeout(tt);
  205. }, 2000);
  206. }
  207. quesCount--;
  208. $countDown.html(quesCount);
  209. }, 1000);
  210. } else {
  211. isQuesEnd=true;
  212. clearTimeout(quesTimer);
  213. success();
  214. }
  215. }
  216. function fail() {
  217. $endBox.remove($success).fadeIn(function() {
  218. $fail.show();
  219. })
  220. }
  221. function success() {
  222. $endBox.remove($fail).fadeIn(function() {
  223. $success.show();
  224. })
  225. }
  226. function nextGame(ques) {
  227. var trapHtml = "";
  228. var rightNum = ansArr.indexOf(ques.right);
  229. for (var i in ansArr) {
  230. // 回答错误
  231. if (ansArr[i] != ques.right) {
  232. var rand = Math.floor(Math.random() * 4) + 1;
  233. trapHtml += `<div class="trap trap${rand} r${i}"></div>`;
  234. if(arr[i].length>0){
  235. for (var k in arr[i]) {
  236. if(arr[i][k]){
  237. var $k = $(arr[i][k]);
  238. $k.removeAttr("style").attr("data-err", 1).transition({
  239. y: "-100vh"
  240. }, 2000, "linear", function() {
  241. removePos($k).css("left", rightNum * 30 + 10 + "vw");
  242. })
  243. }
  244. }}
  245. }
  246. }
  247. $trap.html(trapHtml).show();
  248. quesTimer = setTimeout(startQues, 3000);
  249. }
  250. function getTempPeople(){
  251. var tempArr=[];
  252. for(var i=0;i<3;i++){
  253. for(var j=0;j<36;j++){
  254. if(arr[i][j]){
  255. tempArr.push(arr[i][j]);
  256. }
  257. }
  258. }
  259. return tempArr;
  260. }
  261. function getAnswer(res) {
  262. var tempPeople = peopleArr.concat();
  263. if(ans[0].length<36 && ans[1].length<36 && ans[2].length<36){
  264. tempPeople=getTempPeople();
  265. }
  266. var temp = [new Array(36), new Array(36), new Array(36)];
  267. temp[me.row][me.col]=me.id;
  268. for (var i = 0; i < 3; i++) {
  269. var a = ans[i] > 36 ? 36 : ans[i];
  270. if(i==me.row){
  271. a=a>=36?36:a-1;
  272. }
  273. $rens[i].html(ans[i]);
  274. for (var j = 0; j < a; j++) {
  275. if (i == me.row && j == me.col) {
  276. temp[i][j] = me.id;
  277. continue;
  278. }
  279. var rand = Math.floor(Math.random() * tempPeople.length);
  280. temp[i][j] = tempPeople.splice(rand, 1)[0];
  281. if (temp[i][j]) {
  282. $temp = $(temp[i][j]);
  283. removePos($temp).removeAttr("style").addClass("d" + i + "-" + j);
  284. } else {
  285. console.log(temp[i][j]);
  286. }
  287. }
  288. }
  289. var len = tempPeople.length;
  290. for (var i = 0; i < len; i++) {
  291. removePos($(tempPeople[i])).removeAttr("style");
  292. }
  293. arr = temp.concat();
  294. }
  295. // ws
  296. function getCounter(){
  297. var ws1 = new WebSocket(wsCountUrl);
  298. var tempAns=new Array(3);
  299. var isSame=true;
  300. var $ren=$("#ren");
  301. var rens=0;
  302. ws1.onopen = function(e){
  303. ws1.send("getcounter")
  304. }
  305. ws1.onmessage = function(e) {
  306. var res = JSON.parse(e.data);
  307. console.log(res,4444444444444)
  308. rens=0;
  309. for (var i = 0; i < res.length; i++) {
  310. var len=res[i].data.length;
  311. switch (res[i].ans) {
  312. case 0:
  313. tempAns[0] = len;
  314. break;
  315. case 1:
  316. tempAns[1] = len;
  317. break;
  318. case 2:
  319. tempAns[2] = len;
  320. break;
  321. }
  322. rens+=len;
  323. }
  324. for(var i=0;i<3;i++){
  325. if(tempAns[i]!=ans[i]){
  326. isSame=false;
  327. break;
  328. }
  329. }
  330. if(!isSame){
  331. $ren.html(rens);
  332. ans=tempAns.concat();
  333. ans=[0,30,0];
  334. getAnswer();
  335. }
  336. }
  337. }
  338. var $load=$("#load"),images=new Array();
  339. function preload(imgs, callback) {
  340. var iCur = 0,
  341. i = 0;
  342. for (i = 0; i < imgs.length; i++) {
  343. images[i] = new Image();
  344. images[i].src = "img/"+imgs[i];
  345. images[i].onload = function() {
  346. iCur++;
  347. if (iCur == imgs.length) {
  348. console.log("加载完成");
  349. callback();
  350. }
  351. }
  352. }
  353. }
  354. var isWait=true;
  355. var uid=getQueryString('uid');
  356. var uname=getQueryString('uname');
  357. function waiting(callback){
  358. var ws = new WebSocket(wsWaitUrl);
  359. var timer = null
  360. ws.onopen = function() {
  361. console.log("正在等待");
  362. console.log("open success");
  363. localStorage.removeItem("lockReconnect");
  364. //定时发送
  365. timer = window.setInterval(function () {
  366. ws.send(uid+"_"+uname)
  367. }, 1000)
  368. }
  369. ws.onmessage = function(e) {
  370. var res=JSON.parse(e.data);
  371. if(res.status==1 && isWait){
  372. isWait=false;
  373. callback();
  374. }
  375. if(res.status==2 && !isWait){
  376. isQuesEnd=true;
  377. clearTimeout(quesTimer);
  378. success();
  379. }
  380. }
  381. ws.onclose = function () {
  382. console.log("closed...")
  383. window.clearInterval(timer)
  384. //这里仿照page.html添加上重连机制
  385. }
  386. ws.onerror = function (err) {
  387. window.clearInterval(timer)
  388. };
  389. }
  390. function getQueryString(name) {
  391. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  392. var r = window.location.search.substr(1).match(reg);
  393. if (r != null) return unescape(r[2]); return null;
  394. }
  395. var uid=getQueryString('uid');
  396. var uname=getQueryString('uname');
  397. if(!uid || !uname){
  398. window.location.href="https://www.100t.com/gjnh/2022.php";
  399. }else{
  400. $.post(registUrl,JSON.stringify({uid,uname}),function(res){
  401. preload(["bg.png","figure.png","img1.png","img2.png","img3.png","line.png","qus-top.png","text-bg.png","time-box.png","trap1.png","trap2.png","trap3.png","trap4.png"],function(){
  402. waiting(function(){
  403. $load.fadeOut(function(){
  404. $(this).remove();
  405. init();
  406. });
  407. })
  408. })
  409. })
  410. }