1 line
No EOL
15 KiB
JavaScript
1 line
No EOL
15 KiB
JavaScript
(()=>{"use strict";var t,e=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}(),i=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}(),n=function(){function t(t){void 0===t&&(t=""),this.tiles=[];for(var i=0;2*i+1<t.length;i++){var n=t.substring(2*i,2*(i+1));"m"===n[0]?this.tiles.push(new e(1,Number(n[1]),!1)):"p"===n[0]?this.tiles.push(new e(2,Number(n[1]),!1)):"s"===n[0]?this.tiles.push(new e(3,Number(n[1]),!1)):"w"===n[0]?this.tiles.push(new e(4,Number(n[1]),!1)):"d"===n[0]&&this.tiles.push(new e(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 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}},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 i=this.tiles.shift();return this.sort(),i},t.prototype.sort=function(){this.tiles.sort((function(t,e){return t.isLessThan(e)?-1:1}))},t.prototype.count=function(t,e){var i=0;return this.tiles.forEach((function(n){n.getFamily()===t&&n.getValue()===e&&i++})),i},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 r=this.find(e.getFamily(),e.getValue()),o=this.toGroup(!0);if(this.tiles.push(r),this.sort(),void 0!==o)return this.tiles.push(e),this.sort(),o.push(new i([e,r])),o}if(n>=2){r=this.find(e.getFamily(),e.getValue());var s=this.find(e.getFamily(),e.getValue());if(o=this.toGroup(t),this.tiles.push(r),this.tiles.push(s),this.sort(),void 0!==o)return o.push(new i([e,r,s])),this.tiles.push(e),this.sort(),o}if(this.count(e.getFamily(),e.getValue()-1)*this.count(e.getFamily(),e.getValue()-2)>0&&(r=this.find(e.getFamily(),e.getValue()-1),s=this.find(e.getFamily(),e.getValue()-2),o=this.toGroup(t),this.tiles.push(r),this.tiles.push(s),this.sort(),void 0!==o))return o.push(new i([s,r,e])),this.tiles.push(e),this.sort(),o;this.tiles.push(e),this.tiles.sort()},t.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)},t.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},t.prototype.cleanup=function(){this.tiles.forEach((function(t){return t.cleanup()})),this.tiles=[]},t}(),r=function(){function t(t){this.tiles=[],this.initTiles(t)}return t.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},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 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},t.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},t.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},t.prototype.getRandomHand=function(){var t=new n;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,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},t.prototype.initTiles=function(t){for(var i=1;i<6;i++)if(i<4)for(var n=1;n<10;n++)this.tiles.push(new e(i,n,!1)),this.tiles.push(new e(i,n,!1)),this.tiles.push(new e(i,n,!1)),5===n&&t?this.tiles.push(new e(i,n,!0)):this.tiles.push(new e(i,n,!1));else if(4===i)for(n=1;n<5;n++)for(var r=0;r<4;r++)this.tiles.push(new e(i,n,!1));else if(5===i)for(n=1;n<4;n++)for(r=0;r<4;r++)this.tiles.push(new e(i,n,!1))},t}(),o=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())}))},s=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])}}},l=1e3/30,a=[],u=[],h=void 0,c=0,f=[],p=document.getElementById("myCanvas"),d=p.getContext("2d"),y=document.createElement("canvas"),v=y.getContext("2d");function m(e){t=requestAnimationFrame(m);var i=e-c;i<l||(c=e-i%l,function(){if(d){v.clearRect(0,0,p.width,p.height),v.fillStyle="#007730",v.fillRect(50,50,1e3,1e3),v.fillStyle="#DFDFFF",v.font="50px serif",v.fillText("Chii:",75,220),u[0].drawHand(v,300,150,5,.75),u[1].drawHand(v,543.75,150,5,.75),v.fillText("Pon:",75,370),u[2].drawHand(v,300,300,5,.75),u[3].drawHand(v,543.75,300,5,.75),u[4].drawHand(v,787.5,450,5,.75),v.fillText("Invalide:",75,520),u[5].drawHand(v,300,450,5,.75),u[6].drawHand(v,543.75,450,5,.75),u[7].drawHand(v,787.5,450,5,.75),u[8].drawHand(v,100,800,5,.75,h);var t=u[8].toGroup();void 0!==t&&(v.fillStyle="#FF0000",v.font="50px serif",v.fillText("Tous les groupes sont formés !",100,750)),t=[],d.clearRect(0,0,p.width,p.height),d.drawImage(y,0,0)}}())}function g(){cancelAnimationFrame(t),f.forEach((function(t){return t()}))}y.width=p.width,y.height=p.height,"undefined"!=typeof window&&function(){return o(this,void 0,void 0,(function(){return s(this,(function(t){switch(t.label){case 0:return d?(a.push(new r(!1)),u.push(new n("s1s2s3"),new n("m2m3m4"),new n("p5p5p5"),new n("w2w2w2"),new n("d3d3d3"),new n("s4p5m6"),new n("m9s9p9"),new n("d1d2d3"),a[0].getRandomHand()),[4,Promise.all(a.map((function(t){return function(t){return o(this,void 0,void 0,(function(){return s(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 t.sent(),[4,Promise.all(u.map((function(t){return function(t){return o(this,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return[4,t.preload()];case 1:return e.sent(),[2]}}))}))}(t)})))];case 2:return t.sent(),u[8].push(a[0].pop()),u[8].sort(),e=function(t){var e=p.getBoundingClientRect(),i=t.clientX-e.left-100,n=t.clientY-e.top,r=Math.floor(i/60);h=i-80*r*.75<=75&&r>=0&&r<14&&n>=800&&n<=875?r:void 0},i=function(t){void 0!==h&&(a[0].push(u[8].eject(h)),a[0].shuffle(),u[8].push(a[0].pop()),u[8].sort())},f.push((function(){p.removeEventListener("mousemove",e),p.removeEventListener("mousedown",i)})),p.addEventListener("mousemove",e),p.addEventListener("mousedown",i),requestAnimationFrame(m),window.cleanup=g,[2]}var e,i}))}))}().catch(console.error)})(); |