diff --git a/city-symbol/index.ts b/city-symbol/index.ts index dccead16..07b22cbc 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; } } @@ -484,25 +483,25 @@ 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 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,23 +666,33 @@ 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) { + const hintCount = result.hintIndex + (prefectureSpecified ? 1 : 0); + + if (result.state === 'solved') { await increment(result.correctAnswerer, 'city-symbol-answer'); - if (result.hintIndex === 0) { + if (hintCount === 0) { await increment(result.correctAnswerer, 'city-symbol-answer-no-hint'); } - if (result.hintIndex <= 1) { + if (hintCount <= 1) { await increment(result.correctAnswerer, 'city-symbol-answer-no-chatgpt-hint'); } - } - if (city.cityName === '博多市') { - await increment(result.correctAnswerer, 'city-symbol-answer-hakatashi'); + if (city.cityName === '博多市') { + 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-${city.prefectureName}`); + } + if (hintCount <= 1) { + await increment(result.correctAnswerer, `city-symbol-answer-no-chatgpt-hint-${city.prefectureName}`); + } + } } } } catch (error) {