Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

市章当てクイズで都道府県を指定できるように #952

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 67 additions & 38 deletions city-symbol/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,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';
import chakuwikiTitles from './chakuwiki-title-map.json';

const chakuwikiTitleMap = new Map(Object.entries(chakuwikiTitles));
Expand Down Expand Up @@ -349,27 +349,29 @@ const getCityInformation = async (city: CitySymbol): Promise<CityInformation> =>
};
};

const getRandomCitySymbol = async (): Promise<City> => {
if (Math.random() < 1 / 1719) {
return {
prefectureName: '',
cityName: '博多市',
cityWikipediaName: '博多市',
reason: '伯方の塩のパッケージに描かれている赤と青のストライプを直方体にあしらったもの',
plainText: '博多市は、TSG CTF や TSG LIVE! の開催などを行ったTSG部員である。',
kotobankDescription: '博多市は、JavaScriptやTypeScriptなどのプログラミング言語を得意とするプログラマであり、AtCoderの最高レーティングは2309である。',
kotobankUrl: 'https://hakatashi.com',
chakuwikiDescription: '博多市は、加熱した玉ねぎを食べることを苦手としており、麦から作られたウイスキーやビールなどの飲み物を嫌っている。',
chakuwikiUrl: 'https://hakatashi.com',
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 (prefList: PrefectureKanji[] = Object.keys(prefectures) as PrefectureKanji[], allowEasterEgg: boolean = true): Promise<City> => {
if (allowEasterEgg) {
if (Math.random() < 1 / 1719) {
return {
prefectureName: '',
cityName: '博多市',
cityWikipediaName: '博多市',
reason: '伯方の塩のパッケージに描かれている赤と青のストライプを直方体にあしらったもの',
plainText: '博多市は、TSG CTF や TSG LIVE! の開催などを行ったTSG部員である。',
kotobankDescription: '博多市は、JavaScriptやTypeScriptなどのプログラミング言語を得意とするプログラマであり、AtCoderの最高レーティングは2309である。',
kotobankUrl: 'https://hakatashi.com',
chakuwikiDescription: '博多市は、加熱した玉ねぎを食べることを苦手としており、麦から作られたウイスキーやビールなどの飲み物を嫌っている。',
chakuwikiUrl: 'https://hakatashi.com',
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(prefList);
const citySymbols = await getWikipediaSource(prefectureChosen);
const citySymbol = sample(citySymbols);
const cityInformation = await getCityInformation(citySymbol);
Expand Down Expand Up @@ -451,14 +453,24 @@ const generateAiHints = async (city: City): Promise<string[] | null> => {
};

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;
}
}

Expand All @@ -472,11 +484,25 @@ export default (slackClients: SlackInterface) => {

mutex.runExclusive(async () => {
try {
let matches;
if (
message.text &&
message.text.match(/^(?:市?区?町?村?)章当てクイズ$/)
(matches = message.text.match(/^(?:市?区?町?村?)章当てクイズ\s?(?<pref>\p{sc=Han}+[都道府県])?$/u))
) {
const city = await getRandomCitySymbol();
const prefecture = matches?.groups?.pref;

if (prefecture && !Object.hasOwn(prefectures, 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([prefecture as PrefectureKanji], false) : await getRandomCitySymbol();
const quizText = 'この市区町村章ど~こだ?';
const imageUrl = getWikimediaImageUrl(sample(city.files));
const correctAnswers = getCorrectAnswers(city);
Expand Down Expand Up @@ -514,16 +540,18 @@ export default (slackClients: SlackInterface) => {
],
},
get hintMessages() {
const hints = aiHintsLoader.get() ?? [];
const aiHints = aiHintsLoader.get() ?? [];
return [
{
channel: message.channel,
text: `ヒント: ${city.prefectureName}の市区町村ですよ~`,
},
...hints.map((hint, index) => ({
channel: message.channel,
text: `ChatGPTヒント${index + 1}: ${hint}`,
})),
...(needPrefHint ? [
{
channel: message.channel,
text: `ヒント: ${city.prefectureName}の市区町村ですよ~`,
},
] : []),
...aiHints.map((hint, index) => ({
channel: message.channel,
text: `ChatGPTヒント${index + 1}: ${hint}`,
})),
];
},
immediateMessage: {
Expand Down Expand Up @@ -639,12 +667,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 (result.hintIndex === 0) {
await increment(result.correctAnswerer, 'city-symbol-answer-no-hint');
Expand Down
Loading