From d30a98bf2e77a496a6fd04e7987d0c9a981ea9a2 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 27 Aug 2018 06:16:56 +0900 Subject: [PATCH] =?UTF-8?q?=E8=A8=98=E6=86=B6=E3=82=92=E6=B0=B8=E7=B6=9A?= =?UTF-8?q?=E5=8C=96=E3=81=99=E3=82=8B=E3=81=AA=E3=81=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/ai.ts | 40 ++++++++++++++++++++++++++---- src/memory.ts | 17 ------------- src/modules/fortune/index.ts | 2 +- src/modules/guessing-game/index.ts | 40 +++++++++++++++++------------- 5 files changed, 60 insertions(+), 40 deletions(-) delete mode 100644 src/memory.ts diff --git a/.gitignore b/.gitignore index 8c3b883..a7b211e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ config.json built node_modules +memory.json diff --git a/src/ai.ts b/src/ai.ts index a492257..f8ac972 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -1,12 +1,12 @@ // AI CORE +import * as loki from 'lokijs'; import * as WebSocket from 'ws'; import * as request from 'request-promise-native'; import serifs from './serifs'; import config from './config'; import IModule from './module'; import MessageLike from './message-like'; -import { contexts } from './memory'; const ReconnectingWebSocket = require('../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js'); /** @@ -27,9 +27,40 @@ export default class 藍 { private modules: IModule[] = []; + public db: loki; + + private contexts: loki.Collection<{ + isMessage: boolean; + noteId?: string; + userId?: string; + module: string; + key: string; + }>; + constructor(account: any) { this.account = account; + this.db = new loki('memory.json', { + autoload: true, + autosave: true, + autosaveInterval: 1000, + autoloadCallback: this.init + }); + } + + private init = () => { + //#region Init DB + this.contexts = this.db.getCollection('contexts'); + if (this.contexts === null) { + this.contexts = this.db.addCollection('contexts', { + indices: ['key'] + }); + } + //#endregion + + // Install modules + this.modules.forEach(m => m.install(this)); + //#region Home stream this.connection = new ReconnectingWebSocket(`${config.wsUrl}/?i=${config.i}`, [], { WebSocket: WebSocket @@ -74,7 +105,6 @@ export default class 藍 { } public install = (module: IModule) => { - module.install(this); this.modules.push(module); } @@ -132,7 +162,7 @@ export default class 藍 { } }, 1000); - const context = !msg.isMessage && msg.replyId == null ? null : contexts.findOne(msg.isMessage ? { + const context = !msg.isMessage && msg.replyId == null ? null : this.contexts.findOne(msg.isMessage ? { isMessage: true, userId: msg.userId } : { @@ -170,7 +200,7 @@ export default class 藍 { }; public subscribeReply = (module: IModule, key: string, isMessage: boolean, id: string) => { - contexts.insertOne(isMessage ? { + this.contexts.insertOne(isMessage ? { isMessage: true, userId: id, module: module.name, @@ -184,7 +214,7 @@ export default class 藍 { } public unsubscribeReply = (module: IModule, key: string) => { - contexts.findAndRemove({ + this.contexts.findAndRemove({ key: key, module: module.name }); diff --git a/src/memory.ts b/src/memory.ts deleted file mode 100644 index 0b748b0..0000000 --- a/src/memory.ts +++ /dev/null @@ -1,17 +0,0 @@ -// 藍の記憶 - -import * as loki from 'lokijs'; - -const db = new loki('ai'); - -export default db; - -export const contexts = db.addCollection<{ - isMessage: boolean; - noteId?: string; - userId?: string; - module: string; - key: string; -}>('contexts', { - indices: ['key'] -}); diff --git a/src/modules/fortune/index.ts b/src/modules/fortune/index.ts index 12fd3c9..35b2313 100644 --- a/src/modules/fortune/index.ts +++ b/src/modules/fortune/index.ts @@ -30,7 +30,7 @@ export default class FortuneModule implements IModule { public install = (ai: 藍) => { } public onMention = (msg: MessageLike) => { - if (msg.text && (msg.text.includes('占') || msg.text.includes('運勢'))) { + if (msg.text && (msg.text.includes('占') || msg.text.includes('うらな') || msg.text.includes('運勢'))) { const date = new Date(); const seed = `${date.getFullYear()}/${date.getMonth()}/${date.getDay()}@${msg.userId}`; const rng = seedrandom(seed); diff --git a/src/modules/guessing-game/index.ts b/src/modules/guessing-game/index.ts index e8b619c..5ac982a 100644 --- a/src/modules/guessing-game/index.ts +++ b/src/modules/guessing-game/index.ts @@ -1,31 +1,37 @@ +import * as loki from 'lokijs'; import 藍 from '../../ai'; import IModule from '../../module'; import MessageLike from '../../message-like'; import serifs from '../../serifs'; -import db from '../../memory'; - -export const guesses = db.addCollection<{ - userId: string; - secret: number; - tries: number[]; - isEnded: boolean; - startedAt: number; - endedAt: number; -}>('guessingGame', { - indices: ['userId'] -}); export default class GuessingGameModule implements IModule { public name = 'guessingGame'; private ai: 藍; + private guesses: loki.Collection<{ + userId: string; + secret: number; + tries: number[]; + isEnded: boolean; + startedAt: number; + endedAt: number; + }>; public install = (ai: 藍) => { this.ai = ai; + + //#region Init DB + this.guesses = this.ai.db.getCollection('guessingGame'); + if (this.guesses === null) { + this.guesses = this.ai.db.addCollection('guessingGame', { + indices: ['userId'] + }); + } + //#endregion } public onMention = (msg: MessageLike) => { if (msg.text && (msg.text.includes('数当て') || msg.text.includes('数あて'))) { - const exist = guesses.findOne({ + const exist = this.guesses.findOne({ userId: msg.userId, isEnded: false }); @@ -42,7 +48,7 @@ export default class GuessingGameModule implements IModule { const secret = Math.floor(Math.random() * 100); - guesses.insertOne({ + this.guesses.insertOne({ userId: msg.userId, secret: secret, tries: [], @@ -64,7 +70,7 @@ export default class GuessingGameModule implements IModule { public onReplyThisModule = (msg: MessageLike) => { if (msg.text == null) return; - const exist = guesses.findOne({ + const exist = this.guesses.findOne({ userId: msg.userId, isEnded: false }); @@ -73,7 +79,7 @@ export default class GuessingGameModule implements IModule { msg.reply(serifs.GUESSINGGAME_CANCEL); exist.isEnded = true; exist.endedAt = Date.now(); - guesses.update(exist); + this.guesses.update(exist); this.ai.unsubscribeReply(this, msg.userId); return; } @@ -115,7 +121,7 @@ export default class GuessingGameModule implements IModule { this.ai.unsubscribeReply(this, msg.userId); } - guesses.update(exist); + this.guesses.update(exist); msg.reply(text).then(reply => { if (!end) {