Riichi/build/dp7.js
2025-04-15 22:32:14 +02:00

1 line
No EOL
12 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,n=!1,l=0,h=!1,r=!0){const a=75*s,o=100*s,u=a/2,c=o/2;t.save(),t.translate(i+u,e+c),t.rotate(l+(r?this.tilt:0));const d=-.92*a/2,m=-.92*o/2;if(t.drawImage(this.imgGray,d,m,a,o),n)t.drawImage(this.imgBack,-u,-c,a,o);else{t.drawImage(this.imgFront,-u,-c,a,o);const i=.9,e=a*i,n=o*i,l=-68*s/2,r=-90*s/2;t.drawImage(this.img,l,r,e,n),h&&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(n,l){function h(t){try{a(s.next(t))}catch(t){l(t)}}function r(t){try{a(s.throw(t))}catch(t){l(t)}}function a(t){var i;t.done?n(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(h,r)}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,n,l,h){t.save(),t.translate(525,525),t.rotate(l),t.translate(-525,-525);const r=75*n,a=90*n,o=25*n/2,u=(this.belongsTo-this.stolenFrom-1+4)%4,c=void 0===h?-1:h.getFamily(),d=void 0===h?0:h.getValue(),m=(i,e,s,l)=>{i.drawTile(t,e,s,n,!1,l,i.isEqual(c,d))},f=Math.PI/2;switch(u){case 0:m(this.tiles[0],i,e+o,f),m(this.tiles[1],i+a,e,0),m(this.tiles[2],i+a+r+s*n,e,0);break;case 1:m(this.tiles[0],i,e,0),m(this.tiles[1],i+a,e+o,-f),m(this.tiles[2],i+a+r+3*s*n,e,0);break;case 2:m(this.tiles[0],i,e,0),m(this.tiles[1],i+r+s*n,e,0),m(this.tiles[2],i+a+r+s*n,e+o,-f);break;default:console.error(`Position non prise en charge: ${u}`)}t.restore()}}const e="m",s=1,n="p",l=2,h="s",r=3,a="w",o=4,u="d",c=5;class d{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 n=this.count(e,s-1)>0,l=this.count(e,s-2)>0;if(n&&l){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()),n=this.find(t.getFamily(),t.getValue()),l=this.findGroups(e);if(this.tiles.push(s),this.tiles.push(n),this.sort(),void 0!==l)return l.push(new i([t,s,n],0,0)),this.tiles.push(t),this.sort(),l}tryFormSequence(t,e){const s=this.find(t.getFamily(),t.getValue()-1),n=this.find(t.getFamily(),t.getValue()-2),l=this.findGroups(e);if(this.tiles.push(s),this.tiles.push(n),this.sort(),void 0!==l)return l.push(new i([n,s,t],0,0)),this.tiles.push(t),this.sort(),l}}class m{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],n=Number(e[1]);this.isValidTileCode(s,n)&&this.addTileFromCode(s,n)}}isValidTileCode(t,i){return t===e||t===n||t===h||t===a||t===u}addTileFromCode(i,d){const m={[e]:s,[n]:l,[h]:r,[a]:o,[u]:c}[i];void 0!==m&&this.tiles.push(new t(m,d,!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 d(this.tiles).findGroups(t)}drawHand(t,i,e,s,n,l=void 0,h=!1,r=0){const a=(75+s)*n,o=Math.cos(r)*a,u=Math.sin(r)*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*n*Math.cos(r),d=e+s*u+a*n*Math.sin(r);s===l&&(c+=25*n*Math.sin(r),d-=25*n*Math.cos(r)),this.tiles[s].drawTile(t,c,d,n,h,r)}}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(n,l){function h(t){try{a(s.next(t))}catch(t){l(t)}}function r(t){try{a(s.throw(t))}catch(t){l(t)}}function a(t){var i;t.done?n(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(h,r)}a((s=s.apply(t,i||[])).next())}));var t,i,e,s}cleanup(){this.tiles.forEach((t=>t.cleanup())),this.tiles=[]}}class f{constructor(t=!1){this.tiles=[],this.tileIndexMap=new Map,this.initTiles(t)}displayFamilies(t,i,e,s,n=0,l=0){let h=i,r=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 l=this.find(e.family,i);l&&(l.drawTile(t,h,r,s,!1,0,!1),h+=(75+n)*s)}h=i,r+=(100+l)*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 n=s[0];if(n>=this.tiles.length)return this.rebuildIndexMap(),this.find(t,i);[this.tiles[n],this.tiles[0]]=[this.tiles[0],this.tiles[n]],this.updateIndicesAfterSwap(0,n);const l=this.tiles.shift();return this.decrementIndicesAfterShift(),l}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 m;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(n,l){function h(t){try{a(s.next(t))}catch(t){l(t)}}function r(t){try{a(s.throw(t))}catch(t){l(t)}}function a(t){var i;t.done?n(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(h,r)}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],n=this.getTileKey(e.getFamily(),e.getValue()),l=this.getTileKey(s.getFamily(),s.getValue()),h=this.tileIndexMap.get(n)||[],r=this.tileIndexMap.get(l)||[],a=h.indexOf(i),o=r.indexOf(t);-1!==a&&(h[a]=t),-1!==o&&(r[o]=i),this.tileIndexMap.set(n,h),this.tileIndexMap.set(l,r)}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 n=5===s&&i;for(let i=0;i<3;i++)this.push(new t(e,s,!1));this.push(new t(e,s,n))}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))}}const g={ordinaires(){let t=new m,i=new f;i.shuffle();let e=i.pop(),s=i.find(e.getFamily(),e.getValue());t.push(e),t.push(s);let n=2;for(;14!==n;)if(Math.random()<.5){let e=i.pop(),s=e.getFamily(),l=e.getValue();if(s<4&&1!==l&&9!==l&&i.count(s,l)>1){let h=i.find(s,l),r=i.find(s,l);t.push(e),t.push(h),t.push(r),n+=3}else i.push(e),i.shuffle()}else{let e=i.pop(),s=e.getFamily(),l=e.getValue();if(s<4&&1!==l&&l<6&&i.count(s,l+1)>0&&i.count(s,l+2)>0){let h=i.find(s,l+1),r=i.find(s,l+2);t.push(e),t.push(h),t.push(r),n+=3}else i.push(e),i.shuffle()}return t.sort(),t},sept_pairs(){let i=new m,e=new f;e.shuffle();let s=0;for(;7!==s;){let n=e.pop();i.getTiles().every((t=>0!==t.compare(n)))&&(s++,i.push(n),i.push(new t(n.getFamily(),n.getValue(),!1)))}return i.sort(),i}};var p=function(t,i,e,s){return new(e||(e=Promise))((function(n,l){function h(t){try{a(s.next(t))}catch(t){l(t)}}function r(t){try{a(s.throw(t))}catch(t){l(t)}}function a(t){var i;t.done?n(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(h,r)}a((s=s.apply(t,i||[])).next())}))};const y="myCanvas",w=1e3/30;class v{constructor(t=y){this.hands=[],this.animationFrameId=null,this.lastFrameTime=0,this.cleanupCallbacks=[],this.BASE_X=50,this.BASE_Y=90,this.DY=180,this.SIZE=.75;const i=document.getElementById(t);if(!i)throw new Error(`Canvas avec ID '${t}' introuvable`);this.canvas=i;const e=i.getContext("2d");if(!e)throw new Error("Impossible d'obtenir le contexte 2D du canvas");this.ctx=e}drawFrame(){this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.drawHandsAndLabels()}drawHandsAndLabels(){for(let t=0;t<this.hands.length;t++)this.hands[t].drawHand(this.ctx,this.BASE_X,this.BASE_Y+t*this.DY,5,this.SIZE)}startAnimationLoop(){const t=i=>{this.animationFrameId=requestAnimationFrame(t);const e=i-this.lastFrameTime;e<w||(this.lastFrameTime=i-e%w,this.drawFrame())};this.animationFrameId=requestAnimationFrame(t)}preloadHand(t){return p(this,void 0,void 0,(function*(){yield t.preload()}))}initialize(){return p(this,void 0,void 0,(function*(){try{this.hands.push(g.ordinaires(),g.sept_pairs()),yield Promise.all([...this.hands.map((t=>this.preloadHand(t)))]),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.hands.forEach((t=>t.cleanup())),this.hands=[]}}let I=null;function F(){I&&(I.cleanup(),I=null)}"undefined"!=typeof window&&function(){return p(this,void 0,void 0,(function*(){try{I=new v(y),yield I.initialize(),window.cleanup=F}catch(t){console.error("Erreur lors de l'initialisation:",t)}}))}().catch(console.error)})();