Feat: 地震の震度分布画像生成 / Chg: 震度分布画像のズームレベルを8から9へ / Chg: 震度分布画像の生成条件を震源と位置が存在するへ変更 / Feat: 震度分布画に全タイルが埋まる機能 / Feat: 震度分布画像の震源がタイル単位で中央になる機能 / Feat: 震度分布画像で欠けているタイルが描画される機能 / Chg: 震度分布画像のアセットを並列に取得 / Feat: 震度分布画像のアセットの位置が中央基準になる機能 / Chg: 震度分布画像の震源のアセットに縁取りを追加 / Chg: 震度分布画像の震度ではないアセットを拡大 / Chg: ユーズの分割の案内文を太字に / Del: 最大震度が不明な場合に投稿するかどうかのconfigを削除
This commit is contained in:
+1
-1
@@ -3,4 +3,4 @@
|
|||||||
/memory.json
|
/memory.json
|
||||||
/config/**
|
/config/**
|
||||||
!/config/example.yaml
|
!/config/example.yaml
|
||||||
/src/feature/earthquake/generateImage/data.json
|
/src/feature/earthquake/generateImage/data*.json
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ earthquake:
|
|||||||
# 例: 30を指定すると、最大震度が震度3以上の地震発生情報のみを投稿します。
|
# 例: 30を指定すると、最大震度が震度3以上の地震発生情報のみを投稿します。
|
||||||
# 10(震度1), 20(震度2), 30(震度3), 40(震度4), 45(震度5弱), 50(震度5強), 55(震度6弱), 60(震度6強), 70(震度7)が有効です。
|
# 10(震度1), 20(震度2), 30(震度3), 40(震度4), 45(震度5弱), 50(震度5強), 55(震度6弱), 60(震度6強), 70(震度7)が有効です。
|
||||||
requireMaxScale: 30
|
requireMaxScale: 30
|
||||||
# 最大震度が不明な地震発生情報を投稿するかどうか boolean
|
|
||||||
canUnknownMaxScale: true
|
|
||||||
# 過去のデバッグ用データを使用して地震情報を配信するかどうか boolean
|
# 過去のデバッグ用データを使用して地震情報を配信するかどうか boolean
|
||||||
# デバッグ用途のみで使用してください。
|
# デバッグ用途のみで使用してください。
|
||||||
useHistoryData: false
|
useHistoryData: false
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 693 B After Width: | Height: | Size: 559 B |
Binary file not shown.
|
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 17 KiB |
@@ -153,12 +153,12 @@ function getEdge<T extends any[]>(arr: T, property: keyof T[number]) {
|
|||||||
|
|
||||||
export default async function generateImage(message: any) {
|
export default async function generateImage(message: any) {
|
||||||
if (
|
if (
|
||||||
message.earthquake.hypocenter === undefined &&
|
message.earthquake.hypocenter === undefined ||
|
||||||
message.points === undefined
|
message.points === undefined
|
||||||
)
|
)
|
||||||
return "input_lack";
|
return "input_lack";
|
||||||
|
|
||||||
const ZOOM_LEVEL = 8;
|
const ZOOM_LEVEL = 9;
|
||||||
|
|
||||||
// タイル・地点取得
|
// タイル・地点取得
|
||||||
const tiles: {
|
const tiles: {
|
||||||
@@ -278,23 +278,120 @@ export default async function generateImage(message: any) {
|
|||||||
scale: number;
|
scale: number;
|
||||||
}))[] = [];
|
}))[] = [];
|
||||||
|
|
||||||
// タイルのXYの各最大、最小を取得
|
// タイルの幅、最大最小
|
||||||
const tileXCount = getEdge(tiles, "tileX");
|
let tileXCount = getEdge(tiles, "tileX");
|
||||||
const tileYCount = getEdge(tiles, "tileY");
|
let tileYCount = getEdge(tiles, "tileY");
|
||||||
|
let xSize = tileXCount.most - tileXCount.least + 1;
|
||||||
|
let ySize = tileYCount.most - tileYCount.least + 1;
|
||||||
|
|
||||||
// タイルサイズ決定
|
// タイルサイズ仮計算
|
||||||
let tileSize: number;
|
let tileSize: number;
|
||||||
|
|
||||||
const xSize = tileXCount.most - tileXCount.least + 1;
|
xSize = tileXCount.most - tileXCount.least + 1;
|
||||||
const ySize = tileYCount.most - tileYCount.least + 1;
|
ySize = tileYCount.most - tileYCount.least + 1;
|
||||||
|
|
||||||
if (xSize > ySize) {
|
if (xSize > ySize) {
|
||||||
tileSize = Math.round(WIDTH / xSize);
|
tileSize = Math.round(WIDTH / xSize);
|
||||||
} else {
|
} else {
|
||||||
tileSize = Math.round(HEIGHT / ySize);
|
tileSize = Math.round(HEIGHT / ySize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 全画面
|
||||||
|
if (WIDTH - xSize * tileSize > 10) {
|
||||||
|
const requireTilesX = Math.ceil(WIDTH / tileSize);
|
||||||
|
const halfTilesX = Math.ceil(requireTilesX / 2);
|
||||||
|
|
||||||
|
tileXCount = {
|
||||||
|
least: tileXCount.least - halfTilesX,
|
||||||
|
most: tileXCount.most + halfTilesX,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (HEIGHT - ySize * tileSize > 10) {
|
||||||
|
const requireTilesY = Math.ceil(HEIGHT / tileSize);
|
||||||
|
const halfTilesY = Math.ceil(requireTilesY / 2);
|
||||||
|
|
||||||
|
tileYCount = {
|
||||||
|
least: tileYCount.least - halfTilesY,
|
||||||
|
most: tileYCount.most + halfTilesY,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 震源を中心とする
|
||||||
|
const epicenterPosition = positions.filter(position => position.type === "epicenter")[0];
|
||||||
|
if (!epicenterPosition)
|
||||||
|
return "input_lack";
|
||||||
|
|
||||||
|
const epicenterTile = tiles[epicenterPosition.tileIndex];
|
||||||
|
if (!epicenterTile)
|
||||||
|
return "input_lack";
|
||||||
|
|
||||||
|
const leftPx = (epicenterTile.tileX - tileXCount.least) * tileSize + epicenterPosition.innerX;
|
||||||
|
const rightPx = xSize * tileSize - leftPx;
|
||||||
|
const topPx = (epicenterTile.tileY - tileYCount.least) * tileSize + epicenterPosition.innerY;
|
||||||
|
const bottomPx = ySize * tileSize - topPx;
|
||||||
|
|
||||||
|
if (leftPx > rightPx) {
|
||||||
|
const leftTiles = epicenterTile.tileX - tileXCount.least;
|
||||||
|
tileXCount = {
|
||||||
|
...tileXCount,
|
||||||
|
most: epicenterTile.tileX + leftTiles,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const rightTiles = tileXCount.most - epicenterTile.tileX;
|
||||||
|
tileXCount = {
|
||||||
|
...tileXCount,
|
||||||
|
least: epicenterTile.tileX - rightTiles,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (topPx > bottomPx) {
|
||||||
|
const topTiles = epicenterTile.tileY - tileYCount.least;
|
||||||
|
tileYCount = {
|
||||||
|
...tileYCount,
|
||||||
|
most: epicenterTile.tileY + topTiles,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const bottomTiles = tileYCount.most - epicenterTile.tileY;
|
||||||
|
tileYCount = {
|
||||||
|
...tileYCount,
|
||||||
|
least: epicenterTile.tileY - bottomTiles,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// タイルサイズ再計算
|
||||||
|
xSize = tileXCount.most - tileXCount.least + 1;
|
||||||
|
ySize = tileYCount.most - tileYCount.least + 1;
|
||||||
|
|
||||||
|
if (xSize > ySize) {
|
||||||
|
tileSize = Math.round(WIDTH / xSize);
|
||||||
|
} else {
|
||||||
|
tileSize = Math.round(HEIGHT / ySize);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 欠けているタイルを取得
|
||||||
|
for (let xIndex = 0; xIndex < xSize; xIndex++) {
|
||||||
|
const itX = xIndex + tileXCount.least;
|
||||||
|
|
||||||
|
for (let yIndex = 0; yIndex < ySize; yIndex++) {
|
||||||
|
const itY = yIndex + tileYCount.least;
|
||||||
|
|
||||||
|
const itTile = tiles.filter(tile => tile.tileX === itX && tile.tileY === itY)[0];
|
||||||
|
if (itTile)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
tiles.push({
|
||||||
|
tileX: itX,
|
||||||
|
tileY: itY,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// アセット
|
// アセット
|
||||||
const assets: Record<string, Buffer<ArrayBuffer>> = {}
|
const assets: Record<string, {
|
||||||
|
buffer: Buffer<ArrayBuffer>;
|
||||||
|
size: number;
|
||||||
|
}> = {}
|
||||||
const assetsList = [
|
const assetsList = [
|
||||||
"scales/10",
|
"scales/10",
|
||||||
"scales/20",
|
"scales/20",
|
||||||
@@ -309,15 +406,20 @@ export default async function generateImage(message: any) {
|
|||||||
"epicenter",
|
"epicenter",
|
||||||
];
|
];
|
||||||
|
|
||||||
assetsList.forEach(name => {
|
await Promise.all(assetsList.map(async name => {
|
||||||
const asset = readFileSync(`${import.meta.dirname}/assets/${name}.png`);
|
|
||||||
|
|
||||||
const key = name.split("/").at(-1);
|
const key = name.split("/").at(-1);
|
||||||
if (!key)
|
if (!key)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
assets[key] = asset;
|
const asset = readFileSync(`${import.meta.dirname}/assets/${name}.png`);
|
||||||
});
|
const assetMeta = await sharp(asset).metadata();
|
||||||
|
const assetSize = assetMeta.width;
|
||||||
|
|
||||||
|
assets[key] = {
|
||||||
|
buffer: asset,
|
||||||
|
size: assetSize,
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
// 各画像処理
|
// 各画像処理
|
||||||
await Promise.all(tiles.map(async (tile, index) => {
|
await Promise.all(tiles.map(async (tile, index) => {
|
||||||
@@ -371,9 +473,9 @@ export default async function generateImage(message: any) {
|
|||||||
|
|
||||||
compounds.push({
|
compounds.push({
|
||||||
...about,
|
...about,
|
||||||
input: asset,
|
input: asset.buffer,
|
||||||
top: Math.round(top + position.innerY * tileResizedSize),
|
top: Math.round(top + position.innerY * tileResizedSize - asset.size / 2),
|
||||||
left: Math.round(left + position.innerX * tileResizedSize),
|
left: Math.round(left + position.innerX * tileResizedSize - asset.size / 2),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
@@ -424,6 +526,4 @@ export default async function generateImage(message: any) {
|
|||||||
|
|
||||||
const buffer = await result.png().toBuffer();
|
const buffer = await result.png().toBuffer();
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
writeFileSync("result.png", await generateImage(JSON.parse(readFileSync(`${import.meta.dirname}/data.json`, "utf8"))));
|
|
||||||
@@ -7,6 +7,7 @@ import i18next from "i18next";
|
|||||||
import { readFileSync } from "node:fs";
|
import { readFileSync } from "node:fs";
|
||||||
import { EOL } from "node:os";
|
import { EOL } from "node:os";
|
||||||
import { WebSocket } from "ws";
|
import { WebSocket } from "ws";
|
||||||
|
import generateImage from "@/feature/earthquake/generateImage";
|
||||||
|
|
||||||
await initI18n();
|
await initI18n();
|
||||||
|
|
||||||
@@ -104,15 +105,6 @@ const processMessage = async (message: any) => {
|
|||||||
{
|
{
|
||||||
console.log("地震発生情報を受信しました");
|
console.log("地震発生情報を受信しました");
|
||||||
|
|
||||||
if (
|
|
||||||
(message.earthquake.maxScale === -1 ||
|
|
||||||
message.earthquake.maxScale === undefined) &&
|
|
||||||
!config.earthquake.canUnknownMaxScale
|
|
||||||
) {
|
|
||||||
console.log("最大震度が不明であり、最大震度が不明な場合の投稿が許可されていないため、スキップします");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
message.earthquake.maxScale !== -1 &&
|
message.earthquake.maxScale !== -1 &&
|
||||||
message.earthquake.maxScale < config.earthquake.requireMaxScale
|
message.earthquake.maxScale < config.earthquake.requireMaxScale
|
||||||
@@ -177,33 +169,61 @@ const processMessage = async (message: any) => {
|
|||||||
.join(EOL.repeat(2))
|
.join(EOL.repeat(2))
|
||||||
.trim();
|
.trim();
|
||||||
|
|
||||||
await createUeuse({
|
let earthquakeUniqid: string | null = null;
|
||||||
text: i18next.t("earthquakeNotice", {
|
|
||||||
type: typeMessage[message.issue.type] ?? "地震情報",
|
await Promise.allSettled([
|
||||||
occuredTime: format(new Date(message.earthquake.time), "yyyy年M月d日 H:mm"),
|
(async () => {
|
||||||
maxScale: scaleMessages[String(message.earthquake.maxScale)],
|
const ueuses = await createUeuse({
|
||||||
epicenter: message.earthquake.hypocenter.name === ""
|
text: i18next.t("earthquakeNotice", {
|
||||||
? "不明"
|
type: typeMessage[message.issue.type] ?? "地震情報",
|
||||||
: message.earthquake.hypocenter.name,
|
occuredTime: format(new Date(message.earthquake.time), "yyyy年M月d日 H:mm"),
|
||||||
magnitude: message.earthquake.hypocenter.magnitude === -1
|
maxScale: scaleMessages[String(message.earthquake.maxScale)],
|
||||||
? "不明"
|
epicenter: message.earthquake.hypocenter.name === ""
|
||||||
: `M${message.earthquake.hypocenter.magnitude.toFixed(1)}`,
|
? "不明"
|
||||||
depth: message.earthquake.hypocenter.depth === 0
|
: message.earthquake.hypocenter.name,
|
||||||
? "ごく浅い"
|
magnitude: message.earthquake.hypocenter.magnitude === -1
|
||||||
: (message.earthquake.hypocenter.depth === -1
|
? "不明"
|
||||||
? "不明"
|
: `M${message.earthquake.hypocenter.magnitude.toFixed(1)}`,
|
||||||
: `${message.earthquake.hypocenter.depth}km`),
|
depth: message.earthquake.hypocenter.depth === 0
|
||||||
domesticTsunami: domesticTsunamiMessages[(message.earthquake.domesticTsunami ?? "Unknown")],
|
? "ごく浅い"
|
||||||
foreignTsunami: foreignTsunamiMessages[(message.earthquake.foreignTsunami ?? "Unknown")],
|
: (message.earthquake.hypocenter.depth === -1
|
||||||
points: pointsMsg === ""
|
? "不明"
|
||||||
? ""
|
: `${message.earthquake.hypocenter.depth}km`),
|
||||||
: EOL.repeat(2) + pointsMsg,
|
domesticTsunami: domesticTsunamiMessages[(message.earthquake.domesticTsunami ?? "Unknown")],
|
||||||
source: message.issue.source ?? "不明",
|
foreignTsunami: foreignTsunamiMessages[(message.earthquake.foreignTsunami ?? "Unknown")],
|
||||||
comment: message.comments.freeFormComment === ""
|
points: pointsMsg === ""
|
||||||
? ""
|
? ""
|
||||||
: EOL + message.comments.freeFormComment + EOL,
|
: EOL.repeat(2) + pointsMsg,
|
||||||
}),
|
source: message.issue.source ?? "不明",
|
||||||
}, "地震発生情報");
|
comment: message.comments.freeFormComment === ""
|
||||||
|
? ""
|
||||||
|
: EOL + message.comments.freeFormComment + EOL,
|
||||||
|
}),
|
||||||
|
}, "地震発生情報");
|
||||||
|
|
||||||
|
earthquakeUniqid = ueuses[0]?.uniqid ?? null;
|
||||||
|
})(),
|
||||||
|
(async () => {
|
||||||
|
const result = await generateImage(message);
|
||||||
|
|
||||||
|
if (typeof result === "string") {
|
||||||
|
console.warn("情報が不足しているため、地震の画像生成ができませんでした");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (typeof (earthquakeUniqid as string | null) !== "string") {}
|
||||||
|
|
||||||
|
await createUeuse({
|
||||||
|
text: "この地震の震度分布画像を生成しました。",
|
||||||
|
media: {
|
||||||
|
photo: [
|
||||||
|
result.toString("base64"),
|
||||||
|
]
|
||||||
|
},
|
||||||
|
reuseid: (earthquakeUniqid as unknown as string),
|
||||||
|
}, "震度分布画像");
|
||||||
|
})(),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 552:
|
case 552:
|
||||||
|
|||||||
+7
-1
@@ -15,12 +15,15 @@ export default client;
|
|||||||
|
|
||||||
export const createUeuse = async (data: ApiMap["ueuse/create"]["body"], title: string) => {
|
export const createUeuse = async (data: ApiMap["ueuse/create"]["body"], title: string) => {
|
||||||
const mem = Memory.memory;
|
const mem = Memory.memory;
|
||||||
const excessedMessage = "👉返信に続きがあります。";
|
const excessedMessage = "**👉返信に続きがあります。**";
|
||||||
|
|
||||||
let lines = data.text.split(EOL);
|
let lines = data.text.split(EOL);
|
||||||
let firstUniqid = "";
|
let firstUniqid = "";
|
||||||
let count = 0;
|
let count = 0;
|
||||||
|
|
||||||
|
type ExtractSuccess<T> = T extends { success: true } ? T : never;
|
||||||
|
const results: ExtractSuccess<ApiMap["ueuse/create"]["response"]>[] = [];
|
||||||
|
|
||||||
while (lines.length > 0) {
|
while (lines.length > 0) {
|
||||||
count++;
|
count++;
|
||||||
let currentText = "";
|
let currentText = "";
|
||||||
@@ -76,6 +79,7 @@ export const createUeuse = async (data: ApiMap["ueuse/create"]["body"], title: s
|
|||||||
if (response.success) {
|
if (response.success) {
|
||||||
success = true;
|
success = true;
|
||||||
postedUniqid = response.uniqid;
|
postedUniqid = response.uniqid;
|
||||||
|
results.push(response);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,4 +103,6 @@ export const createUeuse = async (data: ApiMap["ueuse/create"]["body"], title: s
|
|||||||
lines.shift();
|
lines.shift();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,6 @@ const schema = z.object({
|
|||||||
z.literal(60),
|
z.literal(60),
|
||||||
z.literal(70),
|
z.literal(70),
|
||||||
]),
|
]),
|
||||||
canUnknownMaxScale: z.boolean(),
|
|
||||||
useHistoryData: z.boolean(),
|
useHistoryData: z.boolean(),
|
||||||
reconnectInterval: z.number().positive(),
|
reconnectInterval: z.number().positive(),
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in New Issue
Block a user