implement cactus kev's poker hand evaluator
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -1,2 +1,3 @@
 | 
				
			|||||||
node_modules
 | 
					node_modules
 | 
				
			||||||
config.json
 | 
					config.json
 | 
				
			||||||
 | 
					data/HandRanks.dat
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										
											BIN
										
									
								
								data/generate_table
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								data/generate_table
									
									
									
									
									
										Executable file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										3
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										3
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							@@ -7,6 +7,9 @@
 | 
				
			|||||||
    "": {
 | 
					    "": {
 | 
				
			||||||
      "name": "schmirc",
 | 
					      "name": "schmirc",
 | 
				
			||||||
      "version": "1.0.0",
 | 
					      "version": "1.0.0",
 | 
				
			||||||
 | 
					      "cpu": [
 | 
				
			||||||
 | 
					        "x64"
 | 
				
			||||||
 | 
					      ],
 | 
				
			||||||
      "license": "ISC",
 | 
					      "license": "ISC",
 | 
				
			||||||
      "dependencies": {
 | 
					      "dependencies": {
 | 
				
			||||||
        "@xpressit/winning-poker-hand-rank": "^0.1.6",
 | 
					        "@xpressit/winning-poker-hand-rank": "^0.1.6",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,9 +2,13 @@
 | 
				
			|||||||
  "name": "schmirc",
 | 
					  "name": "schmirc",
 | 
				
			||||||
  "version": "1.0.0",
 | 
					  "version": "1.0.0",
 | 
				
			||||||
  "description": "",
 | 
					  "description": "",
 | 
				
			||||||
  "main": "index.js",
 | 
					  "main": "index.mjs",
 | 
				
			||||||
 | 
					  "cpu": [
 | 
				
			||||||
 | 
					    "x64"
 | 
				
			||||||
 | 
					  ],
 | 
				
			||||||
  "scripts": {
 | 
					  "scripts": {
 | 
				
			||||||
    "start": "node ./src/index.mjs"
 | 
					    "start": "node ./src/index.mjs",
 | 
				
			||||||
 | 
					    "build": "cd ./data && ./generate_table"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "author": "Flummi",
 | 
					  "author": "Flummi",
 | 
				
			||||||
  "license": "ISC",
 | 
					  "license": "ISC",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,44 @@
 | 
				
			|||||||
export default new class handranker {
 | 
					import fs from 'node:fs';
 | 
				
			||||||
  constructor() {
 | 
					 | 
				
			||||||
    return this;
 | 
					 | 
				
			||||||
  };
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default new class handranker {
 | 
				
			||||||
 | 
					  #ranks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  constructor() {
 | 
				
			||||||
 | 
					    this.#ranks = fs.readFileSync('./data/HandRanks.dat');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    this.handtypes = [
 | 
				
			||||||
 | 
					      "InvalidHand", "HighCard", "Pair",
 | 
				
			||||||
 | 
					      "TwoPairs", "ThreeOfAKind", "Straight",
 | 
				
			||||||
 | 
					      "Flush", "FullHouse", "FourOfAKind",
 | 
				
			||||||
 | 
					      "StraightFlush"
 | 
				
			||||||
 | 
					    ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    this.cards = [''];
 | 
				
			||||||
 | 
					    for(const cv of [..."23456789TJQKA"])
 | 
				
			||||||
 | 
					      for(const cs of [..."CDHS"])
 | 
				
			||||||
 | 
					        this.cards.push(cv + cs);
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					  evalHand(cards) {
 | 
				
			||||||
 | 
					    if(!this.#ranks)
 | 
				
			||||||
 | 
					      throw new Error("HandRanks.dat not loaded, run 'npm run build' first!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if(![7,6,5,3].includes(cards.length))
 | 
				
			||||||
 | 
					      throw new Error("Hand must be 3, 5, 6, or 7 cards");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    cards = cards.map(c => this.cards.indexOf(c));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let p = 53;
 | 
				
			||||||
 | 
					    for(let i = 0; i < cards.length; i++)
 | 
				
			||||||
 | 
					      p = this.#ranks.readUInt32LE((p + cards[i]) * 4);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if(cards.length == 5 || cards.length == 6)
 | 
				
			||||||
 | 
					      p = this.#ranks.readUInt32LE(p * 4);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return {
 | 
				
			||||||
 | 
					      handType: p >> 12,
 | 
				
			||||||
 | 
					      handRank: p & 0x00000fff,
 | 
				
			||||||
 | 
					      value: p,
 | 
				
			||||||
 | 
					      handName: this.handtypes[p >> 12]
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user