feat(plugins/community/chess): add plugin (#1215) [skip ci]

This commit is contained in:
Simon Lecoq
2022-09-05 22:42:25 -04:00
committed by GitHub
parent d6636c00fe
commit 2239bc9297
18 changed files with 406 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
<!--header-->
<!--/header-->
## ➡️ Available options
<!--options-->
<!--/options-->
## Examples workflows
<!--examples-->
<!--/examples-->

View File

@@ -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

View File

@@ -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)
}
}

View File

@@ -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
}