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
1 parent dd7f1cc commit 0e4a2cd
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
73 changes: 73 additions & 0 deletions .github/scripts/reporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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 top = formattedData.filter((row) => row["#"] <= 10).slice(0, 10);

// top10に自チームが含まれているか確認
const isInTop10 = top.some((row) => row.team === TEAM_NAME);

// メッセージ作成、自分のチームがあれば強調
let message = `
## Scoreboard
time: ${new Date().toLocaleString("ja-JP", { timeZone: "Asia/Tokyo" })}
=== Top 10 ===
`.trim();
message += "\n";
message += top
.map((row) => {
const rank = row["#"];
const team = row.team;
const isUs = row.team === TEAM_NAME;
return `${isUs ? "**" : ""}${rank}\\. ${team}${isUs ? "**" : ""}`;
})
.join("\n");

// 自分のチームが上位10に含まれていない場合はメッセージに追加
if (!isInTop10) {
const rank = formattedData.find((row) => row.team === TEAM_NAME)["#"];
message += `\n~ ~ ~ ~ ~ ~\n**${rank}\\. ${TEAM_NAME}**`;
}

console.log(message);

const res = await fetch(DISCORD_WEBHOOK_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ content: message }),
});

if (!res.ok) {
console.error(res);
throw new Error(`Failed to post to Discord: ${res.statusText}`);
}

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 0e4a2cd

Please sign in to comment.