Skip to content

Commit

Permalink
feat: implement reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
sor4chi committed Jun 28, 2024
0 parents commit fbc2c6f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
58 changes: 58 additions & 0 deletions .github/scripts/reporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const SCOREBOARD_API = "https://boundvariable.space/scoreboard";
const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL;
const TEAM_NAME = "Maximum";

async function postToDiscord() {
try {
const response = await fetch(SCOREBOARD_API);
const data = await response.json();

// dataを構造体に変換
const formattedData = data.rows.map((row) => {
const columns = data.columns;
const obj = {};
columns.forEach((column, index) => {
obj[column] = row.values[index];
});
return obj;
});

// dataを#順にソート
formattedData.sort((a, b) => a["#"] - b["#"]);

// 上位10チーム+自チームのみ抽出
const top10WithUs = formattedData.filter(
(row) => row.team === TEAM_NAME || row["#"] <= 10
);

// メッセージ作成、自分のチームを強調
const message = top10WithUs
.map((row) => {
const rank = row["#"];
const team = row.team;
const isUs = row.team === TEAM_NAME;
return `${isUs ? "**" : ""}${rank}. ${team}${isUs ? "**" : ""}`;
})
.join("\n");

const now = new Date().toLocaleString("ja-JP", {
timeZone: "Asia/Tokyo",
});

await fetch(DISCORD_WEBHOOK_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
content: `**Scoreboard**\n\`\`\`${message}\`\`\`\nLast updated: ${now}`,
}),
});

console.log("Posted to Discord successfully.");
} catch (error) {
console.error("Error posting to Discord:", error);
}
}

postToDiscord();
23 changes: 23 additions & 0 deletions .github/workflows/reporter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: ICFPC2024 Leaderboard Reporter

on:
schedule:
- cron: "0 * * * *"
push:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Bun
uses: oven-sh/setup-bun@v1

- name: Run
run: bun run .github/scripts/reporter.ts
env:
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}

0 comments on commit fbc2c6f

Please sign in to comment.