game - 副本.js 9.2 KB

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