Skip to content

Commit

Permalink
feat(plugins/community/chess): add plugin (#1215) [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter authored Sep 6, 2022
1 parent d6636c0 commit 2239bc9
Show file tree
Hide file tree
Showing 18 changed files with 406 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/actions/spelling/excludes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ ignore$
^\Qsource/templates/terminal/fonts.css\E$
^\Qsource/templates/terminal/partials/screenshot.ejs\E$
^\Qtests/mocks/api/github/rest/emojis/get.mjs\E$
^\Qtests/mocks/api/axios/get/lichess.mjs\E$
3 changes: 3 additions & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ libxml
libxmljs
libxss
libxtst
lichess
linux
lng
localhost
Expand Down Expand Up @@ -236,6 +237,8 @@ params
patchnote
pdated
pened
PGN
Pgn
PGP
playcount
playlists
Expand Down
4 changes: 4 additions & 0 deletions .github/actions/spelling/patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ place_id: ".*"

# ignore long runs of a single character:
\b([A-Za-z])\g{-1}{3,}\b

# Chess patterns
\brnbqkbnr\b
\bRNBQKBNR\b
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions .github/scripts/files/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ on:
required: true
METRICS_TOKEN_PERSONAL:
required: true
CHESS_TOKEN:
required: true
PAGESPEED_TOKEN:
required: true
GOOGLE_MAP_TOKEN:
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"transform": {}
},
"optionalDependencies": {
"chess.js": "^1.0.0-alpha.0",
"gifencoder": "^2.0.1",
"libxmljs2": "^0.30.1",
"node-chartist": "^1.0.5"
Expand Down
24 changes: 24 additions & 0 deletions source/app/web/statics/embed/app.placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,30 @@
},
})
: null),
//Chess
...(set.plugins.enabled.chess
? ({
chess: {
platform: options["chess.platform"] || "(chess platform)",
meta: {
Event: "Casual Correspondence game",
Date: faker.date.recent().toISOString().substring(0, 10),
White: options["chess.user"],
Black: faker.internet.userName(),
WhiteElo: faker.datatype.number(3000),
BlackElo: faker.datatype.number(3000),
},
animation: { size: 40, delay: 3, duration: 0.6 },
result: { white: faker.datatype.number(3), get black() { return this.white + faker.helpers.arrayElement([-1, +1]) } },
moves: [
{color: "w", piece: "p", from: "f2", to: "f4", san: "f4", flags: "b"},
{color: "b", piece: "p", from: "c7", to: "c5", san: "c5", flags: "b"},
{color: "w", piece: "p", from: "e2", to: "e4", san: "e4", flags: "b"},
{color: "b", piece: "p", from: "d7", to: "d6", san: "d6", flags: "n"},
]
},
})
: null),
//Activity
...(set.plugins.enabled.activity
? ({
Expand Down
12 changes: 12 additions & 0 deletions source/plugins/community/chess/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!--header-->
<!--/header-->

## ➡️ Available options

<!--options-->
<!--/options-->

## ℹ️ Examples workflows

<!--examples-->
<!--/examples-->
10 changes: 10 additions & 0 deletions source/plugins/community/chess/examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- name: Last chess game from lichess.org
uses: lowlighter/metrics@latest
with:
filename: metrics.plugin.chess.svg
token: NOT_NEEDED
base: ""
plugin_chess: yes
plugin_chess_token: ${{ secrets.CHESS_TOKEN }}
plugin_chess_platform: lichess.org

48 changes: 48 additions & 0 deletions source/plugins/community/chess/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//Imports
import { Chess } from "chess.js"

//Setup
export default async function({login, q, imports, data, account}, {enabled = false, token = "", extras = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!q.chess) || (!imports.metadata.plugins.chess.enabled(enabled, {extras})))
return null

//Load inputs
const {user, platform, animation} = imports.metadata.plugins.chess.inputs({data, account, q})
for (const [key, defaulted] of Object.entries({size:40, delay:1, duration:4})) {
if (Number.isNaN(Number(animation[key])))
animation[key] = defaulted
if (animation[key] < 0)
animation[key] = defaulted
}

//Fetch PGN
console.debug(`metrics/compute/${login}/plugins > chess > fetching last game from ${platform}`)
let PGN
switch (platform) {
case "lichess.org":
PGN = (await imports.axios.get(`https://lichess.org/api/games/user/${user}?max=1`, {headers: {Authorization: `Bearer ${token}`}})).data
break
case "":
throw {error: {message: "Unspecified platform"}}
default:
throw {error: {message: `Unsupported platform "${platform}"`}}
}

//Parse PGN
const board = new Chess()
board.loadPgn(PGN)
const moves = board.history({verbose: true})
const meta = board.header()
const result = Object.fromEntries(meta.Result.split("-").map((score, i) => [i ? "black" : "white", Number(score)]))

//Results
return {platform, meta, moves, animation, result}
}
//Handle errors
catch (error) {
throw imports.format.error(error)
}
}
61 changes: 61 additions & 0 deletions source/plugins/community/chess/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: ♟️ Chess
category: community
description: |
This plugin displays the last game you played on a supported chess platform.
disclaimer: |
This plugin is not affiliated, associated, authorized, endorsed by, or in any way officially connected with any of the supported provider.
All product and company names are trademarks™ or registered® trademarks of their respective holders.
examples:
default: https://github.com/lowlighter/metrics/blob/examples/metrics.plugin.chess.svg
authors:
- lowlighter
supports:
- user
- organization
- repository
scopes: []
inputs:

plugin_chess:
description: |
Enable chess plugin
type: boolean
default: no

plugin_chess_token:
description: |
Chess platform token
type: token
default: ""
extras:
- metrics.api.chess.any

plugin_chess_user:
description: |
AniList login
type: string
default: .user.login
preset: no

plugin_chess_platform:
description: |
Chess platform
type: string
default: ""
values:
- lichess.org

plugin_chess_animation:
description: |
Animation settings
- `size` is the size of a single chessboard square in pixels (board will be 8 times larger)
- `delay` is the delay before starting animation (in seconds)
- `duration` is the duration of the animation of a move (in seconds)
type: json
default: |
{
"size": 40,
"delay": 3,
"duration": 0.6
}
1 change: 1 addition & 0 deletions source/templates/classic/partials/_.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"achievements",
"screenshot",
"code",
"chess",
"sponsors",
"poopmap",
"fortune"
Expand Down
Loading

0 comments on commit 2239bc9

Please sign in to comment.