From 1e666497577ae435ba526ad93be8d793f61664b5 Mon Sep 17 00:00:00 2001 From: settyan117 Date: Sun, 20 Oct 2024 21:30:06 +0900 Subject: [PATCH 1/9] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- city-symbol/index.ts | 93 ++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 33 deletions(-) diff --git a/city-symbol/index.ts b/city-symbol/index.ts index cc9b8a3f..5560d298 100644 --- a/city-symbol/index.ts +++ b/city-symbol/index.ts @@ -8,7 +8,7 @@ import logger from '../lib/logger'; import openai from '../lib/openai'; import {SlackInterface} from '../lib/slack'; import {Loader} from '../lib/utils'; -import {prefectures} from '../room-gacha/prefectures'; +import {PrefectureKanji, prefectures} from '../room-gacha/prefectures'; const mutex = new Mutex(); @@ -210,23 +210,25 @@ const getCityInformation = async (title: string): Promise => { return {placeImage, ruby, plainText}; }; -const getRandomCitySymbol = async (): Promise => { - if (Math.random() < 1 / 1719) { - return { - prefectureName: '', - cityName: '博多市', - cityWikipediaName: '博多市', - reason: '伯方の塩のパッケージに描かれている赤と青のストライプを直方体にあしらったもの', - plainText: '', - date: '2020年6月21日', - notes: 'なし', - files: ['https://raw.githubusercontent.com/hakatashi/icon/master/images/icon_480px.png'], - placeImage: 'https://raw.githubusercontent.com/hakatashi/icon/master/images/icon_480px.png', - ruby: 'はかたし', - }; +const getRandomCitySymbol = async (prefSet: Set = new Set(Object.keys(prefectures) as PrefectureKanji[]), allowEasterEgg: boolean = true): Promise => { + if (allowEasterEgg) { + if (Math.random() < 1 / 1719) { + return { + prefectureName: '', + cityName: '博多市', + cityWikipediaName: '博多市', + reason: '伯方の塩のパッケージに描かれている赤と青のストライプを直方体にあしらったもの', + plainText: '', + date: '2020年6月21日', + notes: 'なし', + files: ['https://raw.githubusercontent.com/hakatashi/icon/master/images/icon_480px.png'], + placeImage: 'https://raw.githubusercontent.com/hakatashi/icon/master/images/icon_480px.png', + ruby: 'はかたし', + }; + } } - const prefectureChosen = sample(Object.keys(prefectures)); + const prefectureChosen = sample(Array.from(prefSet)); const citySymbols = await getWikipediaSource(prefectureChosen); const citySymbol = sample(citySymbols); const cityInformation = await getCityInformation(citySymbol.cityWikipediaName); @@ -305,14 +307,24 @@ const generateAiHints = async (city: City): Promise => { }; class CitySymbolAteQuiz extends AteQuiz { + hasPrefHint: boolean; + waitSecGen(hintIndex: number): number { - if (hintIndex === 0) { - return 30; + if (this.hasPrefHint) { + if (hintIndex === 0) { + return 30; + } + if (hintIndex === 1) { + return 15; + } + return 10; } - if (hintIndex === 1) { - return 15; + else { + if (hintIndex === 0) { + return 45; + } + return 10; } - return 10; } } @@ -328,9 +340,22 @@ export default (slackClients: SlackInterface) => { try { if ( message.text && - message.text.match(/^(?:市?区?町?村?)章当てクイズ$/) + message.text.match(/^(?:市?区?町?村?)章当てクイズ\s?(?:\p{sc=Han}+[都道府県])?$/u) ) { - const city = await getRandomCitySymbol(); + const prefecture = message.text.match(/^(?:市?区?町?村?)章当てクイズ\s?(?\p{sc=Han}+[都道府県])?$/u)?.groups?.pref; + + if (prefecture && !Object.keys(prefectures).includes(prefecture)) { + await slackClients.webClient.chat.postMessage({ + channel: message.channel, + text: `${prefecture}という都道府県は存在しないよ:angry:`, + username: '市章当てクイズ', + icon_emoji: ':cityscape:', + }); + return; + } + + const needPrefHint = !prefecture; + const city = prefecture ? await getRandomCitySymbol(new Set([prefecture as PrefectureKanji]), false) : await getRandomCitySymbol(); const quizText = 'この市区町村章ど~こだ?'; const imageUrl = getWikimediaImageUrl(sample(city.files)); const correctAnswers = getCorrectAnswers(city); @@ -358,17 +383,18 @@ export default (slackClients: SlackInterface) => { ], }, get hintMessages() { - const hints = aiHintsLoader.get() ?? []; - return [ + const aiHints = aiHintsLoader.get() ?? []; + const hints = (needPrefHint ? [ { channel: message.channel, text: `ヒント: ${city.prefectureName}の市区町村ですよ~`, - }, - ...hints.map((hint, index) => ({ - channel: message.channel, - text: `ChatGPTヒント${index + 1}: ${hint}`, - })), - ]; + } + ] : []) + .concat([...aiHints.map((hint, index) => ({ + channel: message.channel, + text: `ChatGPTヒント${index + 1}: ${hint}`, + }))]); + return hints; }, immediateMessage: { channel: message.channel, @@ -474,12 +500,13 @@ export default (slackClients: SlackInterface) => { }; const quiz = new CitySymbolAteQuiz(slackClients, problem, { - username: '市章当てクイズ', + username: '市章当てクイズ' + (prefecture ? ` (${prefecture})` : ''), icon_emoji: ':cityscape:', }); + quiz.hasPrefHint = needPrefHint; const result = await quiz.start(); - if (result.state === 'solved') { + if (result.state === 'solved' && !prefecture) { await increment(result.correctAnswerer, 'city-symbol-answer'); } if (city.cityName === '博多市') { From 65bc9c77c0259b748ed0f35f5b95dcfe71c53910 Mon Sep 17 00:00:00 2001 From: settyan117 Date: Sun, 20 Oct 2024 22:03:40 +0900 Subject: [PATCH 2/9] Update city-symbol/index.ts Co-authored-by: Koki Takahashi --- city-symbol/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/city-symbol/index.ts b/city-symbol/index.ts index 5560d298..4ddb04c3 100644 --- a/city-symbol/index.ts +++ b/city-symbol/index.ts @@ -344,7 +344,7 @@ export default (slackClients: SlackInterface) => { ) { const prefecture = message.text.match(/^(?:市?区?町?村?)章当てクイズ\s?(?\p{sc=Han}+[都道府県])?$/u)?.groups?.pref; - if (prefecture && !Object.keys(prefectures).includes(prefecture)) { + if (prefecture && !Object.hasOwn(prefectures, prefecture)) { await slackClients.webClient.chat.postMessage({ channel: message.channel, text: `${prefecture}という都道府県は存在しないよ:angry:`, From c010c54b9eb9184997106a5f593f3b3f12faa64f Mon Sep 17 00:00:00 2001 From: settyan117 Date: Sun, 20 Oct 2024 22:13:31 +0900 Subject: [PATCH 3/9] update --- city-symbol/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/city-symbol/index.ts b/city-symbol/index.ts index 4ddb04c3..bf810431 100644 --- a/city-symbol/index.ts +++ b/city-symbol/index.ts @@ -338,11 +338,12 @@ export default (slackClients: SlackInterface) => { mutex.runExclusive(async () => { try { + let matches; if ( message.text && - message.text.match(/^(?:市?区?町?村?)章当てクイズ\s?(?:\p{sc=Han}+[都道府県])?$/u) + (matches = message.text.match(/^(?:市?区?町?村?)章当てクイズ\s?(?\p{sc=Han}+[都道府県])?$/u)) ) { - const prefecture = message.text.match(/^(?:市?区?町?村?)章当てクイズ\s?(?\p{sc=Han}+[都道府県])?$/u)?.groups?.pref; + const prefecture = matches?.groups?.pref; if (prefecture && !Object.hasOwn(prefectures, prefecture)) { await slackClients.webClient.chat.postMessage({ From 422ca9ab9664ba4d088936e1633c5af6da70f77b Mon Sep 17 00:00:00 2001 From: settyan117 Date: Sun, 20 Oct 2024 22:15:35 +0900 Subject: [PATCH 4/9] update --- city-symbol/index.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/city-symbol/index.ts b/city-symbol/index.ts index bf810431..b1d727a0 100644 --- a/city-symbol/index.ts +++ b/city-symbol/index.ts @@ -385,17 +385,18 @@ export default (slackClients: SlackInterface) => { }, get hintMessages() { const aiHints = aiHintsLoader.get() ?? []; - const hints = (needPrefHint ? [ - { - channel: message.channel, - text: `ヒント: ${city.prefectureName}の市区町村ですよ~`, - } - ] : []) - .concat([...aiHints.map((hint, index) => ({ - channel: message.channel, - text: `ChatGPTヒント${index + 1}: ${hint}`, - }))]); - return hints; + return [ + ...(needPrefHint ? [ + { + channel: message.channel, + text: `ヒント: ${city.prefectureName}の市区町村ですよ~`, + }, + ] : []), + ...aiHints.map((hint, index) => ({ + channel: message.channel, + text: `ChatGPTヒント${index + 1}: ${hint}`, + })), + ]; }, immediateMessage: { channel: message.channel, From c33a15bad69ffa5350fb54151db489c08a35d904 Mon Sep 17 00:00:00 2001 From: settyan117 Date: Mon, 21 Oct 2024 15:58:13 +0900 Subject: [PATCH 5/9] =?UTF-8?q?=E9=83=BD=E9=81=93=E5=BA=9C=E7=9C=8C?= =?UTF-8?q?=E3=83=AA=E3=82=B9=E3=83=88=E3=82=92Set=E3=81=A7=E3=81=AF?= =?UTF-8?q?=E3=81=AA=E3=81=8FArray=E3=81=A7=E5=8F=97=E3=81=91=E6=B8=A1?= =?UTF-8?q?=E3=81=99=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- city-symbol/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/city-symbol/index.ts b/city-symbol/index.ts index 73728eed..dccead16 100644 --- a/city-symbol/index.ts +++ b/city-symbol/index.ts @@ -349,7 +349,7 @@ const getCityInformation = async (city: CitySymbol): Promise => }; }; -const getRandomCitySymbol = async (prefSet: Set = new Set(Object.keys(prefectures) as PrefectureKanji[]), allowEasterEgg: boolean = true): Promise => { +const getRandomCitySymbol = async (prefList: PrefectureKanji[] = Object.keys(prefectures) as PrefectureKanji[], allowEasterEgg: boolean = true): Promise => { if (allowEasterEgg) { if (Math.random() < 1 / 1719) { return { @@ -371,7 +371,7 @@ const getRandomCitySymbol = async (prefSet: Set = new Set(Objec } } - const prefectureChosen = sample(Array.from(prefSet)); + const prefectureChosen = sample(prefList); const citySymbols = await getWikipediaSource(prefectureChosen); const citySymbol = sample(citySymbols); const cityInformation = await getCityInformation(citySymbol); @@ -502,7 +502,7 @@ export default (slackClients: SlackInterface) => { } const needPrefHint = !prefecture; - const city = prefecture ? await getRandomCitySymbol(new Set([prefecture as PrefectureKanji]), false) : await getRandomCitySymbol(); + const city = prefecture ? await getRandomCitySymbol([prefecture as PrefectureKanji], false) : await getRandomCitySymbol(); const quizText = 'この市区町村章ど~こだ?'; const imageUrl = getWikimediaImageUrl(sample(city.files)); const correctAnswers = getCorrectAnswers(city); From b730e598230c236f1bff67d899a95b77aac2bea9 Mon Sep 17 00:00:00 2001 From: settyan117 Date: Tue, 22 Oct 2024 16:25:58 +0900 Subject: [PATCH 6/9] =?UTF-8?q?=E5=AE=9F=E7=B8=BE=E5=91=A8=E3=82=8A?= =?UTF-8?q?=E3=81=AE=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- city-symbol/index.ts | 45 +++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/city-symbol/index.ts b/city-symbol/index.ts index dccead16..d4fd01d0 100644 --- a/city-symbol/index.ts +++ b/city-symbol/index.ts @@ -13,6 +13,7 @@ import {SlackInterface} from '../lib/slack'; import {Loader} from '../lib/utils'; import {PrefectureKanji, prefectures} from '../room-gacha/prefectures'; import chakuwikiTitles from './chakuwiki-title-map.json'; +import { boolean } from 'yargs'; const chakuwikiTitleMap = new Map(Object.entries(chakuwikiTitles)); @@ -489,20 +490,20 @@ export default (slackClients: SlackInterface) => { message.text && (matches = message.text.match(/^(?:市?区?町?村?)章当てクイズ\s?(?\p{sc=Han}+[都道府県])?$/u)) ) { - const prefecture = matches?.groups?.pref; + const prefectureSpecified = matches?.groups?.pref; - if (prefecture && !Object.hasOwn(prefectures, prefecture)) { + if (prefectureSpecified && !Object.hasOwn(prefectures, prefectureSpecified)) { await slackClients.webClient.chat.postMessage({ channel: message.channel, - text: `${prefecture}という都道府県は存在しないよ:angry:`, + text: `${prefectureSpecified}という都道府県は存在しないよ:angry:`, username: '市章当てクイズ', icon_emoji: ':cityscape:', }); return; } - const needPrefHint = !prefecture; - const city = prefecture ? await getRandomCitySymbol([prefecture as PrefectureKanji], false) : await getRandomCitySymbol(); + const needPrefHint = !prefectureSpecified; + const city = prefectureSpecified ? await getRandomCitySymbol([prefectureSpecified as PrefectureKanji], false) : await getRandomCitySymbol(); const quizText = 'この市区町村章ど~こだ?'; const imageUrl = getWikimediaImageUrl(sample(city.files)); const correctAnswers = getCorrectAnswers(city); @@ -667,24 +668,38 @@ export default (slackClients: SlackInterface) => { }; const quiz = new CitySymbolAteQuiz(slackClients, problem, { - username: '市章当てクイズ' + (prefecture ? ` (${prefecture})` : ''), + username: '市章当てクイズ' + (prefectureSpecified ? ` (${prefectureSpecified})` : ''), icon_emoji: ':cityscape:', }); quiz.hasPrefHint = needPrefHint; const result = await quiz.start(); - if (result.state === 'solved' && !prefecture) { - await increment(result.correctAnswerer, 'city-symbol-answer'); - if (result.hintIndex === 0) { - await increment(result.correctAnswerer, 'city-symbol-answer-no-hint'); + const hintCount = result.hintIndex + (prefectureSpecified ? 1 : 0); + + if (result.state === 'solved') { + if (city.cityName === '博多市') { + await increment(result.correctAnswerer, 'city-symbol-answer'); + await increment(result.correctAnswerer, 'city-symbol-answer-hakatashi'); + if (hintCount === 0) { + await increment(result.correctAnswerer, 'city-symbol-answer-no-hint'); + } + if (hintCount <= 1) { + await increment(result.correctAnswerer, 'city-symbol-answer-no-chatgpt-hint'); + } } - if (result.hintIndex <= 1) { - await increment(result.correctAnswerer, 'city-symbol-answer-no-chatgpt-hint'); + else { + await increment(result.correctAnswerer, 'city-symbol-answer'); + await increment(result.correctAnswerer, 'city-symbol-answer-' + city.prefectureName); + if (hintCount === 0) { + await increment(result.correctAnswerer, 'city-symbol-answer-no-hint'); + await increment(result.correctAnswerer, 'city-symbol-answer-no-hint-' + city.prefectureName); + } + if (hintCount <= 1) { + await increment(result.correctAnswerer, 'city-symbol-answer-no-chatgpt-hint'); + await increment(result.correctAnswerer, 'city-symbol-answer-no-chatgpt-hint-' + city.prefectureName); + } } } - if (city.cityName === '博多市') { - await increment(result.correctAnswerer, 'city-symbol-answer-hakatashi'); - } } } catch (error) { log.error(error.stack); From 66df3f565138d6166feaa9e3bca8961c6fd87f74 Mon Sep 17 00:00:00 2001 From: settyan117 Date: Tue, 22 Oct 2024 16:30:26 +0900 Subject: [PATCH 7/9] remove unnecessary import --- city-symbol/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/city-symbol/index.ts b/city-symbol/index.ts index d4fd01d0..d21fb61c 100644 --- a/city-symbol/index.ts +++ b/city-symbol/index.ts @@ -13,7 +13,6 @@ import {SlackInterface} from '../lib/slack'; import {Loader} from '../lib/utils'; import {PrefectureKanji, prefectures} from '../room-gacha/prefectures'; import chakuwikiTitles from './chakuwiki-title-map.json'; -import { boolean } from 'yargs'; const chakuwikiTitleMap = new Map(Object.entries(chakuwikiTitles)); From 33f12ef8caff260096ec792e362a5af4b3be5c90 Mon Sep 17 00:00:00 2001 From: settyan117 Date: Tue, 22 Oct 2024 16:47:45 +0900 Subject: [PATCH 8/9] fix --- city-symbol/index.ts | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/city-symbol/index.ts b/city-symbol/index.ts index d21fb61c..2cd2c1db 100644 --- a/city-symbol/index.ts +++ b/city-symbol/index.ts @@ -349,7 +349,7 @@ const getCityInformation = async (city: CitySymbol): Promise => }; }; -const getRandomCitySymbol = async (prefList: PrefectureKanji[] = Object.keys(prefectures) as PrefectureKanji[], allowEasterEgg: boolean = true): Promise => { +const getRandomCitySymbol = async (prefList: PrefectureKanji[] = Object.keys(prefectures) as PrefectureKanji[], allowEasterEgg = true): Promise => { if (allowEasterEgg) { if (Math.random() < 1 / 1719) { return { @@ -465,12 +465,11 @@ class CitySymbolAteQuiz extends AteQuiz { } return 10; } - else { - if (hintIndex === 0) { - return 45; - } - return 10; + + if (hintIndex === 0) { + return 45; } + return 10; } } @@ -667,7 +666,7 @@ export default (slackClients: SlackInterface) => { }; const quiz = new CitySymbolAteQuiz(slackClients, problem, { - username: '市章当てクイズ' + (prefectureSpecified ? ` (${prefectureSpecified})` : ''), + username: `市章当てクイズ${prefectureSpecified ? ` (${prefectureSpecified})` : ''}`, icon_emoji: ':cityscape:', }); quiz.hasPrefHint = needPrefHint; @@ -676,26 +675,22 @@ export default (slackClients: SlackInterface) => { const hintCount = result.hintIndex + (prefectureSpecified ? 1 : 0); if (result.state === 'solved') { + await increment(result.correctAnswerer, 'city-symbol-answer'); + if (hintCount === 0) { + await increment(result.correctAnswerer, 'city-symbol-answer-no-hint'); + } + if (hintCount <= 1) { + await increment(result.correctAnswerer, 'city-symbol-answer-no-chatgpt-hint'); + } if (city.cityName === '博多市') { - await increment(result.correctAnswerer, 'city-symbol-answer'); await increment(result.correctAnswerer, 'city-symbol-answer-hakatashi'); + } else { + await increment(result.correctAnswerer, `city-symbol-answer-${city.prefectureName}`); if (hintCount === 0) { - await increment(result.correctAnswerer, 'city-symbol-answer-no-hint'); - } - if (hintCount <= 1) { - await increment(result.correctAnswerer, 'city-symbol-answer-no-chatgpt-hint'); - } - } - else { - await increment(result.correctAnswerer, 'city-symbol-answer'); - await increment(result.correctAnswerer, 'city-symbol-answer-' + city.prefectureName); - if (hintCount === 0) { - await increment(result.correctAnswerer, 'city-symbol-answer-no-hint'); - await increment(result.correctAnswerer, 'city-symbol-answer-no-hint-' + city.prefectureName); + await increment(result.correctAnswerer, `city-symbol-answer-no-hint-${city.prefectureName}`); } if (hintCount <= 1) { - await increment(result.correctAnswerer, 'city-symbol-answer-no-chatgpt-hint'); - await increment(result.correctAnswerer, 'city-symbol-answer-no-chatgpt-hint-' + city.prefectureName); + await increment(result.correctAnswerer, `city-symbol-answer-no-chatgpt-hint-${city.prefectureName}`); } } } From cbceb678391a397f6228e3056cf4526a1b9dcb8f Mon Sep 17 00:00:00 2001 From: settyan117 Date: Tue, 22 Oct 2024 16:50:56 +0900 Subject: [PATCH 9/9] fix --- city-symbol/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/city-symbol/index.ts b/city-symbol/index.ts index 2cd2c1db..07b22cbc 100644 --- a/city-symbol/index.ts +++ b/city-symbol/index.ts @@ -483,10 +483,10 @@ export default (slackClients: SlackInterface) => { mutex.runExclusive(async () => { try { - let matches; + let matches: RegExpMatchArray = null; if ( message.text && - (matches = message.text.match(/^(?:市?区?町?村?)章当てクイズ\s?(?\p{sc=Han}+[都道府県])?$/u)) + (matches = (message.text as string).match(/^(?:市?区?町?村?)章当てクイズ\s?(?\p{sc=Han}+[都道府県])?$/u)) ) { const prefectureSpecified = matches?.groups?.pref;