chess.js 11 KB

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