1 line
No EOL
15 KiB
JavaScript
1 line
No EOL
15 KiB
JavaScript
(()=>{"use strict";class t{constructor(t,i,s){this.family=t,this.value=i,this.red=s,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,s,e,h=!1,n=0,a=!1,l=!0){const r=75*e,o=100*e,c=r/2,d=o/2;t.save(),t.translate(i+c,s+d),t.rotate(n+(l?this.tilt:0));const u=-.92*r/2,m=-.92*o/2;if(t.drawImage(this.imgGray,u,m,r,o),h)t.drawImage(this.imgBack,-c,-d,r,o);else{t.drawImage(this.imgFront,-c,-d,r,o);const i=.9,s=r*i,h=o*i,n=-68*e/2,l=-90*e/2;t.drawImage(this.img,n,l,s,h),a&&t.drawImage(this.imgGray,-c,-d,r,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,e=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((s=void 0)||(s=Promise))((function(h,n){function a(t){try{r(e.next(t))}catch(t){n(t)}}function l(t){try{r(e.throw(t))}catch(t){n(t)}}function r(t){var i;t.done?h(t.value):(i=t.value,i instanceof s?i:new s((function(t){t(i)}))).then(a,l)}r((e=e.apply(t,i||[])).next())}));var t,i,s,e}loadImg(t,i){return new Promise(((s,e)=>{t.onload=()=>s(),t.onerror=()=>e(),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,s){this.tiles=t,this.stolenFrom=i,this.belongsTo=s}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,s,e,h,n,a){t.save(),t.translate(525,525),t.rotate(n),t.translate(-525,-525);const l=75*h,r=90*h,o=25*h/2,c=(this.belongsTo-this.stolenFrom-1+4)%4,d=void 0===a?-1:a.getFamily(),u=void 0===a?0:a.getValue(),m=(i,s,e,n)=>{i.drawTile(t,s,e,h,!1,n,i.isEqual(d,u))},p=Math.PI/2;switch(c){case 0:m(this.tiles[0],i,s+o,p),m(this.tiles[1],i+r,s,0),m(this.tiles[2],i+r+l+e*h,s,0);break;case 1:m(this.tiles[0],i,s,0),m(this.tiles[1],i+r,s+o,-p),m(this.tiles[2],i+r+l+3*e*h,s,0);break;case 2:m(this.tiles[0],i,s,0),m(this.tiles[1],i+l+e*h,s,0),m(this.tiles[2],i+r+l+e*h,s+o,-p);break;default:console.error(`Position non prise en charge: ${c}`)}t.restore()}}const s="m",e=1,h="p",n=2,a="s",l=3,r="w",o=4,c="d",d=5;class u{constructor(t=""){this.isolate=!1,this.tiles=[],this.initializeFromString(t)}initializeFromString(t){for(let i=0;i<t.length-1;i++){const s=t.substring(i,i+2),e=s[0],h=Number(s[1]);this.isValidTileCode(e,h)&&this.addTileFromCode(e,h)}}isValidTileCode(t,i){return t===s||t===h||t===a||t===r||t===c}addTileFromCode(i,u){const m={[s]:e,[h]:n,[a]:l,[r]:o,[c]:d}[i];void 0!==m&&this.tiles.push(new t(m,u,!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 s=this.findTileIndex(t,i);if(-1!==s){[this.tiles[s],this.tiles[0]]=[this.tiles[0],this.tiles[s]];const t=this.tiles.shift();return this.sort(),t}}findTileIndex(t,i){return this.tiles.findIndex((s=>s.getFamily()===t&&s.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((s=>s.getFamily()===t&&s.getValue()===i)).length}toGroup(t=!1){if(0===this.tiles.length)return[];const i=this.tiles.pop(),s=i.getFamily(),e=i.getValue();if(this.count(s,e)>=1&&!t){const t=this.tryFormPair(i);if(t)return t}if(this.count(s,e)>=2){const s=this.tryFormTriplet(i,t);if(s)return s}const h=this.count(s,e-1)>0,n=this.count(s,e-2)>0;if(h&&n){const s=this.tryFormSequence(i,t);if(s)return s}this.tiles.push(i),this.sort()}tryFormPair(t){const s=this.find(t.getFamily(),t.getValue()),e=this.toGroup(!0);if(this.tiles.push(s),this.sort(),void 0!==e)return this.tiles.push(t),this.sort(),e.push(new i([t,s],0,0)),e}tryFormTriplet(t,s){const e=this.find(t.getFamily(),t.getValue()),h=this.find(t.getFamily(),t.getValue()),n=this.toGroup(s);if(this.tiles.push(e),this.tiles.push(h),this.sort(),void 0!==n)return n.push(new i([t,e,h],0,0)),this.tiles.push(t),this.sort(),n}tryFormSequence(t,s){const e=this.find(t.getFamily(),t.getValue()-1),h=this.find(t.getFamily(),t.getValue()-2),n=this.toGroup(s);if(this.tiles.push(e),this.tiles.push(h),this.sort(),void 0!==n)return n.push(new i([h,e,t],0,0)),this.tiles.push(t),this.sort(),n}drawHand(t,i,s,e,h,n=void 0,a=!1,l=0){const r=(75+e)*h,o=Math.cos(l)*r,c=Math.sin(l)*r;for(let e=0;e<this.tiles.length;e++){const r=e===this.tiles.length-1&&this.isolate?10:0;let d=i+e*o+r*h*Math.cos(l),u=s+e*c+r*h*Math.sin(l);e===n&&(d+=25*h*Math.sin(l),u-=25*h*Math.cos(l)),this.tiles[e].drawTile(t,d,u,h,a,l)}}preload(){return t=this,i=void 0,e=function*(){yield Promise.all(this.tiles.map((t=>t.preloadImg())))},new((s=void 0)||(s=Promise))((function(h,n){function a(t){try{r(e.next(t))}catch(t){n(t)}}function l(t){try{r(e.throw(t))}catch(t){n(t)}}function r(t){var i;t.done?h(t.value):(i=t.value,i instanceof s?i:new s((function(t){t(i)}))).then(a,l)}r((e=e.apply(t,i||[])).next())}));var t,i,s,e}cleanup(){this.tiles.forEach((t=>t.cleanup())),this.tiles=[]}}class m{constructor(t=!1){this.tiles=[],this.tileIndexMap=new Map,this.initTiles(t)}displayFamilies(t,i,s,e,h=0,n=0){let a=i,l=s;const r=[{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 s of r){for(let i=s.start;i<=s.end;i++){const n=this.find(s.family,i);n&&(n.drawTile(t,a,l,e,!1,0,!1),a+=(75+h)*e)}a=i,l+=(100+n)*e}}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()),s=this.tileIndexMap.get(i);return s&&s.length>0&&(s.pop(),0===s.length?this.tileIndexMap.delete(i):this.tileIndexMap.set(i,s)),t}push(t){const i=this.tiles.length;this.tiles.push(t);const s=this.getTileKey(t.getFamily(),t.getValue()),e=this.tileIndexMap.get(s)||[];e.push(i),this.tileIndexMap.set(s,e)}find(t,i){const s=this.getTileKey(t,i),e=this.tileIndexMap.get(s);if(!e||0===e.length)return;const h=e[0];if(h>=this.tiles.length)return this.rebuildIndexMap(),this.find(t,i);[this.tiles[h],this.tiles[0]]=[this.tiles[0],this.tiles[h]],this.updateIndicesAfterSwap(0,h);const n=this.tiles.shift();return this.decrementIndicesAfterShift(),n}count(t,i){const s=this.getTileKey(t,i),e=this.tileIndexMap.get(s);return e?e.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 u;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,e=function*(){const t=this.tiles.map((t=>t.preloadImg()));yield Promise.all(t)},new((s=void 0)||(s=Promise))((function(h,n){function a(t){try{r(e.next(t))}catch(t){n(t)}}function l(t){try{r(e.throw(t))}catch(t){n(t)}}function r(t){var i;t.done?h(t.value):(i=t.value,i instanceof s?i:new s((function(t){t(i)}))).then(a,l)}r((e=e.apply(t,i||[])).next())}));var t,i,s,e}getTileKey(t,i){return`${t}-${i}`}updateIndicesAfterSwap(t,i){if(t===i)return;const s=this.tiles[t],e=this.tiles[i],h=this.getTileKey(s.getFamily(),s.getValue()),n=this.getTileKey(e.getFamily(),e.getValue()),a=this.tileIndexMap.get(h)||[],l=this.tileIndexMap.get(n)||[],r=a.indexOf(i),o=l.indexOf(t);-1!==r&&(a[r]=t),-1!==o&&(l[o]=i),this.tileIndexMap.set(h,a),this.tileIndexMap.set(n,l)}decrementIndicesAfterShift(){var t;for(const[i,s]of this.tileIndexMap.entries())this.tileIndexMap.set(i,s.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],s=this.getTileKey(i.getFamily(),i.getValue()),e=this.tileIndexMap.get(s)||[];e.push(t),this.tileIndexMap.set(s,e)}}initTiles(i){for(let s=1;s<=3;s++)for(let e=1;e<=9;e++){const h=5===e&&i;for(let i=0;i<3;i++)this.push(new t(s,e,!1));this.push(new t(s,e,h))}for(let i=1;i<=4;i++)for(let s=0;s<4;s++)this.push(new t(4,i,!1));for(let i=1;i<=3;i++)for(let s=0;s<4;s++)this.push(new t(5,i,!1))}}var p=function(t,i,s,e){return new(s||(s=Promise))((function(h,n){function a(t){try{r(e.next(t))}catch(t){n(t)}}function l(t){try{r(e.throw(t))}catch(t){n(t)}}function r(t){var i;t.done?h(t.value):(i=t.value,i instanceof s?i:new s((function(t){t(i)}))).then(a,l)}r((e=e.apply(t,i||[])).next())}))};const g="myCanvas",f=1e3/30;class I{constructor(t=g){this.decks=[],this.hands=[],this.selectedTile=void 0,this.isDirty=!0,this.animationFrameId=null,this.lastFrameTime=0,this.cleanupCallbacks=[],this.BASE_X=300,this.BASE_Y=150,this.X_OFFSET=250,this.Y_OFFSET=100,this.INTERACTIVE_X=100,this.INTERACTIVE_Y=800,this.SIZE=.75,this.TILE_WIDTH=78*this.SIZE,this.MAX_TILES=14,this.tileRects=[];const i=document.getElementById(t);if(!i)throw new Error(`Canvas avec ID '${t}' introuvable`);this.canvas=i;const s=i.getContext("2d");if(!s)throw new Error("Impossible d'obtenir le contexte 2D du canvas");this.ctx=s,this.staticCanvas=document.createElement("canvas"),this.staticCanvas.width=i.width,this.staticCanvas.height=i.height;const e=this.staticCanvas.getContext("2d");if(!e)throw new Error("Impossible d'obtenir le contexte du canvas statique");this.staticCtx=e,this.calculateTileHitAreas()}calculateTileHitAreas(){this.tileRects=[];for(let t=0;t<this.MAX_TILES;t++)this.tileRects.push({x:this.INTERACTIVE_X+t*this.TILE_WIDTH+(t===this.MAX_TILES-1?10:0),y:this.INTERACTIVE_Y,width:75,height:100*this.SIZE})}prerenderBackground(){this.staticCtx.clearRect(0,0,this.staticCanvas.width,this.staticCanvas.height),this.staticCtx.fillStyle="#007730",this.staticCtx.fillRect(50,50,1e3,1e3)}drawFrame(){this.isDirty&&(this.prerenderBackground(),this.drawHandsAndLabels(),this.checkAndDisplayGroups(),this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.drawImage(this.staticCanvas,0,0),this.isDirty=!1)}drawHandsAndLabels(){const t=this.staticCtx;t.fillStyle="#DFDFFF",t.font="50px serif",t.fillText("Chii:",75,this.BASE_Y+100*this.SIZE-5),this.hands[0].drawHand(t,this.BASE_X,this.BASE_Y,5,this.SIZE),this.hands[1].drawHand(t,this.BASE_X+(75+this.X_OFFSET)*this.SIZE,this.BASE_Y,5,this.SIZE),t.fillText("Pon:",75,this.BASE_Y+(100+this.Y_OFFSET)*this.SIZE+100*this.SIZE-5),this.hands[2].drawHand(t,this.BASE_X,this.BASE_Y+(100+this.Y_OFFSET)*this.SIZE,5,this.SIZE),this.hands[3].drawHand(t,this.BASE_X+(75+this.X_OFFSET)*this.SIZE,this.BASE_Y+(100+this.Y_OFFSET)*this.SIZE,5,this.SIZE),t.fillText("Invalide:",75,this.BASE_Y+2*(100+this.Y_OFFSET)*this.SIZE+100*this.SIZE-5),this.hands[5].drawHand(t,this.BASE_X,this.BASE_Y+2*(100+this.Y_OFFSET)*this.SIZE,5,this.SIZE),this.hands[6].drawHand(t,this.BASE_X+(75+this.X_OFFSET)*this.SIZE,this.BASE_Y+2*(100+this.Y_OFFSET)*this.SIZE,5,this.SIZE),this.hands[7].drawHand(t,this.BASE_X+2*(75+this.X_OFFSET)*this.SIZE,this.BASE_Y+2*(100+this.Y_OFFSET)*this.SIZE,5,this.SIZE),this.hands[4].drawHand(t,this.BASE_X+2*(75+this.X_OFFSET)*this.SIZE,this.BASE_Y+(100+this.Y_OFFSET)*this.SIZE,5,this.SIZE),this.hands[8].isolate=!0,this.hands[8].drawHand(t,this.INTERACTIVE_X,this.INTERACTIVE_Y,5,this.SIZE,this.selectedTile)}checkAndDisplayGroups(){void 0!==this.hands[8].toGroup()&&(this.staticCtx.fillStyle="#FF0000",this.staticCtx.font="50px serif",this.staticCtx.fillText("Tous les groupes sont formés !",100,750))}startAnimationLoop(){const t=i=>{this.animationFrameId=requestAnimationFrame(t);const s=i-this.lastFrameTime;s<f||(this.lastFrameTime=i-s%f,this.drawFrame())};this.animationFrameId=requestAnimationFrame(t)}initEventListeners(){const t=this.handleMouseMove.bind(this),i=this.handleMouseDown.bind(this);this.canvas.addEventListener("mousemove",t),this.canvas.addEventListener("mousedown",i),this.cleanupCallbacks.push((()=>{this.canvas.removeEventListener("mousemove",t),this.canvas.removeEventListener("mousedown",i)}))}handleMouseMove(t){const i=this.canvas.getBoundingClientRect(),s=t.clientX-i.left,e=t.clientY-i.top,h=this.selectedTile;this.selectedTile=void 0;for(let t=0;t<this.tileRects.length;t++){const i=this.tileRects[t];if(s>=i.x&&s<=i.x+i.width&&e>=i.y&&e<=i.y+i.height){this.selectedTile=t;break}}h!==this.selectedTile&&(this.isDirty=!0)}handleMouseDown(){void 0!==this.selectedTile&&(this.decks[0].push(this.hands[8].eject(this.selectedTile)),this.decks[0].shuffle(),this.hands[8].sort(),this.hands[8].push(this.decks[0].pop()),this.isDirty=!0)}preloadDeck(t){return p(this,void 0,void 0,(function*(){yield t.preload()}))}preloadHand(t){return p(this,void 0,void 0,(function*(){yield t.preload()}))}initialize(){return p(this,void 0,void 0,(function*(){try{this.decks.push(new m(!1)),this.hands.push(new u("s1s2s3"),new u("m2m3m4"),new u("p5p5p5"),new u("w2w2w2"),new u("d3d3d3"),new u("s4p5m6"),new u("m9s9p9"),new u("d1d2d3"),this.decks[0].getRandomHand()),yield Promise.all([...this.decks.map((t=>this.preloadDeck(t))),...this.hands.map((t=>this.preloadHand(t)))]),this.hands[8].push(this.decks[0].pop()),this.hands[8].sort(),this.initEventListeners(),this.isDirty=!0,this.drawFrame(),this.startAnimationLoop()}catch(t){console.error("Erreur d'initialisation:",t)}}))}cleanup(){null!==this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.cleanupCallbacks.forEach((t=>t())),this.cleanupCallbacks=[],this.decks.forEach((t=>t.cleanup())),this.hands.forEach((t=>t.cleanup())),this.decks=[],this.hands=[],this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.staticCtx.clearRect(0,0,this.staticCanvas.width,this.staticCanvas.height)}}let E=null;function v(){E&&(E.cleanup(),E=null)}"undefined"!=typeof window&&function(){return p(this,void 0,void 0,(function*(){try{E=new I(g),yield E.initialize(),window.cleanup=v}catch(t){console.error("Erreur lors de l'initialisation:",t)}}))}().catch(console.error)})(); |