game.js 8.3 KB

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