1 line
No EOL
11 KiB
JavaScript
1 line
No EOL
11 KiB
JavaScript
(()=>{"use strict";class t{constructor(t,i,e){this.family=t,this.value=i,this.red=e,this.imgFront=new Image,this.imgBack=new Image,this.imgGray=new Image,this.img=new Image,this.imgSrc="",this.tilt=0,this.setImgSrc()}getFamily(){return this.family}getValue(){return this.value}isEqual(t,i){return this.family===t&&this.value===i}isRed(){return this.red}compare(t){return this.family!==t.family?this.family<t.family?-1:1:this.value!==t.value?this.value<t.value?-1:1:0}isLessThan(t){return this.family<t.family||this.family===t.family&&this.value<=t.value}setTilt(){this.tilt=.04*(1-2*Math.random())}drawTile(t,i,e,s,l=!1,n=0,r=!1,h=!0){const a=75*s,o=100*s,u=a/2,c=o/2;t.save(),t.translate(i+u,e+c),t.rotate(n+(h?this.tilt:0));const f=-.92*a/2,d=-.92*o/2;if(t.drawImage(this.imgGray,f,d,a,o),l)t.drawImage(this.imgBack,-u,-c,a,o);else{t.drawImage(this.imgFront,-u,-c,a,o);const i=.9,e=a*i,l=o*i,n=-68*s/2,h=-90*s/2;t.drawImage(this.img,n,h,e,l),r&&t.drawImage(this.imgGray,-u,-c,a,o)}t.restore()}cleanup(){[this.imgFront,this.imgBack,this.imgGray,this.img].forEach((t=>{t.onload=null,t.onerror=null}))}preloadImg(){return t=this,i=void 0,s=function*(){const t=[{img:this.imgFront,src:"img/Regular/Front.svg"},{img:this.imgBack,src:"img/Regular/Back.svg"},{img:this.imgGray,src:"img/Regular/Gray.svg"},{img:this.img,src:this.imgSrc}];yield Promise.all(t.map((({img:t,src:i})=>this.loadImg(t,i))))},new((e=void 0)||(e=Promise))((function(l,n){function r(t){try{a(s.next(t))}catch(t){n(t)}}function h(t){try{a(s.throw(t))}catch(t){n(t)}}function a(t){var i;t.done?l(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(r,h)}a((s=s.apply(t,i||[])).next())}));var t,i,e,s}loadImg(t,i){return new Promise(((e,s)=>{t.onload=()=>e(),t.onerror=()=>s(),t.src=i}))}setImgSrc(){if(this.imgSrc="img/Regular/",this.family<=3){const t=["","Man","Pin","Sou"];this.imgSrc+=t[this.family]+String(this.value),this.red&&(this.imgSrc+="-Dora")}else if(4===this.family){const t=["","Ton","Nan","Shaa","Pei"];this.imgSrc+=t[this.value]}else if(5===this.family){const t=["","Chun","Hatsu","Haku"];this.imgSrc+=t[this.value]}this.imgSrc+=".svg"}}class i{constructor(t,i,e){this.tiles=t,this.stolenFrom=i,this.belongsTo=e}push(t){this.tiles.push(t)}pop(){return this.tiles.pop()}getTiles(){return this.tiles}compare(t){const i=this.tiles[0].compare(t.tiles[0]);return 0!==i?i:this.tiles[1].compare(t.tiles[1])}drawGroup(t,i,e,s,l,n,r){t.save(),t.translate(525,525),t.rotate(n),t.translate(-525,-525);const h=75*l,a=90*l,o=25*l/2,u=(this.belongsTo-this.stolenFrom-1+4)%4,c=void 0===r?-1:r.getFamily(),f=void 0===r?0:r.getValue(),d=(i,e,s,n)=>{i.drawTile(t,e,s,l,!1,n,i.isEqual(c,f))},g=Math.PI/2;switch(u){case 0:d(this.tiles[0],i,e+o,g),d(this.tiles[1],i+a,e,0),d(this.tiles[2],i+a+h+s*l,e,0);break;case 1:d(this.tiles[0],i,e,0),d(this.tiles[1],i+a,e+o,-g),d(this.tiles[2],i+a+h+3*s*l,e,0);break;case 2:d(this.tiles[0],i,e,0),d(this.tiles[1],i+h+s*l,e,0),d(this.tiles[2],i+a+h+s*l,e+o,-g);break;default:console.error(`Position non prise en charge: ${u}`)}t.restore()}}const e="m",s=1,l="p",n=2,r="s",h=3,a="w",o=4,u="d",c=5;class f{constructor(i){this.tiles=i.map((i=>new t(i.getFamily(),i.getValue(),i.isRed()))),this.sort()}sort(){this.tiles.sort(((t,i)=>t.isLessThan(i)?-1:1))}find(t,i){const e=this.findTileIndex(t,i);if(-1!==e){[this.tiles[e],this.tiles[0]]=[this.tiles[0],this.tiles[e]];const t=this.tiles.shift();return this.sort(),t}}findTileIndex(t,i){return this.tiles.findIndex((e=>e.getFamily()===t&&e.getValue()===i))}count(t,i){return this.tiles.filter((e=>e.getFamily()===t&&e.getValue()===i)).length}findGroups(t=!1){if(0===this.tiles.length)return[];const i=this.tiles.pop(),e=i.getFamily(),s=i.getValue();if(this.count(e,s)>=1&&!t){const t=this.tryFormPair(i);if(t)return t}if(this.count(e,s)>=2){const e=this.tryFormTriplet(i,t);if(e)return e}if(e<=3){const l=this.count(e,s-1)>0,n=this.count(e,s-2)>0;if(l&&n){const e=this.tryFormSequence(i,t);if(e)return e}}this.tiles.push(i),this.sort()}tryFormPair(t){const e=this.find(t.getFamily(),t.getValue()),s=this.findGroups(!0);if(this.tiles.push(e),this.sort(),void 0!==s)return this.tiles.push(t),this.sort(),s.push(new i([t,e],0,0)),s}tryFormTriplet(t,e){const s=this.find(t.getFamily(),t.getValue()),l=this.find(t.getFamily(),t.getValue()),n=this.findGroups(e);if(this.tiles.push(s),this.tiles.push(l),this.sort(),void 0!==n)return n.push(new i([t,s,l],0,0)),this.tiles.push(t),this.sort(),n}tryFormSequence(t,e){const s=this.find(t.getFamily(),t.getValue()-1),l=this.find(t.getFamily(),t.getValue()-2),n=this.findGroups(e);if(this.tiles.push(s),this.tiles.push(l),this.sort(),void 0!==n)return n.push(new i([l,s,t],0,0)),this.tiles.push(t),this.sort(),n}}class d{constructor(t=""){this.isolate=!1,this.tiles=[],this.initializeFromString(t)}initializeFromString(t){for(let i=0;i<t.length-1;i++){const e=t.substring(i,i+2),s=e[0],l=Number(e[1]);this.isValidTileCode(s,l)&&this.addTileFromCode(s,l)}}isValidTileCode(t,i){return t===e||t===l||t===r||t===a||t===u}addTileFromCode(i,f){const d={[e]:s,[l]:n,[r]:h,[a]:o,[u]:c}[i];void 0!==d&&this.tiles.push(new t(d,f,!1))}getTiles(){return this.tiles}length(){return this.tiles.length}push(t){this.tiles.push(t)}pop(){return this.tiles.pop()}find(t,i){const e=this.findTileIndex(t,i);if(-1!==e){[this.tiles[e],this.tiles[0]]=[this.tiles[0],this.tiles[e]];const t=this.tiles.shift();return this.sort(),t}}findTileIndex(t,i){return this.tiles.findIndex((e=>e.getFamily()===t&&e.getValue()===i))}eject(t){if(t<0||t>=this.tiles.length)throw new Error("Invalid tile index");[this.tiles[0],this.tiles[t]]=[this.tiles[t],this.tiles[0]];const i=this.tiles.shift();return this.sort(),i}get(t){if(!(void 0===t||t<0||t>=this.tiles.length))return this.tiles[t]}sort(){this.tiles.sort(((t,i)=>t.isLessThan(i)?-1:1))}count(t,i){return this.tiles.filter((e=>e.getFamily()===t&&e.getValue()===i)).length}toGroup(t=!1){return new f(this.tiles).findGroups(t)}drawHand(t,i,e,s,l,n=void 0,r=!1,h=0){const a=(75+s)*l,o=Math.cos(h)*a,u=Math.sin(h)*a;for(let s=0;s<this.tiles.length;s++){const a=s===this.tiles.length-1&&this.isolate?10:0;let c=i+s*o+a*l*Math.cos(h),f=e+s*u+a*l*Math.sin(h);s===n&&(c+=25*l*Math.sin(h),f-=25*l*Math.cos(h)),this.tiles[s].drawTile(t,c,f,l,r,h)}}preload(){return t=this,i=void 0,s=function*(){yield Promise.all(this.tiles.map((t=>t.preloadImg())))},new((e=void 0)||(e=Promise))((function(l,n){function r(t){try{a(s.next(t))}catch(t){n(t)}}function h(t){try{a(s.throw(t))}catch(t){n(t)}}function a(t){var i;t.done?l(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(r,h)}a((s=s.apply(t,i||[])).next())}));var t,i,e,s}cleanup(){this.tiles.forEach((t=>t.cleanup())),this.tiles=[]}}class g{constructor(t=!1){this.tiles=[],this.tileIndexMap=new Map,this.initTiles(t)}displayFamilies(t,i,e,s,l=0,n=0){let r=i,h=e;const a=[{family:1,start:1,end:9},{family:2,start:1,end:9},{family:3,start:1,end:9},{family:4,start:1,end:4},{family:5,start:1,end:3}];for(const e of a){for(let i=e.start;i<=e.end;i++){const n=this.find(e.family,i);n&&(n.drawTile(t,r,h,s,!1,0,!1),r+=(75+l)*s)}r=i,h+=(100+n)*s}}length(){return this.tiles.length}pop(){if(0===this.tiles.length)throw new Error("Cannot pop from an empty deck");const t=this.tiles.pop(),i=this.getTileKey(t.getFamily(),t.getValue()),e=this.tileIndexMap.get(i);return e&&e.length>0&&(e.pop(),0===e.length?this.tileIndexMap.delete(i):this.tileIndexMap.set(i,e)),t}push(t){const i=this.tiles.length;this.tiles.push(t);const e=this.getTileKey(t.getFamily(),t.getValue()),s=this.tileIndexMap.get(e)||[];s.push(i),this.tileIndexMap.set(e,s)}find(t,i){const e=this.getTileKey(t,i),s=this.tileIndexMap.get(e);if(!s||0===s.length)return;const l=s[0];if(l>=this.tiles.length)return this.rebuildIndexMap(),this.find(t,i);[this.tiles[l],this.tiles[0]]=[this.tiles[0],this.tiles[l]],this.updateIndicesAfterSwap(0,l);const n=this.tiles.shift();return this.decrementIndicesAfterShift(),n}count(t,i){const e=this.getTileKey(t,i),s=this.tileIndexMap.get(e);return s?s.length:0}shuffle(){for(let t=this.tiles.length-1;t>0;t--){const i=Math.floor(Math.random()*(t+1));[this.tiles[t],this.tiles[i]]=[this.tiles[i],this.tiles[t]]}this.rebuildIndexMap()}getRandomHand(){const t=new d;if(this.shuffle(),this.tiles.length<13)throw new Error("Not enough tiles in deck to create a hand");for(let i=0;i<13;i++)t.push(this.pop());return t}cleanup(){this.tiles.forEach((t=>t.cleanup())),this.tiles=[],this.tileIndexMap.clear()}preload(){return t=this,i=void 0,s=function*(){const t=this.tiles.map((t=>t.preloadImg()));yield Promise.all(t)},new((e=void 0)||(e=Promise))((function(l,n){function r(t){try{a(s.next(t))}catch(t){n(t)}}function h(t){try{a(s.throw(t))}catch(t){n(t)}}function a(t){var i;t.done?l(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(r,h)}a((s=s.apply(t,i||[])).next())}));var t,i,e,s}getTileKey(t,i){return`${t}-${i}`}updateIndicesAfterSwap(t,i){if(t===i)return;const e=this.tiles[t],s=this.tiles[i],l=this.getTileKey(e.getFamily(),e.getValue()),n=this.getTileKey(s.getFamily(),s.getValue()),r=this.tileIndexMap.get(l)||[],h=this.tileIndexMap.get(n)||[],a=r.indexOf(i),o=h.indexOf(t);-1!==a&&(r[a]=t),-1!==o&&(h[o]=i),this.tileIndexMap.set(l,r),this.tileIndexMap.set(n,h)}decrementIndicesAfterShift(){var t;for(const[i,e]of this.tileIndexMap.entries())this.tileIndexMap.set(i,e.filter((t=>0!==t)).map((t=>t>0?t-1:t))),0===(null===(t=this.tileIndexMap.get(i))||void 0===t?void 0:t.length)&&this.tileIndexMap.delete(i)}rebuildIndexMap(){this.tileIndexMap.clear();for(let t=0;t<this.tiles.length;t++){const i=this.tiles[t],e=this.getTileKey(i.getFamily(),i.getValue()),s=this.tileIndexMap.get(e)||[];s.push(t),this.tileIndexMap.set(e,s)}}initTiles(i){for(let e=1;e<=3;e++)for(let s=1;s<=9;s++){const l=5===s&&i;for(let i=0;i<3;i++)this.push(new t(e,s,!1));this.push(new t(e,s,l))}for(let i=1;i<=4;i++)for(let e=0;e<4;e++)this.push(new t(4,i,!1));for(let i=1;i<=3;i++)for(let e=0;e<4;e++)this.push(new t(5,i,!1))}}var m=function(t,i,e,s){return new(e||(e=Promise))((function(l,n){function r(t){try{a(s.next(t))}catch(t){n(t)}}function h(t){try{a(s.throw(t))}catch(t){n(t)}}function a(t){var i;t.done?l(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(r,h)}a((s=s.apply(t,i||[])).next())}))};!function(){m(this,void 0,void 0,(function*(){const t=document.getElementById("myCanvas");if(t){const i=t.getContext("2d");if(i){i.fillStyle="#007730",i.fillRect(50,50,1e3,1e3);let t=180,e=80,s=.75,l=40,n=100;const r=new g(!1);yield function(t){return m(this,void 0,void 0,(function*(){yield t.preload()}))}(r),r.displayFamilies(i,t,e,s,l,n),i.fillStyle="#DFDFFF",i.font="30px serif";let h=50*s,a=30*s;i.fillText("Caractère:",55,e+h+a),i.fillText("Rond:",55,e+3*h+n*s+a),i.fillText("Bambou:",55,e+5*h+2*n*s+a),i.fillText("Vent:",55,e+7*h+3*n*s+a),i.fillText("Dragon:",55,e+9*h+4*n*s+a);for(let r=0;r<3;r++)for(let h=0;h<9;h++)i.fillText(String(h+1),t+h*(l+75)*s+30*s,e+r*(n+100)*s+150*s);i.fillText("Est",t+0*(l+75)*s,e+3*(n+100)*s+150*s),i.fillText("Sud",t+1*(l+75)*s,e+3*(n+100)*s+150*s),i.fillText("Ouest",t+2*(l+75)*s,e+3*(n+100)*s+150*s),i.fillText("Nord",t+3*(l+75)*s,e+3*(n+100)*s+150*s),i.fillText("Rouge",t+0*(l+75)*s,e+4*(n+100)*s+150*s),i.fillText("Vert",t+1*(l+75)*s,e+4*(n+100)*s+150*s),i.fillText("Blanc",t+2*(l+75)*s,e+4*(n+100)*s+150*s),window.cleanup=()=>{r.cleanup()}}else console.error("Impossible d'obtenir le contexte du canvas.")}else console.error("Canvas introuvable dans le DOM.")}))}()})(); |