Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Review Move Icons + LRU Caching For Game Review #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/*
15 changes: 14 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@
from flask import Flask
from views import views
import subprocess
import argparse

app = Flask(__name__)
app.register_blueprint(views, url_prefix="/views")

def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--debug", help="Enable Flask Debug", action="store_true")
return parser.parse_args()

if __name__ == '__main__':
py_args = get_args()
# Run the app in a separate process
process = subprocess.Popen(["python", "-m", "flask", "run", "--no-reload", "--port=8000"])
cli_args = ["python", "-m", "flask", "run", "--port=8000"]
if py_args.debug:
cli_args.append("--debug")
else:
cli_args.append("--no-reload")

process = subprocess.Popen(cli_args)

# Wait in case of unexpected lag
time.sleep(1)
Expand Down
48 changes: 45 additions & 3 deletions chess_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import numpy as np
import io
from tqdm import tqdm
import platform
from functools import lru_cache

stockfish_path = "stockfish.exe"

stockfish_path = "stockfish"
if "windows" in platform.system().lower():
stockfish_path += ".exe"

STOCKFISH_CONFIG = {"time": 0.25}

Expand Down Expand Up @@ -1848,4 +1851,43 @@ def seperate_squares_in_move_list(uci_moves: list):

return seperated_squares


@lru_cache(maxsize=128)
def pgn_game_review(pgn_data: str, roast: bool, limit_type: str, time_limit: float, depth_limit: int):
global STOCKFISH_CONFIG

if limit_type == "time":
STOCKFISH_CONFIG = {'time': float(time_limit)}
else:
STOCKFISH_CONFIG = {'depth': int(depth_limit)}

uci_moves, san_moves, fens = parse_pgn(pgn_data)
scores, cpls_white, cpls_black, average_cpl_white, average_cpl_black = compute_cpl(uci_moves)
n_moves = len(scores)//2
white_elo_est, black_elo_est = estimate_elo(average_cpl_white, n_moves), estimate_elo(average_cpl_black, n_moves)
white_acc, black_acc = calculate_accuracy(scores)
devs, mobs, tens, conts = calculate_metrics(fens)

review_list, best_review_list, classification_list, uci_best_moves, san_best_moves = review_game(uci_moves, roast)

uci_best_moves = seperate_squares_in_move_list(uci_best_moves)

return (
san_moves,
fens,
scores,
classification_list,
review_list,
best_review_list,
san_best_moves,
uci_best_moves,
devs,
tens,
mobs,
conts,
white_acc,
black_acc,
white_elo_est,
black_elo_est,
average_cpl_white,
average_cpl_black
)
16 changes: 14 additions & 2 deletions static/css/analysis.css
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ p {

#move-count-book-text {
font-family: 'Microsoft JhengHei UI Light';
color: gray;
color: saddlebrown;
font-size: 14px;
padding: 5px;
margin: 0px;
Expand Down Expand Up @@ -446,7 +446,7 @@ footer {
}

.move-container-clicked {
background-color: #ffffff;
background-color: #5c5c5c;
width: 40%;
padding: 5px;
padding-left: 10px;
Expand All @@ -460,3 +460,15 @@ footer {
color: rgb(0, 0, 0, 0);
}

.move-class-icon-container {
position: relative;
top: -35px;
right: -55px;
width: 10px;
height: 10px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50%;
}

55 changes: 54 additions & 1 deletion templates/analysis.html
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,31 @@ <h2 id="acpl-black">880</h2>
}
}


var move_class_to_icon_class = {
"book": "bx bx-book-open",
"brilliant": "bx bx-brain",
"best": "bx bxs-badge-check",
"excellent": "bx bx-star",
"good": "bx bx-check-circle",
"inaccuracy": "bx bx-question-mark",
"mistake": "bx bx-x-circle",
"blunder": "bx bx-error-alt"
}

var move_class_to_class_color = {
"book": "saddlebrown",
"brilliant": "#2681ff",
"best": "#6cb400",
"excellent": "#6cb400",
"good": "#758358",
"inaccuracy": "#ffff00",
"mistake": "#ff9500",
"blunder": "#ff0000"
}

var display_classes = ["book", "brilliant", "inaccuracy", "mistake", "blunder"]

document.getElementById('move-count-book-text').innerHTML = numBookMoves[0] + " Book <i class='bx bx-book-open'></i> " + numBookMoves[1];

document.getElementById('move-count-brilliant-text').innerHTML = numBrilliantMoves[0] + " Brilliant <i class='bx bx-brain' ></i> " + numBrilliantMoves[1];
Expand All @@ -273,6 +298,8 @@ <h2 id="acpl-black">880</h2>

for (const [i, move] of moves.entries()) {

var move_class = classifications[i]

if ((i%2) == 0) {

var movebarDiv = document.createElement('div');
Expand All @@ -290,6 +317,19 @@ <h2 id="acpl-black">880</h2>
movewhiteDiv.setAttribute('data-fen', fens[i]);
movebarDiv.appendChild(movewhiteDiv);

if(display_classes.includes(move_class)) {
var class_color = move_class_to_class_color[move_class];
var classIcon = document.createElement('i');
classIcon.className = move_class_to_icon_class[move_class];

var classDiv = document.createElement('p');
classDiv.className = "move-class-icon-container";
classDiv.style = `font-size: 15px; color: ${class_color}`
classDiv.appendChild(classIcon);

movewhiteDiv.appendChild(classDiv);
}

} else {

var moveblackDiv = document.createElement('div');
Expand All @@ -298,6 +338,19 @@ <h2 id="acpl-black">880</h2>
moveblackDiv.setAttribute('data-fen', fens[i]);
movebarDiv.appendChild(moveblackDiv);

if(display_classes.includes(move_class)) {
var class_color = move_class_to_class_color[move_class];
var classIcon = document.createElement('i');
classIcon.className = move_class_to_icon_class[move_class];

var classDiv = document.createElement('p');
classDiv.className = "move-class-icon-container";
classDiv.style = `font-size: 15px; color: ${class_color}`
classDiv.appendChild(classIcon);

moveblackDiv.appendChild(classDiv);
}

}
}

Expand Down Expand Up @@ -499,7 +552,7 @@ <h2 id="acpl-black">880</h2>
classificationText.style.color = '#6cb400'
classificationText.innerHTML = moves[index] + " is " + cls;
} else if (cls.includes("book")) {
classificationText.style.color = 'gray'
classificationText.style.color = 'saddlebrown'
classificationText.innerHTML = moves[index] + " is a book move";
} else if (cls.includes("forced")) {
classificationText.style.color = 'gray'
Expand Down
45 changes: 27 additions & 18 deletions views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,41 @@ def home():
def analyse():

roast = False
config = None

if request.method == 'POST':
pgn_data = request.form['pgn']
print(request.form)
time_limit = request.form['time-limit']
depth_limit = request.form['depth-limit']

if request.form['limits'] == "time":
chess_review.STOCKFISH_CONFIG = {'time': float(time_limit)}
else:
chess_review.STOCKFISH_CONFIG = {'depth': int(depth_limit)}
config = {
"limit_type": request.form['limits'],
"time_limit": time_limit,
"depth_limit": depth_limit,
"roast": 'roastmode' in request.form
}

if ('roastmode' in request.form):
roast = True

uci_moves, san_moves, fens = chess_review.parse_pgn(pgn_data)

scores, cpls_white, cpls_black, average_cpl_white, average_cpl_black = chess_review.compute_cpl(uci_moves)
n_moves = len(scores)//2
white_elo_est, black_elo_est = chess_review.estimate_elo(average_cpl_white, n_moves), chess_review.estimate_elo(average_cpl_black, n_moves)
white_acc, black_acc = chess_review.calculate_accuracy(scores)
devs, mobs, tens, conts = chess_review.calculate_metrics(fens)

review_list, best_review_list, classification_list, uci_best_moves, san_best_moves = chess_review.review_game(uci_moves, roast)

uci_best_moves = chess_review.seperate_squares_in_move_list(uci_best_moves)
(
san_moves,
fens,
scores,
classification_list,
review_list,
best_review_list,
san_best_moves,
uci_best_moves,
devs,
tens,
mobs,
conts,
white_acc,
black_acc,
white_elo_est,
black_elo_est,
average_cpl_white,
average_cpl_black
) = chess_review.pgn_game_review(pgn_data=pgn_data, **config)

return render_template('analysis.html',
move_list = san_moves,
Expand Down