Skip to content

Commit

Permalink
refactor: extract PBN
Browse files Browse the repository at this point in the history
  • Loading branch information
S-N-O-R-L-A-X committed Oct 13, 2024
1 parent d0a2eb4 commit 5cbea18
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 30 deletions.
61 changes: 61 additions & 0 deletions src/Utils/PBN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Hand from "../models/Hand";

export function parseHand(hand: Hand) {
let ret = "";
function replace10(cards: string[]) {
cards.forEach((card) => {
if (card === "10") {
ret += "T";
}
else {
ret += card;
}
})
}
replace10(hand.hand["S"]);
ret += ".";
replace10(hand.hand["H"]);
ret += ".";
replace10(hand.hand["D"]);
ret += ".";
replace10(hand.hand["C"]);
return ret;
}

export function convertAllHandsToPBN(allHands: Hand[]) {
let str = "N:";
str += parseHand(allHands[0]) + " " + parseHand(allHands[2]) + " " + parseHand(allHands[1]) + " " + parseHand(allHands[3]);
return str;
}

export default function exportPBN(allHands: Hand[], boardNumber: number) {
const pbn = `% PBN 2.1
% EXPORT
[Date ${new Date().getFullYear()}]
[Board "${boardNumber}"]
[West ""]
[North ""]
[East ""]
[South ""]
[Deal ${convertAllHandsToPBN(allHands)}]
`

// download
const blob = new Blob([pbn], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
// 创建一个临时的 <a> 标签,设置 href 属性为刚刚创建的 URL
const link = document.createElement('a');
link.href = url;
link.download = 'test.pbn'; // 设置下载文件的名称

// 将 <a> 标签插入到 DOM 中并触发点击事件
document.body.appendChild(link);
link.click();

// 从 DOM 中移除临时的 <a> 标签
document.body.removeChild(link);

// 释放刚刚创建的 URL 对象
URL.revokeObjectURL(url);
}
4 changes: 2 additions & 2 deletions src/Utils/callInterface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Hand from "../models/Hand";
import { retryFetch, parseHand } from "./utils";

import { retryFetch } from "./utils";
import { parseHand } from "./PBN";
interface RequestBoard {
dealstr: string;
sockref?: number;
Expand Down
29 changes: 1 addition & 28 deletions src/Utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Card from "../models/Card";
import Hand from "../models/Hand";
import { COLORS, NUMBER2COLORSHORT, RANK2CARD } from "./maps";
import { convertAllHandsToPBN } from "./PBN";

export function idx2card(idx: number): Card {
const color = Math.floor(idx / 13), rank = idx % 13;
Expand All @@ -11,34 +12,6 @@ export function card2idx(card: Card): number {
return Card.RANK[card.rank] + 13 * (NUMBER2COLORSHORT[card.suit]);
}

export function parseHand(hand: Hand) {
let ret = "";
function replace10(cards: string[]) {
cards.forEach((card) => {
if (card === "10") {
ret += "T";
}
else {
ret += card;
}
})
}
replace10(hand.hand["S"]);
ret += ".";
replace10(hand.hand["H"]);
ret += ".";
replace10(hand.hand["D"]);
ret += ".";
replace10(hand.hand["C"]);
return ret;
}

function convertAllHandsToPBN(allHands: Hand[]) {
let str = "N:";
str += parseHand(allHands[0]) + " " + parseHand(allHands[2]) + " " + parseHand(allHands[1]) + " " + parseHand(allHands[3]);
return str;
}

export function analyzeOffline(allHands: Hand[]) {
// @ts-ignore
const res = calcDDTable(convertAllHandsToPBN(allHands));
Expand Down

0 comments on commit 5cbea18

Please sign in to comment.