game.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. var baseUrl = "gj.100t.com";
  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. var $box = $("#figureBox"),
  7. people = 0;
  8. var me = {
  9. row: 1,
  10. col: 1
  11. };
  12. var $go = $("#go"),
  13. $countDown = $("#countDown"),
  14. $ques = $("#ques"),
  15. $trap = $("#trapID"),
  16. $num = $("#num"),
  17. $fail = $("#fail"),
  18. $success = $("#success"),
  19. $endBox = $("#endBox"),
  20. $ren = $("#ren");
  21. var quesData, quesLen = 0,
  22. quesFlag = 0,
  23. quesHtml, quesTimer, quesInter, quesCount = quesEnd = 100000,
  24. isQuesEnd = false;
  25. function getQueryString(name) {
  26. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  27. var r = window.location.search.substr(1).match(reg);
  28. if (r != null) return unescape(r[2]);
  29. return null;
  30. }
  31. var uid = getQueryString('uid');
  32. var uname = getQueryString('uname');
  33. var round_id = getQueryString('round_id');
  34. var peopleArr = [];
  35. var questionUrl = "question.json";
  36. if (round_id == 2) {
  37. questionUrl = "question2.json";
  38. } else if (round_id == 3) {
  39. questionUrl = "question3.json";
  40. }
  41. $.getJSON(questionUrl, function(res) {
  42. quesData = res.ques;
  43. quesLen = quesData.length;
  44. $("#allQues").html(quesLen);
  45. });
  46. var ansArr = ["A", -1, "B"]
  47. // 预加载
  48. var $load = $("#load"),
  49. images = new Array();
  50. function preload(imgs, callback) {
  51. var iCur = 0,
  52. i = 0;
  53. for (i = 0; i < imgs.length; i++) {
  54. images[i] = new Image();
  55. images[i].src = "img/" + imgs[i];
  56. images[i].onload = function() {
  57. iCur++;
  58. if (iCur == imgs.length) {
  59. console.log("加载完成");
  60. callback();
  61. }
  62. }
  63. }
  64. }
  65. if (!uid || !uname) {
  66. // window.location.href = "http://www.100t.com/gjnh/2022.php";
  67. } else {
  68. $.post(registUrl, JSON.stringify({
  69. uid,
  70. uname
  71. }), function(res) {})
  72. preload(["bg.png", "figure.png", "img1.png", "img2.png", "img3.png", "line.png",
  73. "qus-top.png",
  74. "text-bg.png",
  75. "time-box.png", "trap1.png", "trap2.png", "trap3.png", "trap4.png","mute.png"
  76. ], function() {})
  77. waiting(function() {
  78. $load.fadeOut(function() {
  79. $(this).remove();
  80. init();
  81. });
  82. })
  83. }
  84. // 初始化
  85. function init() {
  86. var html = "",
  87. figure = "";
  88. for (var i = 0; i < 108; i++) {
  89. var left = Math.floor(Math.random() * 30) + 35 + "vw";
  90. figure = 'figure f' + Math.floor(Math.random() * 20 + 1);
  91. html += "<div class='" + figure + " row1' id='ren" + i + "' style='left:" + left + "'></div>";
  92. if (i == 1) {
  93. continue;
  94. }
  95. peopleArr.push("#ren" + i);
  96. }
  97. $box.append(html);
  98. me.$id = $("#ren1");
  99. var timer1 = setTimeout(function() {
  100. for (var j = 0; j < 36; j++) {
  101. if (peopleArr[j]) {
  102. var delay = j * 10 + "ms";
  103. me.$id.addClass("me");
  104. getIn(peopleArr[j], 1, j, delay);
  105. }
  106. }
  107. getIn(me.$id, 1, 1, "20ms");
  108. clearTimeout(timer1);
  109. }, 100);
  110. setTimeout(function() {
  111. startQues();
  112. }, 1000);
  113. }
  114. function getIn(str, i, j, delay) {
  115. var $ren = $(str);
  116. removePos($ren).addClass("d" + i + "-" + j).addClass("row" + i);
  117. $ren.removeAttr("style");
  118. if (delay) {
  119. $ren.css("transition-delay", delay);
  120. }
  121. }
  122. function startQues() {
  123. if (!me.$id.hasClass('row1')) {
  124. postAns(-1, 0, 30);
  125. fail();
  126. isQuesEnd = true;
  127. window.clearInterval(timer);
  128. return false;
  129. }
  130. resetGame();
  131. console.log(quesLen);
  132. if(!quesLen){
  133. alert("题目出现错误");
  134. return false;
  135. }
  136. if (quesLen != 0 && quesFlag < quesLen && !isQuesEnd) {
  137. $num.html(quesFlag + 1);
  138. var ques = quesData[quesFlag];
  139. quesHtml = ques.text;
  140. $ques.html(quesHtml);
  141. $go.fadeIn();
  142. getRandAns();
  143. quesInter = setInterval(function() {
  144. if (quesCount == 1) {
  145. clearInterval(quesInter);
  146. $go.fadeOut();
  147. quesFlag++;
  148. var tt = setTimeout(function() {
  149. nextGame(ques);
  150. clearTimeout(tt);
  151. }, 2000);
  152. }
  153. quesCount--;
  154. $countDown.html(quesCount);
  155. }, 1000);
  156. } else {
  157. isQuesEnd = true;
  158. clearTimeout(quesTimer);
  159. window.clearInterval(timer);
  160. success();
  161. }
  162. }
  163. function nextGame(ques) {
  164. var trapHtml = "";
  165. trapHtml += emptyRow(1);
  166. $(".row1").removeClass("row1");
  167. if (ques.right == "A") {
  168. trapHtml += emptyRow(2);
  169. $(".row2").removeClass("row2");
  170. $(".row0").removeClass("row0").addClass("row1");
  171. } else {
  172. trapHtml += emptyRow(0);
  173. $(".row0").removeClass("row0");
  174. $(".row2").removeClass("row2").addClass("row1");
  175. }
  176. $trap.html(trapHtml).show();
  177. setTimeout(function() {
  178. var $rows = $(".row1"),
  179. rowsLen = $rows.length;
  180. for (var i = 0; i < rowsLen; i++) {
  181. var $k = $rows.eq(i);
  182. removePos($k).removeAttr("style").addClass("d1-" + i).addClass("row1");
  183. }
  184. }, 2000);
  185. quesTimer = setTimeout(startQues, 3000);
  186. }
  187. function emptyRow(i) {
  188. $(".row" + i).removeAttr("style").transition({
  189. y: "-100vh"
  190. }, 2000, "linear", function() {
  191. removePos($(this)).css("left", 40 + "vw");
  192. })
  193. if (i == 1) {
  194. var rand = 4;
  195. } else {
  196. var rand = Math.floor(Math.random() * 3) + 1;
  197. }
  198. return '<div class="trap trap' + rand + ' r' + i + '"></div>';
  199. }
  200. function resetGame() {
  201. isAns = false;
  202. quesCount = quesEnd;
  203. $countDown.html(quesCount);
  204. $trap.html("").hide();
  205. $("#ansBox .ans").show().removeClass("active");
  206. }
  207. function fail() {
  208. window.location.href = "fail.html";
  209. }
  210. function success() {
  211. // window.location.href = "success.html";
  212. console.log("success");
  213. }
  214. // 获取随机答案
  215. function getRandAns() {
  216. var tempPeople = peopleArr.concat();
  217. for (var i = 0; i < 3; i++) {
  218. for (var j = 0; j < 36; j++) {
  219. if (i == 1) {
  220. continue;
  221. }
  222. if (j == 1) {
  223. continue;
  224. }
  225. var rand = Math.floor(Math.random() * tempPeople.length);
  226. var id = tempPeople.splice(rand, 1)[0];
  227. delay = Math.floor(Math.random() * 8000) + "ms";
  228. if (id) {
  229. getIn(id, i, j, delay);
  230. } else {
  231. console.log(id);
  232. }
  233. }
  234. }
  235. }
  236. // 清除原来位置
  237. function removePos($obj) {
  238. if ($obj) {
  239. var c = $obj.attr("class");
  240. var match = c.match(/d\d\-\d{0,2}/);
  241. var m2 = c.match(/row\d{1}/);
  242. if (match) {
  243. $obj.removeClass(function() {
  244. return match[0];
  245. })
  246. }
  247. if (m2) {
  248. $obj.removeClass(function() {
  249. return m2[0];
  250. })
  251. }
  252. return $obj;
  253. }
  254. }
  255. // 选择答案
  256. var isAns = false;
  257. $("#ansBox").on("click", ".ans", function() {
  258. if (quesCount >= 0 && quesCount <= quesEnd) {
  259. if (!isAns) {
  260. var $this = $(this);
  261. var dataId = $this.attr("data-id");
  262. isAns = true;
  263. $this.addClass("active").siblings().fadeOut();
  264. removePos(me.$id).removeAttr("style").addClass("d" + dataId + "-" + 1).addClass("row" + dataId);
  265. me.row = dataId;
  266. var ans = -1,
  267. time = quesEnd - quesCount,
  268. iscorrect = 0;
  269. if (quesData[quesFlag].right == "A") {
  270. ans = dataId == 0 ? 1 : 0;
  271. iscorrect = dataId == 0 ? 1 : 0;
  272. } else if (quesData[quesFlag].right == "B") {
  273. ans = dataId == 2 ? 0 : 1;
  274. iscorrect = dataId == 2 ? 1 : 0;
  275. }
  276. postAns(ans, iscorrect, time);
  277. }
  278. }
  279. });
  280. function postAns(ans, iscorrect, time) {
  281. $.post(ansPostUrl, JSON.stringify({
  282. round_id,
  283. uid,
  284. uname,
  285. qno: (quesFlag + 1),
  286. ans,
  287. iscorrect,
  288. time
  289. }));
  290. }
  291. // 重连
  292. var limitConnect = 5; // 断线重连次数
  293. var timeConnect = 0;
  294. function reconnect(service) {
  295. // lockReconnect加锁,防止onclose、onerror两次重连
  296. if (limitConnect > 0) {
  297. if (localStorage.getItem('lockReconnect') != true) {
  298. localStorage.setItem("lockReconnect", 1);
  299. limitConnect--;
  300. timeConnect++;
  301. console.log("第" + timeConnect + "次重连");
  302. // 进行重连
  303. setTimeout(function() {
  304. webSocketInit(service);
  305. localStorage.removeItem("lockReconnect");
  306. }, 2000);
  307. }
  308. } else {
  309. console.log("TCP连接已超时");
  310. }
  311. }
  312. var isWait = true;
  313. var timer = null;
  314. function waiting(callback) {
  315. var ws = new WebSocket(wsWaitUrl);
  316. ws.onopen = function() {
  317. console.log("open success");
  318. localStorage.removeItem("lockReconnect");
  319. //定时发送
  320. timer = window.setInterval(function() {
  321. ws.send(uid + "_" + uname)
  322. }, 1000)
  323. }
  324. ws.onmessage = function(e) {
  325. var res = JSON.parse(e.data);
  326. if (res.status == 1 && isWait && res.game_status == 1) {
  327. window.location.href = "gameready.html";
  328. }
  329. if (res.status == 1 && isWait && res.game_status != 1) {
  330. isWait = false;
  331. callback();
  332. }
  333. if (res.status == 2 && !isWait && res.game_status == 1) {
  334. isQuesEnd = true;
  335. clearTimeout(quesTimer);
  336. success();
  337. }
  338. /* if(res.status == 1 && !isWait && res.data.length<100){
  339. isQuesEnd = true;
  340. clearTimeout(quesTimer);
  341. window.clearInterval(timer);
  342. success();
  343. } */
  344. $ren.html(res.data.length);
  345. }
  346. ws.onclose = function() {
  347. console.log("closed...")
  348. window.clearInterval(timer)
  349. //这里仿照page.html、
  350. ws.onerror = function(err) {
  351. window.clearInterval(timer)
  352. };
  353. }
  354. ws.onerror = function (err) {
  355. window.clearInterval(timer)
  356. };
  357. }
  358. function getQueryString(name) {
  359. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  360. var r = decodeURI(window.location.search.substr(1)).match(reg);
  361. if (r != null) return unescape(r[2]); return null;
  362. }
  363. var uid=getQueryString('uid');
  364. var uname=getQueryString('uname');
  365. if(!uid || !uname){
  366. window.location.href="https://www.100t.com/gjnh/2022.php";
  367. }else{
  368. $.post(registUrl,JSON.stringify({uid,uname}),function(res){
  369. 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(){
  370. waiting(function(){
  371. $load.fadeOut(function(){
  372. $(this).remove();
  373. init();
  374. });
  375. })
  376. })
  377. })
  378. }