Skip to content

Commit abf94e4

Browse files
committed
feat: implement reporter
0 parents  commit abf94e4

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

.github/scripts/reporter.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const SCOREBOARD_API = "https://boundvariable.space/scoreboard";
2+
const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL;
3+
const TEAM_NAME = "Maximum";
4+
5+
async function postToDiscord() {
6+
try {
7+
const response = await fetch(SCOREBOARD_API);
8+
const data = await response.json();
9+
10+
// dataを構造体に変換
11+
const formattedData = data.rows.map((row) => {
12+
const columns = data.columns;
13+
const obj = {};
14+
columns.forEach((column, index) => {
15+
obj[column] = row.values[index];
16+
});
17+
return obj;
18+
});
19+
20+
// dataを#順にソート
21+
formattedData.sort((a, b) => a["#"] - b["#"]);
22+
23+
// 上位10チームのみ抽出
24+
const top = formattedData.filter((row) => row["#"] <= 10).slice(0, 10);
25+
26+
// top10に自チームが含まれているか確認
27+
const isInTop10 = top.some((row) => row.team === TEAM_NAME);
28+
29+
// メッセージ作成、自分のチームがあれば強調
30+
let message = `
31+
### Scoreboard
32+
time: ${new Date().toLocaleString("ja-JP", { timeZone: "Asia/Tokyo" })}
33+
34+
=== Top 10 ===
35+
`.trim();
36+
message += top
37+
.map((row) => {
38+
const rank = row["#"];
39+
const team = row.team;
40+
const isUs = row.team === TEAM_NAME;
41+
return `${isUs ? "**" : ""}${rank}. ${team}${isUs ? "**" : ""}`;
42+
})
43+
.join("\n");
44+
45+
// 自分のチームが上位10に含まれていない場合はメッセージに追加
46+
if (!isInTop10) {
47+
const rank = formattedData.find((row) => row.team === TEAM_NAME)["#"];
48+
message += `\n...\n**${rank}. ${TEAM_NAME}**`;
49+
}
50+
51+
console.log(message);
52+
53+
const res = await fetch(DISCORD_WEBHOOK_URL, {
54+
method: "POST",
55+
headers: {
56+
"Content-Type": "application/json",
57+
},
58+
body: JSON.stringify({ content: message }),
59+
});
60+
61+
if (!res.ok) {
62+
console.error(res);
63+
throw new Error(`Failed to post to Discord: ${res.statusText}`);
64+
}
65+
66+
console.log("Posted to Discord successfully.");
67+
} catch (error) {
68+
console.error("Error posting to Discord:", error);
69+
}
70+
}
71+
72+
postToDiscord();

.github/workflows/reporter.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: ICFPC2024 Leaderboard Reporter
2+
3+
on:
4+
schedule:
5+
- cron: "0 * * * *"
6+
push:
7+
branches:
8+
- main
9+
10+
jobs:
11+
lint:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v3
16+
17+
- name: Setup Bun
18+
uses: oven-sh/setup-bun@v1
19+
20+
- name: Run
21+
run: bun run .github/scripts/reporter.ts
22+
env:
23+
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}

0 commit comments

Comments
 (0)