From 53c54f6f143c7d9b1e22a5e1f5f012a644f38597 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 25 Aug 2020 10:08:27 +0900 Subject: [PATCH] Resolve #31 --- src/friend.ts | 36 +++++++++++++++++++++++ src/modules/chart/index.ts | 2 +- src/modules/core/index.ts | 38 +++++++++++++++++++++++++ src/modules/fortune/index.ts | 2 +- src/serifs.ts | 8 ++++++ src/{modules/fortune => }/vocabulary.ts | 0 6 files changed, 84 insertions(+), 2 deletions(-) rename src/{modules/fortune => }/vocabulary.ts (100%) diff --git a/src/friend.ts b/src/friend.ts index 70c48d9..d1c46ee 100644 --- a/src/friend.ts +++ b/src/friend.ts @@ -3,6 +3,7 @@ import 藍 from './ai'; import IModule from './module'; import getDate from './utils/get-date'; import { User } from './misskey/user'; +import { itemPrefixes, items, and } from './vocabulary'; export type FriendDoc = { userId: string; @@ -13,6 +14,7 @@ export type FriendDoc = { todayLoveIncrements?: number; perModulesData?: any; married?: boolean; + transferCode?: string; }; export default class Friend { @@ -137,4 +139,38 @@ export default class Friend { public save() { this.ai.friends.update(this.doc); } + + @autobind + public generateTransferCode(): string { + let code = ''; + code += itemPrefixes[Math.floor(Math.random() * itemPrefixes.length)]; + code += items[Math.floor(Math.random() * items.length)]; + code += and[Math.floor(Math.random() * and.length)]; + code += itemPrefixes[Math.floor(Math.random() * itemPrefixes.length)]; + code += items[Math.floor(Math.random() * items.length)]; + + this.doc.transferCode = code; + this.save(); + + return code; + } + + @autobind + public transferMemory(code: string): boolean { + const src = this.ai.friends.findOne({ + transferCode: code + }); + + if (src == null) return false; + + this.doc.name = src.name; + this.doc.love = src.love; + this.doc.married = src.married; + this.doc.perModulesData = src.perModulesData; + this.save(); + + // TODO: 合言葉を忘れる + + return true; + } } diff --git a/src/modules/chart/index.ts b/src/modules/chart/index.ts index 531c7d1..6bcb849 100644 --- a/src/modules/chart/index.ts +++ b/src/modules/chart/index.ts @@ -3,7 +3,7 @@ import Module from '../../module'; import serifs from '../../serifs'; import Message from '../../message'; import { renderChart } from './render-chart'; -import { items } from '../fortune/vocabulary'; +import { items } from '../../vocabulary'; import config from '../../config'; export default class extends Module { diff --git a/src/modules/core/index.ts b/src/modules/core/index.ts index 6148ff3..4f147e8 100644 --- a/src/modules/core/index.ts +++ b/src/modules/core/index.ts @@ -25,6 +25,8 @@ export default class extends Module { if (!msg.text) return false; return ( + this.transferBegin(msg) || + this.transferEnd(msg) || this.setName(msg) || this.greet(msg) || this.erait(msg) || @@ -43,6 +45,42 @@ export default class extends Module { ); } + @autobind + private transferBegin(msg: Message): boolean { + if (!msg.text) return false; + if (!msg.includes(['引継', '引き継ぎ', '引越', '引っ越し'])) return false; + + // メッセージのみ + if (!msg.isDm) { + msg.reply(serifs.core.transferNeedDm); + return true; + } + + const code = msg.friend.generateTransferCode(); + + msg.reply(serifs.core.transferCode(code)); + + return true; + } + + @autobind + private transferEnd(msg: Message): boolean { + if (!msg.text) return false; + if (!msg.text.startsWith('「') || !msg.text.endsWith('」')) return false; + + const code = msg.text.substring(1, msg.text.length - 1); + + const succ = msg.friend.transferMemory(code); + + if (succ) { + msg.reply(serifs.core.transferDone(msg.friend.name)); + } else { + msg.reply(serifs.core.transferFailed); + } + + return true; + } + @autobind private setName(msg: Message): boolean { if (!msg.text) return false; diff --git a/src/modules/fortune/index.ts b/src/modules/fortune/index.ts index 1cfe86d..84bb778 100644 --- a/src/modules/fortune/index.ts +++ b/src/modules/fortune/index.ts @@ -3,7 +3,7 @@ import Module from '../../module'; import Message from '../../message'; import serifs from '../../serifs'; import * as seedrandom from 'seedrandom'; -import { blessing, itemPrefixes, items, and } from './vocabulary'; +import { blessing, itemPrefixes, items, and } from '../../vocabulary'; export default class extends Module { public readonly name = 'fortune'; diff --git a/src/serifs.ts b/src/serifs.ts index 2cfb502..6103ca3 100644 --- a/src/serifs.ts +++ b/src/serifs.ts @@ -145,6 +145,14 @@ export default { }, shutdown: '私まだ眠くないですよ...?', + + transferNeedDm: 'わかりました、それはチャットで話しませんか?', + + transferCode: code => `わかりました。\n合言葉は「${code}」です!`, + + transferFailed: 'うーん、合言葉が間違ってませんか...?', + + transferDone: name => name ? `はっ...! おかえりなさい、${name}!` : `はっ...! おかえりなさい!`, }, keyword: { diff --git a/src/modules/fortune/vocabulary.ts b/src/vocabulary.ts similarity index 100% rename from src/modules/fortune/vocabulary.ts rename to src/vocabulary.ts