From ee4df79622cbfb3350873c3a2441fc6b52ed924d Mon Sep 17 00:00:00 2001 From: yjf2002ghty <47345902+yjf2002ghty@users.noreply.github.com> Date: Sat, 22 Jun 2024 15:51:22 +0800 Subject: [PATCH] Update main.js: Fix memory leak & add gamer result display --- src/main.js | 91 ++++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/src/main.js b/src/main.js index b834796d..abb52a81 100644 --- a/src/main.js +++ b/src/main.js @@ -966,6 +966,7 @@ function getNotation(notation, variant, startfen, is960, ucimovestr) { let tmpboard = new ffish.Board(variant, startfen, is960); let moveslist = ucimoves.split(/[ ]+/).reverse(); let result = ""; + let tmpboardresult = ""; if (moveslist.length == 1 && moveslist[0] == "") { } else { while (moveslist.length > 0) { @@ -974,52 +975,63 @@ function getNotation(notation, variant, startfen, is960, ucimovestr) { } } } + tmpboardresult = tmpboard.result(); if (notation == "DEFAULT") { tmpboard.setFen(startfen); - return tmpboard.variationSan(ucimoves, ffish.Notation.DEFAULT); + result = + tmpboard.variationSan(ucimoves, ffish.Notation.DEFAULT) + + (tmpboardresult !== "*" ? " " + tmpboardresult : ""); } else if (notation == "SAN") { tmpboard.setFen(startfen); - return tmpboard.variationSan(ucimoves, ffish.Notation.SAN); + result = + tmpboard.variationSan(ucimoves, ffish.Notation.SAN) + + (tmpboardresult !== "*" ? " " + tmpboardresult : ""); } else if (notation == "LAN") { tmpboard.setFen(startfen); - return tmpboard.variationSan(ucimoves, ffish.Notation.LAN); + result = + tmpboard.variationSan(ucimoves, ffish.Notation.LAN) + + (tmpboardresult !== "*" ? " " + tmpboardresult : ""); } else if (notation == "SHOGI_HOSKING") { tmpboard.setFen(startfen); - return tmpboard.variationSan( - ucimoves, - ffish.Notation.SHOGI_HOSKING, - ); + result = + tmpboard.variationSan(ucimoves, ffish.Notation.SHOGI_HOSKING) + + (tmpboardresult !== "*" ? " " + tmpboardresult : ""); } else if (notation == "SHOGI_HODGES") { tmpboard.setFen(startfen); - return tmpboard.variationSan( - ucimoves, - ffish.Notation.SHOGI_HODGES, - ); + result = + tmpboard.variationSan(ucimoves, ffish.Notation.SHOGI_HODGES) + + (tmpboardresult !== "*" ? " " + tmpboardresult : ""); } else if (notation == "SHOGI_HODGES_NUMBER") { tmpboard.setFen(startfen); - return tmpboard.variationSan( - ucimoves, - ffish.Notation.SHOGI_HODGES_NUMBER, - ); + result = + tmpboard.variationSan( + ucimoves, + ffish.Notation.SHOGI_HODGES_NUMBER, + ) + (tmpboardresult !== "*" ? " " + tmpboardresult : ""); } else if (notation == "JANGGI") { tmpboard.setFen(startfen); - return tmpboard.variationSan(ucimoves, ffish.Notation.JANGGI); + result = + tmpboard.variationSan(ucimoves, ffish.Notation.JANGGI) + + (tmpboardresult !== "*" ? " " + tmpboardresult : ""); } else if (notation == "XIANGQI_WXF") { tmpboard.setFen(startfen); - return tmpboard.variationSan( - ucimoves, - ffish.Notation.XIANGQI_WXF, - ); + result = + tmpboard.variationSan(ucimoves, ffish.Notation.XIANGQI_WXF) + + (tmpboardresult !== "*" ? " " + tmpboardresult : ""); } else if (notation == "THAI_SAN") { tmpboard.setFen(startfen); - return tmpboard.variationSan(ucimoves, ffish.Notation.THAI_SAN); + result = + tmpboard.variationSan(ucimoves, ffish.Notation.THAI_SAN) + + (tmpboardresult !== "*" ? " " + tmpboardresult : ""); } else if (notation == "THAI_LAN") { tmpboard.setFen(startfen); - return tmpboard.variationSan(ucimoves, ffish.Notation.THAI_LAN); + result = + tmpboard.variationSan(ucimoves, ffish.Notation.THAI_LAN) + + (tmpboardresult !== "*" ? " " + tmpboardresult : ""); } else if (notation == "FEN") { - return tmpboard.fen(); + result = tmpboard.fen(); } else if (notation == "PGN") { - const gameresult = tmpboard.result(); + const gameresult = tmpboardresult; tmpboard.setFen(startfen); const today = new Date(); const year = today.getFullYear(); @@ -1052,7 +1064,6 @@ function getNotation(notation, variant, startfen, is960, ucimovestr) { result += `[Round "1"]\n[White "${whitename}"]\n[Black "${blackname}"]\n`; result += `[FEN "${startfen}"]\n[Result "${gameresult}"]\n[Variant "${tmpboard.variant()}"]\n\n`; result += tmpboard.variationSan(ucimoves, ffish.Notation.SAN); - return result; } else if (notation == "EPD") { const today = new Date(); const year = today.getFullYear(); @@ -1109,13 +1120,12 @@ function getNotation(notation, variant, startfen, is960, ucimovestr) { result += ` variant "${tmpboard.variant()}";`; result += ` site "${window.location.host}";`; result += ` date "${year.toString() + "." + month.toString() + "." + day.toString()}";`; - result += ` result "${tmpboard.result()}";`; + result += ` result "${tmpboardresult}";`; result += ` first_player "${whitename}";`; result += ` second_player "${blackname}";`; - return result; } else if (notation == "FEN+UCIMOVE") { tmpboard.setFen(startfen); - return tmpboard.fen() + "|" + ucimoves; + result = tmpboard.fen() + "|" + ucimoves; } else if (notation == "FEN+USIMOVE") { tmpboard.setFen(startfen); let dimensions = getDimensions(); @@ -1125,12 +1135,11 @@ function getNotation(notation, variant, startfen, is960, ucimovestr) { dimensions.height, ); if (convertedmoves != null) { - return tmpboard.fen() + "|" + convertedmoves; + result = tmpboard.fen() + "|" + convertedmoves; } else { - return ( + result = tmpboard.fen() + - "|(There are moves that cannot be displayed in USI moves, such as pawn promotion or seirawan gating)" - ); + "|(There are moves that cannot be displayed in USI moves, such as pawn promotion or seirawan gating)"; } } else if (notation == "FEN+UCCIMOVE") { tmpboard.setFen(startfen); @@ -1141,12 +1150,11 @@ function getNotation(notation, variant, startfen, is960, ucimovestr) { dimensions.height, ); if (convertedmoves != null) { - return tmpboard.fen() + "|" + convertedmoves; + result = tmpboard.fen() + "|" + convertedmoves; } else { - return ( + result = tmpboard.fen() + - "|(There are moves that cannot be displayed in UCCI moves, such as pawn promotion or seirawan gating)" - ); + "|(There are moves that cannot be displayed in UCCI moves, such as pawn promotion or seirawan gating)"; } } else if (notation == "SFEN+USIMOVE") { tmpboard.setFen(startfen); @@ -1157,23 +1165,22 @@ function getNotation(notation, variant, startfen, is960, ucimovestr) { dimensions.height, ); if (convertedmoves != null) { - return ( + result = fge.ConvertFENtoSFEN(tmpboard.fen()) + "|" + fge.convertUCImovestoUSImoves( ucimoves, dimensions.width, dimensions.height, - ) - ); + ); } else { - return ( + result = fge.ConvertFENtoSFEN(tmpboard.fen()) + - "|(There are moves that cannot be displayed in USI moves, such as pawn promotion or seirawan gating)" - ); + "|(There are moves that cannot be displayed in USI moves, such as pawn promotion or seirawan gating)"; } } tmpboard.delete(); + return result; } else { return "Illegal FEN"; }