UpdateBoard #400
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: "UpdateBoard" | |
on: | |
issue_comment: | |
types: [created] | |
jobs: | |
move: | |
runs-on: ubuntu-latest | |
if: startsWith(github.event.issue.title, '[CHESS]') | |
steps: | |
- uses: actions/checkout@v3 | |
- uses: actions/setup-python@v4 | |
with: | |
python-version: '3.10' | |
- run: pip install chess | |
- name: Find Player Comment | |
uses: peter-evans/find-comment@v2 | |
id: fcc | |
with: | |
issue-number: ${{ github.event.issue.number }} | |
comment-author: "github-actions[bot]" | |
body-includes: "## New Game!" | |
- name: Find Board Comment | |
uses: peter-evans/find-comment@v2 | |
id: fcb | |
with: | |
issue-number: ${{ github.event.issue.number }} | |
comment-author: "github-actions[bot]" | |
body-includes: "## Board" | |
- name: Find Player Colors | |
id: colors | |
run: | | |
WHITE="$(echo '${{ steps.fcc.outputs.comment-body }}' | grep -oP '(?<=### @)(.*)(?= \(White\))')" | |
BLACK="$(echo '${{ steps.fcc.outputs.comment-body }}' | grep -oP '(?<=// @)(.*)(?= \(Black\))')" | |
echo "white=$WHITE" >> $GITHUB_OUTPUT | |
echo "black=$BLACK" >> $GITHUB_OUTPUT | |
- name: Parse Board | |
id: parser | |
run: | | |
CURRFEN="$(echo '${{ steps.fcb.outputs.comment-body }}' | grep -oP '(?<=`)(.*)(?=`)')" | |
echo "current_fen=$CURRFEN" >> $GITHUB_OUTPUT | |
- name: Play Move | |
id: fen | |
run: | | |
FEN="$(python ./scripts/chessmove.py '${{steps.parser.outputs.current_fen}}' '${{github.event.comment.body}}')" | |
echo "fen=$FEN" >> $GITHUB_OUTPUT | |
toplay="$(echo $FEN | cut -d' ' -f2 )" | |
customfen="$(echo $FEN | cut -d' ' -f7 )" | |
if [ ${toplay} == "w" ]; then | |
echo "header=:White to Move" >> $GITHUB_OUTPUT | |
elif [ ${toplay} == "b" ]; then | |
echo "header=:Black to Move" >> $GITHUB_OUTPUT | |
fi | |
if [ ${customfen} == "I" ]; then | |
echo "invalid=:true" >> $GITHUB_OUTPUT | |
elif [ ${customfen} == "W" ]; then | |
if [ ${toplay} == "w" ]; then | |
echo "winner=:Black" >> $GITHUB_OUTPUT | |
echo "header=:Black Wins!" >> $GITHUB_OUTPUT | |
elif [ ${toplay} == "b" ]; then | |
echo "winner=:White" >> $GITHUB_OUTPUT | |
echo "header=:White Wins!" >> $GITHUB_OUTPUT | |
fi | |
elif [ ${customfen} == "D" ]; then | |
echo "draw=:true" >> $GITHUB_OUTPUT | |
echo "header=:Draw!" >> $GITHUB_OUTPUT | |
fi | |
- name: Convert Move to md | |
id: board | |
run: | | |
BOARD="$(python ./scripts/fen2md.py '${{steps.fen.outputs.fen}}' | python ./scripts/replace_images.py)" | |
echo "board<<EOF" >> "$GITHUB_OUTPUT" | |
echo "$BOARD" >> "$GITHUB_OUTPUT" | |
echo "EOF" >> "$GITHUB_OUTPUT" | |
- name: Edit Board Comment | |
uses: peter-evans/create-or-update-comment@v2 | |
with: | |
comment-id: ${{ steps.fcb.outputs.comment-id }} | |
edit-mode: replace | |
body: | | |
## Board | |
### ${{ steps.fen.outputs.header }} | |
`${{steps.fen.outputs.fen}}` | |
${{ steps.board.outputs.board }} | |
- name: React to Invalid Moves | |
if: ${{ steps.fen.outputs.invalid }} | |
uses: peter-evans/create-or-update-comment@v2 | |
with: | |
comment-id: ${{ github.event.comment.id }} | |
reactions: confused | |
- name: Close finished games | |
if: ${{ steps.fen.outputs.draw || steps.fen.outputs.winner }} | |
uses: peter-evans/close-issue@v2 | |
with: | |
issue-number: ${{ github.event.issue.number }} | |
- name: Decide label | |
if: ${{ steps.fen.outputs.draw || steps.fen.outputs.winner }} | |
id: labeler | |
run: | | |
if [ "${{steps.fen.outputs.draw}}" = true ]; then | |
echo "label=:draw" >> $GITHUB_OUTPUT | |
elif [ ${{steps.fen.outputs.winner}} == "Black" ]; then | |
if [ ${{steps.colors.outputs.black}} == ${{ github.event.repository.owner.login }} ]; then | |
echo "label=:win" >> $GITHUB_OUTPUT | |
elif [ ${{steps.colors.outputs.black}} == ${{ github.event.issue.user.login }} ]; then | |
echo "label=:lose" >> $GITHUB_OUTPUT | |
fi | |
elif [ ${{steps.fen.outputs.winner}} == "White" ]; then | |
if [ ${{steps.colors.outputs.white}} == ${{ github.event.repository.owner.login }} ]; then | |
echo "label=:win" >> $GITHUB_OUTPUT | |
elif [ ${{steps.colors.outputs.white}} == ${{ github.event.issue.user.login }} ]; then | |
echo "label=:lose" >> $GITHUB_OUTPUT | |
fi | |
fi | |
- name: Apply Label to closed issue | |
uses: actions-ecosystem/action-add-labels@v1 | |
if: ${{ steps.fen.outputs.draw || steps.fen.outputs.winner }} | |
with: | |
github_token: ${{ secrets.github_token }} | |
labels: ${{steps.labeler.outputs.label}} |