1 line
No EOL
12 KiB
JavaScript
1 line
No EOL
12 KiB
JavaScript
(()=>{"use strict";var t=function(){function t(t,e,n){this.family=t,this.value=e,this.red=n,this.loadded=!1,this.imgSrc="",this.imgFront=new Image,this.imgBack=new Image,this.img=new Image,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.drawTile=function(t,e,n,i){t.drawImage(this.imgFront,e,n,75*i,100*i),t.drawImage(this.img,e,n,75*i,100*i)},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}(),e=function(){function e(e){void 0===e&&(e=""),this.tiles=[];for(var n=0;2*n+1<e.length;n++){var i=e.substring(2*n,2*(n+1));"m"===i[0]?this.tiles.push(new t(1,Number(i[1]),!1)):"p"===i[0]?this.tiles.push(new t(2,Number(i[1]),!1)):"s"===i[0]?this.tiles.push(new t(3,Number(i[1]),!1)):"w"===i[0]?this.tiles.push(new t(4,Number(i[1]),!1)):"d"===i[0]&&this.tiles.push(new t(5,Number(i[1]),!1))}}return e.prototype.push=function(t){this.tiles.push(t)},e.prototype.pop=function(){return this.tiles.pop()},e.prototype.sort=function(){for(var t,e=0;e<this.tiles.length;e++)for(var n=0;n<this.tiles.length-1;n++)this.tiles[n].isLessThan(this.tiles[n+1])||(t=[this.tiles[n+1],this.tiles[n]],this.tiles[n]=t[0],this.tiles[n+1]=t[1])},e.prototype.drawHand=function(t,e,n,i,r,o){void 0===o&&(o=void 0);for(var s=0;s<this.tiles.length;s++)s===o?this.tiles[s].drawTile(t,e+75*s*r+s*i*r,n-25*r,r):this.tiles[s].drawTile(t,e+75*s*r+s*i*r,n,r)},e.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},e.prototype.cleanup=function(){this.tiles.forEach((function(t){return t.cleanup()})),this.tiles=[]},e}(),n=function(){function n(t){this.tiles=[],this.initTiles(t)}return n.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},n.prototype.pop=function(){return this.tiles.length,this.tiles.pop()},n.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},n.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},n.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},n.prototype.getRandomHand=function(){var t=new e;this.shuffle();for(var n=0;n<13;n++)t.push(this.pop());return t},n.prototype.cleanup=function(){this.tiles.forEach((function(t){return t.cleanup()})),this.tiles=[]},n.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},n.prototype.initTiles=function(e){for(var n=1;n<6;n++)if(n<4)for(var i=1;i<10;i++)this.tiles.push(new t(n,i,!1)),this.tiles.push(new t(n,i,!1)),this.tiles.push(new t(n,i,!1)),5===i&&e?this.tiles.push(new t(n,i,!0)):this.tiles.push(new t(n,i,!1));else if(4===n)for(i=1;i<5;i++)for(var r=0;r<4;r++)this.tiles.push(new t(n,i,!1));else if(5===n)for(i=1;i<4;i++)for(r=0;r<4;r++)this.tiles.push(new t(n,i,!1))},n}(),i=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())}))},r=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])}}},o=document.getElementById("myCanvas");function s(t){return i(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,t.preload()];case 1:return e.sent(),[2]}}))}))}!function(){i(this,void 0,void 0,(function(){var t,e,a,l,u,c,h,f,p,d,y,v,g,m,w,b,x,I,S=this;return r(this,(function(k){switch(k.label){case 0:return o?(t=o.getContext("2d"))?((e=document.createElement("canvas")).width=o.width,e.height=o.height,a=e.getContext("2d"),l=0,u=1e3/30,c=150,h=150,f=75,p=.75,[4,s(d=new n(!1))]):[3,3]:[3,5];case 1:for(k.sent(),y=[],v=0;v<4;v++)(g=d.getRandomHand()).sort(),y.push(g);return[4,(m=new n(!1)).preload()];case 2:return k.sent(),(w=m.getRandomHand()).sort(),b=void 0,x=function(){return i(S,void 0,void 0,(function(){var n;return r(this,(function(i){for(a.clearRect(0,0,o.width,o.height),a.fillStyle="#007730",a.fillRect(50,50,1e3,1e3),a.fillStyle="#DFDFFF",a.font="50px serif",a.fillText("Exemples de main:",65,100),n=0;n<y.length;n++)y[n].drawHand(a,c,h+n*p*(100+f),5,p);return w.drawHand(a,c,800,5,p,b),t.clearRect(0,0,o.width,o.height),t.drawImage(e,0,0),[2]}))}))},I=function(t){t-l>=u&&(l=t,x()),requestAnimationFrame(I)},o.addEventListener("mousemove",(function(t){var e=o.getBoundingClientRect(),n=t.clientX-e.left-c,i=t.clientY-e.top,r=Math.floor(n/(80*p));b=n-80*r*p<=75&&r>=0&&r<14&&i>=800&&i<=800+100*p?r:void 0})),requestAnimationFrame(I),window.cleanup=function(){d.cleanup(),y.forEach((function(t){return t.cleanup()})),y=[],m.cleanup(),w.cleanup(),t.clearRect(0,0,o.width,o.height),a.clearRect(0,0,e.width,e.height),b=void 0},[3,4];case 3:console.error("Impossible d'obtenir le contexte du canvas."),k.label=4;case 4:return[3,6];case 5:console.error("Canvas introuvable dans le DOM."),k.label=6;case 6:return[2]}}))}))}()})(); |