mirror of
https://github.com/syuilo/ai.git
synced 2024-11-22 05:08:00 +00:00
記憶を永続化するなど
This commit is contained in:
parent
1dc0bf27e9
commit
d30a98bf2e
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
config.json
|
config.json
|
||||||
built
|
built
|
||||||
node_modules
|
node_modules
|
||||||
|
memory.json
|
||||||
|
|
40
src/ai.ts
40
src/ai.ts
|
@ -1,12 +1,12 @@
|
||||||
// AI CORE
|
// AI CORE
|
||||||
|
|
||||||
|
import * as loki from 'lokijs';
|
||||||
import * as WebSocket from 'ws';
|
import * as WebSocket from 'ws';
|
||||||
import * as request from 'request-promise-native';
|
import * as request from 'request-promise-native';
|
||||||
import serifs from './serifs';
|
import serifs from './serifs';
|
||||||
import config from './config';
|
import config from './config';
|
||||||
import IModule from './module';
|
import IModule from './module';
|
||||||
import MessageLike from './message-like';
|
import MessageLike from './message-like';
|
||||||
import { contexts } from './memory';
|
|
||||||
const ReconnectingWebSocket = require('../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js');
|
const ReconnectingWebSocket = require('../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,9 +27,40 @@ export default class 藍 {
|
||||||
|
|
||||||
private modules: IModule[] = [];
|
private modules: IModule[] = [];
|
||||||
|
|
||||||
|
public db: loki;
|
||||||
|
|
||||||
|
private contexts: loki.Collection<{
|
||||||
|
isMessage: boolean;
|
||||||
|
noteId?: string;
|
||||||
|
userId?: string;
|
||||||
|
module: string;
|
||||||
|
key: string;
|
||||||
|
}>;
|
||||||
|
|
||||||
constructor(account: any) {
|
constructor(account: any) {
|
||||||
this.account = account;
|
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
|
//#region Home stream
|
||||||
this.connection = new ReconnectingWebSocket(`${config.wsUrl}/?i=${config.i}`, [], {
|
this.connection = new ReconnectingWebSocket(`${config.wsUrl}/?i=${config.i}`, [], {
|
||||||
WebSocket: WebSocket
|
WebSocket: WebSocket
|
||||||
|
@ -74,7 +105,6 @@ export default class 藍 {
|
||||||
}
|
}
|
||||||
|
|
||||||
public install = (module: IModule) => {
|
public install = (module: IModule) => {
|
||||||
module.install(this);
|
|
||||||
this.modules.push(module);
|
this.modules.push(module);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +162,7 @@ export default class 藍 {
|
||||||
}
|
}
|
||||||
}, 1000);
|
}, 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,
|
isMessage: true,
|
||||||
userId: msg.userId
|
userId: msg.userId
|
||||||
} : {
|
} : {
|
||||||
|
@ -170,7 +200,7 @@ export default class 藍 {
|
||||||
};
|
};
|
||||||
|
|
||||||
public subscribeReply = (module: IModule, key: string, isMessage: boolean, id: string) => {
|
public subscribeReply = (module: IModule, key: string, isMessage: boolean, id: string) => {
|
||||||
contexts.insertOne(isMessage ? {
|
this.contexts.insertOne(isMessage ? {
|
||||||
isMessage: true,
|
isMessage: true,
|
||||||
userId: id,
|
userId: id,
|
||||||
module: module.name,
|
module: module.name,
|
||||||
|
@ -184,7 +214,7 @@ export default class 藍 {
|
||||||
}
|
}
|
||||||
|
|
||||||
public unsubscribeReply = (module: IModule, key: string) => {
|
public unsubscribeReply = (module: IModule, key: string) => {
|
||||||
contexts.findAndRemove({
|
this.contexts.findAndRemove({
|
||||||
key: key,
|
key: key,
|
||||||
module: module.name
|
module: module.name
|
||||||
});
|
});
|
||||||
|
|
|
@ -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']
|
|
||||||
});
|
|
|
@ -30,7 +30,7 @@ export default class FortuneModule implements IModule {
|
||||||
public install = (ai: 藍) => { }
|
public install = (ai: 藍) => { }
|
||||||
|
|
||||||
public onMention = (msg: MessageLike) => {
|
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 date = new Date();
|
||||||
const seed = `${date.getFullYear()}/${date.getMonth()}/${date.getDay()}@${msg.userId}`;
|
const seed = `${date.getFullYear()}/${date.getMonth()}/${date.getDay()}@${msg.userId}`;
|
||||||
const rng = seedrandom(seed);
|
const rng = seedrandom(seed);
|
||||||
|
|
|
@ -1,31 +1,37 @@
|
||||||
|
import * as loki from 'lokijs';
|
||||||
import 藍 from '../../ai';
|
import 藍 from '../../ai';
|
||||||
import IModule from '../../module';
|
import IModule from '../../module';
|
||||||
import MessageLike from '../../message-like';
|
import MessageLike from '../../message-like';
|
||||||
import serifs from '../../serifs';
|
import serifs from '../../serifs';
|
||||||
import db from '../../memory';
|
|
||||||
|
|
||||||
export const guesses = db.addCollection<{
|
export default class GuessingGameModule implements IModule {
|
||||||
|
public name = 'guessingGame';
|
||||||
|
private ai: 藍;
|
||||||
|
private guesses: loki.Collection<{
|
||||||
userId: string;
|
userId: string;
|
||||||
secret: number;
|
secret: number;
|
||||||
tries: number[];
|
tries: number[];
|
||||||
isEnded: boolean;
|
isEnded: boolean;
|
||||||
startedAt: number;
|
startedAt: number;
|
||||||
endedAt: number;
|
endedAt: number;
|
||||||
}>('guessingGame', {
|
}>;
|
||||||
indices: ['userId']
|
|
||||||
});
|
|
||||||
|
|
||||||
export default class GuessingGameModule implements IModule {
|
|
||||||
public name = 'guessingGame';
|
|
||||||
private ai: 藍;
|
|
||||||
|
|
||||||
public install = (ai: 藍) => {
|
public install = (ai: 藍) => {
|
||||||
this.ai = 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) => {
|
public onMention = (msg: MessageLike) => {
|
||||||
if (msg.text && (msg.text.includes('数当て') || msg.text.includes('数あて'))) {
|
if (msg.text && (msg.text.includes('数当て') || msg.text.includes('数あて'))) {
|
||||||
const exist = guesses.findOne({
|
const exist = this.guesses.findOne({
|
||||||
userId: msg.userId,
|
userId: msg.userId,
|
||||||
isEnded: false
|
isEnded: false
|
||||||
});
|
});
|
||||||
|
@ -42,7 +48,7 @@ export default class GuessingGameModule implements IModule {
|
||||||
|
|
||||||
const secret = Math.floor(Math.random() * 100);
|
const secret = Math.floor(Math.random() * 100);
|
||||||
|
|
||||||
guesses.insertOne({
|
this.guesses.insertOne({
|
||||||
userId: msg.userId,
|
userId: msg.userId,
|
||||||
secret: secret,
|
secret: secret,
|
||||||
tries: [],
|
tries: [],
|
||||||
|
@ -64,7 +70,7 @@ export default class GuessingGameModule implements IModule {
|
||||||
public onReplyThisModule = (msg: MessageLike) => {
|
public onReplyThisModule = (msg: MessageLike) => {
|
||||||
if (msg.text == null) return;
|
if (msg.text == null) return;
|
||||||
|
|
||||||
const exist = guesses.findOne({
|
const exist = this.guesses.findOne({
|
||||||
userId: msg.userId,
|
userId: msg.userId,
|
||||||
isEnded: false
|
isEnded: false
|
||||||
});
|
});
|
||||||
|
@ -73,7 +79,7 @@ export default class GuessingGameModule implements IModule {
|
||||||
msg.reply(serifs.GUESSINGGAME_CANCEL);
|
msg.reply(serifs.GUESSINGGAME_CANCEL);
|
||||||
exist.isEnded = true;
|
exist.isEnded = true;
|
||||||
exist.endedAt = Date.now();
|
exist.endedAt = Date.now();
|
||||||
guesses.update(exist);
|
this.guesses.update(exist);
|
||||||
this.ai.unsubscribeReply(this, msg.userId);
|
this.ai.unsubscribeReply(this, msg.userId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -115,7 +121,7 @@ export default class GuessingGameModule implements IModule {
|
||||||
this.ai.unsubscribeReply(this, msg.userId);
|
this.ai.unsubscribeReply(this, msg.userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
guesses.update(exist);
|
this.guesses.update(exist);
|
||||||
|
|
||||||
msg.reply(text).then(reply => {
|
msg.reply(text).then(reply => {
|
||||||
if (!end) {
|
if (!end) {
|
||||||
|
|
Loading…
Reference in a new issue