2017-10-06 20:36:46 +02:00
|
|
|
|
import * as EventEmitter from 'events';
|
|
|
|
|
import * as bcrypt from 'bcryptjs';
|
|
|
|
|
|
2017-10-07 00:23:00 +02:00
|
|
|
|
import User, { IUser, init as initUser } from '../models/user';
|
2017-10-06 20:36:46 +02:00
|
|
|
|
|
2017-10-06 23:58:50 +02:00
|
|
|
|
import getPostSummary from '../../common/get-post-summary';
|
2017-10-07 10:20:47 +02:00
|
|
|
|
import getUserSummary from '../../common/get-user-summary';
|
2017-10-06 23:43:36 +02:00
|
|
|
|
|
2017-10-07 20:24:10 +02:00
|
|
|
|
import Othello, { ai as othelloAi } from '../../common/othello';
|
|
|
|
|
|
2017-10-08 11:30:37 +02:00
|
|
|
|
const hmm = [
|
|
|
|
|
'?',
|
|
|
|
|
'ふぅ~む...?',
|
|
|
|
|
'ちょっと何言ってるかわからないです',
|
|
|
|
|
'「ヘルプ」と言うと利用可能な操作が確認できますよ'
|
|
|
|
|
];
|
|
|
|
|
|
2017-10-07 10:20:47 +02:00
|
|
|
|
/**
|
|
|
|
|
* Botの頭脳
|
|
|
|
|
*/
|
2017-10-06 20:36:46 +02:00
|
|
|
|
export default class BotCore extends EventEmitter {
|
2017-10-06 21:30:57 +02:00
|
|
|
|
public user: IUser = null;
|
2017-10-06 20:36:46 +02:00
|
|
|
|
|
|
|
|
|
private context: Context = null;
|
|
|
|
|
|
2017-10-06 21:30:57 +02:00
|
|
|
|
constructor(user?: IUser) {
|
2017-10-06 20:36:46 +02:00
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
this.user = user;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 10:20:47 +02:00
|
|
|
|
public clearContext() {
|
|
|
|
|
this.setContext(null);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-06 23:43:36 +02:00
|
|
|
|
public setContext(context: Context) {
|
2017-10-06 22:50:01 +02:00
|
|
|
|
this.context = context;
|
|
|
|
|
this.emit('updated');
|
|
|
|
|
|
|
|
|
|
if (context) {
|
|
|
|
|
context.on('updated', () => {
|
|
|
|
|
this.emit('updated');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public export() {
|
|
|
|
|
return {
|
|
|
|
|
user: this.user,
|
|
|
|
|
context: this.context ? this.context.export() : null
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 11:30:04 +02:00
|
|
|
|
protected _import(data) {
|
|
|
|
|
this.user = data.user ? initUser(data.user) : null;
|
|
|
|
|
this.setContext(data.context ? Context.import(this, data.context) : null);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-06 22:50:01 +02:00
|
|
|
|
public static import(data) {
|
2017-10-07 11:30:04 +02:00
|
|
|
|
const bot = new BotCore();
|
|
|
|
|
bot._import(data);
|
|
|
|
|
return bot;
|
2017-10-06 22:50:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 11:30:04 +02:00
|
|
|
|
public async q(query: string): Promise<string | void> {
|
2017-10-06 20:36:46 +02:00
|
|
|
|
if (this.context != null) {
|
|
|
|
|
return await this.context.q(query);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 11:30:04 +02:00
|
|
|
|
if (/^@[a-zA-Z0-9-]+$/.test(query)) {
|
|
|
|
|
return await this.showUserCommand(query);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-06 20:36:46 +02:00
|
|
|
|
switch (query) {
|
|
|
|
|
case 'ping':
|
|
|
|
|
return 'PONG';
|
2017-10-06 23:43:36 +02:00
|
|
|
|
|
|
|
|
|
case 'help':
|
|
|
|
|
case 'ヘルプ':
|
2017-10-07 00:50:47 +02:00
|
|
|
|
return '利用可能なコマンド一覧です:\n' +
|
2017-10-06 23:43:36 +02:00
|
|
|
|
'help: これです\n' +
|
|
|
|
|
'me: アカウント情報を見ます\n' +
|
|
|
|
|
'login, signin: サインインします\n' +
|
|
|
|
|
'logout, signout: サインアウトします\n' +
|
|
|
|
|
'post: 投稿します\n' +
|
2017-10-07 11:30:04 +02:00
|
|
|
|
'tl: タイムラインを見ます\n' +
|
|
|
|
|
'@<ユーザー名>: ユーザーを表示します';
|
2017-10-06 23:43:36 +02:00
|
|
|
|
|
2017-10-06 22:50:01 +02:00
|
|
|
|
case 'me':
|
2017-10-07 00:50:47 +02:00
|
|
|
|
return this.user ? `${this.user.name}としてサインインしています。\n\n${getUserSummary(this.user)}` : 'サインインしていません';
|
2017-10-06 23:43:36 +02:00
|
|
|
|
|
|
|
|
|
case 'login':
|
|
|
|
|
case 'signin':
|
2017-10-06 20:36:46 +02:00
|
|
|
|
case 'ログイン':
|
|
|
|
|
case 'サインイン':
|
2017-10-07 11:30:04 +02:00
|
|
|
|
if (this.user != null) return '既にサインインしていますよ!';
|
2017-10-06 23:43:36 +02:00
|
|
|
|
this.setContext(new SigninContext(this));
|
2017-10-06 20:36:46 +02:00
|
|
|
|
return await this.context.greet();
|
2017-10-06 23:43:36 +02:00
|
|
|
|
|
|
|
|
|
case 'logout':
|
|
|
|
|
case 'signout':
|
|
|
|
|
case 'ログアウト':
|
|
|
|
|
case 'サインアウト':
|
|
|
|
|
if (this.user == null) return '今はサインインしてないですよ!';
|
|
|
|
|
this.signout();
|
|
|
|
|
return 'ご利用ありがとうございました <3';
|
|
|
|
|
|
|
|
|
|
case 'post':
|
|
|
|
|
case '投稿':
|
|
|
|
|
if (this.user == null) return 'まずサインインしてください。';
|
|
|
|
|
this.setContext(new PostContext(this));
|
|
|
|
|
return await this.context.greet();
|
|
|
|
|
|
|
|
|
|
case 'tl':
|
|
|
|
|
case 'タイムライン':
|
2017-10-07 11:30:04 +02:00
|
|
|
|
return await this.tlCommand();
|
2017-10-06 23:43:36 +02:00
|
|
|
|
|
2017-10-08 05:01:57 +02:00
|
|
|
|
case 'guessing-game':
|
|
|
|
|
case '数当てゲーム':
|
|
|
|
|
this.setContext(new GuessingGameContext(this));
|
|
|
|
|
return await this.context.greet();
|
|
|
|
|
|
2017-10-07 20:24:10 +02:00
|
|
|
|
case 'othello':
|
|
|
|
|
case 'オセロ':
|
|
|
|
|
this.setContext(new OthelloContext(this));
|
|
|
|
|
return await this.context.greet();
|
|
|
|
|
|
2017-10-07 11:30:04 +02:00
|
|
|
|
default:
|
2017-10-08 11:30:37 +02:00
|
|
|
|
return hmm[Math.floor(Math.random() * hmm.length)];
|
2017-10-06 20:36:46 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-06 23:43:36 +02:00
|
|
|
|
public signin(user: IUser) {
|
2017-10-06 20:36:46 +02:00
|
|
|
|
this.user = user;
|
2017-10-06 23:43:36 +02:00
|
|
|
|
this.emit('signin', user);
|
2017-10-06 22:50:01 +02:00
|
|
|
|
this.emit('updated');
|
2017-10-06 20:36:46 +02:00
|
|
|
|
}
|
2017-10-06 23:43:36 +02:00
|
|
|
|
|
|
|
|
|
public signout() {
|
|
|
|
|
const user = this.user;
|
|
|
|
|
this.user = null;
|
|
|
|
|
this.emit('signout', user);
|
|
|
|
|
this.emit('updated');
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 20:24:10 +02:00
|
|
|
|
public async refreshUser() {
|
|
|
|
|
this.user = await User.findOne({
|
|
|
|
|
_id: this.user._id
|
|
|
|
|
}, {
|
|
|
|
|
fields: {
|
|
|
|
|
data: false
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.emit('updated');
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 11:30:04 +02:00
|
|
|
|
public async tlCommand(): Promise<string | void> {
|
2017-10-06 23:43:36 +02:00
|
|
|
|
if (this.user == null) return 'まずサインインしてください。';
|
|
|
|
|
|
2017-10-06 23:58:50 +02:00
|
|
|
|
const tl = await require('../endpoints/posts/timeline')({
|
|
|
|
|
limit: 5
|
|
|
|
|
}, this.user);
|
2017-10-06 23:43:36 +02:00
|
|
|
|
|
|
|
|
|
const text = tl
|
|
|
|
|
.map(post => getPostSummary(post))
|
|
|
|
|
.join('\n-----\n');
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
2017-10-07 11:30:04 +02:00
|
|
|
|
|
|
|
|
|
public async showUserCommand(q: string): Promise<string | void> {
|
|
|
|
|
try {
|
2017-10-08 11:30:37 +02:00
|
|
|
|
const user = await require('../endpoints/users/show')({
|
2017-10-07 11:30:04 +02:00
|
|
|
|
username: q.substr(1)
|
|
|
|
|
}, this.user);
|
|
|
|
|
|
|
|
|
|
const text = getUserSummary(user);
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return `問題が発生したようです...: ${e}`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-06 20:36:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-06 22:50:01 +02:00
|
|
|
|
abstract class Context extends EventEmitter {
|
2017-10-07 11:30:04 +02:00
|
|
|
|
protected bot: BotCore;
|
2017-10-06 20:36:46 +02:00
|
|
|
|
|
|
|
|
|
public abstract async greet(): Promise<string>;
|
|
|
|
|
public abstract async q(query: string): Promise<string>;
|
2017-10-06 22:50:01 +02:00
|
|
|
|
public abstract export(): any;
|
2017-10-06 20:36:46 +02:00
|
|
|
|
|
2017-10-07 11:30:04 +02:00
|
|
|
|
constructor(bot: BotCore) {
|
2017-10-06 22:50:01 +02:00
|
|
|
|
super();
|
2017-10-07 11:30:04 +02:00
|
|
|
|
this.bot = bot;
|
2017-10-06 20:36:46 +02:00
|
|
|
|
}
|
2017-10-06 22:50:01 +02:00
|
|
|
|
|
2017-10-07 11:30:04 +02:00
|
|
|
|
public static import(bot: BotCore, data: any) {
|
2017-10-08 05:01:57 +02:00
|
|
|
|
if (data.type == 'guessing-game') return GuessingGameContext.import(bot, data.content);
|
2017-10-07 20:24:10 +02:00
|
|
|
|
if (data.type == 'othello') return OthelloContext.import(bot, data.content);
|
2017-10-07 11:30:04 +02:00
|
|
|
|
if (data.type == 'post') return PostContext.import(bot, data.content);
|
|
|
|
|
if (data.type == 'signin') return SigninContext.import(bot, data.content);
|
2017-10-06 22:50:01 +02:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-10-06 20:36:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SigninContext extends Context {
|
2017-10-06 23:03:16 +02:00
|
|
|
|
private temporaryUser: IUser = null;
|
2017-10-06 20:36:46 +02:00
|
|
|
|
|
|
|
|
|
public async greet(): Promise<string> {
|
|
|
|
|
return 'まずユーザー名を教えてください:';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async q(query: string): Promise<string> {
|
|
|
|
|
if (this.temporaryUser == null) {
|
|
|
|
|
// Fetch user
|
|
|
|
|
const user: IUser = await User.findOne({
|
|
|
|
|
username_lower: query.toLowerCase()
|
|
|
|
|
}, {
|
|
|
|
|
fields: {
|
2017-10-07 01:04:55 +02:00
|
|
|
|
data: false
|
2017-10-06 20:36:46 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (user === null) {
|
|
|
|
|
return `${query}というユーザーは存在しませんでした... もう一度教えてください:`;
|
|
|
|
|
} else {
|
|
|
|
|
this.temporaryUser = user;
|
2017-10-06 22:50:01 +02:00
|
|
|
|
this.emit('updated');
|
2017-10-06 20:36:46 +02:00
|
|
|
|
return `パスワードを教えてください:`;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Compare password
|
|
|
|
|
const same = bcrypt.compareSync(query, this.temporaryUser.password);
|
|
|
|
|
|
|
|
|
|
if (same) {
|
2017-10-07 11:30:04 +02:00
|
|
|
|
this.bot.signin(this.temporaryUser);
|
|
|
|
|
this.bot.clearContext();
|
2017-10-06 20:36:46 +02:00
|
|
|
|
return `${this.temporaryUser.name}さん、おかえりなさい!`;
|
|
|
|
|
} else {
|
|
|
|
|
return `パスワードが違います... もう一度教えてください:`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-06 22:50:01 +02:00
|
|
|
|
|
|
|
|
|
public export() {
|
|
|
|
|
return {
|
2017-10-06 23:03:16 +02:00
|
|
|
|
type: 'signin',
|
2017-10-06 23:43:36 +02:00
|
|
|
|
content: {
|
|
|
|
|
temporaryUser: this.temporaryUser
|
|
|
|
|
}
|
2017-10-06 22:50:01 +02:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 11:30:04 +02:00
|
|
|
|
public static import(bot: BotCore, data: any) {
|
|
|
|
|
const context = new SigninContext(bot);
|
2017-10-06 22:50:01 +02:00
|
|
|
|
context.temporaryUser = data.temporaryUser;
|
|
|
|
|
return context;
|
|
|
|
|
}
|
2017-10-06 20:36:46 +02:00
|
|
|
|
}
|
2017-10-06 23:43:36 +02:00
|
|
|
|
|
|
|
|
|
class PostContext extends Context {
|
|
|
|
|
public async greet(): Promise<string> {
|
|
|
|
|
return '内容:';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async q(query: string): Promise<string> {
|
|
|
|
|
await require('../endpoints/posts/create')({
|
|
|
|
|
text: query
|
2017-10-07 11:30:04 +02:00
|
|
|
|
}, this.bot.user);
|
|
|
|
|
this.bot.clearContext();
|
2017-10-06 23:43:36 +02:00
|
|
|
|
return '投稿しましたよ!';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public export() {
|
|
|
|
|
return {
|
|
|
|
|
type: 'post'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 11:30:04 +02:00
|
|
|
|
public static import(bot: BotCore, data: any) {
|
|
|
|
|
const context = new PostContext(bot);
|
2017-10-06 23:43:36 +02:00
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-07 20:24:10 +02:00
|
|
|
|
|
2017-10-08 05:01:57 +02:00
|
|
|
|
class GuessingGameContext extends Context {
|
|
|
|
|
private secret: number;
|
2017-10-08 11:30:37 +02:00
|
|
|
|
private history: number[] = [];
|
2017-10-08 05:01:57 +02:00
|
|
|
|
|
|
|
|
|
public async greet(): Promise<string> {
|
|
|
|
|
this.secret = Math.floor(Math.random() * 100);
|
|
|
|
|
this.emit('updated');
|
|
|
|
|
return '0~100の秘密の数を当ててみてください:';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async q(query: string): Promise<string> {
|
|
|
|
|
if (query == 'やめる') {
|
|
|
|
|
this.bot.clearContext();
|
|
|
|
|
return 'やめました。';
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 05:09:10 +02:00
|
|
|
|
const guess = parseInt(query, 10);
|
|
|
|
|
|
|
|
|
|
if (isNaN(guess)) {
|
|
|
|
|
return '整数で推測してください。「やめる」と言うとゲームをやめます。';
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 11:30:37 +02:00
|
|
|
|
this.history.push(guess);
|
2017-10-08 05:01:57 +02:00
|
|
|
|
this.emit('updated');
|
|
|
|
|
|
|
|
|
|
if (this.secret < guess) {
|
2017-10-08 11:30:37 +02:00
|
|
|
|
return this.history.indexOf(guess) === -1 ? `${guess}よりも小さいですね` : `もう一度言いますが${guess}より小さいですよ`;
|
2017-10-08 05:01:57 +02:00
|
|
|
|
} else if (this.secret > guess) {
|
2017-10-08 11:30:37 +02:00
|
|
|
|
return this.history.indexOf(guess) === -1 ? `${guess}よりも大きいですね` : `もう一度言いますが${guess}より大きいですよ`;
|
2017-10-08 05:01:57 +02:00
|
|
|
|
} else {
|
|
|
|
|
this.bot.clearContext();
|
2017-10-08 11:30:37 +02:00
|
|
|
|
return `正解です🎉 (${this.history.length}回目で当てました)`;
|
2017-10-08 05:01:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public export() {
|
|
|
|
|
return {
|
|
|
|
|
type: 'guessing-game',
|
|
|
|
|
content: {
|
|
|
|
|
secret: this.secret,
|
2017-10-08 11:30:37 +02:00
|
|
|
|
history: this.history
|
2017-10-08 05:01:57 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static import(bot: BotCore, data: any) {
|
|
|
|
|
const context = new GuessingGameContext(bot);
|
|
|
|
|
context.secret = data.secret;
|
2017-10-08 11:30:37 +02:00
|
|
|
|
context.history = data.history;
|
2017-10-08 05:01:57 +02:00
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 20:24:10 +02:00
|
|
|
|
class OthelloContext extends Context {
|
|
|
|
|
private othello: Othello = null;
|
|
|
|
|
|
2017-10-07 20:36:34 +02:00
|
|
|
|
constructor(bot: BotCore) {
|
|
|
|
|
super(bot);
|
|
|
|
|
|
2017-10-07 20:24:10 +02:00
|
|
|
|
this.othello = new Othello();
|
2017-10-07 20:36:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async greet(): Promise<string> {
|
2017-10-07 20:24:10 +02:00
|
|
|
|
return this.othello.toPatternString('black');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async q(query: string): Promise<string> {
|
2017-10-08 05:01:57 +02:00
|
|
|
|
if (query == 'やめる') {
|
|
|
|
|
this.bot.clearContext();
|
|
|
|
|
return 'オセロをやめました。';
|
|
|
|
|
}
|
2017-10-08 11:30:37 +02:00
|
|
|
|
|
|
|
|
|
const n = parseInt(query, 10);
|
|
|
|
|
|
|
|
|
|
if (isNaN(n)) {
|
|
|
|
|
return '番号で指定してください。「やめる」と言うとゲームをやめます。';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.othello.setByNumber('black', n);
|
2017-10-07 21:13:38 +02:00
|
|
|
|
const s = this.othello.toString() + '\n\n...(AI)...\n\n';
|
2017-10-07 20:24:10 +02:00
|
|
|
|
othelloAi('white', this.othello);
|
2017-10-07 21:13:38 +02:00
|
|
|
|
if (this.othello.getPattern('black').length === 0) {
|
|
|
|
|
this.bot.clearContext();
|
2017-10-08 05:01:57 +02:00
|
|
|
|
const blackCount = this.othello.board.map(row => row.filter(s => s == 'black').length).reduce((a, b) => a + b);
|
|
|
|
|
const whiteCount = this.othello.board.map(row => row.filter(s => s == 'white').length).reduce((a, b) => a + b);
|
|
|
|
|
const winner = blackCount == whiteCount ? '引き分け' : blackCount > whiteCount ? '黒の勝ち' : '白の勝ち';
|
|
|
|
|
return this.othello.toString() + `\n\n~終了~\n\n黒${blackCount}、白${whiteCount}で${winner}です。`;
|
2017-10-07 21:13:38 +02:00
|
|
|
|
} else {
|
|
|
|
|
this.emit('updated');
|
|
|
|
|
return s + this.othello.toPatternString('black');
|
|
|
|
|
}
|
2017-10-07 20:24:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public export() {
|
|
|
|
|
return {
|
|
|
|
|
type: 'othello',
|
|
|
|
|
content: {
|
|
|
|
|
board: this.othello.board
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static import(bot: BotCore, data: any) {
|
|
|
|
|
const context = new OthelloContext(bot);
|
|
|
|
|
context.othello = new Othello();
|
|
|
|
|
context.othello.board = data.board;
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
}
|