chess_move_f1e2 #55
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Update Chess | |
on: | |
issues: | |
types: [opened] | |
jobs: | |
update_chess: | |
concurrency: | |
group: chess-update | |
cancel-in-progress: true | |
runs-on: ubuntu-latest | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
ISSUE: ${{ github.event.issue.html_url }} | |
TITLE: ${{ github.event.issue.title }} | |
CHANGE: false | |
GAME_STATUS: "stillon" | |
if: startsWith(github.event.issue.title, 'chess_move_') || ${{ github.event.issue.title == 'chess_restart' }} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Chess Restart | |
if: ${{ github.event.issue.title == 'chess_restart' }} | |
run: | | |
game_status=$(jq -r '.stats.current_game' game.json) | |
if [ "$game_status" == "stillon" ]; then | |
gh issue comment $ISSUE --body "Game is still on." | |
exit 0 | |
fi | |
echo "CHANGE=true" >> $GITHUB_ENV | |
pip install chess | |
game_fen=$(jq -r '.restart_game_fen' game.json) | |
stats=$(jq -r '.stats.previous_games' game.json -c) | |
jq --arg a "$game_fen" --arg b "stillon" '.current_game_fen = $a | .stats.current_game = $b | .moves_history = []' game.json > tmp.json && mv tmp.json game.json | |
chess_board_markdown=$(python3 render/chess_board.py "$game_fen" "pieces/style-2") | |
moves_markdown=$(python3 render/moves.py "$game_fen") | |
stats_markdown=$(python3 render/stats.py "$stats") | |
echo "$chess_board_markdown" > README.md && echo "" >> README.md && echo "" >> README.md | |
echo "$moves_markdown" >> README.md && echo "" >> README.md && echo "" >> README.md | |
echo "$stats_markdown" >> README.md && echo "" >> README.md && echo "" >> README.md | |
- name: Check Valid Move | |
if: github.event.issue.title != 'chess_restart' | |
id: move | |
run: | | |
pip install chess | |
move=${TITLE#chess_move_} | |
game_fen=$(cat game.json | jq -r '.current_game_fen') | |
is_valid=$(python3 logic/is_valid_move.py "$game_fen" "$move") | |
echo "uci=$move" >> "$GITHUB_OUTPUT" | |
echo "is_valid=$is_valid" >> "$GITHUB_OUTPUT" | |
- name: Make Human Move | |
if: steps.move.outputs.is_valid == 'yes' | |
id: human_move | |
run: | | |
game_fen=$(cat game.json | jq -r '.current_game_fen') | |
move="${{ steps.move.outputs.uci }}" | |
username="${{ github.event.issue.user.login }}" | |
datetime=$(date -u +"%Y-%m-%d %H:%M:%S") | |
game_fen=$(python3 logic/make_move.py "$game_fen" "$move") | |
game_status=$(python3 logic/check_game_status.py "$game_fen") | |
jq --arg a "$game_fen" --arg b "$game_status" '.current_game_fen = $a | .stats.current_game = $b' game.json > temp.json && mv temp.json game.json | |
jq --arg a "$username" --arg b "${move:0:2}" --arg c "${move:2}" --arg d "$datetime" -r '.moves_history |= if length >= 5 then [{"datetime": $d, "human": {"username": $a, "from": $b, "to": $c}}] + .[0:-1] else [{"datetime": $d, "human": {"username": $a, "from": $b, "to": $c}}] + .[0:] end' game.json > temp.json && mv temp.json game.json | |
if [ "$game_status" == "stillon" ]; then | |
echo "gameover=no" >> $GITHUB_OUTPUT | |
else | |
if [ "$game_status" == "checkmate" ]; then | |
jq -r '.stats.previous_games.humans += 1' game.json > tmp.json && mv tmp.json game.json | |
echo "GAME_STATUS=humans" >> $GITHUB_ENV | |
else | |
jq -r '.stats.previous_games.draw += 1' game.json > tmp.json && mv tmp.json game.json | |
echo "GAME_STATUS=draw" >> $GITHUB_ENV | |
fi | |
echo "gameover=yes" >> $GITHUB_OUTPUT | |
fi | |
- name: Make Stockfish Move | |
id: stockfish_move | |
if: steps.human_move.outputs.gameover == 'no' | |
run: | | |
game_fen=$(cat game.json | jq -r '.current_game_fen') | |
move=$(python3 logic/compute_stockfish_move.py "$game_fen") | |
game_fen=$(python3 logic/make_move.py "$game_fen" "$move") | |
game_status=$(python3 logic/check_game_status.py "$game_fen") | |
jq --arg a "$game_fen" --arg b "$game_status" '.current_game_fen = $a | .stats.current_game = $b' game.json > temp.json && mv temp.json game.json | |
jq --arg b "${move:0:2}" --arg c "${move:2}" -r '.moves_history[0] |= . +{"stockfish": {"from": $b, "to": $c}}' game.json > temp.json && mv temp.json game.json | |
if [ "$game_status" == "stillon" ]; then | |
echo "gameover=no" >> $GITHUB_OUTPUT | |
else | |
if [ "$game_status" == "checkmate" ]; then | |
jq -r '.stats.previous_games.stockfish += 1' game.json > tmp.json && mv tmp.json game.json | |
echo "GAME_STATUS=stockfish" >> $GITHUB_ENV | |
else | |
jq -r '.stats.previous_games.draw += 1' game.json > tmp.json && mv tmp.json game.json | |
echo "GAME_STATUS=draw" >> $GITHUB_ENV | |
fi | |
echo "gameover=yes" >> $GITHUB_OUTPUT | |
fi | |
- name: Render (Game) | |
if: steps.stockfish_move.outputs.gameover == 'no' | |
run: | | |
game_fen=$(jq -r '.current_game_fen' game.json) | |
moves_history=$(jq -r '.moves_history' game.json -c) | |
stats=$(jq -r '.stats.previous_games' game.json -c) | |
chess_board_markdown=$(python3 render/chess_board.py "$game_fen" "pieces/style-2") | |
moves_markdown=$(python3 render/moves.py "$game_fen") | |
moves_history_markdown=$(python3 render/history.py "$moves_history") | |
stats_markdown=$(python3 render/stats.py "$stats") | |
echo "$chess_board_markdown" > README.md && echo "" >> README.md && echo "" >> README.md | |
echo "$moves_markdown" >> README.md && echo "" >> README.md && echo "" >> README.md | |
echo "$moves_history_markdown" >> README.md && echo "" >> README.md && echo "" >> README.md | |
echo "$stats_markdown" >> README.md && echo "" >> README.md && echo "" >> README.md | |
echo "CHANGE=true" >> $GITHUB_ENV | |
- name: Render (Game Over) | |
if: steps.human_move.outputs.gameover == 'yes' || steps.stockfish_move.outputs.gameover == 'yes' | |
run: | | |
game_fen=$(jq -r '.current_game_fen' game.json) | |
moves_history=$(jq -r '.moves_history' game.json -c) | |
stats=$(jq -r '.stats.previous_games' game.json -c) | |
chess_board_markdown=$(python3 render/chess_board.py "$game_fen" "pieces/style-2") | |
gif_restart_markdown=$(python3 render/restart.py "$GAME_STATUS") | |
moves_history_markdown=$(python3 render/history.py "$moves_history") | |
stats_markdown=$(python3 render/stats.py "$stats") | |
echo "$chess_board_markdown" > README.md && echo "" >> README.md && echo "" >> README.md | |
echo "$gif_restart_markdown" >> README.md && echo "" >> README.md && echo "" >> README.md | |
echo "$moves_history_markdown" >> README.md && echo "" >> README.md && echo "" >> README.md | |
echo "$stats_markdown" >> README.md && echo "" >> README.md && echo "" >> README.md | |
echo "CHANGE=true" >> $GITHUB_ENV | |
- name: Repo Update | |
run: | | |
if [ "$CHANGE" == "false" ]; then | |
exit 0 | |
fi | |
git config --global user.name "Tanishq Singh" | |
git config --global user.email "[email protected]" | |
git add README.md game.json | |
git commit -m 'CI: Updated `data.json` and `README.md`' | |
git push origin main | |
gh issue comment $ISSUE --body "Repo Update, Thankyou for your interest." | |
- name: IDK | |
if: ${{ failure() }} | |
run: gh issue comment $ISSUE --body "something went wrong." | |
- name: Not Valid UCI Move | |
if: steps.move.outputs.is_valid == 'no' | |
run: gh issue comment $ISSUE --body "Your move is not valid." | |
- name: Add Comment (if cancelled) | |
if: ${{ cancelled() }} | |
run: gh issue comment $ISSUE --body "Someone else just made another move! try again please!" | |
- name: Close Issue | |
if: ${{ always() }} | |
run: gh issue close $ISSUE |