tsgammon-core
Version:
A Backgammon library for Typescript, formerly developed as a part of tsgammon-ui
101 lines (100 loc) • 3.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildMatchRecorder = exports.matchRecorderAsBG = exports.matchRecorderAsSG = void 0;
const MatchRecord_1 = require("./MatchRecord");
const PlyRecord_1 = require("./PlyRecord");
const SGResult_1 = require("./SGResult");
/**
* MatchRecorderをラップして、SingleGameListnerとして返す
* @param matchRecorder ラップされるMatchRecorder
* @returns
*/
function matchRecorderAsSG(matchRecorder) {
return {
onCheckerPlayCommitted: (committedState) => {
matchRecorder.recordPly((0, PlyRecord_1.plyRecordForCheckerPlay)(committedState.curPly), committedState);
},
onGameStarted: () => {
matchRecorder.resetCurGame();
},
onEndOfGame: (sgEoG) => {
const { stake, result, eogStatus } = sgEoG;
matchRecorder.recordEoG((0, PlyRecord_1.plyRecordForEoG)(stake, result, eogStatus));
},
};
}
exports.matchRecorderAsSG = matchRecorderAsSG;
/**
* MatchRecorderをラップして、BGListnerとして返す
* @param matchRecorder ラップされるMatchRecorder
* @returns
*/
function matchRecorderAsBG(gameConf, matchRecorder) {
return {
onDoubled: (bgState, lastState) => {
const plyRecord = (0, PlyRecord_1.plyRecordForDouble)(lastState.cubeState, lastState.isRed);
matchRecorder.recordPly(plyRecord, {
cbState: lastState,
sgState: bgState.sgState,
});
},
onDoubleAccepted: (bgState, lastState) => {
const plyRecord = (0, PlyRecord_1.plyRecordForTake)(lastState.isRed);
matchRecorder.recordPly(plyRecord, {
cbState: lastState,
sgState: bgState.sgState,
});
},
onPassed: (bgState, isRedWon) => {
const plyRecord = (0, PlyRecord_1.plyRecordForPass)(isRedWon ? SGResult_1.SGResult.REDWON : SGResult_1.SGResult.WHITEWON);
matchRecorder.recordPly(plyRecord, bgState);
},
onBGGameStarted: () => {
matchRecorder.resetCurGame();
},
onCommitted: (bgState) => {
const committedState = bgState.sgState;
matchRecorder.recordPly((0, PlyRecord_1.plyRecordForCheckerPlay)(committedState.curPly), bgState);
},
onEndOfBGGame: (bgState) => {
const { stake, eogStatus } = bgState.cbState.calcStake(gameConf);
const plyRecordEoG = (0, PlyRecord_1.plyRecordForEoG)(stake, bgState.cbState.result, eogStatus);
matchRecorder.recordEoG(plyRecordEoG);
},
};
}
exports.matchRecorderAsBG = matchRecorderAsBG;
/**
* MatchRecorderオブジェクトを生成する
*
* @param matchRecord
* @param setMatchRecord
* @returns
*/
function buildMatchRecorder(matchRecord, setMatchRecord) {
function recordPly(plyRecord, state) {
setMatchRecord((prev) => prev.isEoG ? prev : (0, MatchRecord_1.addPlyRecord)(prev, plyRecord, state));
}
function recordEoG(eogPlyRecord) {
setMatchRecord((prev) => {
if (prev.isEoG) {
return prev;
}
return (0, MatchRecord_1.eogRecord)(prev, eogPlyRecord);
});
}
function resetCurGame() {
setMatchRecord((prev) => prev.isEoG ? (0, MatchRecord_1.recordFinishedGame)(prev) : (0, MatchRecord_1.discardCurrentGame)(prev));
}
function resumeTo(index) {
setMatchRecord((prev) => (0, MatchRecord_1.trimPlyRecords)(prev, index));
return matchRecord.curGameRecord.plyRecords[index];
}
return {
recordEoG,
resetCurGame,
recordPly,
resumeTo,
};
}
exports.buildMatchRecorder = buildMatchRecorder;