Play Chess! Comment to play! Game#1 #160
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: Chess Workflow on Issue Comment | |
on: | |
issue_comment: | |
types: [created] | |
jobs: | |
Make-a-move: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Get chess move notation from comment | |
run: | | |
echo "COMMENT_BODY=${{ github.event.comment.body }}" >> $GITHUB_ENV | |
echo "${{ github.event.comment.user.login }} wants to move ${{ github.event.comment.body }}" | |
- name: Set up Python | |
uses: actions/setup-python@v2 | |
with: | |
python-version: "3.x" | |
cache: "pip" | |
- name: Generate Chessboard Image and update readme | |
run: | | |
# Install dependencies | |
pip install -r scripts/requirements.txt | |
# generate new chessboard image and export .env variables | |
# parameters: comment_body, comment_author, game_number | |
python scripts/export_chessboard.py "$COMMENT_BODY" @${{ github.event.comment.user.login }} ${{ github.event.issue.number }} | |
# export environment variables from .env file | |
while IFS= read -r line; do | |
echo "$line" >> $GITHUB_ENV | |
done < <(grep -v '^#' .env) | |
if [ ! -f "chessboard.png" ]; then | |
echo "Move Invalid, no chessboard.png file generated" | |
exit 0 | |
fi | |
# delete all other chessboard images | |
rm -f games/game${{ github.event.issue.number }}/chessboard*.png | |
# rename file to chessboard.png + timestamp | |
TIMESTAMP=-$(date +%s) | |
mv -f chessboard.png games/game${{ github.event.issue.number }}/chessboard$TIMESTAMP.png | |
# add Filename to github_env | |
echo "CHESSBOARD_FILENAME=games/game${{ github.event.issue.number }}/chessboard$TIMESTAMP.png" >> $GITHUB_ENV | |
- name: Commit changes | |
if: ${{ env.MOVE_STATUS == 'valid' }} | |
run: | | |
# parameters: moves, image_url, link_url | |
python scripts/update_readme.py "${{ env.GAME_HISTORY }}" "https://raw.githubusercontent.com/thieleju/thieleju/main/${{ env.CHESSBOARD_FILENAME }}" "https://github.com/thieleju/thieleju/issues/${{ github.event.issue.number }}" "${{ env.VALID_MOVES }}" | |
# commit changes | |
git config --global user.email "[email protected]" | |
git config --global user.name "GitHub Actions" | |
git add . | |
git commit -m "π Update chessboard image, readme and pgn" | |
git push origin HEAD | |
- name: Update issue | |
if: ${{ env.MOVE_STATUS == 'valid' && env.GAME_STATUS == 'in_progress' }} | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const moveHistory = "${{ env.GAME_HISTORY }}".replace(/\.\s/g, ".  ").replace(/\|/g, "<br>") | |
const gifUrl = "https://raw.githubusercontent.com/thieleju/thieleju/main/games/game${{ env.GAME_NUMBER }}/game.gif" | |
const issueBody = ` | |
### Comment on this issue to make a move! It's ${{ env.WHICH_TURN }}'s turn to move. | |
> Move has to be in Standard Algebraic Notation ([SAN](https://www.chess.com/terms/chess-notation)) (e4, Bf4, exd5, Qxf7+, ...) <br> | |
<div> | |
<img src="https://raw.githubusercontent.com/thieleju/thieleju/main/${{ env.CHESSBOARD_FILENAME }}" \> | |
</div> | |
### Legal moves for ${{ env.WHICH_TURN }}: | |
> ${{ env.VALID_MOVES }} | |
<details> | |
<summary>Show Move History</summary><br> | |
<div> | |
<img width="350" src="${gifUrl}" \> | |
</div> | |
${moveHistory} | |
</details> | |
` | |
await github.rest.issues.update({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: issueBody | |
}) | |
- name: Game Ended | |
if: ${{ env.GAME_STATUS != 'in_progress' }} | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const moveHistory = "${{ env.GAME_HISTORY }}".replace(/\.\s/g, ".  ").replace(/\|/g, "<br>") | |
const gifUrl = "https://raw.githubusercontent.com/thieleju/thieleju/main/games/game${{ env.GAME_NUMBER }}/game.gif" | |
const gameResult = "" | |
switch (${{ env.GAME_STATUS }}) { | |
case "white_wins": | |
gameResult = "White Won" | |
break; | |
case "black_wins": | |
gameResult = "Black Won" | |
break; | |
case "stalemate": | |
case "insufficient_material": | |
case "threefold_repetition": | |
case "draw": | |
gameResult = "Game ended in a draw" | |
break; | |
default: | |
gameResult = "*Game ended with unknown result*" | |
} | |
const issueBody = ` | |
### ${gameResult} | |
### Final Move History | |
<div> | |
<img width="350" src="${gifUrl}" \> | |
</div> | |
${moveHistory} | |
Thank you all for playing! | |
` | |
await github.rest.issues.update({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: issueBody | |
}) | |
await github.rest.issues.update({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
state: "closed" | |
}) | |
- name: React to issue comment | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
if ("${{ env.MOVE_STATUS }}" == 'valid') { | |
await github.rest.reactions.createForIssueComment({ | |
comment_id: "${{ github.event.comment.id }}", | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
content: '+1' | |
}) | |
} else if("${{ env.MOVE_STATUS }}" == 'invalid') { | |
await github.rest.reactions.createForIssueComment({ | |
comment_id: "${{ github.event.comment.id }}", | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
content: 'confused' | |
}) | |
} else { | |
await github.rest.reactions.createForIssueComment({ | |
comment_id: "${{ github.event.comment.id }}", | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
content: 'eyes' | |
}) | |
} |