更新计分板 #34112
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: 更新计分板 | |
concurrency: update-scoreboard | |
permissions: | |
issues: write | |
on: | |
workflow_dispatch: | |
inputs: | |
player_name: | |
description: '玩家名' | |
required: true | |
rows: | |
description: '行数' | |
required: true | |
cols: | |
description: '列数' | |
required: true | |
score: | |
description: '用时' | |
required: true | |
time: | |
description: '时间' | |
required: true | |
jobs: | |
add-a-line: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Get Comment | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const line = "| " + context.payload.inputs.player_name | |
+ " | " + context.payload.inputs.rows | |
+ " | " + context.payload.inputs.cols | |
+ " | " + context.payload.inputs.score | |
+ " | " + context.payload.inputs.time + " |"; | |
console.log(context.payload.inputs); | |
console.log(line); | |
core.exportVariable("line", line); | |
const fs = require("fs"); | |
// 获取当前计分板所使用的评论ID和下一个计分板的评论ID(后者不一定存在) | |
const id_list_body = (await github.rest.issues.getComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: 1605261184 | |
})).data.body; | |
const id_list = id_list_body.split("\r\n", 3); | |
console.log(id_list); | |
const id = id_list[1]; | |
core.exportVariable("id1", id); | |
core.exportVariable("id2", id_list[2]); | |
core.exportVariable("id_list_body", id_list_body); | |
let body = (await github.rest.issues.getComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: id | |
})).data.body; | |
fs.writeFileSync("old-body.txt", body); | |
const encoder = new TextEncoder(); | |
console.log(encoder.encode(body).length); | |
body += "\n" + line; | |
console.log(encoder.encode(body).length); | |
await io.mkdirP(".idea"); | |
fs.writeFileSync(".idea/comment.txt", body); | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.x' | |
- name: 生成新评论 | |
run: | | |
wget https://raw.githubusercontent.com/plazum/find-keke/master/high-score.py | |
python high-score.py | |
ls -alFR | |
- name: 更新评论 | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const fs = require("fs"); | |
const body = fs.readFileSync(".idea/processed.txt", "utf8"); | |
const body_length = (new TextEncoder()).encode(body).length; | |
console.log(body_length); | |
const record_count = parseInt(body.match(/<summary>展开(\d+)条记录/)[1]); | |
console.log(record_count); | |
// 当记录达到一定数量时,发布新评论,为新计分板作准备 | |
if (record_count >= 1800 && isNaN(process.env.id2)) { | |
const new_comment = (await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: 10, | |
body: "<!-->" | |
})).data; | |
console.log(new_comment); | |
let id_list_body = process.env.id_list_body.split("\r\n"); | |
id_list_body.splice(2, 0, new_comment.id); | |
const new_id_list_body = id_list_body.join("\r\n"); | |
console.log(new_id_list_body); | |
await github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: 1605261184, | |
body: new_id_list_body | |
}); | |
} | |
console.log(body); | |
if (record_count <= 2000) { | |
await github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: process.env.id1, | |
body: body | |
}); | |
} else { | |
let old_body = fs.readFileSync("old-body.txt", "utf8").split("\n"); | |
old_body[0] = old_body[0].replaceAll(/<!--|-->/g, "").replace("next_id", process.env.id2); | |
[old_body[1], old_body[2]] = [old_body[2], old_body[1]]; | |
const details_index = old_body.findIndex(s => s.startsWith("# 计分板")); // 其实是“# 计分板”一行的index | |
const board_num = parseInt(old_body[details_index].slice(5)) + 1; | |
old_body.splice(details_index + 4, 1); // 如果先删前面的,后面的坐标会偏移 | |
old_body.splice(details_index + 1, 1); | |
old_body.push("</details>"); | |
const new_old_body = old_body.join("\n"); | |
// 启用新计分板 | |
const new_body = "<!--" + process.env.id1 | |
+ "-->\n# 计分板" + board_num + "\n| --- | --- | --- | --- | --- |\n" + process.env.line; | |
console.log(new_body); | |
await github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: process.env.id2, | |
body: new_body | |
}); | |
// 更新最新计分板所使用的评论ID | |
let id_list_body = process.env.id_list_body.split("\r\n"); | |
id_list_body[0] += " " + process.env.id2; | |
id_list_body.splice(1, 1); | |
const new_id_list_body = id_list_body.join("\r\n"); | |
console.log(new_id_list_body); | |
await github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: 1605261184, | |
body: new_id_list_body | |
}); | |
// 更新指向最新计分板的链接 | |
let issue_body = (await github.rest.issues.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: 10 | |
})).data.body; | |
console.log(issue_body); | |
issue_body = issue_body.replace(/\d+/, process.env.id2); | |
console.log(issue_body); | |
await github.rest.issues.update({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: 10, | |
body: issue_body | |
}); | |
// 给旧的计分板作收尾工作 | |
console.log(new_old_body); | |
await github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: process.env.id1, | |
body: new_old_body | |
}); | |
} |