chess.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. }
  69. var timer, st = 1;
  70. function uploadMessages() {
  71. timer && clearTimeout(timer);
  72. timer = setTimeout(function () { st = 1; }, 100);
  73. //FUUUULL BRiDGE RECtiFyer!!!
  74. if (st) {
  75. console.log('Messages updated.')
  76. sendMessage()
  77. }
  78. st = 0;
  79. }
  80. function updateCol() {
  81. updateCurLight();
  82. if (curCol) {
  83. $('.pointer').removeClass("black");
  84. $('.pointer').addClass("white");
  85. } else {
  86. $('.pointer').removeClass("white");
  87. $('.pointer').addClass("black");
  88. }
  89. }
  90. class Chess {
  91. constructor(ele) {
  92. this.ele = ele;
  93. this.init();
  94. }
  95. init() {
  96. this.arr = [];
  97. this.render();
  98. $('.pointer')[0].style.display = 'block'
  99. $('.pointer').animate({
  100. left: $('#game')[0].offsetLeft - 50,
  101. top: $('#game')[0].offsetTop + 30,
  102. }, 0);
  103. if (isMobile()) $('.pointer').hide();
  104. $('.pos').on('mouseenter', function () {
  105. if ($(this).hasClass("black") || $(this).hasClass("white") || (myCol != 2 && myCol != curCol)) return;
  106. document.onmousemove = null
  107. $('.pointer').animate({
  108. left: this.parentNode.parentNode.parentNode.parentNode.offsetLeft + this.parentNode.offsetLeft + 3,
  109. top: this.parentNode.parentNode.parentNode.parentNode.offsetTop + this.parentNode.offsetTop + 3,
  110. opacity: 0.7,
  111. }, 0)
  112. $('.pointer')[0].style.boxShadow = '3px 3px 7px rgba(15,15,15,0.5)'
  113. $('.pointer')[0].style.transform = 'scale(100%)'
  114. })
  115. $('.pos').mouseleave(function (e) {
  116. updateCol();
  117. document.onmousemove = function (e) {
  118. $('.pointer').animate({
  119. left: e.pageX - 14,
  120. top: e.pageY - 14,
  121. opacity: 0.3,
  122. scale: 1
  123. }, 0)
  124. }
  125. $('.pointer')[0].style.boxShadow = ''
  126. $('.pointer')[0].style.transform = ''
  127. })
  128. this.span = $("#tbodys span");
  129. let that = this
  130. $("#tbodys").on('click', 'tr td span', function () {
  131. that.click(this);
  132. });
  133. $("#regret").on('click', function () {
  134. that.regret();
  135. })
  136. $("#agin").on('click', function () {
  137. that.agin();
  138. })
  139. $("#check").on('click', function () {
  140. that.check();
  141. })
  142. $(".message1").on('click', function () {
  143. messages[0] = !messages[0];
  144. updateMessages();
  145. uploadMessages();
  146. })
  147. $(".message2").on('click', function () {
  148. messages[1] = !messages[1];
  149. updateMessages();
  150. uploadMessages();
  151. })
  152. $(".message3").on('click', function () {
  153. messages[2] = !messages[2];
  154. updateMessages();
  155. uploadMessages();
  156. })
  157. $(".message4").on('click', function () {
  158. messages[3] = !messages[3];
  159. messages[4] = $("#messageText")[0].value;
  160. updateMessages();
  161. uploadMessages();
  162. })
  163. $("#messageText").on('input blur', function () {
  164. messages[4] = $("#messageText")[0].value;
  165. updateMessages();
  166. uploadMessages();
  167. })
  168. }
  169. render() {
  170. let div = $("<div id='xq-tips'>").appendTo($(this.ele))
  171. $("<div class='xq-txt'>").appendTo($(div))
  172. let table2 = $("<table><tbody id='tbodys'>").addClass("tables").attr("border", "0").appendTo($(this.ele));
  173. for (let i = 0; i < 16; i++) {
  174. let tr2 = $("<tr>").appendTo($("#tbodys"));
  175. for (let j = 0; j < 16; j++) {
  176. $("<td><span class='pos' id=" + (i * 16 + j) + ">").appendTo($(tr2));
  177. }
  178. }
  179. }
  180. click(_this) {
  181. console.log(_this)
  182. this.ifs(_this)
  183. this.win();
  184. }
  185. upd() {
  186. $(this.span).each((index, item) => {
  187. this.arr.forEach((arrItem, arrIndex) => {
  188. if (index == arrItem) {
  189. if (arrIndex % 2 == 0) $(item).addClass("black");
  190. else $(item).addClass("white");
  191. }
  192. })
  193. })
  194. let num = this.arr.length;
  195. curCol = num % 2;
  196. updateCurLight();
  197. }
  198. ifs(_this) {
  199. if (myCol == 2) {
  200. myCol = curCol;
  201. setCookie('color', myCol, 1)
  202. }
  203. if ($(_this).hasClass("black") || $(_this).hasClass("white") || myCol != curCol) return;
  204. myCol = curCol;
  205. updateMyLight();
  206. let num = this.arr.length;
  207. console.log(this.arr)
  208. $(this.span).each((index, item) => {
  209. $(item).removeClass("highlight");
  210. if (item === _this) {
  211. this.arr.push(index);
  212. curCol = (num + 1) % 2;
  213. updateCurLight();
  214. if (num % 2 == 0) {
  215. $(_this).addClass("black");
  216. }
  217. else {
  218. $(_this).addClass("white");
  219. }
  220. // return;
  221. }
  222. })
  223. sendMessage(this.arr)
  224. }
  225. win() {
  226. let this_ = this;
  227. let brr = [], wrr = [];
  228. $(this_.arr).each((index, item) => {
  229. if ($(this_.span[item]).hasClass("black")) {
  230. brr.push(item);
  231. }
  232. else {
  233. wrr.push(item);
  234. }
  235. })
  236. brr = brr.sort(function (a, b) {
  237. return a - b;
  238. })
  239. wrr = wrr.sort(function (a, b) {
  240. return a - b;
  241. })
  242. if (this.isInclude(brr, 1) || this.isInclude(brr, 16) || this.isInclude(brr, 17) || this.isInclude(brr, 15)) {
  243. this.arr = [];
  244. $("#xq-tips").show();
  245. $(".xq-txt").html("Black Wins!");
  246. $("#regret").hide();
  247. }
  248. if (this.isInclude(wrr, 1) || this.isInclude(wrr, 16) || this.isInclude(wrr, 17) || this.isInclude(wrr, 15)) {
  249. this.arr = [];
  250. $("#xq-tips").show();
  251. $(".xq-txt").html("White Wins!");
  252. $("#regret").hide();
  253. }
  254. }
  255. isInclude(brr, x) {
  256. for (let i = 0; i < brr.length; i++) {
  257. 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)) {
  258. return true;
  259. }
  260. }
  261. }
  262. regret() {
  263. if (this.arr.length == 0 || curCol == myCol) {
  264. return false;
  265. }
  266. let len = this.arr.length - 1;
  267. let num = this.arr[len]
  268. $(this.span[num]).removeClass("black white");
  269. curCol = num % 2;
  270. updateCol();
  271. this.arr.pop();
  272. sendMessage(this.arr)
  273. }
  274. reset() {
  275. this.arr = [];
  276. for (let i = 0; i < this.span.length; i++) {
  277. $(this.span[i]).removeClass("black white");
  278. }
  279. }
  280. agin() {
  281. $('.pointer').removeClass("white");
  282. $('.pointer').addClass("black");
  283. checkCookie();
  284. var exp = new Date();
  285. exp.setTime(exp.getTime() - 1)
  286. setCookie('color', myCol, exp)
  287. myCol = 2;
  288. updateMyLight();
  289. this.reset();
  290. $("#xq-tips").hide();
  291. $(".xq-txt").html("");
  292. $("#regret").show();
  293. sendMessage(this.arr)
  294. }
  295. check() {
  296. $(this.span).each((index, item) => {
  297. if (index == this.arr[this.arr.length - 1]) {
  298. $(item).addClass("highlight");
  299. return;
  300. }
  301. })
  302. }
  303. }
  304. var game = new Chess($("#game"));
  305. checkCookie();
  306. var conversation = document.getElementById('conversation');
  307. var sendBtn = document.getElementById('sendBtn');
  308. var socket = null;
  309. var createSocket = function () {
  310. if (socket) {
  311. socket.close();
  312. }
  313. var url = 'ws://' + window.location.host + '/ws/chess';
  314. socket = new WebSocket(url);
  315. socket.onopen = function () {
  316. console.log('connected to ' + url);
  317. }
  318. socket.onmessage = function (event) {
  319. var map = JSON.parse(event.data).content;
  320. messages = JSON.parse(event.data).message
  321. console.log(messages)
  322. updateMessages();
  323. game.reset();
  324. game.arr = map;
  325. game.upd();
  326. curCol = map.length % 2;
  327. if (map.length) {
  328. game.win()
  329. } else {
  330. $('.pointer').removeClass("white");
  331. $('.pointer').addClass("black");
  332. checkCookie();
  333. var exp = new Date();
  334. exp.setTime(exp.getTime() - 1)
  335. setCookie('color', myCol, exp)
  336. myCol = 2;
  337. $("#xq-tips").hide();
  338. $(".xq-txt").html("");
  339. $("#regret").show();
  340. }
  341. console.log('Updated')
  342. }
  343. socket.onclose = function () {
  344. console.log('close connect to' + url);
  345. }
  346. };
  347. var sendMessage = function (map = game.arr, msg = messages, type = '') {
  348. var bd = JSON.stringify({
  349. from: myCol,
  350. content: map,
  351. message: msg,
  352. type: type
  353. })
  354. console.log(bd)
  355. fetch('/chess/setboard', {
  356. method: 'POST',
  357. headers: {
  358. 'Content-type': 'application/json',
  359. },
  360. body: bd
  361. }).then(res => {
  362. return res.json();
  363. })
  364. };
  365. createSocket();
  366. checkCookie();
  367. game.upd();
  368. updateCol();
  369. updateMessages();
  370. $("#begin").hide();
  371. $("#regret").show();
  372. $("#agin").show();
  373. $("#game").show();