1 line
No EOL
14 KiB
JavaScript
1 line
No EOL
14 KiB
JavaScript
(()=>{"use strict";var t=function(){function t(t,e,i){this.family=t,this.value=e,this.red=i,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,i,n,r,o){void 0===r&&(r=!1),void 0===o&&(o=0),t.save(),t.translate(e+75*n/2,i+100*n/2),t.rotate(o+this.tilt),r?t.drawImage(this.imgBack,-75*n/2,-100*n/2,75*n,100*n):(t.drawImage(this.imgFront,-75*n/2,-100*n/2,75*n,100*n),t.drawImage(this.img,-75*n/2,-100*n/2,75*n,100*n)),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,n=function(){return function(t,e){var i,n,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=l(0),s.throw=l(1),s.return=l(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function l(l){return function(a){return function(l){if(i)throw new TypeError("Generator is already executing.");for(;s&&(s=0,l[0]&&(o=0)),o;)try{if(i=1,n&&(r=2&l[0]?n.return:l[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,l[1])).done)return r;switch(n=0,r&&(l=[2&l[0],r.value]),l[0]){case 0:case 1:r=l;break;case 4:return o.label++,{value:l[1],done:!1};case 5:o.label++,n=l[1],l=[0];continue;case 7:l=o.ops.pop(),o.trys.pop();continue;default:if(!((r=(r=o.trys).length>0&&r[r.length-1])||6!==l[0]&&2!==l[0])){o=0;continue}if(3===l[0]&&(!r||l[1]>r[0]&&l[1]<r[3])){o.label=l[1];break}if(6===l[0]&&o.label<r[1]){o.label=r[1],r=l;break}if(r&&o.label<r[2]){o.label=r[2],o.ops.push(l);break}r[2]&&o.ops.pop(),o.trys.pop();continue}l=e.call(t,o)}catch(t){l=[6,t],n=0}finally{i=r=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,a])}}}(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((i=void 0)||(i=Promise))((function(r,o){function s(t){try{a(n.next(t))}catch(t){o(t)}}function l(t){try{a(n.throw(t))}catch(t){o(t)}}function a(t){var e;t.done?r(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(s,l)}a((n=n.apply(t,e||[])).next())}));var t,e,i,n},t.prototype.loadImg=function(t,e){return new Promise((function(i,n){t.onload=function(){return i()},t.onerror=function(){return n()},t.src=e}))},t}(),e=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}(),i=function(){function i(e){void 0===e&&(e=""),this.tiles=[];for(var i=0;2*i+1<e.length;i++){var n=e.substring(2*i,2*(i+1));"m"===n[0]?this.tiles.push(new t(1,Number(n[1]),!1)):"p"===n[0]?this.tiles.push(new t(2,Number(n[1]),!1)):"s"===n[0]?this.tiles.push(new t(3,Number(n[1]),!1)):"w"===n[0]?this.tiles.push(new t(4,Number(n[1]),!1)):"d"===n[0]&&this.tiles.push(new t(5,Number(n[1]),!1))}}return i.prototype.length=function(){return this.tiles.length},i.prototype.push=function(t){this.tiles.push(t)},i.prototype.pop=function(){return this.tiles.pop()},i.prototype.find=function(t,e){for(var i,n=void 0,r=0;r<this.tiles.length;r++)if(this.tiles[r].getFamily()===t&&this.tiles[r].getValue()===e){n=r;break}if(void 0!==n){i=[this.tiles[0],this.tiles[n]],this.tiles[n]=i[0],this.tiles[0]=i[1];var o=this.tiles.shift();return this.sort(),o}},i.prototype.eject=function(t){var e;e=[this.tiles[t],this.tiles[0]],this.tiles[0]=e[0],this.tiles[t]=e[1];var i=this.tiles.shift();return this.sort(),i},i.prototype.sort=function(){this.tiles.sort((function(t,e){return t.isLessThan(e)?-1:1}))},i.prototype.count=function(t,e){var i=0;return this.tiles.forEach((function(n){n.getFamily()===t&&n.getValue()===e&&i++})),i},i.prototype.toGroup=function(t){if(void 0===t&&(t=!1),!(this.tiles.length>0))return[];var i=this.tiles.pop(),n=this.count(i.getFamily(),i.getValue());if(n>=1&&!t){var r=this.find(i.getFamily(),i.getValue()),o=this.toGroup(!0);if(this.tiles.push(r),this.sort(),void 0!==o)return this.tiles.push(i),this.sort(),o.push(new e([i,r])),o}if(n>=2){r=this.find(i.getFamily(),i.getValue());var s=this.find(i.getFamily(),i.getValue());if(o=this.toGroup(t),this.tiles.push(r),this.tiles.push(s),this.sort(),void 0!==o)return o.push(new e([i,r,s])),this.tiles.push(i),this.sort(),o}if(this.count(i.getFamily(),i.getValue()-1)*this.count(i.getFamily(),i.getValue()-2)>0&&(r=this.find(i.getFamily(),i.getValue()-1),s=this.find(i.getFamily(),i.getValue()-2),o=this.toGroup(t),this.tiles.push(r),this.tiles.push(s),this.sort(),void 0!==o))return o.push(new e([s,r,i])),this.tiles.push(i),this.sort(),o;this.tiles.push(i),this.tiles.sort()},i.prototype.drawHand=function(t,e,i,n,r,o,s,l){void 0===o&&(o=void 0),void 0===s&&(s=!1),void 0===l&&(l=0);for(var a=(75+n)*r,u=Math.cos(l)*a,h=Math.sin(l)*a,c=0;c<this.tiles.length;c++)c===o?this.tiles[c].drawTile(t,e+c*u+25*r*Math.sin(l),i+c*h-25*r*Math.cos(l),r,s,l):this.tiles[c].drawTile(t,e+c*u,i+c*h,r,s,l)},i.prototype.preload=function(){return t=this,e=void 0,n=function(){return function(t,e){var i,n,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=l(0),s.throw=l(1),s.return=l(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function l(l){return function(a){return function(l){if(i)throw new TypeError("Generator is already executing.");for(;s&&(s=0,l[0]&&(o=0)),o;)try{if(i=1,n&&(r=2&l[0]?n.return:l[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,l[1])).done)return r;switch(n=0,r&&(l=[2&l[0],r.value]),l[0]){case 0:case 1:r=l;break;case 4:return o.label++,{value:l[1],done:!1};case 5:o.label++,n=l[1],l=[0];continue;case 7:l=o.ops.pop(),o.trys.pop();continue;default:if(!((r=(r=o.trys).length>0&&r[r.length-1])||6!==l[0]&&2!==l[0])){o=0;continue}if(3===l[0]&&(!r||l[1]>r[0]&&l[1]<r[3])){o.label=l[1];break}if(6===l[0]&&o.label<r[1]){o.label=r[1],r=l;break}if(r&&o.label<r[2]){o.label=r[2],o.ops.push(l);break}r[2]&&o.ops.pop(),o.trys.pop();continue}l=e.call(t,o)}catch(t){l=[6,t],n=0}finally{i=r=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,a])}}}(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((i=void 0)||(i=Promise))((function(r,o){function s(t){try{a(n.next(t))}catch(t){o(t)}}function l(t){try{a(n.throw(t))}catch(t){o(t)}}function a(t){var e;t.done?r(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(s,l)}a((n=n.apply(t,e||[])).next())}));var t,e,i,n},i.prototype.cleanup=function(){this.tiles.forEach((function(t){return t.cleanup()})),this.tiles=[]},i}(),n=function(){function e(t){this.tiles=[],this.initTiles(t)}return e.prototype.displayFamilies=function(t,e,i,n,r,o){void 0===r&&(r=0),void 0===o&&(o=0);for(var s=e,l=i,a=1;a<6;a++)if(a<4){for(var u=1;u<10;u++)this.find(a,u).drawTile(t,s,l,n),s+=(75+r)*n;s=e,l+=(100+o)*n}else if(4===a){for(u=1;u<5;u++)this.find(a,u).drawTile(t,s,l,n),s+=(75+r)*n;s=e,l+=(100+o)*n}else if(5===a)for(u=1;u<4;u++)this.find(a,u).drawTile(t,s,l,n),s+=(75+r)*n},e.prototype.length=function(){return this.tiles.length},e.prototype.pop=function(){return this.tiles.length,this.tiles.pop()},e.prototype.push=function(t){this.tiles.push(t)},e.prototype.find=function(t,e){for(var i,n=void 0,r=0;r<this.tiles.length;r++)this.tiles[r].getFamily()===t&&this.tiles[r].getValue()===e&&(n=r);return void 0!==n?(i=[this.tiles[0],this.tiles[n]],this.tiles[n]=i[0],this.tiles[0]=i[1],this.tiles.shift()):void 0},e.prototype.count=function(t,e){for(var i=0,n=0;n<this.tiles.length;n++)this.tiles[n].getFamily()===t&&this.tiles[n].getValue()===e&&i++;return i},e.prototype.shuffle=function(){for(var t,e=[];this.tiles.length>0;){var i=Math.floor(Math.random()*this.tiles.length);t=[this.tiles[0],this.tiles[i]],this.tiles[i]=t[0],this.tiles[0]=t[1],e.push(this.tiles.shift())}this.tiles=e},e.prototype.getRandomHand=function(){var t=new i;this.shuffle();for(var e=0;e<13;e++)t.push(this.pop());return t},e.prototype.cleanup=function(){this.tiles.forEach((function(t){return t.cleanup()})),this.tiles=[]},e.prototype.preload=function(){return t=this,e=void 0,n=function(){var t;return function(t,e){var i,n,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=l(0),s.throw=l(1),s.return=l(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function l(l){return function(a){return function(l){if(i)throw new TypeError("Generator is already executing.");for(;s&&(s=0,l[0]&&(o=0)),o;)try{if(i=1,n&&(r=2&l[0]?n.return:l[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,l[1])).done)return r;switch(n=0,r&&(l=[2&l[0],r.value]),l[0]){case 0:case 1:r=l;break;case 4:return o.label++,{value:l[1],done:!1};case 5:o.label++,n=l[1],l=[0];continue;case 7:l=o.ops.pop(),o.trys.pop();continue;default:if(!((r=(r=o.trys).length>0&&r[r.length-1])||6!==l[0]&&2!==l[0])){o=0;continue}if(3===l[0]&&(!r||l[1]>r[0]&&l[1]<r[3])){o.label=l[1];break}if(6===l[0]&&o.label<r[1]){o.label=r[1],r=l;break}if(r&&o.label<r[2]){o.label=r[2],o.ops.push(l);break}r[2]&&o.ops.pop(),o.trys.pop();continue}l=e.call(t,o)}catch(t){l=[6,t],n=0}finally{i=r=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,a])}}}(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((i=void 0)||(i=Promise))((function(r,o){function s(t){try{a(n.next(t))}catch(t){o(t)}}function l(t){try{a(n.throw(t))}catch(t){o(t)}}function a(t){var e;t.done?r(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(s,l)}a((n=n.apply(t,e||[])).next())}));var t,e,i,n},e.prototype.initTiles=function(e){for(var i=1;i<6;i++)if(i<4)for(var n=1;n<10;n++)this.tiles.push(new t(i,n,!1)),this.tiles.push(new t(i,n,!1)),this.tiles.push(new t(i,n,!1)),5===n&&e?this.tiles.push(new t(i,n,!0)):this.tiles.push(new t(i,n,!1));else if(4===i)for(n=1;n<5;n++)for(var r=0;r<4;r++)this.tiles.push(new t(i,n,!1));else if(5===i)for(n=1;n<4;n++)for(r=0;r<4;r++)this.tiles.push(new t(i,n,!1))},e}(),r=function(t,e,i,n){return new(i||(i=Promise))((function(r,o){function s(t){try{a(n.next(t))}catch(t){o(t)}}function l(t){try{a(n.throw(t))}catch(t){o(t)}}function a(t){var e;t.done?r(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(s,l)}a((n=n.apply(t,e||[])).next())}))},o=function(t,e){var i,n,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=l(0),s.throw=l(1),s.return=l(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function l(l){return function(a){return function(l){if(i)throw new TypeError("Generator is already executing.");for(;s&&(s=0,l[0]&&(o=0)),o;)try{if(i=1,n&&(r=2&l[0]?n.return:l[0]?n.throw||((r=n.return)&&r.call(n),0):n.next)&&!(r=r.call(n,l[1])).done)return r;switch(n=0,r&&(l=[2&l[0],r.value]),l[0]){case 0:case 1:r=l;break;case 4:return o.label++,{value:l[1],done:!1};case 5:o.label++,n=l[1],l=[0];continue;case 7:l=o.ops.pop(),o.trys.pop();continue;default:if(!((r=(r=o.trys).length>0&&r[r.length-1])||6!==l[0]&&2!==l[0])){o=0;continue}if(3===l[0]&&(!r||l[1]>r[0]&&l[1]<r[3])){o.label=l[1];break}if(6===l[0]&&o.label<r[1]){o.label=r[1],r=l;break}if(r&&o.label<r[2]){o.label=r[2],o.ops.push(l);break}r[2]&&o.ops.pop(),o.trys.pop();continue}l=e.call(t,o)}catch(t){l=[6,t],n=0}finally{i=r=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,a])}}},s=document.getElementById("myCanvas");function l(t){return r(this,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return[4,t.preload()];case 1:return e.sent(),[2]}}))}))}!function(){r(this,void 0,void 0,(function(){var t,e,i,a,u,h,c,f,p,d,y,g,v,m,w,b,x,F,I=this;return o(this,(function(k){switch(k.label){case 0:return s?(t=s.getContext("2d"))?((e=document.createElement("canvas")).width=s.width,e.height=s.height,i=e.getContext("2d"),a=0,u=1e3/30,h=100,c=150,f=75,p=.75,[4,l(d=new n(!1))]):[3,3]:[3,5];case 1:for(k.sent(),y=[],g=0;g<4;g++)(v=d.getRandomHand()).sort(),y.push(v);return[4,(m=new n(!1)).preload()];case 2:return k.sent(),(w=m.getRandomHand()).push(m.pop()),w.sort(),b=void 0,x=function(){return r(I,void 0,void 0,(function(){var n;return o(this,(function(r){for(i.clearRect(0,0,s.width,s.height),i.fillStyle="#007730",i.fillRect(50,50,1e3,1e3),i.fillStyle="#DFDFFF",i.font="50px serif",i.fillText("Exemples de main:",65,100),n=0;n<y.length;n++)y[n].drawHand(i,h,c+n*p*(100+f),5,p);return w.drawHand(i,h,800,5,p,b),t.clearRect(0,0,s.width,s.height),t.drawImage(e,0,0),[2]}))}))},F=function(t){t-a>=u&&(a=t,x()),requestAnimationFrame(F)},s.addEventListener("mousemove",(function(t){var e=s.getBoundingClientRect(),i=t.clientX-e.left-h,n=t.clientY-e.top,r=Math.floor(i/(80*p));b=i-80*r*p<=75&&r>=0&&r<14&&n>=800&&n<=800+100*p?r:void 0})),s.addEventListener("mousedown",(function(t){void 0!==b&&(m.push(w.eject(b)),m.shuffle(),w.push(m.pop()),w.sort())})),requestAnimationFrame(F),window.cleanup=function(){d.cleanup(),y.forEach((function(t){return t.cleanup()})),y=[],m.cleanup(),w.cleanup(),t.clearRect(0,0,s.width,s.height),i.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]}}))}))}()})(); |