game.js.bak 8.4 KB

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