1 line
No EOL
17 KiB
JavaScript
1 line
No EOL
17 KiB
JavaScript
(()=>{"use strict";var t,e,n,i=function(){function t(t,e,n){this.family=t,this.value=e,this.red=n,this.imgSrc="",this.imgFront=new Image,this.imgBack=new Image,this.img=new Image,this.tilt=0,this.setImgSrc()}return t.prototype.getFamily=function(){return this.family},t.prototype.getValue=function(){return this.value},t.prototype.isRed=function(){return this.red},t.prototype.setTilt=function(){this.tilt=.05*(1-2*Math.random())},t.prototype.drawTile=function(t,e,n,i,r,o){void 0===r&&(r=!1),void 0===o&&(o=0),t.save(),t.translate(e+75*i/2,n+100*i/2),t.rotate(o+this.tilt),r?t.drawImage(this.imgBack,-75*i/2,-100*i/2,75*i,100*i):(t.drawImage(this.imgFront,-75*i/2,-100*i/2,75*i,100*i),t.drawImage(this.img,-75*i/2,-100*i/2,75*i,100*i)),t.restore()},t.prototype.isLessThan=function(t){return this.family<t.family||this.family===t.family&&this.value<=t.value},t.prototype.cleanup=function(){this.imgFront.onload=null,this.imgFront.onerror=null,this.imgBack.onload=null,this.imgBack.onerror=null,this.img.onload=null,this.img.onerror=null},t.prototype.setImgSrc=function(){this.imgSrc="/img/Regular/",this.family<=3?(this.imgSrc+=["","Man","Pin","Sou"][this.family],this.imgSrc+=String(this.value),this.red&&(this.imgSrc+="-Dora"),this.imgSrc+=".svg"):4===this.family?this.imgSrc+=["","Ton","Nan","Shaa","Pei"][this.value]+".svg":5===this.family&&(this.imgSrc+=["","Chun","Hatsu","Haku"][this.value]+".svg")},t.prototype.preloadImg=function(){return t=this,e=void 0,i=function(){return function(t,e){var n,i,r,o={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(l){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(o=0)),o;)try{if(n=1,i&&(r=2&a[0]?i.return:a[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,a[1])).done)return r;switch(i=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,i=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!((r=(r=o.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){o.label=a[1];break}if(6===a[0]&&o.label<r[1]){o.label=r[1],r=a;break}if(r&&o.label<r[2]){o.label=r[2],o.ops.push(a);break}r[2]&&o.ops.pop(),o.trys.pop();continue}a=e.call(t,o)}catch(t){a=[6,t],i=0}finally{n=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,l])}}}(this,(function(t){switch(t.label){case 0:return[4,Promise.all([this.loadImg(this.imgFront,"/img/Regular/Front.svg"),this.loadImg(this.imgBack,"/img/Regular/Back.svg"),this.loadImg(this.img,this.imgSrc)])];case 1:return t.sent(),[2]}}))},new((n=void 0)||(n=Promise))((function(r,o){function s(t){try{l(i.next(t))}catch(t){o(t)}}function a(t){try{l(i.throw(t))}catch(t){o(t)}}function l(t){var e;t.done?r(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}l((i=i.apply(t,e||[])).next())}));var t,e,n,i},t.prototype.loadImg=function(t,e){return new Promise((function(n,i){t.onload=function(){return n()},t.onerror=function(){return i()},t.src=e}))},t}(),r=function(){function t(t){void 0===t&&(t=[]),this.tiles=t}return t.prototype.push=function(t){this.tiles.push(t)},t.prototype.pop=function(){return this.tiles.pop()},t.prototype.getTiles=function(){return this.tiles},t}(),o=function(){function t(t){void 0===t&&(t=""),this.tiles=[];for(var e=0;2*e+1<t.length;e++){var n=t.substring(2*e,2*(e+1));"m"===n[0]?this.tiles.push(new i(1,Number(n[1]),!1)):"p"===n[0]?this.tiles.push(new i(2,Number(n[1]),!1)):"s"===n[0]?this.tiles.push(new i(3,Number(n[1]),!1)):"w"===n[0]?this.tiles.push(new i(4,Number(n[1]),!1)):"d"===n[0]&&this.tiles.push(new i(5,Number(n[1]),!1))}}return t.prototype.length=function(){return this.tiles.length},t.prototype.push=function(t){this.tiles.push(t)},t.prototype.pop=function(){return this.tiles.pop()},t.prototype.find=function(t,e){for(var n,i=void 0,r=0;r<this.tiles.length;r++)if(this.tiles[r].getFamily()===t&&this.tiles[r].getValue()===e){i=r;break}if(void 0!==i){n=[this.tiles[0],this.tiles[i]],this.tiles[i]=n[0],this.tiles[0]=n[1];var o=this.tiles.shift();return this.sort(),o}},t.prototype.eject=function(t){var e;e=[this.tiles[t],this.tiles[0]],this.tiles[0]=e[0],this.tiles[t]=e[1];var n=this.tiles.shift();return this.sort(),n},t.prototype.sort=function(){this.tiles.sort((function(t,e){return t.isLessThan(e)?-1:1}))},t.prototype.count=function(t,e){var n=0;return this.tiles.forEach((function(i){i.getFamily()===t&&i.getValue()===e&&n++})),n},t.prototype.toGroup=function(t){if(void 0===t&&(t=!1),!(this.tiles.length>0))return[];var e=this.tiles.pop(),n=this.count(e.getFamily(),e.getValue());if(n>=1&&!t){var i=this.find(e.getFamily(),e.getValue()),o=this.toGroup(!0);if(this.tiles.push(i),this.sort(),void 0!==o)return this.tiles.push(e),this.sort(),o.push(new r([e,i])),o}if(n>=2){i=this.find(e.getFamily(),e.getValue());var s=this.find(e.getFamily(),e.getValue());if(o=this.toGroup(t),this.tiles.push(i),this.tiles.push(s),this.sort(),void 0!==o)return o.push(new r([e,i,s])),this.tiles.push(e),this.sort(),o}if(this.count(e.getFamily(),e.getValue()-1)*this.count(e.getFamily(),e.getValue()-2)>0&&(i=this.find(e.getFamily(),e.getValue()-1),s=this.find(e.getFamily(),e.getValue()-2),o=this.toGroup(t),this.tiles.push(i),this.tiles.push(s),this.sort(),void 0!==o))return o.push(new r([s,i,e])),this.tiles.push(e),this.sort(),o;this.tiles.push(e),this.tiles.sort()},t.prototype.drawHand=function(t,e,n,i,r,o,s,a){void 0===o&&(o=void 0),void 0===s&&(s=!1),void 0===a&&(a=0);for(var l=(75+i)*r,u=Math.cos(a)*l,c=Math.sin(a)*l,h=0;h<this.tiles.length;h++)h===o?this.tiles[h].drawTile(t,e+h*u+25*r*Math.sin(a),n+h*c-25*r*Math.cos(a),r,s,a):this.tiles[h].drawTile(t,e+h*u,n+h*c,r,s,a)},t.prototype.preload=function(){return t=this,e=void 0,i=function(){return function(t,e){var n,i,r,o={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(l){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(o=0)),o;)try{if(n=1,i&&(r=2&a[0]?i.return:a[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,a[1])).done)return r;switch(i=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,i=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!((r=(r=o.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){o.label=a[1];break}if(6===a[0]&&o.label<r[1]){o.label=r[1],r=a;break}if(r&&o.label<r[2]){o.label=r[2],o.ops.push(a);break}r[2]&&o.ops.pop(),o.trys.pop();continue}a=e.call(t,o)}catch(t){a=[6,t],i=0}finally{n=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,l])}}}(this,(function(t){switch(t.label){case 0:return[4,Promise.all(this.tiles.map((function(t){return t.preloadImg()})))];case 1:return t.sent(),[2]}}))},new((n=void 0)||(n=Promise))((function(r,o){function s(t){try{l(i.next(t))}catch(t){o(t)}}function a(t){try{l(i.throw(t))}catch(t){o(t)}}function l(t){var e;t.done?r(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}l((i=i.apply(t,e||[])).next())}));var t,e,n,i},t.prototype.cleanup=function(){this.tiles.forEach((function(t){return t.cleanup()})),this.tiles=[]},t}(),s=function(){function t(t){this.tiles=[],this.initTiles(t)}return t.prototype.displayFamilies=function(t,e,n,i,r,o){void 0===r&&(r=0),void 0===o&&(o=0);for(var s=e,a=n,l=1;l<6;l++)if(l<4){for(var u=1;u<10;u++)this.find(l,u).drawTile(t,s,a,i),s+=(75+r)*i;s=e,a+=(100+o)*i}else if(4===l){for(u=1;u<5;u++)this.find(l,u).drawTile(t,s,a,i),s+=(75+r)*i;s=e,a+=(100+o)*i}else if(5===l)for(u=1;u<4;u++)this.find(l,u).drawTile(t,s,a,i),s+=(75+r)*i},t.prototype.length=function(){return this.tiles.length},t.prototype.pop=function(){return this.tiles.length,this.tiles.pop()},t.prototype.push=function(t){this.tiles.push(t)},t.prototype.find=function(t,e){for(var n,i=void 0,r=0;r<this.tiles.length;r++)this.tiles[r].getFamily()===t&&this.tiles[r].getValue()===e&&(i=r);return void 0!==i?(n=[this.tiles[0],this.tiles[i]],this.tiles[i]=n[0],this.tiles[0]=n[1],this.tiles.shift()):void 0},t.prototype.count=function(t,e){for(var n=0,i=0;i<this.tiles.length;i++)this.tiles[i].getFamily()===t&&this.tiles[i].getValue()===e&&n++;return n},t.prototype.shuffle=function(){for(var t,e=[];this.tiles.length>0;){var n=Math.floor(Math.random()*this.tiles.length);t=[this.tiles[0],this.tiles[n]],this.tiles[n]=t[0],this.tiles[0]=t[1],e.push(this.tiles.shift())}this.tiles=e},t.prototype.getRandomHand=function(){var t=new o;this.shuffle();for(var e=0;e<13;e++)t.push(this.pop());return t},t.prototype.cleanup=function(){this.tiles.forEach((function(t){return t.cleanup()})),this.tiles=[]},t.prototype.preload=function(){return t=this,e=void 0,i=function(){var t;return function(t,e){var n,i,r,o={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(l){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(o=0)),o;)try{if(n=1,i&&(r=2&a[0]?i.return:a[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,a[1])).done)return r;switch(i=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,i=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!((r=(r=o.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){o.label=a[1];break}if(6===a[0]&&o.label<r[1]){o.label=r[1],r=a;break}if(r&&o.label<r[2]){o.label=r[2],o.ops.push(a);break}r[2]&&o.ops.pop(),o.trys.pop();continue}a=e.call(t,o)}catch(t){a=[6,t],i=0}finally{n=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,l])}}}(this,(function(e){switch(e.label){case 0:t=0,e.label=1;case 1:return t<this.tiles.length?[4,this.tiles[t].preloadImg()]:[3,4];case 2:e.sent(),e.label=3;case 3:return t++,[3,1];case 4:return[2]}}))},new((n=void 0)||(n=Promise))((function(r,o){function s(t){try{l(i.next(t))}catch(t){o(t)}}function a(t){try{l(i.throw(t))}catch(t){o(t)}}function l(t){var e;t.done?r(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}l((i=i.apply(t,e||[])).next())}));var t,e,n,i},t.prototype.initTiles=function(t){for(var e=1;e<6;e++)if(e<4)for(var n=1;n<10;n++)this.tiles.push(new i(e,n,!1)),this.tiles.push(new i(e,n,!1)),this.tiles.push(new i(e,n,!1)),5===n&&t?this.tiles.push(new i(e,n,!0)):this.tiles.push(new i(e,n,!1));else if(4===e)for(n=1;n<5;n++)for(var r=0;r<4;r++)this.tiles.push(new i(e,n,!1));else if(5===e)for(n=1;n<4;n++)for(r=0;r<4;r++)this.tiles.push(new i(e,n,!1))},t}(),a=function(){function t(t){void 0===t&&(t=!1),this.deck=new s(t),this.hands=[this.deck.getRandomHand(),this.deck.getRandomHand(),this.deck.getRandomHand(),this.deck.getRandomHand()],this.discards=[[],[],[],[]]}return t.prototype.getDeck=function(){return this.deck},t.prototype.getHands=function(){return this.hands},t.prototype.drawGame=function(t,e,n,i){void 0===i&&(i=void 0);var r=3.141592;this.hands[0].drawHand(t,140.625,1e3-150*n,5*n,.75,i,!1,0),this.hands[1].drawHand(t,1e3-150*n,1e3-375*n,5*n,n,void 0,!0,-r/2),this.hands[2].drawHand(t,1e3-375*n,150*n,5*n,n,void 0,!0,-r),this.hands[3].drawHand(t,150*n,375*n,5*n,n,void 0,!0,r/2);for(var o=0;o<4;o++)this.drawDiscrad(t,o,e)},t.prototype.pick=function(t){this.hands[t].push(this.deck.pop())},t.prototype.discard=function(t,e){var n=this.hands[t].eject(e);n.setTilt(),this.discards[t].push(n)},t.prototype.preload=function(){return t=this,e=void 0,i=function(){return function(t,e){var n,i,r,o={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(l){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(o=0)),o;)try{if(n=1,i&&(r=2&a[0]?i.return:a[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,a[1])).done)return r;switch(i=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,i=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!((r=(r=o.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){o.label=a[1];break}if(6===a[0]&&o.label<r[1]){o.label=r[1],r=a;break}if(r&&o.label<r[2]){o.label=r[2],o.ops.push(a);break}r[2]&&o.ops.pop(),o.trys.pop();continue}a=e.call(t,o)}catch(t){a=[6,t],i=0}finally{n=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,l])}}}(this,(function(t){switch(t.label){case 0:return[4,this.deck.preload()];case 1:return t.sent(),[4,Promise.all(this.hands.map((function(t){return t.preload()})))];case 2:return t.sent(),[2]}}))},new((n=void 0)||(n=Promise))((function(r,o){function s(t){try{l(i.next(t))}catch(t){o(t)}}function a(t){try{l(i.throw(t))}catch(t){o(t)}}function l(t){var e;t.done?r(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}l((i=i.apply(t,e||[])).next())}));var t,e,n,i},t.prototype.drawDiscrad=function(t,e,n){var i=3.141592;t.save(),t.translate(525,525),t.rotate([0,-i/2,-i,i/2][e]);for(var r=475*-n/2,o=242.5*n,s=0;s<this.discards[e].length;s++)s<12?this.discards[e][s].drawTile(t,r+s%6*80*n,o+105*Math.floor(s/6)*n,n):this.discards[e][s].drawTile(t,r+80*(s-12)*n,o+210*n,n);t.restore()},t}(),l=function(t,e,n,i){return new(n||(n=Promise))((function(r,o){function s(t){try{l(i.next(t))}catch(t){o(t)}}function a(t){try{l(i.throw(t))}catch(t){o(t)}}function l(t){var e;t.done?r(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}l((i=i.apply(t,e||[])).next())}))},u=function(t,e){var n,i,r,o={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(l){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(o=0)),o;)try{if(n=1,i&&(r=2&a[0]?i.return:a[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,a[1])).done)return r;switch(i=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,i=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!((r=(r=o.trys).length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){o.label=a[1];break}if(6===a[0]&&o.label<r[1]){o.label=r[1],r=a;break}if(r&&o.label<r[2]){o.label=r[2],o.ops.push(a);break}r[2]&&o.ops.pop(),o.trys.pop();continue}a=e.call(t,o)}catch(t){a=[6,t],i=0}finally{n=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,l])}}},c=1e3/30,h=[],f=[],p=0,d=0,y=0,v=[],g=document.getElementById("myCanvas"),m=g.getContext("2d"),w=document.createElement("canvas"),b=w.getContext("2d");function k(i){if(n=requestAnimationFrame(k),0!==p&&i-d>500&&t.getDeck().length()>0){d=i;var r=Math.floor(Math.random()*t.getHands()[p].length());null==t||t.discard(p,r),p=(p+1)%4,null==t||t.pick(p),null==t||t.getHands()[0].sort()}var o=i-y;o<c||(y=i-o%c,m&&(b.clearRect(0,0,g.width,g.height),b.fillStyle="#007730",b.fillRect(50,50,1e3,1e3),null==t||t.drawGame(b,.65,.6,e),m.clearRect(0,0,g.width,g.height),m.drawImage(w,0,0)))}function x(){cancelAnimationFrame(n),v.forEach((function(t){return t()}))}w.width=g.width,w.height=g.height,"undefined"!=typeof window&&function(){return l(this,void 0,void 0,(function(){return u(this,(function(n){switch(n.label){case 0:return m?(h.push(),f.push(),t=new a,[4,Promise.all(h.map((function(t){return function(t){return l(this,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,t.preload()];case 1:return e.sent(),[2]}}))}))}(t)})))]):(console.error("Context canvas indisponible"),[2]);case 1:return n.sent(),[4,Promise.all(f.map((function(t){return function(t){return l(this,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,t.preload()];case 1:return e.sent(),[2]}}))}))}(t)})))];case 2:return n.sent(),[4,null==t?void 0:t.preload()];case 3:return n.sent(),t.pick(0),t.getHands()[0].sort(),i=function(n){var i=g.getBoundingClientRect(),r=n.clientX-i.left-140.625,o=n.clientY-i.top,s=Math.floor(r/60);e=r-80*s*.75<=75&&s>=0&&s<t.getHands()[0].length()&&o>=910&&o<=985?s:void 0},r=function(n){0===p&&void 0!==e&&t.getDeck().length()>0&&(null==t||t.discard(0,e),p++,null==t||t.pick(p))},v.push((function(){g.removeEventListener("mousemove",i),g.removeEventListener("mousedown",r)})),g.addEventListener("mousemove",i),g.addEventListener("mousedown",r),requestAnimationFrame(k),window.cleanup=x,[2]}var i,r}))}))}().catch(console.error)})(); |