chess.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. $(window).on({
  2. contextmenu: function () {
  3. return false;
  4. },
  5. dragstart: function () {
  6. return false;
  7. },
  8. beforeunload: function () {
  9. return true;
  10. }
  11. })
  12. var curCol = 0, myCol = 2, cur, messages = [0, 0, 0];
  13. function isMobile() {
  14. let flag = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  15. // alert(navigator.userAgent + '\n' + flag)
  16. return flag;
  17. }
  18. function setCookie(cname, cvalue, exdays) {
  19. var d = new Date();
  20. d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  21. var expires = "expires=" + d.toGMTString();
  22. document.cookie = cname + "=" + cvalue + "; " + expires;
  23. }
  24. function getCookie(cname) {
  25. var name = cname + "=";
  26. var ca = document.cookie.split(';');
  27. for (var i = 0; i < ca.length; i++) {
  28. var c = ca[i].trim();
  29. if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
  30. }
  31. return "";
  32. }
  33. function checkCookie() {
  34. myCol = parseInt(getCookie('color'))
  35. if (myCol != 0 && myCol != 1) myCol = 2;
  36. }
  37. function updateCurLight() {
  38. if (curCol == 1) {
  39. $('.light').removeClass("black");
  40. $('.light').addClass("white");
  41. } else {
  42. $('.light').removeClass("white");
  43. $('.light').addClass("black");
  44. }
  45. }
  46. function updateMyLight() {
  47. if (myCol == 1) {
  48. $('.mylight').removeClass("black");
  49. $('.mylight').addClass("white");
  50. } else if (myCol == 0) {
  51. $('.mylight').removeClass("white");
  52. $('.mylight').addClass("black");
  53. } else {
  54. $('.mylight').removeClass("black white");
  55. }
  56. }
  57. function updateMessages() {
  58. if (messages[0]) $('.message1').addClass("red");
  59. else $('.message1').removeClass("red");
  60. if (messages[1]) $('.message2').addClass("red");
  61. else $('.message2').removeClass("red");
  62. if (messages[2]) $('.message3').addClass("red");
  63. else $('.message3').removeClass("red");
  64. if (messages[3]) $('.message4').addClass("red");
  65. else $('.message4').removeClass("red");
  66. $("#messageText")[0].value = messages[4];
  67. $("#messageServer")[0].innerHTML = messages[5];
  68. if (messages[5] == 2) $("#messageServer").addClass('label-success')
  69. else $("#messageServer").removeClass('label-success')
  70. if (messages[5] > 2) $("#messageServer").addClass('label-warning')
  71. else $("#messageServer").removeClass('label-warning')
  72. }
  73. var timer, st = 1;
  74. function uploadMessages() {
  75. timer && clearTimeout(timer);
  76. timer = setTimeout(function () { st = 1; }, 100);
  77. //FUUUULL BRiDGE RECtiFyer!!!
  78. if (st) {
  79. console.log('Messages updated.')
  80. sendMessage()
  81. }
  82. st = 0;
  83. }
  84. function updateCol() {
  85. updateCurLight();
  86. if (curCol) {
  87. $('.pointer').removeClass("black");
  88. $('.pointer').addClass("white");
  89. } else {
  90. $('.pointer').removeClass("white");
  91. $('.pointer').addClass("black");
  92. }
  93. }
  94. class Chess {
  95. constructor(ele) {
  96. this.ele = ele;
  97. this.init();
  98. }
  99. init() {
  100. this.arr = [];
  101. this.render();
  102. $('.pointer')[0].style.display = 'block'
  103. $('.pointer').animate({
  104. left: $('#game')[0].offsetLeft - 50,
  105. top: $('#game')[0].offsetTop + 30,
  106. }, 0);
  107. if (isMobile()) $('.pointer').hide();
  108. $('.pos').on('mouseenter', function () {
  109. if ($(this).hasClass("black") || $(this).hasClass("white") || (myCol != 2 && myCol != curCol)) return;
  110. document.onmousemove = null
  111. $('.pointer').animate({
  112. left: this.parentNode.parentNode.parentNode.parentNode.offsetLeft + this.parentNode.offsetLeft + 3,
  113. top: this.parentNode.parentNode.parentNode.parentNode.offsetTop + this.parentNode.offsetTop + 3,
  114. opacity: 0.7,
  115. }, 0)
  116. $('.pointer')[0].style.boxShadow = '3px 3px 7px rgba(15,15,15,0.5)'
  117. $('.pointer')[0].style.transform = 'scale(100%)'
  118. })
  119. $('.pos').mouseleave(function (e) {
  120. updateCol();
  121. document.onmousemove = function (e) {
  122. $('.pointer').animate({
  123. left: e.pageX - 14,
  124. top: e.pageY - 14,
  125. opacity: 0.3,
  126. scale: 1
  127. }, 0)
  128. }
  129. $('.pointer')[0].style.boxShadow = ''
  130. $('.pointer')[0].style.transform = ''
  131. })
  132. this.span = $("#tbodys span");
  133. let that = this
  134. $("#tbodys").on('click', 'tr td span', function () {
  135. that.click(this);
  136. });
  137. $("#regret").on('click', function () {
  138. that.regret();
  139. })
  140. $("#agin").on('click', function () {
  141. that.agin();
  142. })
  143. $("#check").on('click', function () {
  144. that.check();
  145. })
  146. $(".message1").on('click', function () {
  147. messages[0] = !messages[0];
  148. updateMessages();
  149. uploadMessages();
  150. })
  151. $(".message2").on('click', function () {
  152. messages[1] = !messages[1];
  153. updateMessages();
  154. uploadMessages();
  155. })
  156. $(".message3").on('click', function () {
  157. messages[2] = !messages[2];
  158. updateMessages();
  159. uploadMessages();
  160. })
  161. $(".message4").on('click', function () {
  162. messages[3] = !messages[3];
  163. messages[4] = $("#messageText")[0].value;
  164. updateMessages();
  165. uploadMessages();
  166. })
  167. $("#messageText").on('input blur', function () {
  168. messages[4] = $("#messageText")[0].value;
  169. updateMessages();
  170. uploadMessages();
  171. })
  172. }
  173. render() {
  174. let div = $("<div id='xq-tips'>").appendTo($(this.ele))
  175. $("<div class='xq-txt'>").appendTo($(div))
  176. let table2 = $("<table><tbody id='tbodys'>").addClass("tables").attr("border", "0").appendTo($(this.ele));
  177. for (let i = 0; i < 16; i++) {
  178. let tr2 = $("<tr>").appendTo($("#tbodys"));
  179. for (let j = 0; j < 16; j++) {
  180. $("<td><span class='pos' id=" + (i * 16 + j) + ">").appendTo($(tr2));
  181. }
  182. }
  183. }
  184. click(_this) {
  185. console.log(_this)
  186. this.ifs(_this)
  187. this.win();
  188. }
  189. upd() {
  190. $(this.span).each((index, item) => {
  191. this.arr.forEach((arrItem, arrIndex) => {
  192. if (index == arrItem) {
  193. if (arrIndex % 2 == 0) $(item).addClass("black");
  194. else $(item).addClass("white");
  195. }
  196. })
  197. })
  198. let num = this.arr.length;
  199. curCol = num % 2;
  200. updateCurLight();
  201. }
  202. ifs(_this) {
  203. if (myCol == 2) {
  204. myCol = curCol;
  205. setCookie('color', myCol, 1)
  206. }
  207. if ($(_this).hasClass("black") || $(_this).hasClass("white") || myCol != curCol) return;
  208. myCol = curCol;
  209. updateMyLight();
  210. let num = this.arr.length;
  211. console.log(this.arr)
  212. $(this.span).each((index, item) => {
  213. $(item).removeClass("highlight");
  214. if (item === _this) {
  215. this.arr.push(index);
  216. curCol = (num + 1) % 2;
  217. updateCurLight();
  218. if (num % 2 == 0) {
  219. $(_this).addClass("black");
  220. }
  221. else {
  222. $(_this).addClass("white");
  223. }
  224. // return;
  225. }
  226. })
  227. sendMessage(this.arr)
  228. }
  229. win() {
  230. let this_ = this;
  231. let brr = [], wrr = [];
  232. $(this_.arr).each((index, item) => {
  233. if ($(this_.span[item]).hasClass("black")) {
  234. brr.push(item);
  235. }
  236. else {
  237. wrr.push(item);
  238. }
  239. })
  240. brr = brr.sort(function (a, b) {
  241. return a - b;
  242. })
  243. wrr = wrr.sort(function (a, b) {
  244. return a - b;
  245. })
  246. if (this.isInclude(brr, 1) || this.isInclude(brr, 16) || this.isInclude(brr, 17) || this.isInclude(brr, 15)) {
  247. this.arr = [];
  248. $("#xq-tips").show();
  249. $(".xq-txt").html("Black Wins!");
  250. $("#regret").hide();
  251. }
  252. if (this.isInclude(wrr, 1) || this.isInclude(wrr, 16) || this.isInclude(wrr, 17) || this.isInclude(wrr, 15)) {
  253. this.arr = [];
  254. $("#xq-tips").show();
  255. $(".xq-txt").html("White Wins!");
  256. $("#regret").hide();
  257. }
  258. }
  259. isInclude(brr, x) {
  260. for (let i = 0; i < brr.length; i++) {
  261. if (brr.includes(brr[i]) && brr.includes(brr[i] + x * 1) && brr.includes(brr[i] + x * 2) && brr.includes(brr[i] + x * 3) && brr.includes(brr[i] + x * 4)) {
  262. return true;
  263. }
  264. }
  265. }
  266. regret() {
  267. if (this.arr.length == 0 || curCol == myCol) {
  268. return false;
  269. }
  270. let len = this.arr.length - 1;
  271. let num = this.arr[len]
  272. $(this.span[num]).removeClass("black white");
  273. curCol = num % 2;
  274. updateCol();
  275. this.arr.pop();
  276. sendMessage(this.arr)
  277. }
  278. reset() {
  279. this.arr = [];
  280. for (let i = 0; i < this.span.length; i++) {
  281. $(this.span[i]).removeClass("black white");
  282. }
  283. }
  284. agin() {
  285. $('.pointer').removeClass("white");
  286. $('.pointer').addClass("black");
  287. checkCookie();
  288. var exp = new Date();
  289. exp.setTime(exp.getTime() - 1)
  290. setCookie('color', myCol, exp)
  291. myCol = 2;
  292. updateMyLight();
  293. this.reset();
  294. $("#xq-tips").hide();
  295. $(".xq-txt").html("");
  296. $("#regret").show();
  297. sendMessage(this.arr)
  298. }
  299. check() {
  300. $(this.span).each((index, item) => {
  301. if (index == this.arr[this.arr.length - 1]) {
  302. $(item).addClass("highlight");
  303. return;
  304. }
  305. })
  306. }
  307. }
  308. var game = new Chess($("#game"));
  309. checkCookie();
  310. var conversation = document.getElementById('conversation');
  311. var sendBtn = document.getElementById('sendBtn');
  312. var socket = null;
  313. var createSocket = function () {
  314. if (socket) {
  315. socket.close();
  316. }
  317. var url = 'wss://' + window.location.host + '/ws/chess';
  318. socket = new WebSocket(url);
  319. socket.onopen = function () {
  320. $('.ws').removeClass('glyphicon-remove')
  321. $('.ws').addClass('glyphicon-ok')
  322. $("#messageServer").removeClass('label-danger')
  323. console.log('Connected: ' + url);
  324. }
  325. socket.onmessage = function (event) {
  326. var map = JSON.parse(event.data).content;
  327. messages = JSON.parse(event.data).message
  328. console.log(messages)
  329. updateMessages();
  330. game.reset();
  331. game.arr = map;
  332. game.upd();
  333. curCol = map.length % 2;
  334. if (map.length) {
  335. game.win()
  336. } else {
  337. $('.pointer').removeClass("white");
  338. $('.pointer').addClass("black");
  339. checkCookie();
  340. var exp = new Date();
  341. exp.setTime(exp.getTime() - 1)
  342. setCookie('color', myCol, exp)
  343. myCol = 2;
  344. $("#xq-tips").hide();
  345. $(".xq-txt").html("");
  346. $("#regret").show();
  347. }
  348. console.log('Updated')
  349. }
  350. socket.onclose = function (e) {
  351. $('.ws').removeClass('glyphicon-ok')
  352. $('.ws').addClass('glyphicon-remove')
  353. $("#messageServer").addClass('label-danger')
  354. $("#messageServer")[0].innerHTML = '连接断开,请刷新页面'
  355. console.log('Disconnected: ' + url + ',' + e.code + ',' + e.reason + ',' + e.wasClean)
  356. }
  357. };
  358. var sendMessage = function (map = game.arr, msg = messages, type = '') {
  359. var bd = JSON.stringify({
  360. from: myCol,
  361. content: map,
  362. message: msg,
  363. type: type
  364. })
  365. console.log(bd)
  366. fetch('/chess/setboard', {
  367. method: 'POST',
  368. headers: {
  369. 'Content-type': 'application/json',
  370. },
  371. body: bd
  372. })
  373. };
  374. createSocket();
  375. checkCookie();
  376. game.upd();
  377. updateCol();
  378. updateMessages();
  379. $("#begin").hide();
  380. $("#regret").show();
  381. $("#agin").show();
  382. $("#game").show();